feat: implement category management API with CRUD operations for categories.
This commit is contained in:
parent
1caeac6226
commit
4b335b6b49
5 changed files with 220 additions and 2 deletions
84
Backend/src/services/categories.service.ts
Normal file
84
Backend/src/services/categories.service.ts
Normal file
|
|
@ -0,0 +1,84 @@
|
|||
import { prisma } from '../config/database';
|
||||
import { Prisma } from '@prisma/client';
|
||||
import { config } from '../config';
|
||||
import { logger } from '../config/logger';
|
||||
import jwt from 'jsonwebtoken';
|
||||
import { createCategory, createCategoryResponse, deleteCategoryResponse, updateCategory, updateCategoryResponse, listCategoriesResponse, Category } from '../types/categories.type';
|
||||
import { UnauthorizedError, ValidationError, ForbiddenError } from '../middleware/errorHandler';
|
||||
|
||||
export class CategoryService {
|
||||
async listCategories(): Promise<listCategoriesResponse> {
|
||||
try {
|
||||
const categories = await prisma.category.findMany();
|
||||
return {
|
||||
code: 200,
|
||||
message: 'Categories fetched successfully',
|
||||
data: {
|
||||
categories: categories as unknown as Category[],
|
||||
total: categories.length
|
||||
}
|
||||
};
|
||||
} catch (error) {
|
||||
logger.error('Failed to list categories', { error });
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
async createCategory(token: string, category: createCategory): Promise<createCategoryResponse> {
|
||||
try {
|
||||
const decoded = jwt.verify(token, config.jwt.secret) as { id: number; username: string; email: string; roleCode: string };
|
||||
const newCategory = await prisma.category.create({
|
||||
data: category
|
||||
});
|
||||
return {
|
||||
code: 200,
|
||||
message: 'Category created successfully',
|
||||
data: {
|
||||
id: newCategory.id,
|
||||
name: newCategory.name as { th: string; en: string },
|
||||
slug: newCategory.slug,
|
||||
description: newCategory.description as { th: string; en: string },
|
||||
created_by: decoded.id,
|
||||
}
|
||||
};
|
||||
} catch (error) {
|
||||
logger.error('Failed to create category', { error });
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
async updateCategory(token: string, id: number, category: updateCategory): Promise<updateCategoryResponse> {
|
||||
try {
|
||||
const decoded = jwt.verify(token, config.jwt.secret) as { id: number; username: string; email: string; roleCode: string };
|
||||
const updatedCategory = await prisma.category.update({
|
||||
where: { id },
|
||||
data: category
|
||||
});
|
||||
return {
|
||||
id: updatedCategory.id,
|
||||
name: updatedCategory.name as { th: string; en: string },
|
||||
slug: updatedCategory.slug,
|
||||
description: updatedCategory.description as { th: string; en: string },
|
||||
updated_by: decoded.id,
|
||||
};
|
||||
} catch (error) {
|
||||
logger.error('Failed to update category', { error });
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
async deleteCategory(id: number): Promise<deleteCategoryResponse> {
|
||||
try {
|
||||
const deletedCategory = await prisma.category.delete({
|
||||
where: { id }
|
||||
});
|
||||
return {
|
||||
code: 200,
|
||||
message: 'Category deleted successfully',
|
||||
};
|
||||
} catch (error) {
|
||||
logger.error('Failed to delete category', { error });
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue