add business categories CRUD module

This commit is contained in:
2026-05-07 19:11:14 +03:30
parent 53113d8108
commit 729cc9f4b9
16 changed files with 3263 additions and 1 deletions
@@ -0,0 +1,49 @@
import {
Body,
Controller,
Delete,
Get,
Param,
Patch,
Post,
} from '@nestjs/common';
import { BusinessCategoryEntity } from '../../entities/business-category.entity';
import { BusinessCategoriesService } from './business-categories.service';
import { CreateBusinessCategoryDto } from './dto/create-business-category.dto';
import { UpdateBusinessCategoryDto } from './dto/update-business-category.dto';
@Controller('business-categories')
export class BusinessCategoriesController {
constructor(private readonly service: BusinessCategoriesService) {}
@Get()
findAll(): Promise<BusinessCategoryEntity[]> {
return this.service.findAll();
}
@Get(':id')
findOne(@Param('id') id: string): Promise<BusinessCategoryEntity> {
return this.service.findOne(id);
}
@Post()
create(
@Body() payload: CreateBusinessCategoryDto,
): Promise<BusinessCategoryEntity> {
return this.service.create(payload);
}
@Patch(':id')
update(
@Param('id') id: string,
@Body() payload: UpdateBusinessCategoryDto,
): Promise<BusinessCategoryEntity> {
return this.service.update(id, payload);
}
@Delete(':id')
async remove(@Param('id') id: string): Promise<{ success: true }> {
await this.service.remove(id);
return { success: true };
}
}
@@ -0,0 +1,12 @@
import { Module } from '@nestjs/common';
import { TypeOrmModule } from '@nestjs/typeorm';
import { BusinessCategoryEntity } from '../../entities/business-category.entity';
import { BusinessCategoriesController } from './business-categories.controller';
import { BusinessCategoriesService } from './business-categories.service';
@Module({
imports: [TypeOrmModule.forFeature([BusinessCategoryEntity])],
controllers: [BusinessCategoriesController],
providers: [BusinessCategoriesService],
})
export class BusinessCategoriesModule {}
@@ -0,0 +1,70 @@
import {
ConflictException,
Injectable,
NotFoundException,
} from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { Repository } from 'typeorm';
import { BusinessCategoryEntity } from '../../entities/business-category.entity';
import { CreateBusinessCategoryDto } from './dto/create-business-category.dto';
import { UpdateBusinessCategoryDto } from './dto/update-business-category.dto';
@Injectable()
export class BusinessCategoriesService {
constructor(
@InjectRepository(BusinessCategoryEntity)
private readonly repo: Repository<BusinessCategoryEntity>,
) {}
findAll(): Promise<BusinessCategoryEntity[]> {
return this.repo.find({ order: { createdAt: 'DESC' } });
}
async findOne(id: string): Promise<BusinessCategoryEntity> {
const category = await this.repo.findOne({ where: { id } });
if (!category) {
throw new NotFoundException('Business category not found');
}
return category;
}
async create(
payload: CreateBusinessCategoryDto,
): Promise<BusinessCategoryEntity> {
const exists = await this.repo.findOne({ where: { slug: payload.slug } });
if (exists) {
throw new ConflictException('Slug already exists');
}
const entity = this.repo.create({
title: payload.title,
slug: payload.slug,
isActive: payload.isActive ?? true,
});
return this.repo.save(entity);
}
async update(
id: string,
payload: UpdateBusinessCategoryDto,
): Promise<BusinessCategoryEntity> {
const category = await this.findOne(id);
if (payload.slug && payload.slug !== category.slug) {
const slugTaken = await this.repo.findOne({ where: { slug: payload.slug } });
if (slugTaken) {
throw new ConflictException('Slug already exists');
}
}
Object.assign(category, payload);
return this.repo.save(category);
}
async remove(id: string): Promise<void> {
const result = await this.repo.delete(id);
if (!result.affected) {
throw new NotFoundException('Business category not found');
}
}
}
@@ -0,0 +1,5 @@
export class CreateBusinessCategoryDto {
title!: string;
slug!: string;
isActive?: boolean;
}
@@ -0,0 +1,5 @@
export class UpdateBusinessCategoryDto {
title?: string;
slug?: string;
isActive?: boolean;
}