👔 create boilerplate for user role module

add a few boilerplate including get by id and name for user role module
This commit is contained in:
Rafi Arrafif
2025-07-20 22:21:48 +07:00
parent 5c6e072622
commit e60bf0fd3e
17 changed files with 120 additions and 4 deletions

View File

@ -0,0 +1,13 @@
import { Context } from "elysia";
import { mainErrorHandler } from "../../../helpers/error/handler";
import { getUserRoleByIdSchema } from "../schemas/getUserRoleById.schema";
import { findUserRoleByIdService } from "../services/http/findUserRoleById.service";
export const getUserRoleByIdController = async (ctx: Context) => {
try {
const params = getUserRoleByIdSchema.parse(ctx.params);
return await findUserRoleByIdService(params.id);
} catch (error) {
return mainErrorHandler(ctx.set, error);
}
};

View File

@ -0,0 +1,13 @@
import { Context } from "elysia";
import { mainErrorHandler } from "../../../helpers/error/handler";
import { getUserRoleByNameSchema } from "../schemas/getUserRoleByName.schema";
import { findUserRoleByNameService } from "../services/http/findUserRoleByName.service";
export const getUserRoleByNameController = async (ctx: Context) => {
try {
const params = getUserRoleByNameSchema.parse(ctx.params);
return await findUserRoleByNameService(params.name);
} catch (error) {
return mainErrorHandler(ctx.set, error);
}
};

View File

@ -0,0 +1,7 @@
import Elysia from "elysia";
import { getUserRoleByNameController } from "./controllers/getUserRoleByName.controller";
import { getUserRoleByIdController } from "./controllers/getUserRoleById.controller";
export const userRoleModule = new Elysia({ prefix: "/users/roles" })
.get("/n/:name", getUserRoleByNameController)
.get("/id/:id", getUserRoleByIdController);

View File

@ -0,0 +1,9 @@
import { userRoleModel } from "../userRole.model";
export const findUserRoleByIdRepository = async (id: string) => {
return await userRoleModel.findUnique({
where: {
id,
},
});
};

View File

@ -0,0 +1,9 @@
import { userRoleModel } from "../userRole.model";
export const findUserRoleByNameRepository = async (name: string) => {
return await userRoleModel.findUnique({
where: {
name,
},
});
};

View File

@ -0,0 +1,5 @@
import z from "zod";
export const getUserRoleByIdSchema = z.object({
id: z.string(),
});

View File

@ -0,0 +1,5 @@
import z from "zod";
export const getUserRoleByNameSchema = z.object({
name: z.string(),
});

View File

@ -0,0 +1,13 @@
import { ErrorForwarder } from "../../../../helpers/error/instances/forwarder";
import { findUserRoleService } from "../internal/findUserRole.service";
export const findUserRoleByIdService = async (id: string) => {
try {
return await findUserRoleService({
identifier: id,
query_target: "id",
});
} catch (error) {
ErrorForwarder(error);
}
};

View File

@ -0,0 +1,13 @@
import { ErrorForwarder } from "../../../../helpers/error/instances/forwarder";
import { findUserRoleService } from "../internal/findUserRole.service";
export const findUserRoleByNameService = async (name: string) => {
try {
return await findUserRoleService({
identifier: name,
query_target: "name",
});
} catch (error) {
ErrorForwarder(error);
}
};

View File

@ -0,0 +1,22 @@
import { AppError } from "../../../../helpers/error/instances/app";
import { ErrorForwarder } from "../../../../helpers/error/instances/forwarder";
import { findUserRoleByIdRepository } from "../../repositories/findUserRoleById.repository";
import { findUserRoleByNameRepository } from "../../repositories/findUserRoleByName.repository";
import { findUserRolePayload } from "../../userRole.types";
export const findUserRoleService = async (payload: findUserRolePayload) => {
try {
const repositoryMap = {
id: findUserRoleByIdRepository,
name: findUserRoleByNameRepository,
};
const repoFn = repositoryMap[payload.query_target];
if (!repoFn) throw new AppError(503, "Repository handler not found");
const userRoleData = await repoFn(payload.identifier);
if (userRoleData) throw new AppError(404, "User role not found");
} catch (error) {
ErrorForwarder(error);
}
};

View File

@ -0,0 +1,3 @@
import { prisma } from "../../utils/databases/prisma/connection";
export const userRoleModel = prisma.userRole;

View File

@ -0,0 +1,4 @@
export interface findUserRolePayload {
identifier: string;
query_target: "id" | "name";
}