add profilio
This commit is contained in:
parent
28bb5e2246
commit
72a045fcfc
4 changed files with 214 additions and 0 deletions
162
src/controllers/PortfolioController.ts
Normal file
162
src/controllers/PortfolioController.ts
Normal file
|
|
@ -0,0 +1,162 @@
|
|||
import {
|
||||
Controller,
|
||||
Post,
|
||||
Put,
|
||||
Delete,
|
||||
Route,
|
||||
Security,
|
||||
Tags,
|
||||
Body,
|
||||
Path,
|
||||
Request,
|
||||
SuccessResponse,
|
||||
Response,
|
||||
Get,
|
||||
} from "tsoa";
|
||||
import { AppDataSource } from "../database/data-source";
|
||||
import HttpSuccess from "../interfaces/http-success";
|
||||
import HttpStatusCode from "../interfaces/http-status";
|
||||
import HttpError from "../interfaces/http-error";
|
||||
import { Not } from "typeorm";
|
||||
import { CreatePortfolio, Portfolio } from "../entities/Portfolio";
|
||||
import { RequestWithUser } from "../middlewares/user";
|
||||
|
||||
@Route("api/v1/development/portfolio")
|
||||
@Tags("Portfolio")
|
||||
@Security("bearerAuth")
|
||||
@Response(
|
||||
HttpStatusCode.INTERNAL_SERVER_ERROR,
|
||||
"เกิดข้อผิดพลาด ไม่สามารถแสดงรายการได้ กรุณาลองใหม่ในภายหลัง",
|
||||
)
|
||||
@SuccessResponse(HttpStatusCode.OK, "สำเร็จ")
|
||||
export class PortfolioController extends Controller {
|
||||
private portfolioRepository = AppDataSource.getRepository(Portfolio);
|
||||
|
||||
/**
|
||||
* API list รายการผลงาน
|
||||
*
|
||||
* @summary ORG_058 - CRUD ผลงาน (ADMIN) #62
|
||||
*
|
||||
*/
|
||||
@Get()
|
||||
async GetResult(@Request() request: RequestWithUser) {
|
||||
const _portfolio = await this.portfolioRepository.find({
|
||||
where: { createdUserId: request.user.sub },
|
||||
select: [
|
||||
"id",
|
||||
"name",
|
||||
"detail",
|
||||
"createdAt",
|
||||
"lastUpdatedAt",
|
||||
"createdFullName",
|
||||
"lastUpdateFullName",
|
||||
],
|
||||
order: { name: "ASC" },
|
||||
});
|
||||
return new HttpSuccess(_portfolio);
|
||||
}
|
||||
|
||||
/**
|
||||
* API รายละเอียดรายการผลงาน
|
||||
*
|
||||
* @summary ORG_058 - CRUD ผลงาน (ADMIN) #62
|
||||
*
|
||||
* @param {string} id Id ผลงาน
|
||||
*/
|
||||
@Get("{id}")
|
||||
async GetById(@Path() id: string, @Request() request: RequestWithUser) {
|
||||
const _portfolio = await this.portfolioRepository.findOne({
|
||||
where: { id: id, createdUserId: request.user.sub },
|
||||
select: ["id", "name", "detail"],
|
||||
});
|
||||
if (!_portfolio) {
|
||||
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลผลงานนี้");
|
||||
}
|
||||
|
||||
return new HttpSuccess(_portfolio);
|
||||
}
|
||||
|
||||
/**
|
||||
* API สร้างรายการ body ผลงาน
|
||||
*
|
||||
* @summary ORG_058 - CRUD ผลงาน (ADMIN) #62
|
||||
*
|
||||
*/
|
||||
@Post()
|
||||
async Post(
|
||||
@Body()
|
||||
requestBody: CreatePortfolio,
|
||||
@Request() request: RequestWithUser,
|
||||
) {
|
||||
const _portfolio = Object.assign(new Portfolio(), requestBody);
|
||||
if (!_portfolio) {
|
||||
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลผลงานนี้");
|
||||
}
|
||||
|
||||
const checkName = await this.portfolioRepository.findOne({
|
||||
where: { name: requestBody.name, createdUserId: request.user.sub },
|
||||
});
|
||||
|
||||
if (checkName) {
|
||||
throw new HttpError(HttpStatusCode.NOT_FOUND, "ชื่อนี้มีอยู่ในระบบแล้ว");
|
||||
}
|
||||
|
||||
_portfolio.createdUserId = request.user.sub;
|
||||
_portfolio.createdFullName = request.user.name;
|
||||
_portfolio.lastUpdateUserId = request.user.sub;
|
||||
_portfolio.lastUpdateFullName = request.user.name;
|
||||
await this.portfolioRepository.save(_portfolio);
|
||||
return new HttpSuccess(_portfolio.id);
|
||||
}
|
||||
|
||||
/**
|
||||
* API แก้ไขรายการ body ผลงาน
|
||||
*
|
||||
* @summary ORG_058 - CRUD ผลงาน (ADMIN) #62
|
||||
*
|
||||
* @param {string} id Id ผลงาน
|
||||
*/
|
||||
@Put("{id}")
|
||||
async Put(
|
||||
@Path() id: string,
|
||||
@Body()
|
||||
requestBody: CreatePortfolio,
|
||||
@Request() request: RequestWithUser,
|
||||
) {
|
||||
const _portfolio = await this.portfolioRepository.findOne({ where: { id: id } });
|
||||
if (!_portfolio) {
|
||||
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลผลงานนี้");
|
||||
}
|
||||
const checkName = await this.portfolioRepository.findOne({
|
||||
where: { id: Not(id), name: requestBody.name, createdUserId: request.user.sub },
|
||||
});
|
||||
if (checkName) {
|
||||
throw new HttpError(HttpStatusCode.NOT_FOUND, "ชื่อนี้มีอยู่ในระบบแล้ว");
|
||||
}
|
||||
|
||||
_portfolio.lastUpdateUserId = request.user.sub;
|
||||
_portfolio.lastUpdateFullName = request.user.name;
|
||||
Object.assign(_portfolio, requestBody);
|
||||
await this.portfolioRepository.save(_portfolio);
|
||||
return new HttpSuccess(_portfolio.id);
|
||||
}
|
||||
|
||||
/**
|
||||
* API ลบรายการผลงาน
|
||||
*
|
||||
* @summary ORG_058 - CRUD ผลงาน (ADMIN) #62
|
||||
*
|
||||
* @param {string} id Id ผลงาน
|
||||
*/
|
||||
@Delete("{id}")
|
||||
async Delete(@Path() id: string, @Request() request: RequestWithUser) {
|
||||
const _delPortfolio = await this.portfolioRepository.findOne({
|
||||
where: { id: id, createdUserId: request.user.sub },
|
||||
});
|
||||
if (!_delPortfolio) {
|
||||
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลผลงานนี้");
|
||||
}
|
||||
await this.portfolioRepository.delete(_delPortfolio.id);
|
||||
return new HttpSuccess();
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue