Skip to content

Commit

Permalink
feat: 馃幐 add product api
Browse files Browse the repository at this point in the history
  • Loading branch information
yeukfei02 committed Apr 26, 2022
1 parent 6c4d6de commit 71f4c29
Show file tree
Hide file tree
Showing 13 changed files with 765 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/app.module.ts
Expand Up @@ -10,6 +10,7 @@ import { TagModule } from './tag/tag.module';
import { CommentModule } from './comment/comment.module';
import { TodoModule } from './todo/todo.module';
import { CartModule } from './cart/cart.module';
import { ProductModule } from './product/product.module';
import { QuoteModule } from './quote/quote.module';

@Module({
Expand All @@ -23,6 +24,7 @@ import { QuoteModule } from './quote/quote.module';
CommentModule,
TodoModule,
CartModule,
ProductModule,
QuoteModule,
],
controllers: [AppController],
Expand Down
36 changes: 36 additions & 0 deletions src/product/dto/create-product.dto.ts
@@ -0,0 +1,36 @@
import { ApiProperty } from '@nestjs/swagger';

export class CreateProductDto {
@ApiProperty()
title: string;

@ApiProperty()
description: string;

@ApiProperty()
price: number;

@ApiProperty()
discount_percentage: number;

@ApiProperty()
rating: number;

@ApiProperty()
stock: number;

@ApiProperty()
brand: string;

@ApiProperty()
category: string;

@ApiProperty()
thumbnail: string;

@ApiProperty({ default: [], isArray: true })
images: string[];

@ApiProperty()
cart_id: string;
}
36 changes: 36 additions & 0 deletions src/product/dto/update-product.dto.ts
@@ -0,0 +1,36 @@
import { ApiProperty } from '@nestjs/swagger';

export class UpdateProductDto {
@ApiProperty()
title: string;

@ApiProperty()
description: string;

@ApiProperty()
price: number;

@ApiProperty()
discount_percentage: number;

@ApiProperty()
rating: number;

@ApiProperty()
stock: number;

@ApiProperty()
brand: string;

@ApiProperty()
category: string;

@ApiProperty()
thumbnail: string;

@ApiProperty({ default: [], isArray: true })
images: string[];

@ApiProperty()
cart_id: string;
}
18 changes: 18 additions & 0 deletions src/product/product.controller.spec.ts
@@ -0,0 +1,18 @@
import { Test, TestingModule } from '@nestjs/testing';
import { ProductController } from './product.controller';

describe('ProductController', () => {
let controller: ProductController;

beforeEach(async () => {
const module: TestingModule = await Test.createTestingModule({
controllers: [ProductController],
}).compile();

controller = module.get<ProductController>(ProductController);
});

it('should be defined', () => {
expect(controller).toBeDefined();
});
});
164 changes: 164 additions & 0 deletions src/product/product.controller.ts
@@ -0,0 +1,164 @@
import {
Controller,
Post,
Get,
Put,
Patch,
Delete,
Body,
Param,
Query,
} from '@nestjs/common';
import { ProductService } from './product.service';
import { CreateProductDto } from './dto/create-product.dto';
import { UpdateProductDto } from './dto/update-product.dto';

import {
ApiBearerAuth,
ApiHeader,
ApiQuery,
ApiResponse,
} from '@nestjs/swagger';
import { CreateProductResponse } from './response/create-product.response';
import { GetProductsResponse } from './response/get-products.response';
import { GetProductByIdResponse } from './response/get-product-by-id.response';
import { UpdateProductByIdResponse } from './response/update-product-by-id.response';
import { DeleteProductByIdResponse } from './response/delete-product-by-id.response';

@ApiBearerAuth()
@ApiHeader({
name: 'Authorization',
description: 'Jwt Token',
})
@Controller('product')
export class ProductController {
constructor(private readonly productService: ProductService) {}

@Post()
@ApiResponse({
status: 201,
description: 'Successful response',
type: CreateProductResponse,
})
async createProduct(
@Body() createProductDto: CreateProductDto,
): Promise<any> {
const product = await this.productService.createProduct(createProductDto);

const response = { message: 'createProduct', product: product };
return response;
}

@Get()
@ApiQuery({
name: 'cart_id',
description: 'cart_id',
required: false,
type: String,
})
@ApiQuery({
name: 'page',
description: 'page',
required: false,
type: String,
})
@ApiQuery({
name: 'per_page',
description: 'per_page',
required: false,
type: String,
})
@ApiResponse({
status: 200,
description: 'Successful response',
type: GetProductsResponse,
})
async getProducts(
@Query('cart_id') cart_id: string,
@Query('page') page: string,
@Query('per_page') perPage: string,
): Promise<any> {
const cartId = cart_id;
const pageInt = page ? parseInt(page, 10) : 1;
const perPageInt = page ? parseInt(perPage, 10) : 20;

const products = await this.productService.getProducts(
cartId,
pageInt,
perPageInt,
);

const response = {
message: 'getProducts',
data: products,
total: products.length,
page: pageInt,
limit: perPageInt,
};
return response;
}

@Get('/:id')
@ApiResponse({
status: 200,
description: 'Successful response',
type: GetProductByIdResponse,
})
async getProductById(@Param('id') id: string): Promise<any> {
const product = await this.productService.getProductById(id);

const response = { message: 'getProductById', product: product };
return response;
}

@Put('/:id')
@ApiResponse({
status: 200,
description: 'Successful response',
type: UpdateProductByIdResponse,
})
async updateProductById(
@Param('id') id: string,
@Body() updateProductDto: UpdateProductDto,
): Promise<any> {
const product = await this.productService.updateProductById(
id,
updateProductDto,
);

const response = { message: 'updateProductById', product: product };
return response;
}

@Patch('/:id')
@ApiResponse({
status: 200,
description: 'Successful response',
type: UpdateProductByIdResponse,
})
async patchProductById(
@Param('id') id: string,
@Body() updateProductDto: UpdateProductDto,
): Promise<any> {
const product = await this.productService.updateProductById(
id,
updateProductDto,
);

const response = { message: 'updateProductById', product: product };
return response;
}

@Delete('/:id')
@ApiResponse({
status: 200,
description: 'Successful response',
type: DeleteProductByIdResponse,
})
async deleteProductById(@Param('id') id: string): Promise<any> {
const product = await this.productService.deleteProductById(id);

const response = { message: 'deleteProductById', product: product };
return response;
}
}
16 changes: 16 additions & 0 deletions src/product/product.module.ts
@@ -0,0 +1,16 @@
import { Module, MiddlewareConsumer } from '@nestjs/common';
import { ProductController } from './product.controller';
import { ProductService } from './product.service';
import { PrismaService } from '../prisma.service';
import { AuthMiddleware } from '../auth.middleware';

@Module({
imports: [],
controllers: [ProductController],
providers: [ProductService, PrismaService],
})
export class ProductModule {
configure(consumer: MiddlewareConsumer) {
consumer.apply(AuthMiddleware).forRoutes('product');
}
}
18 changes: 18 additions & 0 deletions src/product/product.service.spec.ts
@@ -0,0 +1,18 @@
import { Test, TestingModule } from '@nestjs/testing';
import { ProductService } from './product.service';

describe('ProductService', () => {
let service: ProductService;

beforeEach(async () => {
const module: TestingModule = await Test.createTestingModule({
providers: [ProductService],
}).compile();

service = module.get<ProductService>(ProductService);
});

it('should be defined', () => {
expect(service).toBeDefined();
});
});

0 comments on commit 71f4c29

Please sign in to comment.