-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
SWD-29-UserEndPoint-Create-Read-Product
- Loading branch information
Showing
9 changed files
with
105 additions
and
104 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
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 |
---|---|---|
@@ -1,8 +1,4 @@ | ||
import {Injectable} from '@nestjs/common'; | ||
|
||
@Injectable() | ||
export class AppService { | ||
getHello(): string { | ||
return 'Hello World!'; | ||
} | ||
} | ||
export class AppService {} |
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,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; | ||
} | ||
} |
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,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' | ||
}; |
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 |
---|---|---|
@@ -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 | ||
); | ||
} | ||
} | ||
} |
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 was deleted.
Oops, something went wrong.