-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2 from WanderDinizVeloso/feature/orders
Feature/orders
- Loading branch information
Showing
11 changed files
with
237 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import { ApiProperty } from '@nestjs/swagger'; | ||
import { IsNotEmpty, IsNotEmptyObject } from 'class-validator'; | ||
|
||
const PROPERTIES_DESCRIPTION = { | ||
customerName: 'Customer name. Requirements: not empty', | ||
description: 'Order description. Requirements: not empty', | ||
}; | ||
|
||
export class CreateOrderDto { | ||
@ApiProperty({ description: PROPERTIES_DESCRIPTION.customerName }) | ||
@IsNotEmptyObject() | ||
readonly customerName: string; | ||
|
||
@ApiProperty({ description: PROPERTIES_DESCRIPTION.description }) | ||
@IsNotEmpty() | ||
readonly description: string; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
import { PartialType } from '@nestjs/swagger'; | ||
import { CreateOrderDto } from './create-order.dto'; | ||
|
||
export class UpdateOrderDto extends PartialType(CreateOrderDto) {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
interface IPatchAndDeleteReturn { | ||
message: string; | ||
} | ||
|
||
interface IPostReturn extends IPatchAndDeleteReturn { | ||
_id: string; | ||
} | ||
|
||
export { IPatchAndDeleteReturn, IPostReturn }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import { Test, TestingModule } from '@nestjs/testing'; | ||
import { OrdersController } from './orders.controller'; | ||
import { OrdersService } from './orders.service'; | ||
|
||
describe('OrdersController', () => { | ||
let controller: OrdersController; | ||
|
||
beforeEach(async () => { | ||
const module: TestingModule = await Test.createTestingModule({ | ||
controllers: [OrdersController], | ||
providers: [OrdersService], | ||
}).compile(); | ||
|
||
controller = module.get<OrdersController>(OrdersController); | ||
}); | ||
|
||
it('should be defined', () => { | ||
expect(controller).toBeDefined(); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
import { Controller, Get, Post, Body, Patch, Param, Delete } from '@nestjs/common'; | ||
|
||
import { CreateOrderDto } from './dto/create-order.dto'; | ||
import { UpdateOrderDto } from './dto/update-order.dto'; | ||
import { IPatchAndDeleteReturn, IPostReturn } from './interface/order.interface'; | ||
import { OrdersService } from './orders.service'; | ||
import { Order } from './schema/order.schema'; | ||
import { ApiOperation, ApiTags } from '@nestjs/swagger'; | ||
|
||
const SUMMARY_MESSAGE = { | ||
create: 'Create order.', | ||
findAll: 'Search all orders.', | ||
findOne: 'Search for a order by ID.', | ||
update: 'Update order data by ID.', | ||
remove: 'Remove order by ID.', | ||
}; | ||
|
||
@ApiTags('Orders') | ||
@Controller('orders') | ||
export class OrdersController { | ||
constructor(private readonly ordersService: OrdersService) {} | ||
|
||
@Post() | ||
@ApiOperation({ summary: SUMMARY_MESSAGE.create }) | ||
async create(@Body() createOrderDto: CreateOrderDto): Promise<IPostReturn> { | ||
return this.ordersService.create(createOrderDto); | ||
} | ||
|
||
@Get() | ||
@ApiOperation({ summary: SUMMARY_MESSAGE.findAll }) | ||
async findAll(): Promise<Order[]> { | ||
return this.ordersService.findAll(); | ||
} | ||
|
||
@Get(':id') | ||
@ApiOperation({ summary: SUMMARY_MESSAGE.findOne }) | ||
async findOne(@Param('id') id: string): Promise<Order> { | ||
return this.ordersService.findOne(id); | ||
} | ||
|
||
@Patch(':id') | ||
@ApiOperation({ summary: SUMMARY_MESSAGE.update }) | ||
async update( | ||
@Param('id') id: string, | ||
@Body() updateOrderDto: UpdateOrderDto, | ||
): Promise<IPatchAndDeleteReturn> { | ||
return this.ordersService.update(id, updateOrderDto); | ||
} | ||
|
||
@Delete(':id') | ||
@ApiOperation({ summary: SUMMARY_MESSAGE.remove }) | ||
async remove(@Param('id') id: string): Promise<IPatchAndDeleteReturn> { | ||
return this.ordersService.remove(id); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import { Module } from '@nestjs/common'; | ||
import { OrdersService } from './orders.service'; | ||
import { OrdersController } from './orders.controller'; | ||
import { MongooseModule } from '@nestjs/mongoose'; | ||
import { Order, OrderSchema } from './schema/order.schema'; | ||
|
||
@Module({ | ||
imports: [MongooseModule.forFeature([{ name: Order.name, schema: OrderSchema }])], | ||
controllers: [OrdersController], | ||
providers: [OrdersService], | ||
exports: [], | ||
}) | ||
export class OrdersModule {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import { Test, TestingModule } from '@nestjs/testing'; | ||
import { OrdersService } from './orders.service'; | ||
|
||
describe('OrdersService', () => { | ||
let service: OrdersService; | ||
|
||
beforeEach(async () => { | ||
const module: TestingModule = await Test.createTestingModule({ | ||
providers: [OrdersService], | ||
}).compile(); | ||
|
||
service = module.get<OrdersService>(OrdersService); | ||
}); | ||
|
||
it('should be defined', () => { | ||
expect(service).toBeDefined(); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
import { Injectable, NotFoundException } from '@nestjs/common'; | ||
import { InjectModel } from '@nestjs/mongoose'; | ||
import { Model } from 'mongoose'; | ||
|
||
import { CreateOrderDto } from './dto/create-order.dto'; | ||
import { UpdateOrderDto } from './dto/update-order.dto'; | ||
import { IPatchAndDeleteReturn, IPostReturn } from './interface/order.interface'; | ||
import { Order, OrderDocument } from './schema/order.schema'; | ||
|
||
export const MESSAGES = { | ||
orderCreated: 'order created successfully.', | ||
orderUpdated: 'order updated successfully.', | ||
orderRemoved: 'order removed successfully.', | ||
orderNotFound: 'order not found or not active.', | ||
}; | ||
|
||
@Injectable() | ||
export class OrdersService { | ||
constructor(@InjectModel(Order.name) private orderModel: Model<OrderDocument>) {} | ||
async create(createOrderDto: CreateOrderDto): Promise<IPostReturn> { | ||
const { _id } = await this.orderModel.create({ ...createOrderDto, active: true }); | ||
|
||
return { _id, message: MESSAGES.orderCreated }; | ||
} | ||
|
||
async findAll(): Promise<Order[]> { | ||
return this.orderModel.find(); | ||
} | ||
|
||
async findOne(id: string): Promise<Order> { | ||
const order = this.orderModel.findOne({ _id: id }); | ||
|
||
if (!order) { | ||
throw new NotFoundException(MESSAGES.orderNotFound); | ||
} | ||
|
||
return order; | ||
} | ||
|
||
async update(id: string, updateOrderDto: UpdateOrderDto): Promise<IPatchAndDeleteReturn> { | ||
const order = this.orderModel.findOneAndUpdate({ _id: id, active: true }, updateOrderDto, { | ||
new: true, | ||
}); | ||
|
||
if (!order) { | ||
throw new NotFoundException(MESSAGES.orderNotFound); | ||
} | ||
|
||
return { message: MESSAGES.orderUpdated }; | ||
} | ||
|
||
async remove(id: string): Promise<IPatchAndDeleteReturn> { | ||
const order = this.orderModel.findOneAndUpdate( | ||
{ _id: id, active: true }, | ||
{ active: false }, | ||
{ new: true }, | ||
); | ||
|
||
if (!order) { | ||
throw new NotFoundException(MESSAGES.orderNotFound); | ||
} | ||
|
||
return { message: MESSAGES.orderRemoved }; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import { Prop, Schema, SchemaFactory, raw } from '@nestjs/mongoose'; | ||
import { Document } from 'mongoose'; | ||
|
||
type OrderDocument = Order & Document; | ||
|
||
@Schema({ timestamps: true, versionKey: false }) | ||
class Order extends Document { | ||
@Prop() | ||
customerName: string; | ||
|
||
@Prop() | ||
description: string; | ||
|
||
@Prop() | ||
preparerName: string; | ||
|
||
@Prop( | ||
raw({ | ||
ticketCreated: { type: Date }, | ||
preparationStarted: { type: Date }, | ||
preparationFinished: { type: Date }, | ||
customerReceived: { type: Date }, | ||
}), | ||
) | ||
status: Record<string, Date>; | ||
|
||
@Prop() | ||
active: boolean; | ||
} | ||
|
||
const OrderSchema = SchemaFactory.createForClass(Order); | ||
|
||
export { OrderDocument, Order, OrderSchema }; |