Compare commits

..

2 Commits

  1. 2
      src/app.module.ts
  2. 8
      src/cart/cart.controller.ts
  3. 12
      src/cart/cart.module.ts
  4. 7
      src/cart/cart.service.ts
  5. 23
      src/cart/entities/cart.entity.ts
  6. 22
      src/products/products.controller.ts
  7. 19
      src/products/products.service.ts

@ -6,6 +6,7 @@ import { SequelizeModule } from "@nestjs/sequelize";
import { databaseConfig } from "./config/database.config"; import { databaseConfig } from "./config/database.config";
import { UsersModule } from './users/users.module'; import { UsersModule } from './users/users.module';
import { ProductsModule } from './products/products.module'; import { ProductsModule } from './products/products.module';
import { CartModule } from './cart/cart.module';
@Module({ @Module({
imports: [ imports: [
@ -15,6 +16,7 @@ import { ProductsModule } from './products/products.module';
SequelizeModule.forRoot(databaseConfig), SequelizeModule.forRoot(databaseConfig),
UsersModule, UsersModule,
ProductsModule, ProductsModule,
CartModule,
], ],
controllers: [AppController], controllers: [AppController],
providers: [AppService], providers: [AppService],

@ -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) {}
}

@ -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 {}

@ -0,0 +1,7 @@
import { Injectable } from '@nestjs/common';
@Injectable()
export class CartService {
}

@ -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<Cart> {
@ForeignKey(() => User)
@Column
userId: number;
@BelongsTo(() => User)
user: User;
@ForeignKey(() => Product)
@Column
productId: number;
@BelongsTo(() => Product)
product: Product;
@Column
quantity: number;
}

@ -10,26 +10,26 @@ export class ProductsController {
const { name, description, price } = body; const { name, description, price } = body;
const product = await this.productsService.create(name, description, price); const product = await this.productsService.create(name, description, price);
return { return {
message: 'Product created successfully!', message: "Product created successfully!",
product product,
}; };
} }
@Get() @Get()
async findAll(@Query() query: { search?: string; priceMin?: number; priceMax?: number }){ async findAll(@Query() query: { search?: string; priceMin?: number; priceMax?: number }) {
const { search, priceMin, priceMax } = query; const { search, priceMin, priceMax } = query;
return this.productsService.findAll(search, priceMin, priceMax); return this.productsService.findAll(search, priceMin, priceMax);
} }
@Get(':id') @Get(":id")
async findOne(@Param('id') id: string): Promise<Product> { async findOne(@Param("id") id: string): Promise<Product> {
return this.productsService.findOne(id); return this.productsService.findOne(id);
} }
@Put(':id') @Put(":id")
async update( async update(@Param("id") id: string, @Body() body: { name?: string; description?: string; price?: number }): Promise<Product> {
@Param('id') id: string,
@Body() body: { name?: string; description?: string; price?: number },
): Promise<Product> {
const { name, description, price } = body; const { name, description, price } = body;
return this.productsService.update(id, name, description, price); return this.productsService.update(id, name, description, price);
} }
@Delete(':id')
async remove(@Param('id') id: string): Promise<{ message: string }> {
return this.productsService.remove(id);
}
} }

@ -91,4 +91,23 @@ export class ProductsService {
throw new HttpException("An error occurred while updating the product.", HttpStatus.INTERNAL_SERVER_ERROR); throw new HttpException("An error occurred while updating the product.", HttpStatus.INTERNAL_SERVER_ERROR);
} }
} }
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);
}
await product.destroy();
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);
}
}
} }

Loading…
Cancel
Save