Skip to content

Commit

Permalink
SWD-29-UserEndPoint-Create-Read-Product
Browse files Browse the repository at this point in the history
  • Loading branch information
thaintd authored and thongdanghoang committed May 29, 2024
1 parent 4ca7ae8 commit 300725f
Show file tree
Hide file tree
Showing 9 changed files with 105 additions and 104 deletions.
22 changes: 0 additions & 22 deletions backend/src/app.controller.spec.ts

This file was deleted.

2 changes: 1 addition & 1 deletion backend/src/app.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ import {Controller, Get, UseGuards} from '@nestjs/common';
import {JwtAuthGuard} from '@5stones/nest-oidc';

@Controller()
@UseGuards(JwtAuthGuard)
export class AppController {
@Get()
@UseGuards(JwtAuthGuard)
getHello(): any {
return 'Hello World!';
}
Expand Down
6 changes: 1 addition & 5 deletions backend/src/app.service.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
import {Injectable} from '@nestjs/common';

@Injectable()
export class AppService {
getHello(): string {
return 'Hello World!';
}
}
export class AppService {}
11 changes: 11 additions & 0 deletions backend/src/global/globalClass.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
export class ResponseData<D> {
data: D | D[];
message: string;
statusCode: number;
constructor(data: D | D[], message: string, statusCode: number) {
this.data = data;
this.message = message;
this.statusCode = statusCode;
return this;
}
}
29 changes: 29 additions & 0 deletions backend/src/global/globalEnum.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
export enum HttpStatus {
OK = 200,
CREATED = 201,
ACCEPTED = 202,
NO_CONTENT = 204,
BAD_REQUEST = 400,
UNAUTHORIZED = 401,
FORBIDDEN = 403,
NOT_FOUND = 404,
INTERNAL_SERVER_ERROR = 500,
BAD_GATEWAY = 502,
SERVICE_UNAVAILABLE = 503,
GATEWAY_TIMEOUT = 504
}

export const HttpMessage = {
OK: 'OK',
CREATED: 'Created',
ACCEPTED: 'Accepted',
NO_CONTENT: 'No Content',
BAD_REQUEST: 'Bad Request',
UNAUTHORIZED: 'Unauthorized',
FORBIDDEN: 'Forbidden',
NOT_FOUND: 'Not Found',
INTERNAL_SERVER_ERROR: 'Internal Server Error',
BAD_GATEWAY: 'Bad Gateway',
SERVICE_UNAVAILABLE: 'Service Unavailable',
GATEWAY_TIMEOUT: 'Gateway Timeout'
};
61 changes: 52 additions & 9 deletions backend/src/modules/product/product.controller.ts
Original file line number Diff line number Diff line change
@@ -1,25 +1,68 @@
import {Body, Controller, Get, Post, UseGuards} from '@nestjs/common';
import {Body, Controller, Get, Param, Post, UseGuards} from '@nestjs/common';
import {JwtAuthGuard} from '@5stones/nest-oidc';
import {ProductService} from './product.service';
import {CreateProductDto} from './product.dto';
import {Product} from './product.entity';
import {ResponseData} from '../../global/globalClass';
import {HttpMessage, HttpStatus} from '../../global/globalEnum';

@Controller('products')
@UseGuards(JwtAuthGuard)
export class ProductController {
constructor(private readonly productService: ProductService) {}

@Post()
@UseGuards(JwtAuthGuard)
async createProduct(
@Body() createProductDto: CreateProductDto
): Promise<Product> {
const newProduct =
await this.productService.createProduct(createProductDto);
return newProduct;
): Promise<ResponseData<Product>> {
try {
const newProduct =
await this.productService.createProduct(createProductDto);
return new ResponseData(
newProduct,
HttpMessage.CREATED,
HttpStatus.CREATED
);
} catch (error) {
return new ResponseData<Product>(
null,
HttpMessage.INTERNAL_SERVER_ERROR,
HttpStatus.INTERNAL_SERVER_ERROR
);
}
}

@Get('/:id')
async getProductDetails(
@Param('id') id: number
): Promise<ResponseData<Product>> {
try {
const product = await this.productService.getProductDetails(id);
return new ResponseData(product, HttpMessage.OK, HttpStatus.OK);
} catch (error) {
return new ResponseData<Product>(
null,
HttpMessage.INTERNAL_SERVER_ERROR,
HttpStatus.INTERNAL_SERVER_ERROR
);
}
}

@Get()
@UseGuards(JwtAuthGuard)
findAll(): string {
return this.productService.findAll();
async getAllProducts(): Promise<ResponseData<Product[]>> {
try {
const products = await this.productService.getAllProducts();
return new ResponseData<Product[]>(
products,
HttpMessage.OK,
HttpStatus.OK
);
} catch (error) {
return new ResponseData<Product[]>(
null,
HttpMessage.INTERNAL_SERVER_ERROR,
HttpStatus.INTERNAL_SERVER_ERROR
);
}
}
}
21 changes: 10 additions & 11 deletions backend/src/modules/product/product.service.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {Injectable, InternalServerErrorException} from '@nestjs/common';
import {Injectable} from '@nestjs/common';
import {InjectRepository} from '@nestjs/typeorm';
import {Repository} from 'typeorm';
import {Product} from './product.entity';
Expand All @@ -11,18 +11,17 @@ export class ProductService {
private readonly productRepository: Repository<Product>
) {}

findAll(): string {
return 'Hello World!';
async getAllProducts(): Promise<Product[]> {
return await this.productRepository.find();
}

async createProduct(createProductDto: CreateProductDto): Promise<Product> {
try {
const product = this.productRepository.create(createProductDto);
await this.productRepository.save(product);
return product;
} catch (error) {
console.error(error);
throw new InternalServerErrorException('Error creating product');
}
const product = this.productRepository.create(createProductDto);
await this.productRepository.save(product);
return product;
}
async getProductDetails(id: number): Promise<Product> {
const products = await this.productRepository.findByIds([id]);
return products[0];
}
}
2 changes: 1 addition & 1 deletion backend/src/modules/user/userSynchronize.interceptor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import {UserEntity} from './user.entity';

@Injectable()
export class UserSynchronizeInterceptor implements NestInterceptor {
private readonly logger = new Logger(UserSynchronizeInterceptor.name);
private readonly logger: Logger = new Logger(UserSynchronizeInterceptor.name);

constructor(private readonly usersService: UsersService) {}

Expand Down
55 changes: 0 additions & 55 deletions infrastructure/docker-compose.yml

This file was deleted.

0 comments on commit 300725f

Please sign in to comment.