👔 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:
@ -1,8 +1,8 @@
|
|||||||
import Elysia from "elysia";
|
import Elysia from "elysia";
|
||||||
import { createUserViaRegisterController } from "./controller/createUserViaRegister.controller";
|
import { createUserViaRegisterController } from "./controllers/createUserViaRegister.controller";
|
||||||
import { getUserByEmailController } from "./controller/getUserByEmail.controller";
|
import { getUserByEmailController } from "./controllers/getUserByEmail.controller";
|
||||||
import { getUserByUsernameController } from "./controller/getUserByUsername.controller";
|
import { getUserByUsernameController } from "./controllers/getUserByUsername.controller";
|
||||||
import { getUserByIdController } from "./controller/getUserById.controller";
|
import { getUserByIdController } from "./controllers/getUserById.controller";
|
||||||
|
|
||||||
export const userModule = new Elysia({ prefix: "/users" })
|
export const userModule = new Elysia({ prefix: "/users" })
|
||||||
.post("/", createUserViaRegisterController)
|
.post("/", createUserViaRegisterController)
|
||||||
|
|||||||
@ -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);
|
||||||
|
}
|
||||||
|
};
|
||||||
@ -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);
|
||||||
|
}
|
||||||
|
};
|
||||||
7
src/modules/userRole/index.ts
Normal file
7
src/modules/userRole/index.ts
Normal 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);
|
||||||
@ -0,0 +1,9 @@
|
|||||||
|
import { userRoleModel } from "../userRole.model";
|
||||||
|
|
||||||
|
export const findUserRoleByIdRepository = async (id: string) => {
|
||||||
|
return await userRoleModel.findUnique({
|
||||||
|
where: {
|
||||||
|
id,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
};
|
||||||
@ -0,0 +1,9 @@
|
|||||||
|
import { userRoleModel } from "../userRole.model";
|
||||||
|
|
||||||
|
export const findUserRoleByNameRepository = async (name: string) => {
|
||||||
|
return await userRoleModel.findUnique({
|
||||||
|
where: {
|
||||||
|
name,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
};
|
||||||
5
src/modules/userRole/schemas/getUserRoleById.schema.ts
Normal file
5
src/modules/userRole/schemas/getUserRoleById.schema.ts
Normal file
@ -0,0 +1,5 @@
|
|||||||
|
import z from "zod";
|
||||||
|
|
||||||
|
export const getUserRoleByIdSchema = z.object({
|
||||||
|
id: z.string(),
|
||||||
|
});
|
||||||
5
src/modules/userRole/schemas/getUserRoleByName.schema.ts
Normal file
5
src/modules/userRole/schemas/getUserRoleByName.schema.ts
Normal file
@ -0,0 +1,5 @@
|
|||||||
|
import z from "zod";
|
||||||
|
|
||||||
|
export const getUserRoleByNameSchema = z.object({
|
||||||
|
name: z.string(),
|
||||||
|
});
|
||||||
@ -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);
|
||||||
|
}
|
||||||
|
};
|
||||||
@ -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);
|
||||||
|
}
|
||||||
|
};
|
||||||
@ -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);
|
||||||
|
}
|
||||||
|
};
|
||||||
3
src/modules/userRole/userRole.model.ts
Normal file
3
src/modules/userRole/userRole.model.ts
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
import { prisma } from "../../utils/databases/prisma/connection";
|
||||||
|
|
||||||
|
export const userRoleModel = prisma.userRole;
|
||||||
4
src/modules/userRole/userRole.types.ts
Normal file
4
src/modules/userRole/userRole.types.ts
Normal file
@ -0,0 +1,4 @@
|
|||||||
|
export interface findUserRolePayload {
|
||||||
|
identifier: string;
|
||||||
|
query_target: "id" | "name";
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user