catalogue

This commit is contained in:
2026-03-09 14:19:14 +03:30
parent 4c1d747068
commit 3549d3eab8
5 changed files with 97 additions and 74 deletions
@@ -1,11 +1,12 @@
import { Controller, Get, Post, Body, Patch, Param, Delete } from '@nestjs/common'; import { Controller, Get, Post, Body, Patch, Param, Delete, Query } from '@nestjs/common';
import { CatalogueService } from './catalogue.service'; import { CatalogueService } from './catalogue.service';
import { CreateCatalogueDto } from './dto/create-catalogue.dto'; import { CreateCatalogueDto } from './dto/create-catalogue.dto';
import { UpdateCatalogueDto } from './dto/update-catalogue.dto'; import { UpdateCatalogueDto } from './dto/update-catalogue.dto';
import { FindCataloguesDto } from './dto/find-catalogues.dto';
@Controller('catalogue') @Controller('catalogue')
export class CatalogueController { export class CatalogueController {
constructor(private readonly catalogueService: CatalogueService) {} constructor(private readonly catalogueService: CatalogueService) { }
@Post() @Post()
create(@Body() createCatalogueDto: CreateCatalogueDto) { create(@Body() createCatalogueDto: CreateCatalogueDto) {
@@ -13,22 +14,22 @@ export class CatalogueController {
} }
@Get() @Get()
findAll() { findAll(@Query() queryDto: FindCataloguesDto) {
return this.catalogueService.findAll(); return this.catalogueService.findAll(queryDto);
} }
@Get(':id') @Get(':id')
findOne(@Param('id') id: string) { findOne(@Param('id') id: string) {
return this.catalogueService.findOne(+id); return this.catalogueService.findOne(id);
} }
@Patch(':id') @Patch(':id')
update(@Param('id') id: string, @Body() updateCatalogueDto: UpdateCatalogueDto) { update(@Param('id') id: string, @Body() updateCatalogueDto: UpdateCatalogueDto) {
return this.catalogueService.update(+id, updateCatalogueDto); return this.catalogueService.update(id, updateCatalogueDto);
} }
@Delete(':id') @Delete(':id')
remove(@Param('id') id: string) { remove(@Param('id') id: string) {
return this.catalogueService.remove(+id); return this.catalogueService.remove(id);
} }
} }
+28 -10
View File
@@ -1,15 +1,18 @@
import { BadRequestException, Injectable } from '@nestjs/common'; import { BadRequestException, Injectable, NotFoundException } from '@nestjs/common';
import { CreateCatalogueDto } from './dto/create-catalogue.dto'; import { CreateCatalogueDto } from './dto/create-catalogue.dto';
import { UpdateCatalogueDto } from './dto/update-catalogue.dto'; import { UpdateCatalogueDto } from './dto/update-catalogue.dto';
import { EntityManager } from '@mikro-orm/postgresql'; import { EntityManager } from '@mikro-orm/postgresql';
import { Catalogue } from './entities/catalogue.entity'; import { Catalogue } from './entities/catalogue.entity';
import { Business } from '../business/entities/business.entity'; import { Business } from '../business/entities/business.entity';
import { CatalogueStatus } from './enums/company-status.enum'; import { CatalogueStatus } from './enums/company-status.enum';
import { CatalogueRepository } from './repositories/catalogue.repository';
import { FindCataloguesDto } from './dto/find-catalogues.dto';
@Injectable() @Injectable()
export class CatalogueService { export class CatalogueService {
constructor( constructor(
private readonly em: EntityManager, private readonly em: EntityManager,
private readonly catalogueRepository: CatalogueRepository
) { } ) { }
async create(dto: CreateCatalogueDto) { async create(dto: CreateCatalogueDto) {
@@ -18,23 +21,38 @@ export class CatalogueService {
throw new BadRequestException() throw new BadRequestException()
} }
const catalogue = this.em.create(Catalogue, const catalogue = this.em.create(Catalogue,
{ ...dto, business, status: CatalogueStatus.PENDING }) { ...dto, business, status: CatalogueStatus.PENDING })
return this.em.persistAndFlush(catalogue) return this.em.persistAndFlush(catalogue)
} }
findAll() { findAll(dto: FindCataloguesDto) {
return `This action returns all catalogue`; return this.catalogueRepository.findAllPaginated(dto)
} }
findOne(id: number) { async findOne(id: string) {
return `This action returns a #${id} catalogue`; const catalogue = await this.findOneOrFail(id)
return catalogue
} }
update(id: number, updateCatalogueDto: UpdateCatalogueDto) { async findOneOrFail(id: string) {
return `This action updates a #${id} catalogue`; const catalogue = await this.catalogueRepository.findOne({ id })
if (!catalogue) {
throw new NotFoundException("catalogue not found")
}
return catalogue
} }
remove(id: number) { async update(id: string, dto: UpdateCatalogueDto) {
return `This action removes a #${id} catalogue`; const catalogue = await this.findOneOrFail(id)
this.em.assign(catalogue, dto)
await this.em.flush()
return catalogue
}
async remove(id: string) {
const catalogue = await this.findOneOrFail(id)
catalogue.deletedAt = new Date()
await this.em.flush()
return catalogue
} }
} }
@@ -2,7 +2,7 @@
import { IsString, IsOptional, IsBoolean, IsInt, Min, IsEnum } from 'class-validator'; import { IsString, IsOptional, IsBoolean, IsInt, Min, IsEnum } from 'class-validator';
import { ApiPropertyOptional, ApiProperty } from '@nestjs/swagger'; import { ApiPropertyOptional, ApiProperty } from '@nestjs/swagger';
import { Type } from 'class-transformer'; import { Type } from 'class-transformer';
import { CatalogueSize, CatalogueStatus } from '../enums/company-status.enum'; import { CatalogueSize } from '../enums/company-status.enum';
export class CreateCatalogueDto { export class CreateCatalogueDto {
@IsString() @IsString()
@@ -0,0 +1,35 @@
import { IsOptional, IsNumber, IsString, IsIn, IsBoolean } from 'class-validator';
import { ApiPropertyOptional } from '@nestjs/swagger';
import { Type } from 'class-transformer';
export class FindCataloguesDto {
@IsOptional()
@IsNumber()
@Type(() => Number)
@ApiPropertyOptional({ example: 1 })
page?: number;
@IsOptional()
@IsNumber()
@Type(() => Number)
@ApiPropertyOptional({ example: 10 })
limit?: number;
@IsOptional()
@IsString()
@ApiPropertyOptional({ example: 'createdAt' })
orderBy?: string;
@IsOptional()
@IsIn(['asc', 'desc'])
@ApiPropertyOptional({ example: 'desc' })
order?: 'asc' | 'desc';
@IsOptional()
@IsBoolean()
@Type(() => Boolean)
@ApiPropertyOptional({ example: true })
isActive?: boolean;
}
@@ -1,67 +1,36 @@
import { EntityRepository, FilterQuery } from "@mikro-orm/postgresql"; import { EntityRepository, FilterQuery } from "@mikro-orm/postgresql";
import { Catalogue } from "../entities/catalogue.entity"; import { Catalogue } from "../entities/catalogue.entity";
import { CatalogueStatus } from "../enums/company-status.enum"; import { PaginatedResult } from "src/common/interfaces/pagination.interface";
import { FindCataloguesDto } from "../dto/find-catalogues.dto";
export class CatalogueRepository extends EntityRepository<Catalogue> { export class CatalogueRepository extends EntityRepository<Catalogue> {
//
// async getCataloguesForAdmin(queryDto: CompanyListQueryDto, businessId: string) {
// const { } = queryDto
// const where: FilterQuery<Catalogue> = { async findAllPaginated( opts: FindCataloguesDto = {}): Promise<PaginatedResult<Catalogue>> {
// deletedAt: null, const { page = 1, limit = 10, orderBy = 'order', order = 'asc', isActive } = opts;
// business: { id: businessId },
// };
// if (queryDto.status) { const offset = (page - 1) * limit;
// where.status = queryDto.status;
// }
// if (queryDto.isActive !== undefined) {
// where.isActive = queryDto.isActive === 1;
// }
// if (queryDto.q) {
// where.name = { $ilike: `%${queryDto.q}%` };
// }
// if (queryDto.registrationDate) { const where: FilterQuery<Catalogue> = {};
// where.createdAt = {
// $gte: queryDto.registrationDate,
// $lte: queryDto.registrationDate,
// };
// }
// if (queryDto.name) {
// where.name = { $ilike: `%${queryDto.name}%` };
// }
// return this.findAndCount(where, { const [data, total] = await this.findAndCount(where, {
// populate: [], limit,
// limit, offset,
// offset: skip, orderBy: { [orderBy]: order.toLowerCase() as 'asc' | 'desc' },
// orderBy: { createdAt: "DESC" }, populate: [],
// fields: [ });
// "id",
// "name",
// "isActive",
// "dateOfEstablishment",
// "status",
// "createdAt",
// ],
// });
// }
// async getCataloguesForPublic(queryDto: CompanyListPublicQueryDto, businessId: string) { const totalPages = Math.ceil(total / limit);
// const where: FilterQuery<Catalogue> = {
// deletedAt: null, return {
// business: { id: businessId }, data,
// isActive: true, meta: {
// status: CatalogueStatus.APPROVED, total,
// }; page,
limit,
totalPages,
},
};
// return this.find(where, { }
// populate: [],
// orderBy: { createdAt: "DESC" },
// fields: ["id", "name", "description", "profileImageUrl", "coverImageUrl", "createdAt"],
// });
// }
} }