code made pretty

This commit is contained in:
Kevin Puig
2025-09-27 21:42:43 -04:00
parent 11f25ca915
commit c305298927
17 changed files with 1394 additions and 1292 deletions

View File

@@ -1,297 +1,315 @@
import {
createNewCategory,
fetchCategoryData,
fetchCategoriesByInstance,
updateExistingCategory,
deleteExistingCategory,
deleteAllCategoriesByInstance,
createNewCategory,
fetchCategoryData,
fetchCategoriesByInstance,
updateExistingCategory,
deleteExistingCategory,
deleteAllCategoriesByInstance,
} from "../controller/categoryController";
import {
createCategorySchema,
getCategorySchema,
getCategoriesByInstanceIdSchema,
updateCategorySchema,
deleteCategorySchema,
deleteCategoriesByInstanceIdSchema,
CreateCategoryInput,
GetCategoryInput,
GetCategoriesByInstanceIdInput,
UpdateCategoryInput,
DeleteCategoryInput,
DeleteCategoriesByInstanceIdInput,
createCategorySchema,
getCategorySchema,
getCategoriesByInstanceIdSchema,
updateCategorySchema,
deleteCategorySchema,
deleteCategoriesByInstanceIdSchema,
CreateCategoryInput,
GetCategoryInput,
GetCategoriesByInstanceIdInput,
UpdateCategoryInput,
DeleteCategoryInput,
DeleteCategoriesByInstanceIdInput,
} from "../validators/categoryValidator";
import { zValidator } from "@hono/zod-validator";
import { Hono } from "hono";
import { describeRoute, resolver } from "hono-openapi";
const categoryRoutes = new Hono()
const categoryRoutes = new Hono();
// Create a new category
categoryRoutes.post(
"/",
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)},
},
},
"/",
describeRoute({
description: "Create a new category",
responses: {
200: {
description: "Success creating category",
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);
}
},
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);
}
)
},
);
// Get a category by ID
categoryRoutes.get(
"/:id",
describeRoute({
description: "Get category by id",
responses: {
200: {
description: "Success getting category",
content: {
"application/json": { schema: resolver(getCategorySchema) },
},
},
404: {
description: "Category id not found",
content: {
"application/json": { schema: resolver(getCategorySchema) },
},
},
"/:id",
describeRoute({
description: "Get category by id",
responses: {
200: {
description: "Success getting category",
content: {
"application/json": { schema: resolver(getCategorySchema) },
},
}),
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);
}
},
404: {
description: "Category id not found",
content: {
"application/json": { schema: resolver(getCategorySchema) },
},
},
},
}),
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);
}
},
);
// Get all categories by instance ID
categoryRoutes.get(
"/instance/:instanceId",
describeRoute({
description: "Get all categories by instance id",
responses: {
200: {
description: "Success getting all categories in instance",
content: {
"application/json": { schema: resolver(getCategoriesByInstanceIdSchema) },
},
},
400: {
description: "Bad Request - Missing instance ID",
content: {
"application/json": { schema: resolver(getCategoriesByInstanceIdSchema) },
},
},
"/instance/:instanceId",
describeRoute({
description: "Get all categories by instance id",
responses: {
200: {
description: "Success getting all categories in instance",
content: {
"application/json": {
schema: resolver(getCategoriesByInstanceIdSchema),
},
},
}),
async (c) => {
const instanceId = c.req.param("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);
}
},
400: {
description: "Bad Request - Missing instance ID",
content: {
"application/json": {
schema: resolver(getCategoriesByInstanceIdSchema),
},
},
},
},
}),
async (c) => {
const instanceId = c.req.param("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,
);
}
},
);
// Update a category
categoryRoutes.put(
"/:id",
describeRoute({
description: "Update an existing category",
responses: {
200: {
description: "Success updating category",
content: {
"application/json": { schema: resolver(updateCategorySchema) },
},
},
400: {
description: "Bad Request - Invalid input data",
content: {
"application/json": { schema: resolver(updateCategorySchema)},
},
},
401: {
description: "Unauthorized - Admin access required",
content: {
"application/json": { schema: resolver(updateCategorySchema)},
},
},
404: {
description: "Category id or User Id not found",
content: {
"application/json": { schema: resolver(updateCategorySchema)},
},
},
"/:id",
describeRoute({
description: "Update an existing category",
responses: {
200: {
description: "Success updating category",
content: {
"application/json": { schema: resolver(updateCategorySchema) },
},
}),
zValidator("json", updateCategorySchema),
async (c) => {
const id = c.req.param("id");
const data = c.req.valid("json") as UpdateCategoryInput;
// Ensure the ID in the path matches the one in the body
if (data.id && data.id !== id) {
return c.json({ error: "ID in path does not match ID in body" }, 400);
}
// Set ID from path if not in body
if (!data.id) {
data.id = id;
}
const categoryData = await updateExistingCategory(data);
if (categoryData) {
return c.json(categoryData);
} else {
return c.json({ error: "Failed to update category" }, 400);
}
},
400: {
description: "Bad Request - Invalid input data",
content: {
"application/json": { schema: resolver(updateCategorySchema) },
},
},
401: {
description: "Unauthorized - Admin access required",
content: {
"application/json": { schema: resolver(updateCategorySchema) },
},
},
404: {
description: "Category id or User Id not found",
content: {
"application/json": { schema: resolver(updateCategorySchema) },
},
},
},
}),
zValidator("json", updateCategorySchema),
async (c) => {
const id = c.req.param("id");
const data = c.req.valid("json") as UpdateCategoryInput;
// Ensure the ID in the path matches the one in the body
if (data.id && data.id !== id) {
return c.json({ error: "ID in path does not match ID in body" }, 400);
}
// Set ID from path if not in body
if (!data.id) {
data.id = id;
}
const categoryData = await updateExistingCategory(data);
if (categoryData) {
return c.json(categoryData);
} else {
return c.json({ error: "Failed to update category" }, 400);
}
},
);
// Delete a specific category
categoryRoutes.delete(
"/:id",
describeRoute({
description: "Delete an existing category",
responses: {
200: {
description: "Success deleting category",
content: {
"application/json": { schema: resolver(deleteCategorySchema) },
},
},
400: {
description: "Bad Request - Invalid input data",
content: {
"application/json": { schema: resolver(deleteCategorySchema)},
},
},
401: {
description: "Unauthorized - Admin access required",
content: {
"application/json": { schema: resolver(deleteCategorySchema)},
},
},
404: {
description: "Category id or User Id not found",
content: {
"application/json": { schema: resolver(deleteCategorySchema)},
},
},
"/:id",
describeRoute({
description: "Delete an existing category",
responses: {
200: {
description: "Success deleting category",
content: {
"application/json": { schema: resolver(deleteCategorySchema) },
},
}),
zValidator("json", deleteCategorySchema),
async (c) => {
const id = c.req.param("id");
const data = c.req.valid("json") as DeleteCategoryInput;
// Ensure the ID in the path matches the one in the body
if (data.id !== id) {
return c.json({ error: "ID in path does not match ID in body" }, 400);
}
const categoryData = await deleteExistingCategory(data);
if (categoryData) {
return c.json(categoryData);
} else {
return c.json({ error: "Failed to delete category" }, 400);
}
},
400: {
description: "Bad Request - Invalid input data",
content: {
"application/json": { schema: resolver(deleteCategorySchema) },
},
},
401: {
description: "Unauthorized - Admin access required",
content: {
"application/json": { schema: resolver(deleteCategorySchema) },
},
},
404: {
description: "Category id or User Id not found",
content: {
"application/json": { schema: resolver(deleteCategorySchema) },
},
},
},
}),
zValidator("json", deleteCategorySchema),
async (c) => {
const id = c.req.param("id");
const data = c.req.valid("json") as DeleteCategoryInput;
// Ensure the ID in the path matches the one in the body
if (data.id !== id) {
return c.json({ error: "ID in path does not match ID in body" }, 400);
}
)
const categoryData = await deleteExistingCategory(data);
if (categoryData) {
return c.json(categoryData);
} else {
return c.json({ error: "Failed to delete category" }, 400);
}
},
);
// Delete all categories by instance ID
categoryRoutes.delete(
"/instance/:instanceId",
describeRoute({
description: "Delete all categories by instance id",
responses: {
200: {
description: "Success deleting all categories in instance",
content: {
"application/json": { schema: resolver(deleteCategoriesByInstanceIdSchema) },
},
},
400: {
description: "Bad Request - Invalid input data",
content: {
"application/json": { schema: resolver(deleteCategoriesByInstanceIdSchema)},
},
},
401: {
description: "Unauthorized - Admin access required",
content: {
"application/json": { schema: resolver(deleteCategoriesByInstanceIdSchema)},
},
},
404: {
description: "Instance id or User Id not found",
content: {
"application/json": { schema: resolver(deleteCategoriesByInstanceIdSchema)},
},
},
"/instance/:instanceId",
describeRoute({
description: "Delete all categories by instance id",
responses: {
200: {
description: "Success deleting all categories in instance",
content: {
"application/json": {
schema: resolver(deleteCategoriesByInstanceIdSchema),
},
},
}),
zValidator("json", deleteCategoriesByInstanceIdSchema),
async (c) => {
const instanceId = c.req.param("instanceId");
const data = c.req.valid("json") as DeleteCategoriesByInstanceIdInput;
// Ensure the instanceId in the path matches the one in the body
if (data.instanceId !== instanceId) {
return c.json({ error: "Instance ID in path does not match Instance ID in body" }, 400);
}
const categoryData = await deleteAllCategoriesByInstance(data);
if (categoryData) {
return c.json(categoryData);
} else {
return c.json({ error: "Failed to delete categories" }, 400);
}
}
)
},
400: {
description: "Bad Request - Invalid input data",
content: {
"application/json": {
schema: resolver(deleteCategoriesByInstanceIdSchema),
},
},
},
401: {
description: "Unauthorized - Admin access required",
content: {
"application/json": {
schema: resolver(deleteCategoriesByInstanceIdSchema),
},
},
},
404: {
description: "Instance id or User Id not found",
content: {
"application/json": {
schema: resolver(deleteCategoriesByInstanceIdSchema),
},
},
},
},
}),
zValidator("json", deleteCategoriesByInstanceIdSchema),
async (c) => {
const instanceId = c.req.param("instanceId");
const data = c.req.valid("json") as DeleteCategoriesByInstanceIdInput;
export { categoryRoutes };
// Ensure the instanceId in the path matches the one in the body
if (data.instanceId !== instanceId) {
return c.json(
{ error: "Instance ID in path does not match Instance ID in body" },
400,
);
}
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

@@ -1,299 +1,313 @@
import {
createNewChannel,
fetchChannelData,
fetchChannelsByCategory,
updateExistingChannel,
deleteExistingChannel,
deleteAllChannelsByCategory,
createNewChannel,
fetchChannelData,
fetchChannelsByCategory,
updateExistingChannel,
deleteExistingChannel,
deleteAllChannelsByCategory,
} from "../controller/channelController";
import {
createChannelSchema,
getChannelSchema,
getChannelsByCategoryIdSchema,
updateChannelSchema,
deleteChannelSchema,
deleteChannelsByCategoryIdSchema,
CreateChannelInput,
GetChannelInput,
GetChannelsByCategoryIdInput,
UpdateChannelInput,
DeleteChannelInput,
DeleteChannelsByCategoryIdInput,
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()
const channelRoutes = new Hono();
// Create a new channel
channelRoutes.post(
"/",
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)},
},
},
"/",
describeRoute({
description: "Create a new channel",
responses: {
200: {
description: "Success creating channel",
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);
}
},
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);
}
)
},
);
// Get a channel by ID
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) },
},
},
"/:id",
describeRoute({
description: "Get channel by id",
responses: {
200: {
description: "Success getting channel",
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);
}
},
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);
}
},
);
// Get all channels by category ID
channelRoutes.get(
"/category/:categoryId",
describeRoute({
description: "Get all channels by category id",
responses: {
200: {
description: "Success getting all channels in category",
content: {
"application/json": { schema: resolver(getChannelsByCategoryIdSchema) },
},
},
400: {
description: "Bad Request - Missing category ID",
content: {
"application/json": { schema: resolver(getChannelsByCategoryIdSchema) },
},
},
"/category/:categoryId",
describeRoute({
description: "Get all channels by category id",
responses: {
200: {
description: "Success getting all channels in category",
content: {
"application/json": {
schema: resolver(getChannelsByCategoryIdSchema),
},
},
}),
async (c) => {
const categoryId = c.req.param("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);
}
},
400: {
description: "Bad Request - Missing category ID",
content: {
"application/json": {
schema: resolver(getChannelsByCategoryIdSchema),
},
},
},
},
}),
async (c) => {
const categoryId = c.req.param("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);
}
},
);
// Update a channel
channelRoutes.put(
"/:id",
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)},
},
},
"/:id",
describeRoute({
description: "Update an existing channel",
responses: {
200: {
description: "Success updating channel",
content: {
"application/json": { schema: resolver(updateChannelSchema) },
},
}),
zValidator("json", updateChannelSchema),
async (c) => {
const id = c.req.param("id");
const data = c.req.valid("json") as UpdateChannelInput;
// Ensure the ID in the path matches the one in the body
if (data.id && data.id !== id) {
return c.json({ error: "ID in path does not match ID in body" }, 400);
}
// Set ID from path if not in body
if (!data.id) {
data.id = id;
}
const result = await updateExistingChannel(data);
if (result) {
return c.json(result);
} else {
return c.json({ error: "Failed to update channel" }, 400);
}
},
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 id = c.req.param("id");
const data = c.req.valid("json") as UpdateChannelInput;
// Ensure the ID in the path matches the one in the body
if (data.id && data.id !== id) {
return c.json({ error: "ID in path does not match ID in body" }, 400);
}
// Set ID from path if not in body
if (!data.id) {
data.id = id;
}
const result = await updateExistingChannel(data);
if (result) {
return c.json(result);
} else {
return c.json({ error: "Failed to update channel" }, 400);
}
},
);
// Delete a specific channel
channelRoutes.delete(
"/:id",
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)},
},
},
"/:id",
describeRoute({
description: "Delete an existing channel",
responses: {
200: {
description: "Success deleting channel",
content: {
"application/json": { schema: resolver(deleteChannelSchema) },
},
}),
zValidator("json", deleteChannelSchema),
async (c) => {
const id = c.req.param("id");
const data = c.req.valid("json") as DeleteChannelInput;
// Ensure the ID in the path matches the one in the body
if (data.id !== id) {
return c.json({ error: "ID in path does not match ID in body" }, 400);
}
const result = await deleteExistingChannel(data);
if (result) {
return c.json({ success: true });
} else {
return c.json({ error: "Failed to delete channel" }, 400);
}
},
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 id = c.req.param("id");
const data = c.req.valid("json") as DeleteChannelInput;
// Ensure the ID in the path matches the one in the body
if (data.id !== id) {
return c.json({ error: "ID in path does not match ID in body" }, 400);
}
const result = await deleteExistingChannel(data);
if (result) {
return c.json({ success: true });
} else {
return c.json({ error: "Failed to delete channel" }, 400);
}
},
);
// Delete all channels by category ID
channelRoutes.delete(
"/category/:categoryId",
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)},
},
},
"/category/:categoryId",
describeRoute({
description: "Delete all channels by category id",
responses: {
200: {
description: "Success deleting all channels in category",
content: {
"application/json": {
schema: resolver(deleteChannelsByCategoryIdSchema),
},
},
}),
zValidator("json", deleteChannelsByCategoryIdSchema),
async (c) => {
const categoryId = c.req.param("categoryId");
const data = c.req.valid("json") as DeleteChannelsByCategoryIdInput;
// Ensure the categoryId in the path matches the one in the body
if (data.categoryId !== categoryId) {
return c.json({ error: "Category ID in path does not match Category ID in body" }, 400);
}
const result = await deleteAllChannelsByCategory(data);
if (result) {
return c.json({ success: true });
} else {
return c.json({ error: "Failed to delete channels" }, 400);
}
}
)
},
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 categoryId = c.req.param("categoryId");
const data = c.req.valid("json") as DeleteChannelsByCategoryIdInput;
export { channelRoutes };
// Ensure the categoryId in the path matches the one in the body
if (data.categoryId !== categoryId) {
return c.json(
{ error: "Category ID in path does not match Category ID in body" },
400,
);
}
const result = await deleteAllChannelsByCategory(data);
if (result) {
return c.json({ success: true });
} else {
return c.json({ error: "Failed to delete channels" }, 400);
}
},
);
export { channelRoutes };

View File

@@ -1,63 +1,77 @@
import { Hono } from "hono";
import { describeRoute, resolver } from "hono-openapi";
import { createInstanceRequestSchema, getAllInstancesResponseSchema } from "../validators/instanceValidator";
import {
createInstanceRequestSchema,
getAllInstancesResponseSchema,
} from "../validators/instanceValidator";
import { zValidator } from "@hono/zod-validator";
import { createInstanceReq, getAllInstancesReq } from "../controller/instanceController";
import {
createInstanceReq,
getAllInstancesReq,
} from "../controller/instanceController";
const instanceRoutes = new Hono();
instanceRoutes.post(
"",
describeRoute({
description: "Create instance",
responses: {
200: {
description: "Instance created",
content: {
"application/json": { schema: resolver(createInstanceRequestSchema) }
},
},
400: {
description: "Invalid request",
},
}
}),
zValidator('json', createInstanceRequestSchema),
async (c) => {
const data = await c.req.json();
if (!data) {
return c.json({ error: "could not parse data" }, 400);
}
const instance = await createInstanceReq(data);
return c.json(instance, 201);
"",
describeRoute({
description: "Create instance",
responses: {
200: {
description: "Instance created",
content: {
"application/json": { schema: resolver(createInstanceRequestSchema) },
},
},
400: {
description: "Invalid request",
},
},
}),
zValidator("json", createInstanceRequestSchema),
async (c) => {
const data = await c.req.json();
if (!data) {
return c.json({ error: "could not parse data" }, 400);
}
const instance = await createInstanceReq(data);
return c.json(instance, 201);
},
);
instanceRoutes.get(
"",
describeRoute({
description: "Get all instances",
responses: {
200: {
description: "List of all instances",
content: {
"application/json": { schema: resolver(getAllInstancesResponseSchema) }
},
},
500: {
description: "Server error",
},
}
}),
async (c) => {
const instances = await getAllInstancesReq();
if (instances.success) {
return c.json(instances, 200);
} else {
return c.json({ success: false, error: instances.error || "Failed to fetch instances" }, 500);
}
"",
describeRoute({
description: "Get all instances",
responses: {
200: {
description: "List of all instances",
content: {
"application/json": {
schema: resolver(getAllInstancesResponseSchema),
},
},
},
500: {
description: "Server error",
},
},
}),
async (c) => {
const instances = await getAllInstancesReq();
if (instances.success) {
return c.json(instances, 200);
} else {
return c.json(
{
success: false,
error: instances.error || "Failed to fetch instances",
},
500,
);
}
},
);
export default instanceRoutes;

View File

@@ -1,121 +1,146 @@
import { Hono } from "hono";
import { describeResponse, describeRoute, resolver } from "hono-openapi";
import { getMessageByIdSchema, getMessagesBeforeDate, sendMessageSchema } from "../validators/messageValidator";
import {
getMessageByIdSchema,
getMessagesBeforeDate,
sendMessageSchema,
} from "../validators/messageValidator";
import { zValidator } from "@hono/zod-validator";
import { fetchMessageData, fetchMessagesBefore, sendMessage } from "../controller/messageController";
import {
fetchMessageData,
fetchMessagesBefore,
sendMessage,
} from "../controller/messageController";
const messageRoutes = new Hono();
messageRoutes.get(
"/:id",
describeRoute({
description: "Get message by id",
responses: {
200: {
description: "Success getting message",
content: {
"application/json": { schema: resolver(getMessageByIdSchema) }
}
},
404: {
description: "Message id not found",
content: {
"application/json": { schema: resolver(getMessageByIdSchema) }
}
}
}
}),
zValidator("param", getMessageByIdSchema),
async (c) => {
const id = c.req.param("id");
const messageData = await fetchMessageData(id);
"/:id",
describeRoute({
description: "Get message by id",
responses: {
200: {
description: "Success getting message",
content: {
"application/json": { schema: resolver(getMessageByIdSchema) },
},
},
404: {
description: "Message id not found",
content: {
"application/json": { schema: resolver(getMessageByIdSchema) },
},
},
},
}),
zValidator("param", getMessageByIdSchema),
async (c) => {
const id = c.req.param("id");
const messageData = await fetchMessageData(id);
if (messageData) {
return c.json(messageData, 200);
} else {
return c.json({ error: "Message not found" }, 404);
}
if (messageData) {
return c.json(messageData, 200);
} else {
return c.json({ error: "Message not found" }, 404);
}
)
},
);
messageRoutes.get(
"",
describeRoute({
description: "Get up to 50 messages prior to given datetime",
responses: {
200: {
description: "Success getting up to 50 messages",
content: {
"application/json": { schema: resolver(getMessagesBeforeDate) }
}
}
}
}),
zValidator("query", getMessagesBeforeDate),
async (c) => {
const date = c.req.query("date");
if (!date) {
return c.json({ error: "date not provided" }, 400);
}
const channelId = c.req.query("channelId");
if (!channelId) {
return c.json({ error: "channelId not provided" }, 400);
}
const messagesArr = await fetchMessagesBefore(date, channelId);
if (messagesArr) {
return c.json(messagesArr, 200);
} else {
return c.json({ error: "Failed to fetch messages" }, 500);
}
"",
describeRoute({
description: "Get up to 50 messages prior to given datetime",
responses: {
200: {
description: "Success getting up to 50 messages",
content: {
"application/json": { schema: resolver(getMessagesBeforeDate) },
},
},
},
}),
zValidator("query", getMessagesBeforeDate),
async (c) => {
const date = c.req.query("date");
if (!date) {
return c.json({ error: "date not provided" }, 400);
}
)
const channelId = c.req.query("channelId");
if (!channelId) {
return c.json({ error: "channelId not provided" }, 400);
}
const messagesArr = await fetchMessagesBefore(date, channelId);
if (messagesArr) {
return c.json(messagesArr, 200);
} else {
return c.json({ error: "Failed to fetch messages" }, 500);
}
},
);
messageRoutes.post(
"",
describeRoute({
description: "Send a message to a channel",
responses: {
201: {
description: "Message sent successfully",
content: {
"application/json": { schema: resolver(sendMessageSchema) }
}
"",
describeRoute({
description: "Send a message to a channel",
responses: {
201: {
description: "Message sent successfully",
content: {
"application/json": { schema: resolver(sendMessageSchema) },
},
},
401: {
description: "Unauthorized - invalid token or user credentials",
content: {
"application/json": {
schema: {
type: "object",
properties: { error: { type: "string" } },
},
401: {
description: "Unauthorized - invalid token or user credentials",
content: {
"application/json": { schema: { type: "object", properties: { error: { type: "string" } } } }
}
},
},
},
500: {
description: "Server error",
content: {
"application/json": {
schema: {
type: "object",
properties: { error: { type: "string" } },
},
500: {
description: "Server error",
content: {
"application/json": { schema: { type: "object", properties: { error: { type: "string" } } } }
}
}
}
}),
zValidator("json", sendMessageSchema),
async (c) => {
const { channelId, userId, content, token, repliedMessageId } = await c.req.json();
const result = await sendMessage(
channelId,
userId,
content,
token,
repliedMessageId || null
);
},
},
},
},
}),
zValidator("json", sendMessageSchema),
async (c) => {
const { channelId, userId, content, token, repliedMessageId } =
await c.req.json();
if (result) {
return c.json(result, 201);
} else {
return c.json({ error: "Failed to send message. Check your credentials and try again." }, 401);
}
const result = await sendMessage(
channelId,
userId,
content,
token,
repliedMessageId || null,
);
if (result) {
return c.json(result, 201);
} else {
return c.json(
{
error:
"Failed to send message. Check your credentials and try again.",
},
401,
);
}
)
},
);
export default messageRoutes;
export default messageRoutes;