Skip to content

Commit

Permalink
[Finishes #187354261] buyer wishlist for products
Browse files Browse the repository at this point in the history
  • Loading branch information
P-Rwirangira committed May 1, 2024
1 parent 6de33c7 commit 9acff56
Show file tree
Hide file tree
Showing 6 changed files with 270 additions and 0 deletions.
40 changes: 40 additions & 0 deletions src/database/migrations/20240426195145-create-category.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/* eslint-disable @typescript-eslint/no-var-requires */
'use strict';

const sequelize = require('sequelize');

/** @type {import('sequelize-cli').Migration} */
module.exports = {
async up(queryInterface, Sequelize) {
await queryInterface.createTable('categories', {
id: {
allowNull: false,
primaryKey: true,
type: Sequelize.UUID,
defaultValue: sequelize.UUIDV4,
unique: true,
},
name: {
type: Sequelize.STRING,
allowNull: false,
},
description: {
type: Sequelize.TEXT,
allowNull: false,
},
createdAt: {
allowNull: false,
type: Sequelize.DATE,
defaultValue: Sequelize.literal('NOW()'),
},
updatedAt: {
allowNull: false,
type: Sequelize.DATE,
defaultValue: Sequelize.literal('CURRENT_TIMESTAMP'),
},
});
},
async down(queryInterface) {
await queryInterface.dropTable('categories');
},
};
63 changes: 63 additions & 0 deletions src/database/migrations/20240429115230-create-product.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/* eslint-disable @typescript-eslint/no-var-requires */
'use strict';

const sequelize = require('sequelize');

/** @type {import('sequelize-cli').Migration} */
module.exports = {
async up(queryInterface, Sequelize) {
await queryInterface.createTable('products', {
id: {
allowNull: false,
primaryKey: true,
type: Sequelize.UUID,
defaultValue: sequelize.UUIDV4,
unique: true,
},
name: {
type: Sequelize.STRING,
allowNull: false,
},
description: {
type: Sequelize.TEXT,
allowNull: false,
},
price: {
type: Sequelize.FLOAT,
allowNull: false,
},
discount: {
type: Sequelize.FLOAT,
allowNull: true,
defaultValue: 0, // Default discount is 0
},
images: {
type: Sequelize.ARRAY(Sequelize.STRING),
allowNull: false,
},
categoryId: {
type: Sequelize.UUID,
allowNull: false,
references: {
model: 'categories',
key: 'id',
},
onUpdate: 'CASCADE', // Optional: Update the behavior on category deletion
onDelete: 'CASCADE', // Optional: Delete associated products on category deletion
},
createdAt: {
allowNull: false,
type: Sequelize.DATE,
defaultValue: Sequelize.literal('NOW()'),
},
updatedAt: {
allowNull: false,
type: Sequelize.DATE,
defaultValue: Sequelize.literal('CURRENT_TIMESTAMP'),
},
});
},
async down(queryInterface) {
await queryInterface.dropTable('products');
},
};
53 changes: 53 additions & 0 deletions src/database/migrations/20240501215816-wish-list.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/* eslint-disable @typescript-eslint/no-var-requires */
'use strict';

const sequelize = require('sequelize');

/** @type {import('sequelize-cli').Migration} */
module.exports = {
async up(queryInterface, Sequelize) {
await queryInterface.createTable('wish-list', {
id: {
allowNull: false,
primaryKey: true,
type: Sequelize.UUID,
defaultValue: sequelize.UUIDV4,
unique: true,
},
productId: {
type: Sequelize.UUID,
allowNull: false,
foreignKey: true,
references: {
model: 'products',
key: 'id',
},
},
userId: {
type: Sequelize.UUID,
allowNull: false,
foreignKey: true,
references: {
model: 'Users',
key: 'id',
},
},

name: {
type: Sequelize.STRING,
allowNull: false,
},
price: {
type: Sequelize.FLOAT,
allowNull: false,
},
images: {
type: Sequelize.ARRAY(Sequelize.STRING),
allowNull: false,
},
});
},
async down(queryInterface) {
await queryInterface.dropTable('wish-list');
},
};
41 changes: 41 additions & 0 deletions src/database/models/Category.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import { Model, Optional, DataTypes, UUIDV4 } from 'sequelize';
import sequelize from './index';

interface CategoryAttributes {
id: number;
name: string;
description?: string;
}

export interface CategoryCreationAttributes extends Optional<CategoryAttributes, 'id'> {}

export class Category extends Model<CategoryAttributes, CategoryCreationAttributes> implements CategoryAttributes {
public id!: number;
public name!: string;
public description!: string;
}

Category.init(
{
id: {
type: DataTypes.UUID,
defaultValue: UUIDV4,
primaryKey: true,
autoIncrement: true,
},
name: {
type: DataTypes.STRING,
allowNull: false,
},
description: {
type: DataTypes.TEXT,
allowNull: true,
},
},
{
sequelize,
modelName: 'Category',
tableName: 'categories',
timestamps: true,
}
);
72 changes: 72 additions & 0 deletions src/database/models/Products.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
import { Model, Optional, DataTypes } from 'sequelize';
import sequelize from './index';
import { UUIDV4 } from 'sequelize';
import { Category } from './Category';

interface ProductAttributes {
id: number;
name: string;
description: string;
price: number;
discount?: number;
categoryId: string;
createdAt?: Date;
updatedAt?: Date;
images: string[];
}

export interface ProductCreationAttributes extends Optional<ProductAttributes, 'id'> {}

export class Product extends Model<ProductAttributes, ProductCreationAttributes> implements ProductAttributes {
public id!: number;
public name!: string;
public description!: string;
public price!: number;
public discount!: number;
public categoryId!: string;
public images!: string[];
public readonly createdAt!: Date | undefined;
public readonly updatedAt!: Date | undefined;
}

Product.init(
{
id: {
type: DataTypes.UUID,
defaultValue: UUIDV4,
autoIncrement: true,
primaryKey: true,
},
name: {
type: DataTypes.STRING,
allowNull: false,
},
description: {
type: DataTypes.TEXT,
allowNull: false,
},
price: {
type: DataTypes.FLOAT,
allowNull: false,
},
discount: {
type: DataTypes.FLOAT,
allowNull: true,
defaultValue: 0, // Default discount is 0
},
images: {
type: DataTypes.ARRAY(DataTypes.STRING),
allowNull: false,
},
categoryId: {
type: DataTypes.UUID,
references: {
model: 'Category',
key: 'id',
},
},
},
{ sequelize: sequelize, timestamps: true, modelName: 'Product', tableName: 'products' }
);

Product.belongsTo(Category, { foreignKey: 'categoryId' });
1 change: 1 addition & 0 deletions src/database/models/wish-list.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
//Wish list model

0 comments on commit 9acff56

Please sign in to comment.