crud auth role
This commit is contained in:
parent
3874df5cea
commit
cbe54233ca
3 changed files with 203 additions and 7 deletions
88
src/controllers/AuthRoleController.ts
Normal file
88
src/controllers/AuthRoleController.ts
Normal file
|
|
@ -0,0 +1,88 @@
|
|||
import {
|
||||
Body,
|
||||
Controller,
|
||||
Delete,
|
||||
Get,
|
||||
Patch,
|
||||
Path,
|
||||
Post,
|
||||
Request,
|
||||
Route,
|
||||
Security,
|
||||
Tags,
|
||||
} from "tsoa";
|
||||
import { AppDataSource } from "../database/data-source";
|
||||
import { RequestWithUser } from "../middlewares/user";
|
||||
import HttpError from "../interfaces/http-error";
|
||||
import HttpStatus from "../interfaces/http-status";
|
||||
import HttpSuccess from "../interfaces/http-success";
|
||||
import { AuthRole, CreateAuthRole, UpdateAuthRole } from "../entities/AuthRole";
|
||||
|
||||
@Route("api/v1/org/auth/authRole")
|
||||
@Tags("AuthRole")
|
||||
@Security("bearerAuth")
|
||||
export class AuthRoleController extends Controller {
|
||||
private authRoleRepo = AppDataSource.getRepository(AuthRole);
|
||||
|
||||
@Get("list")
|
||||
public async listAuthRole() {
|
||||
const getList = await this.authRoleRepo.find();
|
||||
if (!getList || getList.length === 0) {
|
||||
throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูล");
|
||||
}
|
||||
return new HttpSuccess(getList);
|
||||
}
|
||||
|
||||
@Get("{roleId}")
|
||||
public async detailAuthRole(@Path() roleId: string) {
|
||||
const getDetail = await this.authRoleRepo.findBy({ id: roleId });
|
||||
if (!getDetail) {
|
||||
throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูล");
|
||||
}
|
||||
return new HttpSuccess(getDetail);
|
||||
}
|
||||
|
||||
@Post()
|
||||
public async newAuthRole(@Request() req: RequestWithUser, @Body() body: CreateAuthRole) {
|
||||
|
||||
const data = new AuthRole();
|
||||
const meta = {
|
||||
createdUserId: req.user.sub,
|
||||
createdFullName: req.user.name,
|
||||
lastUpdateUserId: req.user.sub,
|
||||
lastUpdateFullName: req.user.name,
|
||||
};
|
||||
|
||||
Object.assign(data, { ...body, ...meta });
|
||||
|
||||
await this.authRoleRepo.save(data);
|
||||
|
||||
return new HttpSuccess();
|
||||
}
|
||||
|
||||
@Patch("{roleId}")
|
||||
public async editAuthRole(
|
||||
@Body() requestBody: UpdateAuthRole,
|
||||
@Request() req: RequestWithUser,
|
||||
@Path() roleId: string,
|
||||
) {
|
||||
const record = await this.authRoleRepo.findOneBy({ id: roleId });
|
||||
if (!record) throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูล");
|
||||
Object.assign(record, requestBody);
|
||||
record.lastUpdateFullName = req.user.name;
|
||||
|
||||
await Promise.all([this.authRoleRepo.save(record)]);
|
||||
|
||||
return new HttpSuccess();
|
||||
}
|
||||
|
||||
@Delete("{roleId}")
|
||||
public async deleteRole(@Path() roleId: string) {
|
||||
const result = await this.authRoleRepo.delete({ id: roleId });
|
||||
|
||||
if (result.affected == undefined || result.affected <= 0)
|
||||
throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูล");
|
||||
|
||||
return new HttpSuccess();
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue