crud auth role
This commit is contained in:
parent
3874df5cea
commit
cbe54233ca
3 changed files with 203 additions and 7 deletions
113
src/controllers/AuthRoleAttrController.ts
Normal file
113
src/controllers/AuthRoleAttrController.ts
Normal file
|
|
@ -0,0 +1,113 @@
|
||||||
|
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 { AuthRoleAttr, CreateAuthRoleAttr, UpdateAuthRoleAttr } from "../entities/AuthRoleAttr";
|
||||||
|
import { AuthRole } from "../entities/AuthRole";
|
||||||
|
import { AuthSys } from "../entities/AuthSys";
|
||||||
|
|
||||||
|
@Route("api/v1/org/auth/authRoleAttr")
|
||||||
|
@Tags("AuthRoleAttr")
|
||||||
|
@Security("bearerAuth")
|
||||||
|
export class AuthRoleAttrController extends Controller {
|
||||||
|
private authRoleRepo = AppDataSource.getRepository(AuthRole);
|
||||||
|
private authSysRepo = AppDataSource.getRepository(AuthSys);
|
||||||
|
private authRoleAttrRepo = AppDataSource.getRepository(AuthRoleAttr);
|
||||||
|
|
||||||
|
@Get("list")
|
||||||
|
public async listAuthRoleAttr() {
|
||||||
|
const getList = await this.authRoleAttrRepo.find();
|
||||||
|
if (!getList || getList.length === 0) {
|
||||||
|
throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูล");
|
||||||
|
}
|
||||||
|
return new HttpSuccess(getList);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Get("{roleAttrId}")
|
||||||
|
public async detailAuthRoleAttr(@Path() roleAttrId: string) {
|
||||||
|
const getDetail = await this.authRoleAttrRepo.findBy({ id: roleAttrId });
|
||||||
|
if (!getDetail) {
|
||||||
|
throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูล");
|
||||||
|
}
|
||||||
|
return new HttpSuccess(getDetail);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Post()
|
||||||
|
public async newAuthRoleAttr(@Request() req: RequestWithUser, @Body() body: CreateAuthRoleAttr) {
|
||||||
|
|
||||||
|
const chkAuthRole = await this.authRoleRepo.findOneBy({ id: body.authRoleId });
|
||||||
|
if (!chkAuthRole) {
|
||||||
|
throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูล roleId");
|
||||||
|
}
|
||||||
|
|
||||||
|
const chkAuthSys = await this.authSysRepo.findOneBy({ id: body.authSysId });
|
||||||
|
if (!chkAuthSys) {
|
||||||
|
throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูล systemId");
|
||||||
|
}
|
||||||
|
|
||||||
|
const data = new AuthRoleAttr();
|
||||||
|
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.authRoleAttrRepo.save(data);
|
||||||
|
|
||||||
|
return new HttpSuccess();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Patch("{roleAttrId}")
|
||||||
|
public async editAuthRoleAttr(
|
||||||
|
@Body() requestBody: UpdateAuthRoleAttr,
|
||||||
|
@Request() req: RequestWithUser,
|
||||||
|
@Path() roleAttrId: string,
|
||||||
|
) {
|
||||||
|
const record = await this.authRoleAttrRepo.findOneBy({ id: roleAttrId });
|
||||||
|
if (!record) throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูล");
|
||||||
|
|
||||||
|
const chkAuthRole = await this.authRoleRepo.findOneBy({ id: requestBody.authRoleId });
|
||||||
|
if (!chkAuthRole) {
|
||||||
|
throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูล roleId");
|
||||||
|
}
|
||||||
|
|
||||||
|
const chkAuthSys = await this.authSysRepo.findOneBy({ id: requestBody.authSysId });
|
||||||
|
if (!chkAuthSys) {
|
||||||
|
throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูล systemId");
|
||||||
|
}
|
||||||
|
|
||||||
|
Object.assign(record, requestBody);
|
||||||
|
record.lastUpdateFullName = req.user.name;
|
||||||
|
|
||||||
|
await Promise.all([this.authRoleAttrRepo.save(record)]);
|
||||||
|
|
||||||
|
return new HttpSuccess();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Delete("{roleAttrId}")
|
||||||
|
public async deleteRole(@Path() roleAttrId: string) {
|
||||||
|
const result = await this.authRoleAttrRepo.delete({ id: roleAttrId });
|
||||||
|
|
||||||
|
if (result.affected == undefined || result.affected <= 0)
|
||||||
|
throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูล");
|
||||||
|
|
||||||
|
return new HttpSuccess();
|
||||||
|
}
|
||||||
|
}
|
||||||
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();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -25,11 +25,7 @@ export class AuthSysController extends Controller {
|
||||||
private authSysRepo = AppDataSource.getRepository(AuthSys);
|
private authSysRepo = AppDataSource.getRepository(AuthSys);
|
||||||
|
|
||||||
@Get("list")
|
@Get("list")
|
||||||
public async listAuthSys(@Request() request: { user: Record<string, any> }) {
|
public async listAuthSys() {
|
||||||
const profile = await this.authSysRepo.findOneBy({ id: request.user.sub });
|
|
||||||
if (!profile) {
|
|
||||||
throw new HttpError(HttpStatus.BAD_REQUEST, "ไม่พบ profile ดังกล่าว");
|
|
||||||
}
|
|
||||||
const getList = await this.authSysRepo.find();
|
const getList = await this.authSysRepo.find();
|
||||||
if (!getList || getList.length === 0) {
|
if (!getList || getList.length === 0) {
|
||||||
throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูล");
|
throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูล");
|
||||||
|
|
@ -84,8 +80,7 @@ export class AuthSysController extends Controller {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Delete("{systemId}")
|
@Delete("{systemId}")
|
||||||
public async deleteProfileAbility(@Path() systemId: string) {
|
public async deleteAuthSys(@Path() systemId: string) {
|
||||||
|
|
||||||
const result = await this.authSysRepo.delete({ id: systemId });
|
const result = await this.authSysRepo.delete({ id: systemId });
|
||||||
|
|
||||||
if (result.affected == undefined || result.affected <= 0)
|
if (result.affected == undefined || result.affected <= 0)
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue