diff --git a/src/controllers/category.controller.ts b/src/controllers/category.controller.ts index dd8bdac3..2a89ccd9 100644 --- a/src/controllers/category.controller.ts +++ b/src/controllers/category.controller.ts @@ -1,26 +1,16 @@ import type { Request, Response } from 'express' -import type Category from '../entities/category.entity' import { getAllCategories } from '../services/category.service' -import { type PaginatedApiResponse } from '../types' +import type { ApiResponse } from '../types' +import type Category from '../entities/category.entity' export const getCategories = async ( req: Request, res: Response -): Promise>> => { +): Promise> => { try { - const pageNumber = parseInt(req.query.pageNumber as string) - const pageSize = parseInt(req.query.pageSize as string) - - const { statusCode, items, totalItemCount, message } = - await getAllCategories(pageNumber, pageSize) + const { statusCode, categories, message } = await getAllCategories() - return res.status(statusCode).json({ - pageNumber, - pageSize, - totalItemCount, - items, - message - }) + return res.status(statusCode).json({ categories, message }) } catch (err) { if (err instanceof Error) { console.error('Error executing query', err) diff --git a/src/routes/category/category.route.ts b/src/routes/category/category.route.ts index acdf21b5..3c9a95e3 100644 --- a/src/routes/category/category.route.ts +++ b/src/routes/category/category.route.ts @@ -1,10 +1,8 @@ import express from 'express' import { getCategories } from '../../controllers/category.controller' -import { requestQueryValidator } from '../../middlewares/requestValidator' -import { paginationSchema } from '../../schemas/common/pagination-request.schema' const categoryRouter = express.Router() -categoryRouter.get('/', requestQueryValidator(paginationSchema), getCategories) +categoryRouter.get('/', getCategories) export default categoryRouter diff --git a/src/services/category.service.test.ts b/src/services/category.service.test.ts index 1dfe80e8..2fb8d403 100644 --- a/src/services/category.service.test.ts +++ b/src/services/category.service.test.ts @@ -1,6 +1,6 @@ +import { getAllCategories } from './category.service' import { dataSource } from '../configs/dbConfig' import type Category from '../entities/category.entity' -import { getAllCategories } from './category.service' jest.mock('../configs/dbConfig', () => ({ dataSource: { @@ -22,37 +22,35 @@ describe('Category Service - getAllCategories', () => { ] as Category[] const mockCategoryRepository = { - find: jest.fn().mockResolvedValue(mockCategories), - findAndCount: jest.fn().mockResolvedValue([mockCategories, 2]) + find: jest.fn().mockResolvedValue(mockCategories) } ;(dataSource.getRepository as jest.Mock).mockReturnValueOnce( mockCategoryRepository ) - const result = await getAllCategories(1, 2) + const result = await getAllCategories() expect(result.statusCode).toBe(200) - expect(result.items?.length).toBe(2) - expect(result.items).toEqual([ + expect(result.categories?.length).toBe(2) + expect(result.categories).toEqual([ { uuid: 'mock-uuid-1', category: 'Category 1' }, { uuid: 'mock-uuid-2', category: 'Category 2' } ]) - expect(result.message).toBe('Categories retrieved successfully') + expect(result.message).toBe('All Categories found') }) it('should handle categories not found', async () => { const mockCategoryRepository = { - find: jest.fn().mockResolvedValue([]), - findAndCount: jest.fn().mockResolvedValue([[], 0]) + find: jest.fn().mockResolvedValue([]) } ;(dataSource.getRepository as jest.Mock).mockReturnValueOnce( mockCategoryRepository ) - const result = await getAllCategories(1, 2) + const result = await getAllCategories() - expect(result.items?.length).toBe(0) + expect(result.categories?.length).toBe(0) }) }) diff --git a/src/services/category.service.ts b/src/services/category.service.ts index 0012b080..a19042cf 100644 --- a/src/services/category.service.ts +++ b/src/services/category.service.ts @@ -1,47 +1,35 @@ import { dataSource } from '../configs/dbConfig' import Category from '../entities/category.entity' -import { type PaginatedApiResponse } from '../types' -export const getAllCategories = async ( - pageNumber: number, - pageSize: number -): Promise>> => { +export const getAllCategories = async (): Promise<{ + statusCode: number + categories?: Array> | null + message: string +}> => { try { const categoryRepository = dataSource.getRepository(Category) + const allCategories: Category[] = await categoryRepository.find({ + select: ['category', 'uuid'] + }) - const [allCategories, totalItemCount] = - await categoryRepository.findAndCount({ - select: ['category', 'uuid'], - skip: (pageNumber - 1) * pageSize, - take: pageSize - }) + const categories = allCategories.map((category) => { + return { category: category.category, uuid: category.uuid } + }) - const items = allCategories.map((category) => ({ - category: category.category, - uuid: category.uuid - })) - - if (items.length === 0) { + if (!categories) { return { - pageNumber, - pageSize, - totalItemCount, - items: [], statusCode: 404, - message: 'No categories found' + message: 'Categories not found' } } return { - pageNumber, - pageSize, - totalItemCount, - items, statusCode: 200, - message: 'Categories retrieved successfully' + categories, + message: 'All Categories found' } } catch (err) { - console.error('Error getting categories', err) - throw new Error('Error getting categories') + console.error('Error getting mentor', err) + throw new Error('Error getting mentor') } }