Add business images upload and cascade delete improvements.
This commit is contained in:
@@ -8,6 +8,7 @@ import {
|
||||
import { InjectRepository } from '@nestjs/typeorm';
|
||||
import { DataSource, Repository } from 'typeorm';
|
||||
import { deleteBusinessCascade } from '../../common/utils/business-cascade.util';
|
||||
import { BusinessImagesService } from './business-images.service';
|
||||
import { BusinessCategoryEntity } from '../../entities/business-category.entity';
|
||||
import { BusinessEntity } from '../../entities/business.entity';
|
||||
import { UserEntity, UserRole } from '../../entities/user.entity';
|
||||
@@ -31,13 +32,17 @@ export class BusinessesService {
|
||||
@InjectRepository(UserEntity)
|
||||
private readonly userRepo: Repository<UserEntity>,
|
||||
private readonly dataSource: DataSource,
|
||||
private readonly images: BusinessImagesService,
|
||||
) {}
|
||||
|
||||
async findAll(filters: FindAllFilters): Promise<BusinessEntity[]> {
|
||||
const query = this.repo
|
||||
.createQueryBuilder('business')
|
||||
.leftJoinAndSelect('business.category', 'category')
|
||||
.orderBy('business.createdAt', 'DESC');
|
||||
.leftJoinAndSelect('business.images', 'images')
|
||||
.orderBy('business.createdAt', 'DESC')
|
||||
.addOrderBy('images.isCover', 'DESC')
|
||||
.addOrderBy('images.sortOrder', 'ASC');
|
||||
|
||||
if (filters.categoryId) {
|
||||
query.andWhere('business.categoryId = :categoryId', {
|
||||
@@ -57,7 +62,7 @@ export class BusinessesService {
|
||||
async findOne(id: string): Promise<BusinessEntity> {
|
||||
const business = await this.repo.findOne({
|
||||
where: { id },
|
||||
relations: { category: true },
|
||||
relations: { category: true, images: true },
|
||||
});
|
||||
if (!business) {
|
||||
throw new NotFoundException('Business not found');
|
||||
@@ -116,7 +121,9 @@ export class BusinessesService {
|
||||
await this.findOne(id);
|
||||
|
||||
await this.dataSource.transaction(async (manager) => {
|
||||
await deleteBusinessCascade(manager, id);
|
||||
await deleteBusinessCascade(manager, id, async (businessId) => {
|
||||
await this.images.removeAllForBusiness(businessId);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user