-
Notifications
You must be signed in to change notification settings - Fork 1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Feature/8 crud group table #18
Merged
Merged
Changes from 6 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
537c349
package.json and bun.lock changes
e2dfba7
Merge branch 'main' of https://github.com/GenieWizards/finance-manage…
06236e5
Create Group changes
8fcf2fe
main pull
e4a7697
main pull
299a845
Delete Group changes
a634f5c
logger addition and let-> const review changes
3814682
error debugger addition
a505fdf
upadted package.json
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Binary file not shown.
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
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,81 @@ | ||
import type { AppRouteHandler } from "@/common/lib/types"; | ||
import type { TSelectGroupSchema } from "@/db/schemas/group.model"; | ||
|
||
import * as HTTPStatusCodes from "@/common/utils/http-status-codes.util"; | ||
|
||
import type { TCreateGroupRoute, TDeleteGroupRoute } from "./group.routes"; | ||
|
||
import { createGroupRepository, deleteGroupRepository } from "./group.repository"; | ||
|
||
export const createGroup: AppRouteHandler<TCreateGroupRoute> = async (c) => { | ||
const user = c.get("user"); | ||
const payload = c.req.valid("json"); | ||
|
||
if (!user) { | ||
shivamvijaywargi marked this conversation as resolved.
Show resolved
Hide resolved
|
||
return c.json( | ||
{ | ||
success: false, | ||
message: "You are not authorized, please login", | ||
}, | ||
HTTPStatusCodes.UNAUTHORIZED, | ||
); | ||
} | ||
|
||
let group: TSelectGroupSchema | null = null; | ||
group = await createGroupRepository(payload); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You could directly do const group = await createGroupRepository(payload); |
||
|
||
if (!group) { | ||
return c.json( | ||
{ | ||
success: false, | ||
message: "Failed to create group", | ||
}, | ||
HTTPStatusCodes.INTERNAL_SERVER_ERROR, | ||
); | ||
} | ||
|
||
return c.json( | ||
{ | ||
success: true, | ||
message: "Group created successfully", | ||
data: group, | ||
}, | ||
HTTPStatusCodes.CREATED, | ||
); | ||
}; | ||
|
||
export const deleteGroup: AppRouteHandler<TDeleteGroupRoute> = async (c) => { | ||
const user = c.get("user"); | ||
const params = c.req.valid("param"); | ||
|
||
if (!user) { | ||
return c.json( | ||
{ | ||
success: false, | ||
message: "You are not authorized, please login", | ||
}, | ||
HTTPStatusCodes.UNAUTHORIZED, | ||
); | ||
} | ||
|
||
let deletedGroupId: string | null = null; | ||
deletedGroupId = await deleteGroupRepository(params.id); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same previous comment applies. |
||
|
||
if (!deletedGroupId) { | ||
return c.json( | ||
{ | ||
success: false, | ||
message: `Group with ${params.id} not found`, | ||
}, | ||
HTTPStatusCodes.NOT_FOUND, | ||
); | ||
} | ||
|
||
return c.json( | ||
{ | ||
success: true, | ||
message: `Group with ${deletedGroupId} deleted successfully`, | ||
}, | ||
HTTPStatusCodes.OK, | ||
); | ||
}; |
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,8 @@ | ||
import { createRouter } from "@/common/lib/create-app.lib"; | ||
|
||
import * as handlers from "./group.handler"; | ||
import * as routes from "./group.routes"; | ||
|
||
export const groupRouters = createRouter() | ||
.openapi(routes.createGroupRoute, handlers.createGroup) | ||
.openapi(routes.deleteGroupRoute, handlers.deleteGroup); |
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,28 @@ | ||
import { eq } from "drizzle-orm"; | ||
|
||
import type { TInsertGroupSchema } from "@/db/schemas/group.model"; | ||
|
||
import { db } from "@/db/adapter"; | ||
import groupModel from "@/db/schemas/group.model"; | ||
|
||
export async function createGroupRepository( | ||
groupPayload: TInsertGroupSchema, | ||
) { | ||
const [group] = await db | ||
.insert(groupModel) | ||
.values(groupPayload) | ||
.returning(); | ||
|
||
return group; | ||
} | ||
|
||
export async function deleteGroupRepository( | ||
groupId: string, | ||
) { | ||
const [deletedGroups] = await db | ||
.delete(groupModel) | ||
.where(eq(groupModel.id, groupId)) | ||
.returning(); | ||
|
||
return deletedGroups?.id || null; | ||
} |
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,120 @@ | ||
import { createRoute } from "@hono/zod-openapi"; | ||
import { z } from "zod"; | ||
|
||
import { AuthRoles } from "@/common/enums"; | ||
import jsonContentRequired from "@/common/helpers/json-content-required.helper"; | ||
import { jsonContent } from "@/common/helpers/json-content.helper"; | ||
import { authMiddleware, checkRoleGuard, requireAuth } from "@/common/middlewares/auth.middleware"; | ||
import * as HTTPStatusCodes from "@/common/utils/http-status-codes.util"; | ||
import { insertGroupSchema, selectGroupSchema } from "@/db/schemas/group.model"; | ||
|
||
const tags = ["Groups"]; | ||
|
||
export const createGroupRoute = createRoute({ | ||
tags, | ||
method: "post", | ||
path: "/group", | ||
middleware: [ | ||
authMiddleware(), | ||
requireAuth(), | ||
checkRoleGuard(AuthRoles.ADMIN, AuthRoles.USER), | ||
] as const, | ||
request: { | ||
body: jsonContentRequired( | ||
insertGroupSchema.omit({ | ||
id: true, | ||
status: true, | ||
createdAt: true, | ||
updatedAt: true, | ||
}), | ||
"Group creation", | ||
), | ||
}, | ||
responses: { | ||
[HTTPStatusCodes.CREATED]: jsonContent( | ||
z.object({ | ||
success: z.boolean().default(true), | ||
message: z.string(), | ||
data: selectGroupSchema, | ||
}), | ||
"Group created successfully", | ||
), | ||
[HTTPStatusCodes.BAD_REQUEST]: jsonContent( | ||
z.object({ | ||
success: z.boolean().default(false), | ||
message: z.string(), | ||
}), | ||
"Validation error(s)", | ||
), | ||
[HTTPStatusCodes.UNAUTHORIZED]: jsonContent( | ||
z.object({ | ||
success: z.boolean().default(false), | ||
message: z.string(), | ||
}), | ||
"You are not authorized, please login", | ||
), | ||
[HTTPStatusCodes.INTERNAL_SERVER_ERROR]: jsonContent( | ||
z.object({ | ||
success: z.boolean().default(false), | ||
message: z.string(), | ||
}), | ||
"Failed to create the group", | ||
), | ||
}, | ||
}); | ||
|
||
export const deleteGroupRoute = createRoute({ | ||
tags, | ||
method: "delete", | ||
path: "group/:id", | ||
middleware: [ | ||
authMiddleware(), | ||
requireAuth(), | ||
checkRoleGuard(AuthRoles.ADMIN, AuthRoles.USER), | ||
] as const, | ||
request: { | ||
params: z.object({ | ||
id: z.string(), | ||
}), | ||
}, | ||
responses: { | ||
[HTTPStatusCodes.OK]: jsonContent( | ||
z.object({ | ||
success: z.boolean().default(true), | ||
message: z.string(), | ||
}), | ||
"Group deleted successfully", | ||
), | ||
[HTTPStatusCodes.NOT_FOUND]: jsonContent( | ||
z.object({ | ||
success: z.boolean().default(false), | ||
message: z.string(), | ||
}), | ||
"Group with id does not exist", | ||
), | ||
[HTTPStatusCodes.UNAUTHORIZED]: jsonContent( | ||
z.object({ | ||
success: z.boolean().default(false), | ||
message: z.string(), | ||
}), | ||
"You are not authorized, please login", | ||
), | ||
[HTTPStatusCodes.FORBIDDEN]: jsonContent( | ||
z.object({ | ||
success: z.boolean().default(false), | ||
message: z.string(), | ||
}), | ||
"You are not allowed to perform this action", | ||
), | ||
[HTTPStatusCodes.INTERNAL_SERVER_ERROR]: jsonContent( | ||
z.object({ | ||
success: z.boolean().default(false), | ||
message: z.string(), | ||
}), | ||
"Something went wrong, please try again later", | ||
), | ||
}, | ||
}); | ||
|
||
export type TCreateGroupRoute = typeof createGroupRoute; | ||
export type TDeleteGroupRoute = typeof deleteGroupRoute; |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This dependency is no more required and was removed in a previous MR, please take the latest pull from main.