chore: compelete danak category service
This commit is contained in:
@@ -1,17 +1,27 @@
|
||||
import { BadRequestException, Injectable, Logger } from "@nestjs/common";
|
||||
import { BadRequestException, Injectable } from "@nestjs/common";
|
||||
import { FindOptionsWhere, IsNull } from "typeorm";
|
||||
|
||||
import { ParamDto } from "../../../common/DTO/param.dto";
|
||||
import { CategoryMessage, CommonMessage } from "../../../common/enums/message.enum";
|
||||
import { PaginationUtils } from "../../utils/providers/pagination.utils";
|
||||
import { CategoryListSearchQueryDto, CategorySearchQueryDto } from "../DTO/category-search-query.dto";
|
||||
import { CreateCategoryDto } from "../DTO/create-category.dto";
|
||||
import { CreateServiceDto } from "../DTO/create-service.dto";
|
||||
import { DanakServiceCategory } from "../entities/danak-service-category.entity";
|
||||
import { DanakServicesCategoryRepository } from "../repositories/danak-services-category.repository";
|
||||
import { DanakServicesRepository } from "../repositories/danak-services.repository";
|
||||
|
||||
@Injectable()
|
||||
export class DanakServicesService {
|
||||
private readonly logger = new Logger(DanakServicesService.name);
|
||||
constructor(private readonly danakServicesCategoryRepository: DanakServicesCategoryRepository) {}
|
||||
/********** */
|
||||
// private readonly logger = new Logger(DanakServicesService.name);
|
||||
constructor(
|
||||
private readonly danakServicesRepository: DanakServicesRepository,
|
||||
private readonly danakServicesCategoryRepository: DanakServicesCategoryRepository,
|
||||
) {}
|
||||
/******************************************** */
|
||||
|
||||
async createCategory(createDto: CreateCategoryDto) {
|
||||
this.logger.log(`Creating a new category with title: ${createDto.title}`);
|
||||
const existCategory = await this.danakServicesCategoryRepository.findOneBy({ title: createDto.title });
|
||||
const existCategory = await this.danakServicesCategoryRepository.findOneByTitle(createDto.title);
|
||||
if (existCategory) throw new BadRequestException(CategoryMessage.TITLE_EXIST);
|
||||
const category = this.danakServicesCategoryRepository.create(createDto);
|
||||
await this.danakServicesCategoryRepository.save(category);
|
||||
@@ -20,5 +30,81 @@ export class DanakServicesService {
|
||||
category,
|
||||
};
|
||||
}
|
||||
/********** */
|
||||
/******************************************** */
|
||||
async getCategories(queryDto: CategorySearchQueryDto) {
|
||||
const where: FindOptionsWhere<DanakServiceCategory> = {
|
||||
isActive: true,
|
||||
};
|
||||
|
||||
if (queryDto.parentId === undefined) {
|
||||
where.parentId = IsNull();
|
||||
} else {
|
||||
// If parentId is provided, use it
|
||||
where.parentId = queryDto.parentId;
|
||||
}
|
||||
|
||||
const categories = await this.danakServicesCategoryRepository.find({
|
||||
where,
|
||||
order: { createdAt: "ASC" },
|
||||
});
|
||||
|
||||
return { categories };
|
||||
}
|
||||
|
||||
/******************************************** */
|
||||
async getCategoryList(queryDto: CategoryListSearchQueryDto) {
|
||||
const { limit, skip } = PaginationUtils(queryDto);
|
||||
const queryBuilder = this.danakServicesCategoryRepository
|
||||
.createQueryBuilder("category")
|
||||
.loadRelationCountAndMap("category.servicesCount", "category.danakService")
|
||||
.loadRelationCountAndMap("category.childrenCount", "category.children");
|
||||
|
||||
if (queryDto.isActive !== undefined) {
|
||||
queryBuilder.andWhere("category.isActive = :isActive", { isActive: queryDto.isActive === 1 });
|
||||
}
|
||||
|
||||
if (queryDto.q) {
|
||||
queryBuilder.andWhere("category.title LIKE :q", { q: `%${queryDto.q}%` });
|
||||
}
|
||||
|
||||
queryBuilder
|
||||
.leftJoinAndSelect("category.parent", "parent")
|
||||
.select(["category", "parent.id", "parent.title"])
|
||||
.orderBy("category.createdAt", "DESC");
|
||||
|
||||
const [categories, count] = await queryBuilder.skip(skip).take(limit).getManyAndCount();
|
||||
|
||||
return { categories, count };
|
||||
}
|
||||
/******************************************** */
|
||||
|
||||
async toggleCategoryStatus(paramDto: ParamDto) {
|
||||
const category = await this.danakServicesCategoryRepository.findOneById(paramDto.id);
|
||||
if (!category) throw new BadRequestException(CategoryMessage.CATEGORY_NOT_EXIST);
|
||||
category.isActive = !category.isActive;
|
||||
await this.danakServicesCategoryRepository.save(category);
|
||||
return {
|
||||
message: CommonMessage.UPDATE_SUCCESS,
|
||||
isActive: category.isActive,
|
||||
};
|
||||
}
|
||||
/******************************************** */
|
||||
async createService(createDto: CreateServiceDto) {
|
||||
const category = await this.danakServicesCategoryRepository.findOneById(createDto.categoryId);
|
||||
if (!category) throw new BadRequestException(CategoryMessage.CATEGORY_NOT_EXIST);
|
||||
|
||||
const images = createDto.images.map((image) => {
|
||||
return { imageUrl: image };
|
||||
});
|
||||
|
||||
const service = this.danakServicesRepository.create({ ...createDto, category, images });
|
||||
await this.danakServicesRepository.save(service);
|
||||
return {
|
||||
message: CommonMessage.CREATED,
|
||||
service,
|
||||
};
|
||||
}
|
||||
|
||||
/******************************************** */
|
||||
/******************************************** */
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user