add api web service
This commit is contained in:
parent
dab30e6325
commit
89d22b0274
4 changed files with 779 additions and 0 deletions
79
src/controllers/ApiWebServiceController.ts
Normal file
79
src/controllers/ApiWebServiceController.ts
Normal file
|
|
@ -0,0 +1,79 @@
|
|||
import {
|
||||
Controller,
|
||||
Post,
|
||||
Route,
|
||||
Security,
|
||||
Tags,
|
||||
Body,
|
||||
Path,
|
||||
Request,
|
||||
Response,
|
||||
Get,
|
||||
Query,
|
||||
} 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 { ApiName } from "../entities/ApiName";
|
||||
import { Profile } from "../entities/Profile";
|
||||
@Route("api/v2/org/api-service")
|
||||
@Tags("ApiKey")
|
||||
// @Security("bearerAuth")
|
||||
@Response(
|
||||
HttpStatusCode.INTERNAL_SERVER_ERROR,
|
||||
"เกิดข้อผิดพลาด ไม่สามารถแสดงรายการได้ กรุณาลองใหม่ในภายหลัง",
|
||||
)
|
||||
export class ApiWebServiceController extends Controller {
|
||||
private apiNameRepository = AppDataSource.getRepository(ApiName);
|
||||
|
||||
/**
|
||||
* list fields by systems
|
||||
* @summary รายการ fields ตาม systems
|
||||
*/
|
||||
@Get("/:system/:code")
|
||||
async listAttribute(
|
||||
@Path("system") system: "registry" | "registry_emp" | "registry_temp" | "organization",
|
||||
@Path("code") code: string,
|
||||
@Query("page") page: number = 1,
|
||||
@Query("pageSize") pageSize: number = 100,
|
||||
): Promise<HttpSuccess | HttpError> {
|
||||
try {
|
||||
const apiName = await this.apiNameRepository.findOne({
|
||||
where: { code },
|
||||
select: ["id", "code", "methodApi", "system", "isActive"],
|
||||
relations: ["apiAttributes"],
|
||||
order: {
|
||||
apiAttributes: {
|
||||
ordering: "ASC",
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
if (!apiName || apiName.system != system || !apiName.isActive || apiName.methodApi != "GET") {
|
||||
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบ API ที่ระบุ");
|
||||
}
|
||||
|
||||
const offset = (page - 1) * pageSize;
|
||||
|
||||
const propertyKey = apiName.apiAttributes.map((attr) => `${attr.tbName}.${attr.propertyKey}`);
|
||||
const queryBuilder = AppDataSource.getRepository(Profile)
|
||||
.createQueryBuilder("Profile")
|
||||
.select(propertyKey);
|
||||
|
||||
const [items, total] = await queryBuilder
|
||||
.skip(offset)
|
||||
.take(pageSize)
|
||||
.orderBy("Profile.createdAt", "DESC")
|
||||
.getManyAndCount();
|
||||
|
||||
return new HttpSuccess({ items, total });
|
||||
} catch (error) {
|
||||
throw new HttpError(
|
||||
HttpStatusCode.INTERNAL_SERVER_ERROR,
|
||||
(error instanceof Error ? error.message : String(error)) ||
|
||||
"เกิดข้อผิดพลาด ไม่สามารถแสดงรายการได้ กรุณาลองใหม่ในภายหลัง",
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue