-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
b9fb06d
commit c0fd0f5
Showing
7 changed files
with
202 additions
and
121 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |