diff --git a/src/app.module.ts b/src/app.module.ts index 0522777..d20dcea 100644 --- a/src/app.module.ts +++ b/src/app.module.ts @@ -6,6 +6,7 @@ import { SequelizeModule } from "@nestjs/sequelize"; import { databaseConfig } from "./config/database.config"; import { UsersModule } from './users/users.module'; import { ProductsModule } from './products/products.module'; +import { CartModule } from './cart/cart.module'; @Module({ imports: [ @@ -15,6 +16,7 @@ import { ProductsModule } from './products/products.module'; SequelizeModule.forRoot(databaseConfig), UsersModule, ProductsModule, + CartModule, ], controllers: [AppController], providers: [AppService], diff --git a/src/cart/cart.controller.ts b/src/cart/cart.controller.ts new file mode 100644 index 0000000..b584f12 --- /dev/null +++ b/src/cart/cart.controller.ts @@ -0,0 +1,8 @@ +import { Controller, Get, Post, Body, Patch, Param, Delete } from '@nestjs/common'; +import { CartService } from './cart.service'; + +@Controller('cart') +export class CartController { + constructor(private readonly cartService: CartService) {} + +} diff --git a/src/cart/cart.module.ts b/src/cart/cart.module.ts new file mode 100644 index 0000000..d9e45d5 --- /dev/null +++ b/src/cart/cart.module.ts @@ -0,0 +1,12 @@ +import { Module } from "@nestjs/common"; +import { CartService } from "./cart.service"; +import { CartController } from "./cart.controller"; +import { Cart } from "./entities/cart.entity"; +import { SequelizeModule } from "@nestjs/sequelize"; + +@Module({ + imports: [SequelizeModule.forFeature([Cart])], + controllers: [CartController], + providers: [CartService], +}) +export class CartModule {} diff --git a/src/cart/cart.service.ts b/src/cart/cart.service.ts new file mode 100644 index 0000000..8ca559e --- /dev/null +++ b/src/cart/cart.service.ts @@ -0,0 +1,7 @@ +import { Injectable } from '@nestjs/common'; + + +@Injectable() +export class CartService { + +} diff --git a/src/cart/entities/cart.entity.ts b/src/cart/entities/cart.entity.ts new file mode 100644 index 0000000..87b14cb --- /dev/null +++ b/src/cart/entities/cart.entity.ts @@ -0,0 +1,23 @@ +import { Model, Table, Column, ForeignKey, BelongsTo } from "sequelize-typescript"; +import { User } from "../../users/entities/user.entity"; +import { Product } from "../../products/entities/product.entity"; + +@Table +export class Cart extends Model { + @ForeignKey(() => User) + @Column + userId: number; + + @BelongsTo(() => User) + user: User; + + @ForeignKey(() => Product) + @Column + productId: number; + + @BelongsTo(() => Product) + product: Product; + + @Column + quantity: number; +} \ No newline at end of file diff --git a/src/products/products.service.ts b/src/products/products.service.ts index aed1e99..3c3cb54 100644 --- a/src/products/products.service.ts +++ b/src/products/products.service.ts @@ -94,26 +94,20 @@ export class ProductsService { async remove(id: string): Promise<{ message: string }> { try { const product = await this.productModel.findByPk(id); - + if (!product) { - throw new HttpException( - 'Product not found with the given id.', - HttpStatus.NOT_FOUND - ); + throw new HttpException("Product not found with the given id.", HttpStatus.NOT_FOUND); } - + await product.destroy(); - - return { message: 'Product deleted successfully.' }; + + return { message: "Product deleted successfully." }; } catch (error) { if (error instanceof HttpException) { throw error; } - - throw new HttpException( - 'An error occurred while deleting the product.', - HttpStatus.INTERNAL_SERVER_ERROR - ); + + throw new HttpException("An error occurred while deleting the product.", HttpStatus.INTERNAL_SERVER_ERROR); } } }