parent
02e81c73b5
commit
1f90fb794b
14 changed files with 212 additions and 2 deletions
@ -0,0 +1,8 @@ |
||||
export class CreateOrderDto { |
||||
userId: number; |
||||
orderDate: Date; |
||||
status: number; |
||||
comments: string; |
||||
shippedDate: Date; |
||||
shipperName: string; |
||||
} |
@ -0,0 +1,2 @@ |
||||
export * from './create-order.dto'; |
||||
export * from './update-order.dto'; |
@ -0,0 +1,4 @@ |
||||
import { PartialType } from '@nestjs/mapped-types'; |
||||
import { CreateOrderDto } from './create-order.dto'; |
||||
|
||||
export class UpdateOrderDto extends PartialType(CreateOrderDto) {} |
@ -0,0 +1,14 @@ |
||||
import { Table, Column, Model, DataType, HasOne } from 'sequelize-typescript'; |
||||
import { Order } from './order.entity'; |
||||
|
||||
@Table({ tableName: 'order_statuses', timestamps: false }) |
||||
export class OrderStatuses extends Model<OrderStatuses> { |
||||
@Column({ |
||||
type: DataType.STRING, |
||||
allowNull: false, |
||||
}) |
||||
name: string; |
||||
|
||||
@HasOne(() => Order) |
||||
order: Order; |
||||
} |
@ -0,0 +1,52 @@ |
||||
import { |
||||
Table, |
||||
Column, |
||||
Model, |
||||
DataType, |
||||
ForeignKey, |
||||
BelongsTo, |
||||
} from 'sequelize-typescript'; |
||||
import { User } from 'src/modules/users/entities/user.entity'; |
||||
import { OrderStatuses } from 'src/modules/orders/entities/order-status.entity'; |
||||
|
||||
@Table({ tableName: 'orders' }) |
||||
export class Order extends Model<Order> { |
||||
@Column({ |
||||
type: DataType.DATE, |
||||
allowNull: false, |
||||
defaultValue: new Date(), |
||||
}) |
||||
orderDate: Date; |
||||
|
||||
@ForeignKey(() => OrderStatuses) |
||||
@Column |
||||
status: number; |
||||
|
||||
@Column({ |
||||
type: DataType.STRING, |
||||
allowNull: true, |
||||
}) |
||||
comments: string; |
||||
|
||||
@Column({ |
||||
type: DataType.DATE, |
||||
allowNull: true, |
||||
}) |
||||
shippedDate: Date; |
||||
|
||||
@Column({ |
||||
type: DataType.STRING, |
||||
allowNull: true, |
||||
}) |
||||
shipperName: string; |
||||
|
||||
@ForeignKey(() => User) |
||||
@Column |
||||
userId: number; |
||||
|
||||
@BelongsTo(() => User) |
||||
user: User; |
||||
|
||||
@BelongsTo(() => OrderStatuses) |
||||
orderStatuses: OrderStatuses; |
||||
} |
@ -0,0 +1,9 @@ |
||||
import { OrderStatuses } from './entities/order-status.entity'; |
||||
import { ORDER_STATUSES_REPOSITORY } from '../../core/constants'; |
||||
|
||||
export const orderStatusesProviders = [ |
||||
{ |
||||
provide: ORDER_STATUSES_REPOSITORY, |
||||
useValue: OrderStatuses, |
||||
}, |
||||
]; |
@ -0,0 +1,42 @@ |
||||
import { |
||||
Controller, |
||||
Get, |
||||
Post, |
||||
Body, |
||||
Patch, |
||||
Param, |
||||
Delete, |
||||
} from '@nestjs/common'; |
||||
import { OrdersService } from './orders.service'; |
||||
import { CreateOrderDto } from './dto/create-order.dto'; |
||||
import { UpdateOrderDto } from './dto/update-order.dto'; |
||||
|
||||
@Controller('orders') |
||||
export class OrdersController { |
||||
constructor(private readonly ordersService: OrdersService) {} |
||||
|
||||
@Post() |
||||
create(@Body() createOrderDto: CreateOrderDto) { |
||||
return this.ordersService.create(createOrderDto); |
||||
} |
||||
|
||||
@Get() |
||||
findAll() { |
||||
return this.ordersService.findAll(); |
||||
} |
||||
|
||||
@Get(':id') |
||||
findOne(@Param('id') id: string) { |
||||
return this.ordersService.findOne(+id); |
||||
} |
||||
|
||||
@Patch(':id') |
||||
update(@Param('id') id: string, @Body() updateOrderDto: UpdateOrderDto) { |
||||
return this.ordersService.update(+id, updateOrderDto); |
||||
} |
||||
|
||||
@Delete(':id') |
||||
remove(@Param('id') id: string) { |
||||
return this.ordersService.remove(+id); |
||||
} |
||||
} |
@ -0,0 +1,11 @@ |
||||
import { Module } from '@nestjs/common'; |
||||
import { OrdersService } from './orders.service'; |
||||
import { OrdersController } from './orders.controller'; |
||||
import { ordersProviders } from './orders.providers'; |
||||
|
||||
@Module({ |
||||
controllers: [OrdersController], |
||||
providers: [OrdersService, ...ordersProviders], |
||||
exports: [OrdersService], |
||||
}) |
||||
export class OrdersModule {} |
@ -0,0 +1,9 @@ |
||||
import { Order } from './entities/order.entity'; |
||||
import { ORDER_REPOSITORY } from '../../core/constants'; |
||||
|
||||
export const ordersProviders = [ |
||||
{ |
||||
provide: ORDER_REPOSITORY, |
||||
useValue: Order, |
||||
}, |
||||
]; |
@ -0,0 +1,49 @@ |
||||
import { Inject, Injectable } from '@nestjs/common'; |
||||
import { CreateOrderDto, UpdateOrderDto } from './dto'; |
||||
import { Order } from './entities/order.entity'; |
||||
import { ORDER_REPOSITORY } from 'src/core/constants'; |
||||
import { OrderStatuses } from './entities/order-status.entity'; |
||||
import { User } from '../users/entities/user.entity'; |
||||
|
||||
@Injectable() |
||||
export class OrdersService { |
||||
constructor( |
||||
@Inject(ORDER_REPOSITORY) private readonly orderRepository: typeof Order, |
||||
) {} |
||||
|
||||
async create(createOrderDto: CreateOrderDto) { |
||||
return await this.orderRepository.create(createOrderDto); |
||||
} |
||||
|
||||
async findAll() { |
||||
return await this.orderRepository.findAll(); |
||||
|
||||
// For joining all the associated tables with the orders table
|
||||
// return await this.orderRepository.findAll({
|
||||
// include: [{ model: User }, { model: OrderStatuses }],
|
||||
// });
|
||||
} |
||||
|
||||
async findOne(id: number) { |
||||
return await this.orderRepository.findAll({ |
||||
where: { id }, |
||||
}); |
||||
} |
||||
|
||||
async update(id: number, updateOrderDto: UpdateOrderDto) { |
||||
const [numberOfAffectedRows, [updatedOrder]] = |
||||
await this.orderRepository.update( |
||||
{ ...updateOrderDto }, |
||||
{ where: { id }, returning: true }, |
||||
); |
||||
return { numberOfAffectedRows, updatedOrder }; |
||||
} |
||||
|
||||
async remove(id: number) { |
||||
const deletedOrder = await this.findOne(id); |
||||
|
||||
await this.orderRepository.destroy({ where: { id } }); |
||||
|
||||
return deletedOrder; |
||||
} |
||||
} |
Loading…
Reference in new issue