From 355ea984b07fa1d573729cadc893693af51adafe Mon Sep 17 00:00:00 2001 From: Mahdi Date: Thu, 7 May 2026 21:22:50 +0330 Subject: [PATCH] add businesses CRUD module --- src/app.module.ts | 2 + .../businesses/businesses.controller.ts | 57 +++++++++ src/modules/businesses/businesses.module.ts | 13 ++ src/modules/businesses/businesses.service.ts | 114 ++++++++++++++++++ .../businesses/dto/create-business.dto.ts | 12 ++ .../businesses/dto/update-business.dto.ts | 12 ++ 6 files changed, 210 insertions(+) create mode 100644 src/modules/businesses/businesses.controller.ts create mode 100644 src/modules/businesses/businesses.module.ts create mode 100644 src/modules/businesses/businesses.service.ts create mode 100644 src/modules/businesses/dto/create-business.dto.ts create mode 100644 src/modules/businesses/dto/update-business.dto.ts diff --git a/src/app.module.ts b/src/app.module.ts index a81cc7a..417cd6a 100644 --- a/src/app.module.ts +++ b/src/app.module.ts @@ -5,12 +5,14 @@ import { AppController } from './app.controller'; import { AppService } from './app.service'; import { dataSourceOptions } from './config/typeorm.config'; import { BusinessCategoriesModule } from './modules/business-categories/business-categories.module'; +import { BusinessesModule } from './modules/businesses/businesses.module'; @Module({ imports: [ ConfigModule.forRoot({ isGlobal: true }), TypeOrmModule.forRoot(dataSourceOptions), BusinessCategoriesModule, + BusinessesModule, ], controllers: [AppController], providers: [AppService], diff --git a/src/modules/businesses/businesses.controller.ts b/src/modules/businesses/businesses.controller.ts new file mode 100644 index 0000000..43ee0b2 --- /dev/null +++ b/src/modules/businesses/businesses.controller.ts @@ -0,0 +1,57 @@ +import { + Body, + Controller, + Delete, + Get, + Param, + Patch, + Post, + Query, +} from '@nestjs/common'; +import { BusinessEntity } from '../../entities/business.entity'; +import { BusinessesService } from './businesses.service'; +import { CreateBusinessDto } from './dto/create-business.dto'; +import { UpdateBusinessDto } from './dto/update-business.dto'; + +@Controller('businesses') +export class BusinessesController { + constructor(private readonly service: BusinessesService) {} + + @Get() + findAll( + @Query('categoryId') categoryId?: string, + @Query('isActive') isActive?: string, + ): Promise { + const parsedIsActive = + typeof isActive === 'string' ? isActive === 'true' : undefined; + + return this.service.findAll({ + categoryId, + isActive: parsedIsActive, + }); + } + + @Get(':id') + findOne(@Param('id') id: string): Promise { + return this.service.findOne(id); + } + + @Post() + create(@Body() payload: CreateBusinessDto): Promise { + return this.service.create(payload); + } + + @Patch(':id') + update( + @Param('id') id: string, + @Body() payload: UpdateBusinessDto, + ): Promise { + 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 }; + } +} diff --git a/src/modules/businesses/businesses.module.ts b/src/modules/businesses/businesses.module.ts new file mode 100644 index 0000000..f328e20 --- /dev/null +++ b/src/modules/businesses/businesses.module.ts @@ -0,0 +1,13 @@ +import { Module } from '@nestjs/common'; +import { TypeOrmModule } from '@nestjs/typeorm'; +import { BusinessCategoryEntity } from '../../entities/business-category.entity'; +import { BusinessEntity } from '../../entities/business.entity'; +import { BusinessesController } from './businesses.controller'; +import { BusinessesService } from './businesses.service'; + +@Module({ + imports: [TypeOrmModule.forFeature([BusinessEntity, BusinessCategoryEntity])], + controllers: [BusinessesController], + providers: [BusinessesService], +}) +export class BusinessesModule {} diff --git a/src/modules/businesses/businesses.service.ts b/src/modules/businesses/businesses.service.ts new file mode 100644 index 0000000..40150d8 --- /dev/null +++ b/src/modules/businesses/businesses.service.ts @@ -0,0 +1,114 @@ +import { + ConflictException, + Injectable, + NotFoundException, +} from '@nestjs/common'; +import { InjectRepository } from '@nestjs/typeorm'; +import { Repository } from 'typeorm'; +import { BusinessCategoryEntity } from '../../entities/business-category.entity'; +import { BusinessEntity } from '../../entities/business.entity'; +import { CreateBusinessDto } from './dto/create-business.dto'; +import { UpdateBusinessDto } from './dto/update-business.dto'; + +type FindAllFilters = { + categoryId?: string; + isActive?: boolean; +}; + +@Injectable() +export class BusinessesService { + constructor( + @InjectRepository(BusinessEntity) + private readonly repo: Repository, + @InjectRepository(BusinessCategoryEntity) + private readonly categoryRepo: Repository, + ) {} + + async findAll(filters: FindAllFilters): Promise { + const query = this.repo + .createQueryBuilder('business') + .leftJoinAndSelect('business.category', 'category') + .orderBy('business.createdAt', 'DESC'); + + if (filters.categoryId) { + query.andWhere('business.categoryId = :categoryId', { + categoryId: filters.categoryId, + }); + } + + if (typeof filters.isActive === 'boolean') { + query.andWhere('business.isActive = :isActive', { + isActive: filters.isActive, + }); + } + + return query.getMany(); + } + + async findOne(id: string): Promise { + const business = await this.repo.findOne({ + where: { id }, + relations: { category: true }, + }); + if (!business) { + throw new NotFoundException('Business not found'); + } + return business; + } + + async create(payload: CreateBusinessDto): Promise { + const slugTaken = await this.repo.findOne({ where: { slug: payload.slug } }); + if (slugTaken) { + throw new ConflictException('Slug already exists'); + } + + await this.ensureCategoryExists(payload.categoryId); + + const entity = this.repo.create({ + title: payload.title, + slug: payload.slug, + categoryId: payload.categoryId, + description: payload.description ?? null, + contactPhone: payload.contactPhone ?? null, + address: payload.address ?? null, + latitude: payload.latitude ?? null, + longitude: payload.longitude ?? null, + ownerId: payload.ownerId ?? null, + isActive: payload.isActive ?? true, + }); + + return this.repo.save(entity); + } + + async update(id: string, payload: UpdateBusinessDto): Promise { + const business = await this.findOne(id); + + if (payload.slug && payload.slug !== business.slug) { + const slugTaken = await this.repo.findOne({ where: { slug: payload.slug } }); + if (slugTaken) { + throw new ConflictException('Slug already exists'); + } + } + + if (payload.categoryId && payload.categoryId !== business.categoryId) { + await this.ensureCategoryExists(payload.categoryId); + } + + Object.assign(business, payload); + return this.repo.save(business); + } + + async remove(id: string): Promise { + const result = await this.repo.delete(id); + if (!result.affected) { + throw new NotFoundException('Business not found'); + } + } + + private async ensureCategoryExists(categoryId: string): Promise { + const category = await this.categoryRepo.findOne({ where: { id: categoryId } }); + if (!category) { + throw new NotFoundException('Business category not found'); + } + } +} diff --git a/src/modules/businesses/dto/create-business.dto.ts b/src/modules/businesses/dto/create-business.dto.ts new file mode 100644 index 0000000..e9fb391 --- /dev/null +++ b/src/modules/businesses/dto/create-business.dto.ts @@ -0,0 +1,12 @@ +export class CreateBusinessDto { + title!: string; + slug!: string; + categoryId!: string; + description?: string; + contactPhone?: string; + address?: string; + latitude?: number; + longitude?: number; + ownerId?: string; + isActive?: boolean; +} diff --git a/src/modules/businesses/dto/update-business.dto.ts b/src/modules/businesses/dto/update-business.dto.ts new file mode 100644 index 0000000..db97b8d --- /dev/null +++ b/src/modules/businesses/dto/update-business.dto.ts @@ -0,0 +1,12 @@ +export class UpdateBusinessDto { + title?: string; + slug?: string; + categoryId?: string; + description?: string | null; + contactPhone?: string | null; + address?: string | null; + latitude?: number | null; + longitude?: number | null; + ownerId?: string | null; + isActive?: boolean; +}