diff --git a/src/app.module.ts b/src/app.module.ts index 414e5fb..84891d4 100644 --- a/src/app.module.ts +++ b/src/app.module.ts @@ -8,6 +8,7 @@ import { UsersModule } from './users/users.module'; import { ProductsModule } from './products/products.module'; import { CartModule } from './cart/cart.module'; import { WalletModule } from './wallet/wallet.module'; +import { InvoiceModule } from './invoice/invoice.module'; @Module({ imports: [ @@ -19,6 +20,7 @@ import { WalletModule } from './wallet/wallet.module'; ProductsModule, CartModule, WalletModule, + InvoiceModule, ], controllers: [AppController], providers: [AppService], diff --git a/src/invoice/entities/invoice.entity.ts b/src/invoice/entities/invoice.entity.ts new file mode 100644 index 0000000..f72cf23 --- /dev/null +++ b/src/invoice/entities/invoice.entity.ts @@ -0,0 +1,14 @@ +import { Table, Model, Column, BelongsTo, ForeignKey } from "sequelize-typescript"; +import { User } from "../../users/entities/user.entity"; +import { Product } from "../../products/entities/product.entity"; + +@Table +export class Invoice extends Model { + @ForeignKey(() => User) + @Column + userId: number; + @BelongsTo(() => User) + user: User; + @Column + totalAmount: number; +} diff --git a/src/invoice/invoice.controller.ts b/src/invoice/invoice.controller.ts new file mode 100644 index 0000000..868211a --- /dev/null +++ b/src/invoice/invoice.controller.ts @@ -0,0 +1,13 @@ +import { Controller, Get, Post, Body, Patch, Param, Delete } from '@nestjs/common'; +import { InvoiceService } from './invoice.service'; +import { Invoice } from './entities/invoice.entity'; + +@Controller('invoice') +export class InvoiceController { + constructor(private readonly invoiceService: InvoiceService) {} + @Post('create') + async createInvoice(@Body() body:{userId:number, totalAmount:number}):Promise{ + const { userId, totalAmount} = body + return this.invoiceService.createInvoice(userId, totalAmount) + } +} diff --git a/src/invoice/invoice.module.ts b/src/invoice/invoice.module.ts new file mode 100644 index 0000000..3199646 --- /dev/null +++ b/src/invoice/invoice.module.ts @@ -0,0 +1,12 @@ +import { Module } from '@nestjs/common'; +import { InvoiceService } from './invoice.service'; +import { InvoiceController } from './invoice.controller'; +import { SequelizeModule } from '@nestjs/sequelize'; +import { Invoice } from './entities/invoice.entity'; + +@Module({ + imports : [SequelizeModule.forFeature([Invoice])], + controllers: [InvoiceController], + providers: [InvoiceService], +}) +export class InvoiceModule {} diff --git a/src/invoice/invoice.service.ts b/src/invoice/invoice.service.ts new file mode 100644 index 0000000..46f72e8 --- /dev/null +++ b/src/invoice/invoice.service.ts @@ -0,0 +1,26 @@ +import { HttpException, HttpStatus, Injectable } from "@nestjs/common"; +import { InjectModel } from "@nestjs/sequelize"; +import { Invoice } from "./entities/invoice.entity"; + +@Injectable() +export class InvoiceService { + constructor(@InjectModel(Invoice) private readonly invoiceModel: typeof Invoice) {} + + async createInvoice(userId: number, totalAmount: number): Promise { + try { + if (!userId) { + throw new HttpException("User id not found!", HttpStatus.BAD_REQUEST); + } + + const newInvoice = await this.invoiceModel.create({ userId, totalAmount }); + return newInvoice; + } catch (error) { + + if (error instanceof HttpException) { + throw error; + } + + throw new HttpException("An error occurred while creating the invoice.", HttpStatus.INTERNAL_SERVER_ERROR); + } + } +}