feat: category and channel endpoints

This commit is contained in:
PrimarchPaul
2025-09-27 20:45:36 -04:00
parent 9b46852e20
commit d1273ca4ec
10 changed files with 1156 additions and 1 deletions

View File

@@ -0,0 +1,254 @@
import {
createNewCategory,
fetchCategoryData,
fetchCategoriesByInstance,
updateExistingCategory,
deleteExistingCategory,
deleteAllCategoriesByInstance,
} from "../controller/categoryController";
import {
createCategorySchema,
CreateCategoryInput,
UpdateCategoryInput,
DeleteCategoriesByInstanceIdInput,
} from "../validators/categoryValidator";
import { zValidator } from "@hono/zod-validator";
import { Hono } from "hono";
import { describeRoute, resolver } from "hono-openapi";
const categoryRoutes = new Hono()
categoryRoutes.post(
"/category/create",
describeRoute({
description: "Create a new category",
responses: {
200: {
description: "Success creating category",
content: {
"application/json": { schema: resolver(createCategorySchema) },
},
},
400: {
description: "Bad Request - Invalid input data",
content: {
"application/json": { schema: resolver(createCategorySchema)},
},
},
401: {
description: "Unauthorized - Admin access required",
content: {
"application/json": { schema: resolver(createCategorySchema)},
},
},
404: {
description: "User Id not found",
content: {
"application/json": { schema: resolver(createCategorySchema)},
},
},
},
}),
zValidator("json", createCategorySchema),
async (c) => {
const data = c.req.valid("json") as CreateCategoryInput;
const categoryData = await createNewCategory(data);
if (categoryData) {
return c.json(categoryData);
} else {
return c.json({ error: "Failed to create category" }, 400);
}
}
)
categoryRoutes.get(
"/:id",
describeRoute({
description: "Get category by id",
responses: {
200: {
description: "Success getting category",
content: {
"application/json": { schema: resolver(createCategorySchema) },
},
},
404: {
description: "Category id not found",
content: {
"application/json": { schema: resolver(createCategorySchema) },
},
},
},
}),
async (c) => {
const id = c.req.param("id");
const categoryData = await fetchCategoryData(id);
if (categoryData) {
return c.json(categoryData);
} else {
return c.json({ error: "Category not found" }, 404);
}
}
);
categoryRoutes.get(
"",
describeRoute({
description: "Get all categories by instance id",
responses: {
200: {
description: "Success getting all categories in instance",
content: {
"application/json": { schema: resolver(createCategorySchema) },
},
},
},
}),
zValidator("query", createCategorySchema),
async (c) => {
const instanceId = c.req.query("instanceId");
if (!instanceId) {
return c.json({ error: "No instance id provided" }, 400);
}
const categoryData = await fetchCategoriesByInstance(instanceId);
if (categoryData) {
return c.json(categoryData);
} else {
return c.json({ error: "Error getting all categories from instance" }, 500);
}
}
);
categoryRoutes.put(
"/category/update",
describeRoute({
description: "Update an existing category",
responses: {
200: {
description: "Success updating category",
content: {
"application/json": { schema: resolver(createCategorySchema) },
},
},
400: {
description: "Bad Request - Invalid input data",
content: {
"application/json": { schema: resolver(createCategorySchema)},
},
},
401: {
description: "Unauthorized - Admin access required",
content: {
"application/json": { schema: resolver(createCategorySchema)},
},
},
404: {
description: "Category id or User Id not found",
content: {
"application/json": { schema: resolver(createCategorySchema)},
},
},
},
}),
zValidator("json", createCategorySchema),
async (c) => {
const data = c.req.valid("json") as UpdateCategoryInput;
const categoryData = await updateExistingCategory(data);
if (categoryData) {
return c.json(categoryData);
} else {
return c.json({ error: "Failed to update category" }, 400);
}
}
);
categoryRoutes.delete(
"/category/delete",
describeRoute({
description: "Delete an existing category",
responses: {
200: {
description: "Success deleting category",
content: {
"application/json": { schema: resolver(createCategorySchema) },
},
},
400: {
description: "Bad Request - Invalid input data",
content: {
"application/json": { schema: resolver(createCategorySchema)},
},
},
401: {
description: "Unauthorized - Admin access required",
content: {
"application/json": { schema: resolver(createCategorySchema)},
},
},
404: {
description: "Category id or User Id not found",
content: {
"application/json": { schema: resolver(createCategorySchema)},
},
},
},
}),
zValidator("json", createCategorySchema),
async (c) => {
const data = c.req.valid("json") as UpdateCategoryInput;
const categoryData = await deleteExistingCategory(data);
if (categoryData) {
return c.json(categoryData);
} else {
return c.json({ error: "Failed to delete category" }, 400);
}
}
)
categoryRoutes.delete(
"/categories/delete/:id",
describeRoute({
description: "Delete all categories by instance id",
responses: {
200: {
description: "Success deleting all categories in instance",
content: {
"application/json": { schema: resolver(createCategorySchema) },
},
},
400: {
description: "Bad Request - Invalid input data",
content: {
"application/json": { schema: resolver(createCategorySchema)},
},
},
401: {
description: "Unauthorized - Admin access required",
content: {
"application/json": { schema: resolver(createCategorySchema)},
},
},
404: {
description: "Instance id or User Id not found",
content: {
"application/json": { schema: resolver(createCategorySchema)},
},
},
},
}),
zValidator("json", createCategorySchema),
async (c) => {
const data = c.req.valid("json") as DeleteCategoriesByInstanceIdInput;
const categoryData = await deleteAllCategoriesByInstance(data);
if (categoryData) {
return c.json(categoryData);
} else {
return c.json({ error: "Failed to delete categories" }, 400);
}
}
)
export { categoryRoutes };

