From 05edc9abb4c1f6535c137cb349edb029df6fde39 Mon Sep 17 00:00:00 2001 From: morteza-mortezai Date: Sat, 24 Jan 2026 16:13:37 +0330 Subject: [PATCH] section --- src/modules/order/entities/order.entity.ts | 3 + src/modules/print/dto/create-print.dto.ts | 1 - src/modules/print/dto/create-section.dto.ts | 16 +++++ src/modules/print/dto/update-print.dto.ts | 4 +- src/modules/print/dto/update-section.dto.ts | 16 +++++ src/modules/print/entities/section.entity.ts | 5 +- src/modules/print/print.controller.ts | 36 +++++----- src/modules/print/print.module.ts | 8 ++- src/modules/print/print.service.ts | 26 ------- src/modules/print/provider/field.service.ts | 71 +++++++++++++++++++ src/modules/print/provider/section.service.ts | 71 +++++++++++++++++++ .../repository/field-option.repository.ts | 11 +++ .../print/repository/field.repository.ts | 11 +++ .../print/repository/section.repository.ts | 11 +++ 14 files changed, 240 insertions(+), 50 deletions(-) delete mode 100644 src/modules/print/dto/create-print.dto.ts create mode 100644 src/modules/print/dto/create-section.dto.ts create mode 100644 src/modules/print/dto/update-section.dto.ts delete mode 100644 src/modules/print/print.service.ts create mode 100644 src/modules/print/provider/field.service.ts create mode 100644 src/modules/print/provider/section.service.ts create mode 100644 src/modules/print/repository/field-option.repository.ts create mode 100644 src/modules/print/repository/field.repository.ts create mode 100644 src/modules/print/repository/section.repository.ts diff --git a/src/modules/order/entities/order.entity.ts b/src/modules/order/entities/order.entity.ts index 7cfe53c..ff6dbf0 100644 --- a/src/modules/order/entities/order.entity.ts +++ b/src/modules/order/entities/order.entity.ts @@ -100,6 +100,9 @@ export class Order extends BaseEntity { @Property({ type: 'json', nullable: true }) attachments?: string[] + @Property({ type: 'json', nullable: true }) + printIds?: string[] + @Property({ type: 'string', nullable: true }) paymentMethod?: string; diff --git a/src/modules/print/dto/create-print.dto.ts b/src/modules/print/dto/create-print.dto.ts deleted file mode 100644 index c6a88f7..0000000 --- a/src/modules/print/dto/create-print.dto.ts +++ /dev/null @@ -1 +0,0 @@ -export class CreatePrintDto {} diff --git a/src/modules/print/dto/create-section.dto.ts b/src/modules/print/dto/create-section.dto.ts new file mode 100644 index 0000000..05d63c3 --- /dev/null +++ b/src/modules/print/dto/create-section.dto.ts @@ -0,0 +1,16 @@ +import { IsString, IsOptional, IsBoolean, IsInt, Min, IsEnum, IsNotEmpty } from 'class-validator'; +import { ApiPropertyOptional, ApiProperty } from '@nestjs/swagger'; +import { Type } from 'class-transformer'; + +export class CreateSectionDto { + @IsString() + @ApiProperty({ example: ' ' }) + title: string; + + @IsOptional() + @IsInt() + @Min(0) + @Type(() => Number) + @ApiPropertyOptional({ example: 1 }) + order?: number; +} diff --git a/src/modules/print/dto/update-print.dto.ts b/src/modules/print/dto/update-print.dto.ts index f5bfabd..2376d0b 100644 --- a/src/modules/print/dto/update-print.dto.ts +++ b/src/modules/print/dto/update-print.dto.ts @@ -1,4 +1,4 @@ import { PartialType } from '@nestjs/swagger'; -import { CreatePrintDto } from './create-print.dto'; +import { CreateSectionDto } from './create-section.dto'; -export class UpdatePrintDto extends PartialType(CreatePrintDto) {} +export class UpdatePrintDto extends PartialType(CreateSectionDto) { } diff --git a/src/modules/print/dto/update-section.dto.ts b/src/modules/print/dto/update-section.dto.ts new file mode 100644 index 0000000..08d16a6 --- /dev/null +++ b/src/modules/print/dto/update-section.dto.ts @@ -0,0 +1,16 @@ +import { IsString, IsOptional, IsBoolean, IsInt, Min, IsEnum, IsNotEmpty } from 'class-validator'; +import { ApiPropertyOptional, ApiProperty } from '@nestjs/swagger'; +import { Type } from 'class-transformer'; + +export class UpdateSectionDto { + @IsString() + @ApiProperty({ example: ' ' }) + title: string; + + @IsOptional() + @IsInt() + @Min(0) + @Type(() => Number) + @ApiPropertyOptional({ example: 1 }) + order?: number; +} diff --git a/src/modules/print/entities/section.entity.ts b/src/modules/print/entities/section.entity.ts index a97b516..5784866 100644 --- a/src/modules/print/entities/section.entity.ts +++ b/src/modules/print/entities/section.entity.ts @@ -1,14 +1,15 @@ import { Cascade, Collection, Entity, OneToMany, PrimaryKey, Property } from '@mikro-orm/core'; import { Field } from './field.entity'; +import { BaseEntity } from 'src/common/entities/base.entity'; @Entity({ tableName: 'section' }) -export class Section { +export class Section extends BaseEntity { @PrimaryKey({ type: 'bigint', autoincrement: true }) id: bigint @OneToMany(() => Field, (attrValue) => attrValue.section, { cascade: [Cascade.PERSIST, Cascade.REMOVE] }) - Fields = new Collection(this) + fields = new Collection(this) @Property() title: string; diff --git a/src/modules/print/print.controller.ts b/src/modules/print/print.controller.ts index 8df3a73..63c7279 100644 --- a/src/modules/print/print.controller.ts +++ b/src/modules/print/print.controller.ts @@ -1,34 +1,36 @@ import { Controller, Get, Post, Body, Patch, Param, Delete } from '@nestjs/common'; -import { PrintService } from './print.service'; -import { CreatePrintDto } from './dto/create-print.dto'; -import { UpdatePrintDto } from './dto/update-print.dto'; +import { SectionService } from './provider/section.service'; +import { CreateSectionDto } from './dto/create-section.dto'; +import { UpdateSectionDto } from './dto/update-section.dto'; -@Controller('print') +@Controller('admin/print') export class PrintController { - constructor(private readonly printService: PrintService) {} + constructor( + private readonly sectionService: SectionService + ) { } - @Post() - create(@Body() createPrintDto: CreatePrintDto) { - return this.printService.create(createPrintDto); + @Post('section') + create(@Body() dto: CreateSectionDto) { + return this.sectionService.create(dto); } - @Get() + @Get('section') findAll() { - return this.printService.findAll(); + return this.sectionService.findAll(); } - @Get(':id') + @Get('section/:id') findOne(@Param('id') id: string) { - return this.printService.findOne(+id); + return this.sectionService.findById(+id); } - @Patch(':id') - update(@Param('id') id: string, @Body() updatePrintDto: UpdatePrintDto) { - return this.printService.update(+id, updatePrintDto); + @Patch('section/:id') + update(@Param('id') id: string, @Body() dto: UpdateSectionDto) { + return this.sectionService.update(+id, dto); } - @Delete(':id') + @Delete('section/:id') remove(@Param('id') id: string) { - return this.printService.remove(+id); + return this.sectionService.remove(+id); } } diff --git a/src/modules/print/print.module.ts b/src/modules/print/print.module.ts index 0e14a13..8eaaef8 100644 --- a/src/modules/print/print.module.ts +++ b/src/modules/print/print.module.ts @@ -1,14 +1,18 @@ import { Module } from '@nestjs/common'; -import { PrintService } from './print.service'; +import { SectionService } from './provider/section.service'; import { PrintController } from './print.controller'; import { MikroOrmModule } from '@mikro-orm/nestjs'; import { Field } from './entities/field.entity'; import { Section } from './entities/section.entity'; import { FieldOption } from './entities/field-option.entity'; +import { FieldRepository } from './repository/field.repository'; +import { FieldOptionRepository } from './repository/field-option.repository'; +import { SectionRepository } from './repository/section.repository'; @Module({ controllers: [PrintController], - providers: [PrintService], + providers: [SectionService, FieldRepository, FieldOptionRepository, SectionRepository], + exports: [SectionService, FieldRepository, FieldOptionRepository, SectionRepository], imports: [MikroOrmModule.forFeature([Field, Section, FieldOption])] }) export class PrintModule { } diff --git a/src/modules/print/print.service.ts b/src/modules/print/print.service.ts deleted file mode 100644 index 6df60e6..0000000 --- a/src/modules/print/print.service.ts +++ /dev/null @@ -1,26 +0,0 @@ -import { Injectable } from '@nestjs/common'; -import { CreatePrintDto } from './dto/create-print.dto'; -import { UpdatePrintDto } from './dto/update-print.dto'; - -@Injectable() -export class PrintService { - create(createPrintDto: CreatePrintDto) { - return 'This action adds a new print'; - } - - findAll() { - return `This action returns all print`; - } - - findOne(id: number) { - return `This action returns a #${id} print`; - } - - update(id: number, updatePrintDto: UpdatePrintDto) { - return `This action updates a #${id} print`; - } - - remove(id: number) { - return `This action removes a #${id} print`; - } -} diff --git a/src/modules/print/provider/field.service.ts b/src/modules/print/provider/field.service.ts new file mode 100644 index 0000000..d92c1e4 --- /dev/null +++ b/src/modules/print/provider/field.service.ts @@ -0,0 +1,71 @@ +import { BadGatewayException, Injectable, NotFoundException } from '@nestjs/common'; +import { EntityManager } from '@mikro-orm/postgresql'; +import { RequiredEntityData } from '@mikro-orm/core'; +import { FieldRepository } from '../repository/field.repository'; +import { SectionRepository } from '../repository/section.repository'; +import { CreateSectionDto } from '../dto/create-section.dto'; +import { Section } from '../entities/section.entity'; +import { UpdateSectionDto } from '../dto/update-section.dto'; + + +@Injectable() +export class SectionService { + + constructor( + private readonly sectionRepository: SectionRepository, + private readonly fieldRepository: FieldRepository, + private readonly em: EntityManager, + ) { } + + async create(dto: CreateSectionDto) { + + const data: RequiredEntityData
= { + title: dto.title, + order: dto.order, + }; + + const section = this.sectionRepository.create(data) + + await this.em.persistAndFlush(section) + + return section + + } + + async update(sectionId: number, dto: UpdateSectionDto) { + + const section = await this.sectionRepository.findOne({ id: sectionId }) + + if (!section) { + throw new NotFoundException('Section not found'); + } + + this.sectionRepository.assign(section, dto) + + this.em.persistAndFlush(section) + + return section + + } + + findAll() { + return this.sectionRepository.findAll(); + } + + async findById(attributeId: number): Promise
{ + const section = await this.sectionRepository.findOne({ id: attributeId }, + { populate: ['fields'] }); + + if (!section) throw new NotFoundException('Section not found'); + return section; + } + + async remove(id: number): Promise { + const section = await this.findById(id); + + section.deletedAt = new Date(); + return await this.em.persistAndFlush(section); + + } + +} diff --git a/src/modules/print/provider/section.service.ts b/src/modules/print/provider/section.service.ts new file mode 100644 index 0000000..d92c1e4 --- /dev/null +++ b/src/modules/print/provider/section.service.ts @@ -0,0 +1,71 @@ +import { BadGatewayException, Injectable, NotFoundException } from '@nestjs/common'; +import { EntityManager } from '@mikro-orm/postgresql'; +import { RequiredEntityData } from '@mikro-orm/core'; +import { FieldRepository } from '../repository/field.repository'; +import { SectionRepository } from '../repository/section.repository'; +import { CreateSectionDto } from '../dto/create-section.dto'; +import { Section } from '../entities/section.entity'; +import { UpdateSectionDto } from '../dto/update-section.dto'; + + +@Injectable() +export class SectionService { + + constructor( + private readonly sectionRepository: SectionRepository, + private readonly fieldRepository: FieldRepository, + private readonly em: EntityManager, + ) { } + + async create(dto: CreateSectionDto) { + + const data: RequiredEntityData
= { + title: dto.title, + order: dto.order, + }; + + const section = this.sectionRepository.create(data) + + await this.em.persistAndFlush(section) + + return section + + } + + async update(sectionId: number, dto: UpdateSectionDto) { + + const section = await this.sectionRepository.findOne({ id: sectionId }) + + if (!section) { + throw new NotFoundException('Section not found'); + } + + this.sectionRepository.assign(section, dto) + + this.em.persistAndFlush(section) + + return section + + } + + findAll() { + return this.sectionRepository.findAll(); + } + + async findById(attributeId: number): Promise
{ + const section = await this.sectionRepository.findOne({ id: attributeId }, + { populate: ['fields'] }); + + if (!section) throw new NotFoundException('Section not found'); + return section; + } + + async remove(id: number): Promise { + const section = await this.findById(id); + + section.deletedAt = new Date(); + return await this.em.persistAndFlush(section); + + } + +} diff --git a/src/modules/print/repository/field-option.repository.ts b/src/modules/print/repository/field-option.repository.ts new file mode 100644 index 0000000..4407f91 --- /dev/null +++ b/src/modules/print/repository/field-option.repository.ts @@ -0,0 +1,11 @@ +import { Injectable } from '@nestjs/common'; +import { EntityManager, EntityRepository } from '@mikro-orm/postgresql'; +import { FieldOption } from '../entities/field-option.entity'; + + +@Injectable() +export class FieldOptionRepository extends EntityRepository { + constructor(readonly em: EntityManager) { + super(em, FieldOption); + } +} diff --git a/src/modules/print/repository/field.repository.ts b/src/modules/print/repository/field.repository.ts new file mode 100644 index 0000000..19badd1 --- /dev/null +++ b/src/modules/print/repository/field.repository.ts @@ -0,0 +1,11 @@ +import { Injectable } from '@nestjs/common'; +import { EntityManager, EntityRepository } from '@mikro-orm/postgresql'; +import { Field } from '../entities/field.entity'; + + +@Injectable() +export class FieldRepository extends EntityRepository { + constructor(readonly em: EntityManager) { + super(em, Field); + } +} diff --git a/src/modules/print/repository/section.repository.ts b/src/modules/print/repository/section.repository.ts new file mode 100644 index 0000000..7325991 --- /dev/null +++ b/src/modules/print/repository/section.repository.ts @@ -0,0 +1,11 @@ +import { Injectable } from '@nestjs/common'; +import { EntityManager, EntityRepository } from '@mikro-orm/postgresql'; +import { Section } from '../entities/section.entity'; + + +@Injectable() +export class SectionRepository extends EntityRepository
{ + constructor(readonly em: EntityManager) { + super(em, Section); + } +}