HEX
Server: Apache/2.4.52 (Ubuntu)
System: Linux spn-python 5.15.0-89-generic #99-Ubuntu SMP Mon Oct 30 20:42:41 UTC 2023 x86_64
User: arjun (1000)
PHP: 8.1.2-1ubuntu2.20
Disabled: NONE
Upload Files
File: //home/arjun/projects/good-life-be/api/Category/service.js
import BadRequest from '../../helper/exception/badRequest.js';
import Category from '../../models/Category.js';
import SubCategory from '../../models/SubCategory.js';

export const categoryList = async () => {
  const data = await Category.findAll({
    attributes: ['id', 'name', 'color'],
    order: [['updatedAt', 'ASC']],
  });
  if (!data) throw new BadRequest('No data found');

  return data;
};

export const subcategoryList = async (categoryId) => {
  if (!categoryId) throw new BadRequest('Category ID is required');

  const data = await SubCategory.findAll({
    where: { category_id: categoryId },
  });

  if (data.length === 0) {
    throw new BadRequest('No subcategories found for this category');
  }

  return data;
};