View File

@@ -0,0 +1,264 @@
import {
createNewChannel,
fetchChannelData,
fetchChannelsByCategory,
updateExistingChannel,
deleteExistingChannel,
deleteAllChannelsByCategory,
} from "../controller/channelController";
import {
createChannelSchema,
getChannelSchema,
getChannelsByCategoryIdSchema,
updateChannelSchema,
deleteChannelSchema,
deleteChannelsByCategoryIdSchema,
CreateChannelInput,
GetChannelInput,
GetChannelsByCategoryIdInput,
UpdateChannelInput,
DeleteChannelInput,
DeleteChannelsByCategoryIdInput,
} from "../validators/channelValidator";
import { zValidator } from "@hono/zod-validator";
import { Hono } from "hono";
import { describeRoute, resolver } from "hono-openapi";
const channelRoutes = new Hono()
channelRoutes.post(
"/channel/create",
describeRoute({
description: "Create a new channel",
responses: {
200: {
description: "Success creating channel",
content: {
"application/json": { schema: resolver(createChannelSchema) },
},
},
400: {
description: "Bad Request - Invalid input data",
content: {
"application/json": { schema: resolver(createChannelSchema)},
},
},
401: {
description: "Unauthorized - Admin access required",
content: {
"application/json": { schema: resolver(createChannelSchema)},
},
},
404: {
description: "User Id not found",
content: {
"application/json": { schema: resolver(createChannelSchema)},
},
},
},
}),
zValidator("json", createChannelSchema),
async (c) => {
const data = c.req.valid("json") as CreateChannelInput;
const channelData = await createNewChannel(data);
if (channelData) {
return c.json(channelData);
} else {
return c.json({ error: "Failed to create channel" }, 400);
}
}
)
channelRoutes.get(
"/:id",
describeRoute({
description: "Get channel by id",
responses: {
200: {
description: "Success getting channel",
content: {
"application/json": { schema: resolver(getChannelSchema) },
},
},
404: {
description: "Channel id not found",
content: {
"application/json": { schema: resolver(getChannelSchema) },
},
},
},
}),
async (c) => {
const id = c.req.param("id");
const channelData = await fetchChannelData(id);
if (channelData) {
return c.json(channelData);
} else {
return c.json({ error: "Channel not found" }, 404);
}
}
);
channelRoutes.get(
"",
describeRoute({
description: "Get all channels by category id",
responses: {
200: {
description: "Success getting all channels in category",
content: {
"application/json": { schema: resolver(getChannelsByCategoryIdSchema) },
},
},
},
}),
zValidator("query", getChannelsByCategoryIdSchema),
async (c) => {
const categoryId = c.req.query("categoryId");
if (!categoryId) {
return c.json({ error: "No category id provided" }, 400);
}
const channels = await fetchChannelsByCategory(categoryId);
if (channels) {
return c.json(channels);
} else {
return c.json({ error: "Error getting channels from category" }, 500);
}
}
);
channelRoutes.put(
"/channel/update",
describeRoute({
description: "Update an existing channel",
responses: {
200: {
description: "Success updating channel",
content: {
"application/json": { schema: resolver(updateChannelSchema) },
},
},
400: {
description: "Bad Request - Invalid input data",
content: {
"application/json": { schema: resolver(updateChannelSchema)},
},
},
401: {
description: "Unauthorized - Admin access required",
content: {
"application/json": { schema: resolver(updateChannelSchema)},
},
},
404: {
description: "Channel id or User Id not found",
content: {
"application/json": { schema: resolver(updateChannelSchema)},
},
},
},
}),
zValidator("json", updateChannelSchema),
async (c) => {
const data = c.req.valid("json") as UpdateChannelInput;
const result = await updateExistingChannel(data);
if (result) {
return c.json(result);
} else {
return c.json({ error: "Failed to update channel" }, 400);
}
}
);
channelRoutes.delete(
"/channel/delete",
describeRoute({
description: "Delete an existing channel",
responses: {
200: {
description: "Success deleting channel",
content: {
"application/json": { schema: resolver(deleteChannelSchema) },
},
},
400: {
description: "Bad Request - Invalid input data",
content: {
"application/json": { schema: resolver(deleteChannelSchema)},
},
},
401: {
description: "Unauthorized - Admin access required",
content: {
"application/json": { schema: resolver(deleteChannelSchema)},
},
},
404: {
description: "Channel id or User Id not found",
content: {
"application/json": { schema: resolver(deleteChannelSchema)},
},
},
},
}),
zValidator("json", deleteChannelSchema),
async (c) => {
const data = c.req.valid("json") as DeleteChannelInput;
const result = await deleteExistingChannel(data);
if (result) {
return c.json({ success: true });
} else {
return c.json({ error: "Failed to delete channel" }, 400);
}
}
);
channelRoutes.delete(
"/channels/delete-by-category",
describeRoute({
description: "Delete all channels by category id",
responses: {
200: {
description: "Success deleting all channels in category",
content: {
"application/json": { schema: resolver(deleteChannelsByCategoryIdSchema) },
},
},
400: {
description: "Bad Request - Invalid input data",
content: {
"application/json": { schema: resolver(deleteChannelsByCategoryIdSchema)},
},
},
401: {
description: "Unauthorized - Admin access required",
content: {
"application/json": { schema: resolver(deleteChannelsByCategoryIdSchema)},
},
},
404: {
description: "Category id or User Id not found",
content: {
"application/json": { schema: resolver(deleteChannelsByCategoryIdSchema)},
},
},
},
}),
zValidator("json", deleteChannelsByCategoryIdSchema),
async (c) => {
const data = c.req.valid("json") as DeleteChannelsByCategoryIdInput;
const result = await deleteAllChannelsByCategory(data);
if (result) {
return c.json({ success: true });
} else {
return c.json({ error: "Failed to delete channels" }, 400);
}
}
)
export default channelRoutes ;

View File

@@ -2,10 +2,12 @@
import { Hono } from "hono";
import userRoutes from "./userRoutes";
import messageRoutes from "./messageRoutes";
import channelRoutes from "./channelRoutes";
const routes = new Hono();
routes.route("/user", userRoutes);
routes.route("/message", messageRoutes);
routes.route("/channel", channelRoutes);
export default routes;