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 { CreateCatalogueDto } from './dto/create-catalogue.dto';
import { UpdateCatalogueDto } from './dto/update-catalogue.dto';
import { FindCataloguesDto } from './dto/find-catalogues.dto';
@Controller('catalogue')
export class CatalogueController {
constructor(private readonly catalogueService: CatalogueService) {}
constructor(private readonly catalogueService: CatalogueService) { }
@Post()
create(@Body() createCatalogueDto: CreateCatalogueDto) {
@@ -13,22 +14,22 @@ export class CatalogueController {
}
@Get()
findAll() {
return this.catalogueService.findAll();
findAll(@Query() queryDto: FindCataloguesDto) {
return this.catalogueService.findAll(queryDto);
}
@Get(':id')
findOne(@Param('id') id: string) {
return this.catalogueService.findOne(+id);
return this.catalogueService.findOne(id);
}
@Patch(':id')
update(@Param('id') id: string, @Body() updateCatalogueDto: UpdateCatalogueDto) {
return this.catalogueService.update(+id, updateCatalogueDto);
return this.catalogueService.update(id, updateCatalogueDto);
}
@Delete(':id')
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 { UpdateCatalogueDto } from './dto/update-catalogue.dto';
import { EntityManager } from '@mikro-orm/postgresql';
import { Catalogue } from './entities/catalogue.entity';
import { Business } from '../business/entities/business.entity';
import { CatalogueStatus } from './enums/company-status.enum';
import { CatalogueRepository } from './repositories/catalogue.repository';
import { FindCataloguesDto } from './dto/find-catalogues.dto';
@Injectable()
export class CatalogueService {
constructor(
private readonly em: EntityManager,
private readonly catalogueRepository: CatalogueRepository
) { }
async create(dto: CreateCatalogueDto) {
@@ -18,23 +21,38 @@ export class CatalogueService {
throw new BadRequestException()
}
const catalogue = this.em.create(Catalogue,
{ ...dto, business, status: CatalogueStatus.PENDING })
{ ...dto, business, status: CatalogueStatus.PENDING })
return this.em.persistAndFlush(catalogue)
}
findAll() {
return `This action returns all catalogue`;
findAll(dto: FindCataloguesDto) {
return this.catalogueRepository.findAllPaginated(dto)
}
findOne(id: number) {
return `This action returns a #${id} catalogue`;
async findOne(id: string) {
const catalogue = await this.findOneOrFail(id)
return catalogue
}
update(id: number, updateCatalogueDto: UpdateCatalogueDto) {
return `This action updates a #${id} catalogue`;
async findOneOrFail(id: string) {
const catalogue = await this.catalogueRepository.findOne({ id })
if (!catalogue) {
throw new NotFoundException("catalogue not found")
}
return catalogue
}
remove(id: number) {
return `This action removes a #${id} catalogue`;
async update(id: string, dto: UpdateCatalogueDto) {
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 { ApiPropertyOptional, ApiProperty } from '@nestjs/swagger';
import { Type } from 'class-transformer';
import { CatalogueSize, CatalogueStatus } from '../enums/company-status.enum';
import { CatalogueSize } from '../enums/company-status.enum';
export class CreateCatalogueDto {
@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 { 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> {
//
// async getCataloguesForAdmin(queryDto: CompanyListQueryDto, businessId: string) {
// const { } = queryDto
// const where: FilterQuery<Catalogue> = {
// deletedAt: null,
// business: { id: businessId },
// };
async findAllPaginated( opts: FindCataloguesDto = {}): Promise<PaginatedResult<Catalogue>> {
const { page = 1, limit = 10, orderBy = 'order', order = 'asc', isActive } = opts;
// if (queryDto.status) {
// where.status = queryDto.status;
// }
// if (queryDto.isActive !== undefined) {
// where.isActive = queryDto.isActive === 1;
// }
// if (queryDto.q) {
// where.name = { $ilike: `%${queryDto.q}%` };
// }
const offset = (page - 1) * limit;
// if (queryDto.registrationDate) {
// where.createdAt = {
// $gte: queryDto.registrationDate,
// $lte: queryDto.registrationDate,
// };
// }
// if (queryDto.name) {
// where.name = { $ilike: `%${queryDto.name}%` };
// }
const where: FilterQuery<Catalogue> = {};
// return this.findAndCount(where, {
// populate: [],
// limit,
// offset: skip,
// orderBy: { createdAt: "DESC" },
// fields: [
// "id",
// "name",
// "isActive",
// "dateOfEstablishment",
// "status",
// "createdAt",
// ],
// });
// }
const [data, total] = await this.findAndCount(where, {
limit,
offset,
orderBy: { [orderBy]: order.toLowerCase() as 'asc' | 'desc' },
populate: [],
});
// async getCataloguesForPublic(queryDto: CompanyListPublicQueryDto, businessId: string) {
// const where: FilterQuery<Catalogue> = {
// deletedAt: null,
// business: { id: businessId },
// isActive: true,
// status: CatalogueStatus.APPROVED,
// };
const totalPages = Math.ceil(total / limit);
return {
data,
meta: {
total,
page,
limit,
totalPages,
},
};
// return this.find(where, {
// populate: [],
// orderBy: { createdAt: "DESC" },
// fields: ["id", "name", "description", "profileImageUrl", "coverImageUrl", "createdAt"],
// });
// }
}
}