Compare commits

...

2 Commits

  1. 2
      src/app.module.ts
  2. 12
      src/cart/cart.controller.ts
  3. 22
      src/cart/cart.service.ts
  4. 15
      src/wallet/entities/wallet.entity.ts
  5. 8
      src/wallet/wallet.controller.ts
  6. 9
      src/wallet/wallet.module.ts
  7. 7
      src/wallet/wallet.service.ts

@ -7,6 +7,7 @@ import { databaseConfig } from "./config/database.config";
import { UsersModule } from './users/users.module';
import { ProductsModule } from './products/products.module';
import { CartModule } from './cart/cart.module';
import { WalletModule } from './wallet/wallet.module';
@Module({
imports: [
@ -17,6 +18,7 @@ import { CartModule } from './cart/cart.module';
UsersModule,
ProductsModule,
CartModule,
WalletModule,
],
controllers: [AppController],
providers: [AppService],

@ -11,8 +11,16 @@ export class CartController {
const result = await this.cartService.addToCart(userId, productId, quantity);
return result;
}
@Get(':userId')
async getUserCart(@Param('userId') userId: number) {
@Get(":userId")
async getUserCart(@Param("userId") userId: number) {
return this.cartService.getUserCart(userId);
}
@Delete(":userId/:productId")
async removeFromCart(
@Param("userId") userId: number,
@Param("productId") productId: number,
): Promise<{ message: string }> {
return this.cartService.removeFromCart(userId, productId);
}
}

@ -76,4 +76,26 @@ export class CartService {
throw new HttpException("An error occurred while retrieving the cart.", HttpStatus.INTERNAL_SERVER_ERROR);
}
}
async removeFromCart(userId: number, productId: number): Promise<{ message: string }> {
try {
const cartItem = await this.cartModel.findOne({ where: { userId, productId } });
if (!cartItem) {
throw new HttpException('Product not found in the cart.', HttpStatus.NOT_FOUND);
}
await cartItem.destroy();
return { message: 'Item deleted from your cart successfully.' };
} catch (error) {
if (error instanceof HttpException) {
throw error;
}
throw new HttpException(
'An error occurred while removing the product from the cart.',
HttpStatus.INTERNAL_SERVER_ERROR,
);
}
}
}

@ -0,0 +1,15 @@
import { Model, Table, Column, ForeignKey, BelongsTo } from 'sequelize-typescript';
import { User } from '../../users/entities/user.entity';
@Table
export class Wallet extends Model<Wallet> {
@ForeignKey(() => User)
@Column
userId: number;
@BelongsTo(() => User)
user: User;
@Column
balance: number;
}

@ -0,0 +1,8 @@
import { Controller, Get, Post, Body, Patch, Param, Delete } from '@nestjs/common';
import { WalletService } from './wallet.service';
@Controller('wallet')
export class WalletController {
constructor(private readonly walletService: WalletService) {}
}

@ -0,0 +1,9 @@
import { Module } from '@nestjs/common';
import { WalletService } from './wallet.service';
import { WalletController } from './wallet.controller';
@Module({
controllers: [WalletController],
providers: [WalletService],
})
export class WalletModule {}

@ -0,0 +1,7 @@
import { Injectable } from '@nestjs/common';
@Injectable()
export class WalletService {
constructor(){}
}
Loading…
Cancel
Save