Skip to content

Commit

Permalink
Merge pull request #2 from WanderDinizVeloso/feature/orders
Browse files Browse the repository at this point in the history
Feature/orders
  • Loading branch information
WanderDinizVeloso authored Dec 3, 2023
2 parents d07f085 + f8557bc commit de8a988
Show file tree
Hide file tree
Showing 11 changed files with 237 additions and 4 deletions.
3 changes: 2 additions & 1 deletion src/app.module.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { Module } from '@nestjs/common';
import { MongooseModule } from '@nestjs/mongoose';
import { OrdersModule } from './orders/orders.module';

@Module({
imports: [MongooseModule.forRoot(process.env.MONGO_URI)],
imports: [MongooseModule.forRoot(process.env.MONGO_URI), OrdersModule],
controllers: [],
providers: [],
exports: [],
Expand Down
4 changes: 1 addition & 3 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,7 @@ import { version, name, description } from '../package.json';
async function bootstrap(): Promise<void> {
const app = await NestFactory.create(AppModule);

app.useGlobalPipes(
new ValidationPipe({ whitelist: true, forbidNonWhitelisted: true, transform: true }),
);
app.useGlobalPipes(new ValidationPipe({ whitelist: true, transform: true }));

const swaggerConfig = new DocumentBuilder()
.setTitle(name)
Expand Down
17 changes: 17 additions & 0 deletions src/orders/dto/create-order.dto.ts
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;
}
4 changes: 4 additions & 0 deletions src/orders/dto/update-order.dto.ts
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) {}
9 changes: 9 additions & 0 deletions src/orders/interface/order.interface.ts
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 };
20 changes: 20 additions & 0 deletions src/orders/orders.controller.spec.ts
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();
});
});
55 changes: 55 additions & 0 deletions src/orders/orders.controller.ts
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);
}
}
13 changes: 13 additions & 0 deletions src/orders/orders.module.ts
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 {}
18 changes: 18 additions & 0 deletions src/orders/orders.service.spec.ts
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();
});
});
65 changes: 65 additions & 0 deletions src/orders/orders.service.ts
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 };
}
}
33 changes: 33 additions & 0 deletions src/orders/schema/order.schema.ts
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 };

0 comments on commit de8a988

Please sign in to comment.