Compare commits
No commits in common. 'be0f01bc1a5d9991804318804ec072a18902998a' and 'e18858fd60b6005a2325d09f4f28a4971f9d4467' have entirely different histories.
be0f01bc1a
...
e18858fd60
5 changed files with 0 additions and 97 deletions
@ -1,14 +0,0 @@ |
||||
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<Invoice> { |
||||
@ForeignKey(() => User) |
||||
@Column |
||||
userId: number; |
||||
@BelongsTo(() => User) |
||||
user: User; |
||||
@Column |
||||
totalAmount: number; |
||||
} |
@ -1,17 +0,0 @@ |
||||
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<Invoice> { |
||||
const { userId, totalAmount } = body; |
||||
return this.invoiceService.createInvoice(userId, totalAmount); |
||||
} |
||||
@Get(":userId") |
||||
async getInvoices(@Param("userId") userId: number): Promise<any> { |
||||
return this.invoiceService.getInvoicesByUser(userId); |
||||
} |
||||
} |
@ -1,12 +0,0 @@ |
||||
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 {} |
@ -1,52 +0,0 @@ |
||||
import { HttpException, HttpStatus, Injectable } from "@nestjs/common"; |
||||
import { InjectModel } from "@nestjs/sequelize"; |
||||
import { Invoice } from "./entities/invoice.entity"; |
||||
import { where } from "sequelize"; |
||||
|
||||
@Injectable() |
||||
export class InvoiceService { |
||||
constructor(@InjectModel(Invoice) private readonly invoiceModel: typeof Invoice) {} |
||||
|
||||
async createInvoice(userId: number, totalAmount: number): Promise<Invoice> { |
||||
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); |
||||
} |
||||
} |
||||
async getInvoicesByUser(userId: number): Promise<Invoice[]> { |
||||
try { |
||||
if (!userId) { |
||||
throw new HttpException('User ID is required.', HttpStatus.BAD_REQUEST); |
||||
} |
||||
|
||||
const invoices = await this.invoiceModel.findAll({ |
||||
where: { userId }, |
||||
}); |
||||
|
||||
if (!invoices || invoices.length === 0) { |
||||
throw new HttpException('No invoices found for this user.', HttpStatus.NOT_FOUND); |
||||
} |
||||
|
||||
return invoices; |
||||
} catch (error) { |
||||
if (error instanceof HttpException) { |
||||
throw error; |
||||
} |
||||
throw new HttpException( |
||||
'An error occurred while retrieving invoices.', |
||||
HttpStatus.INTERNAL_SERVER_ERROR, |
||||
); |
||||
} |
||||
} |
||||
} |
Loading…
Reference in new issue