Skip to content

Commit

Permalink
chore(api): add handlers for role
Browse files Browse the repository at this point in the history
  • Loading branch information
dipendraupreti committed Apr 10, 2024
1 parent b9fb06d commit c0fd0f5
Show file tree
Hide file tree
Showing 7 changed files with 202 additions and 121 deletions.
128 changes: 7 additions & 121 deletions packages/user/src/model/roles/controller.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
import Service from "./service";
import CustomApiError from "../../customApiError";
import handlers from "./handlers";

import type { RoleUpdateInput } from "../../types";
import type { FastifyInstance, FastifyReply } from "fastify";
import type { SessionRequest } from "supertokens-node/framework/fastify";
import type { FastifyInstance } from "fastify";

const plugin = async (
fastify: FastifyInstance,
Expand All @@ -15,150 +12,39 @@ const plugin = async (
{
preHandler: fastify.verifySession(),
},
async (request: SessionRequest, reply: FastifyReply) => {
const service = new Service(request.config, request.slonik);

const { limit, offset, filters, sort } = request.query as {
limit: number;
offset?: number;
filters?: string;
sort?: string;
};

const data = await service.list(
limit,
offset,
filters ? JSON.parse(filters) : undefined,
sort ? JSON.parse(sort) : undefined
);

reply.send(data);
}
handlers.roles
);

fastify.get(
"/roles/:id(^\\d+)",
{
preHandler: fastify.verifySession(),
},
async (request: SessionRequest, reply) => {
const service = new Service(request.config, request.slonik);

const { id } = request.params as { id: number };

const data = await service.findById(id);

reply.send(data);
}
handlers.role
);

fastify.delete(
"/roles/:id(^\\d+)",
{
preHandler: fastify.verifySession(),
},
async (request: SessionRequest, reply: FastifyReply) => {
const service = new Service(request.config, request.slonik);

const { id } = request.params as { id: number };

try {
const data = await service.delete(id);

reply.send(data);
} catch (error) {
if (error instanceof CustomApiError) {
reply.status(error.statusCode);

return reply.send({
message: error.message,
name: error.name,
statusCode: error.statusCode,
});
}

request.log.error(error);
reply.status(500);

return reply.send({
status: "ERROR",
message: "Oops! Something went wrong",
});
}
}
handlers.deleteRole
);

fastify.post(
"/roles",
{
preHandler: fastify.verifySession(),
},
async (request: SessionRequest, reply: FastifyReply) => {
const service = new Service(request.config, request.slonik);
const input = request.body as RoleUpdateInput;

try {
const data = await service.create(input);

return reply.send(data);
} catch (error) {
if (error instanceof CustomApiError) {
reply.status(error.statusCode);

return reply.send({
message: error.message,
name: error.name,
statusCode: error.statusCode,
});
}

request.log.error(error);
reply.status(500);

return reply.send({
status: "ERROR",
message: "Oops! Something went wrong",
});
}
}
handlers.create
);

fastify.put(
"/roles/:id(^\\d+)",
{
preHandler: fastify.verifySession(),
},
async (request: SessionRequest, reply: FastifyReply) => {
const service = new Service(request.config, request.slonik);

const { id } = request.params as { id: number };

const input = request.body as RoleUpdateInput;

try {
const data = await service.update(id, input);

return reply.send(data);
} catch (error) {
if (error instanceof CustomApiError) {
reply.status(error.statusCode);

return reply.send({
message: error.message,
name: error.name,
statusCode: error.statusCode,
});
}

request.log.error(error);
reply.status(500);

return reply.send({
status: "ERROR",
message: "Oops! Something went wrong",
});
}
}
handlers.update
);

done();
Expand Down
42 changes: 42 additions & 0 deletions packages/user/src/model/roles/handlers/create.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import CustomApiError from "../../../customApiError";
import Service from "../service";

import type { Role, RoleCreateInput, RoleUpdateInput } from "../../../types";
import type { FastifyReply } from "fastify";
import type { SessionRequest } from "supertokens-node/framework/fastify";

const create = async (request: SessionRequest, reply: FastifyReply) => {
const service = new Service<Role, RoleCreateInput, RoleUpdateInput>(
request.config,
request.slonik,
request.dbSchema
);

const input = request.body as RoleCreateInput;

try {
const data = await service.create(input);

return reply.send(data);
} catch (error) {
if (error instanceof CustomApiError) {
reply.status(error.statusCode);

return reply.send({
message: error.message,
name: error.name,
statusCode: error.statusCode,
});
}

request.log.error(error);
reply.status(500);

return reply.send({
status: "ERROR",
message: "Oops! Something went wrong",
});
}
};

export default create;
42 changes: 42 additions & 0 deletions packages/user/src/model/roles/handlers/delete.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import CustomApiError from "../../../customApiError";
import Service from "../service";

import type { Role, RoleCreateInput, RoleUpdateInput } from "../../../types";
import type { FastifyReply } from "fastify";
import type { SessionRequest } from "supertokens-node/framework/fastify";

const deleteRole = async (request: SessionRequest, reply: FastifyReply) => {
const service = new Service<Role, RoleCreateInput, RoleUpdateInput>(
request.config,
request.slonik,
request.dbSchema
);

const { id } = request.params as { id: number };

try {
const data = await service.delete(id);

reply.send(data);
} catch (error) {
if (error instanceof CustomApiError) {
reply.status(error.statusCode);

return reply.send({
message: error.message,
name: error.name,
statusCode: error.statusCode,
});
}

request.log.error(error);
reply.status(500);

return reply.send({
status: "ERROR",
message: "Oops! Something went wrong",
});
}
};

export default deleteRole;
13 changes: 13 additions & 0 deletions packages/user/src/model/roles/handlers/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import create from "./create";
import deleteRole from "./delete";
import role from "./role";
import roles from "./roles";
import update from "./update";

export default {
create,
deleteRole,
role,
roles,
update,
};
21 changes: 21 additions & 0 deletions packages/user/src/model/roles/handlers/role.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import Service from "../service";

import type { Role, RoleCreateInput, RoleUpdateInput } from "../../../types";
import type { FastifyReply } from "fastify";
import type { SessionRequest } from "supertokens-node/framework/fastify";

const role = async (request: SessionRequest, reply: FastifyReply) => {
const service = new Service<Role, RoleCreateInput, RoleUpdateInput>(
request.config,
request.slonik,
request.dbSchema
);

const { id } = request.params as { id: number };

const data = await service.findById(id);

reply.send(data);
};

export default role;
33 changes: 33 additions & 0 deletions packages/user/src/model/roles/handlers/roles.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import Service from "../service";

import type { Role, RoleCreateInput, RoleUpdateInput } from "../../../types";
import type { FastifyReply } from "fastify";
import type { SessionRequest } from "supertokens-node/framework/fastify";

const roles = async (request: SessionRequest, reply: FastifyReply) => {
const { config, dbSchema, query, slonik } = request;

const service = new Service<Role, RoleCreateInput, RoleUpdateInput>(
config,
slonik,
dbSchema
);

const { limit, offset, filters, sort } = query as {
limit: number;
offset?: number;
filters?: string;
sort?: string;
};

const data = await service.list(
limit,
offset,
filters ? JSON.parse(filters) : undefined,
sort ? JSON.parse(sort) : undefined
);

reply.send(data);
};

export default roles;
44 changes: 44 additions & 0 deletions packages/user/src/model/roles/handlers/update.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import CustomApiError from "../../../customApiError";
import Service from "../service";

import type { Role, RoleCreateInput, RoleUpdateInput } from "../../../types";
import type { FastifyReply } from "fastify";
import type { SessionRequest } from "supertokens-node/framework/fastify";

const update = async (request: SessionRequest, reply: FastifyReply) => {
const service = new Service<Role, RoleCreateInput, RoleUpdateInput>(
request.config,
request.slonik,
request.dbSchema
);

const { id } = request.params as { id: number };

const input = request.body as RoleUpdateInput;

try {
const data = await service.update(id, input);

return reply.send(data);
} catch (error) {
if (error instanceof CustomApiError) {
reply.status(error.statusCode);

return reply.send({
message: error.message,
name: error.name,
statusCode: error.statusCode,
});
}

request.log.error(error);
reply.status(500);

return reply.send({
status: "ERROR",
message: "Oops! Something went wrong",
});
}
};

export default update;

0 comments on commit c0fd0f5

Please sign in to comment.