Skip to content
This repository was archived by the owner on Nov 16, 2023. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
add association
  • Loading branch information
yiyione committed May 26, 2021
commit 58061a5b7de1e2476b2517176bb923bf0cc61f92
9 changes: 6 additions & 3 deletions rest_server/src/models/item_category.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,18 @@ class ItemCategory {
defaultValue: DataTypes.UUIDV4,
primaryKey: true,
},
name: DataTypes.STRING,
name: {
type: DataTypes.STRING,
allowNull: false,
},
description: DataTypes.STRING,
extras: DataTypes.TEXT,
});
}

associate(models) {
this.orm.belongsToMany(models.User.orm, {
through: 'StarRelation',
this.orm.belongsToMany(models.MarketplaceItem.orm, {
through: 'ItemCategoryRelation',
});
this.models = models;
}
Expand Down
9 changes: 6 additions & 3 deletions rest_server/src/models/item_tag.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,19 @@ class ItemTag {
defaultValue: DataTypes.UUIDV4,
primaryKey: true,
},
name: DataTypes.STRING,
name: {
type: DataTypes.STRING,
allowNull: false,
},
color: DataTypes.STRING,
description: DataTypes.STRING,
extras: DataTypes.TEXT,
});
}

associate(models) {
this.orm.belongsToMany(models.User.orm, {
through: 'StarRelation',
this.orm.belongsToMany(models.MarketplaceItem.orm, {
through: 'ItemTagRelation',
});
this.models = models;
}
Expand Down
6 changes: 6 additions & 0 deletions rest_server/src/models/marketplace_item.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,12 @@ class MarketplaceItem {
this.orm.belongsToMany(models.User.orm, {
through: 'StarRelation',
});
this.orm.belongsToMany(models.ItemTag.orm, {
through: 'ItemTagRelation',
});
this.orm.belongsToMany(models.ItemCategory.orm, {
through: 'ItemCategoryRelation',
});
this.models = models;
}

Expand Down
33 changes: 32 additions & 1 deletion rest_server/src/models/model_init_handler.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ const descriptionDir = path.join(
);

const createTemplates = async models => {
const itemTags = [];
const itemCategories = [];

await Promise.all(
EXAMPLE_LIST.map(async item => {
try {
Expand Down Expand Up @@ -46,12 +49,40 @@ const createTemplates = async models => {
: [item.categories],
},
};
await models.MarketplaceItem.orm.create(newItem);
const marketplaceItem = await models.MarketplaceItem.orm.create(
newItem,
);
if (Array.isArray(item.categories)) {
for (const categoryName of item.categories) {
itemTags.push([categoryName, marketplaceItem.id]);
}
}
if (Array.isArray(item.tags)) {
for (const tagName of item.tags) {
itemCategories.push([tagName, marketplaceItem.id]);
}
}
} catch (err) {
console.log(err.message);
}
}),
);
try {
for (const [categoryName, itemId] of itemCategories) {
const [category] = await models.ItemCategory.orm.findOrCreate({
where: { name: categoryName },
});
category.addMarketplaceItem(itemId);
}
for (const [tagName, itemId] of itemTags) {
const [tag] = await models.ItemTag.orm.findOrCreate({
where: { name: tagName },
});
tag.addMarketplaceItem(itemId);
}
} catch (err) {
console.log(err.message);
}
};

const createStorageBlobs = async models => {
Expand Down
2 changes: 1 addition & 1 deletion rest_server/src/router.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ router

router
.route('/categories')
.get(token.checkAuthAndGetUserInfo, categoryController.get)
.get(token.checkAuthAndGetUserInfo, categoryController.list)
.post(token.checkAuthAndGetTokenInfo, categoryController.create);

router
Expand Down