add businesses CRUD module

This commit is contained in:
2026-05-07 21:22:50 +03:30
parent 2aac1aaaed
commit 355ea984b0
6 changed files with 210 additions and 0 deletions
+2
View File
@@ -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],
@@ -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<BusinessEntity[]> {
const parsedIsActive =
typeof isActive === 'string' ? isActive === 'true' : undefined;
return this.service.findAll({
categoryId,
isActive: parsedIsActive,
});
}
@Get(':id')
findOne(@Param('id') id: string): Promise<BusinessEntity> {
return this.service.findOne(id);
}
@Post()
create(@Body() payload: CreateBusinessDto): Promise<BusinessEntity> {
return this.service.create(payload);
}
@Patch(':id')
update(
@Param('id') id: string,
@Body() payload: UpdateBusinessDto,
): Promise<BusinessEntity> {
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,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 {}
@@ -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<BusinessEntity>,
@InjectRepository(BusinessCategoryEntity)
private readonly categoryRepo: Repository<BusinessCategoryEntity>,
) {}
async findAll(filters: FindAllFilters): Promise<BusinessEntity[]> {
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<BusinessEntity> {
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<BusinessEntity> {
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<BusinessEntity> {
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<void> {
const result = await this.repo.delete(id);
if (!result.affected) {
throw new NotFoundException('Business not found');
}
}
private async ensureCategoryExists(categoryId: string): Promise<void> {
const category = await this.categoryRepo.findOne({ where: { id: categoryId } });
if (!category) {
throw new NotFoundException('Business category not found');
}
}
}
@@ -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;
}
@@ -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;
}