From a2512fc013e7486d2897c989ef6448bc69f20d2e Mon Sep 17 00:00:00 2001 From: morteza-mortezai Date: Sat, 24 Jan 2026 16:43:44 +0330 Subject: [PATCH] field --- src/modules/print/dto/create-field.dto.ts | 32 ++++++++ src/modules/print/entities/field.entity.ts | 5 +- src/modules/print/provider/field.service.ts | 74 +++++++++++-------- .../product/providers/product.service.ts | 2 + 4 files changed, 79 insertions(+), 34 deletions(-) create mode 100644 src/modules/print/dto/create-field.dto.ts diff --git a/src/modules/print/dto/create-field.dto.ts b/src/modules/print/dto/create-field.dto.ts new file mode 100644 index 0000000..bfa5afa --- /dev/null +++ b/src/modules/print/dto/create-field.dto.ts @@ -0,0 +1,32 @@ +import { IsString, IsOptional, IsBoolean, IsInt, Min, IsEnum, IsNotEmpty } from 'class-validator'; +import { ApiPropertyOptional, ApiProperty } from '@nestjs/swagger'; +import { Type } from 'class-transformer'; +import { FieldType } from '../interface/print'; + +export class CreateFieldDto { + @IsString() + @ApiProperty() + name: string; + + @IsString() + @ApiProperty() + sectionId: string; + + + @IsBoolean() + @Type(() => Boolean) + @ApiPropertyOptional({ example: true }) + isRequired: boolean; + + @IsNotEmpty() + @IsEnum(FieldType) + @ApiProperty({ enum: FieldType, example: FieldType.select }) + type: FieldType; + + @IsOptional() + @IsInt() + @Min(0) + @Type(() => Number) + @ApiPropertyOptional({ example: 1 }) + order?: number; +} diff --git a/src/modules/print/entities/field.entity.ts b/src/modules/print/entities/field.entity.ts index dbe589d..01d3e12 100644 --- a/src/modules/print/entities/field.entity.ts +++ b/src/modules/print/entities/field.entity.ts @@ -2,10 +2,11 @@ import { Collection, Entity, Enum, ManyToOne, OneToMany, PrimaryKey, Property } import { FieldOption } from './field-option.entity'; import { FieldType } from '../interface/print'; import { Section } from './section.entity'; +import { BaseEntity } from 'src/common/entities/base.entity'; @Entity({ tableName: 'field' }) -export class Field { +export class Field extends BaseEntity { @PrimaryKey({ type: 'bigint', autoincrement: true }) id: bigint @@ -13,7 +14,7 @@ export class Field { options = new Collection(this) @ManyToOne(() => Section) - section: Section + section!: Section @Property() name: string; diff --git a/src/modules/print/provider/field.service.ts b/src/modules/print/provider/field.service.ts index d92c1e4..70ce28b 100644 --- a/src/modules/print/provider/field.service.ts +++ b/src/modules/print/provider/field.service.ts @@ -1,4 +1,4 @@ -import { BadGatewayException, Injectable, NotFoundException } from '@nestjs/common'; +import { BadGatewayException, BadRequestException, Injectable, NotFoundException } from '@nestjs/common'; import { EntityManager } from '@mikro-orm/postgresql'; import { RequiredEntityData } from '@mikro-orm/core'; import { FieldRepository } from '../repository/field.repository'; @@ -6,6 +6,8 @@ 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'; +import { Field } from '../entities/field.entity'; +import { CreateFieldDto } from '../dto/create-field.dto'; @Injectable() @@ -17,54 +19,62 @@ export class SectionService { 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) { - + async create(sectionId: number, dto: CreateFieldDto) { const section = await this.sectionRepository.findOne({ id: sectionId }) if (!section) { - throw new NotFoundException('Section not found'); + throw new BadRequestException("Section not found !") } - this.sectionRepository.assign(section, dto) + const data: RequiredEntityData = { + name: dto.name, + order: dto.order, + type: dto.type, + section, + isRequired: dto.isRequired + }; - this.em.persistAndFlush(section) + const field = this.fieldRepository.create(data) - return section + await this.em.persistAndFlush(field) + + return field } - findAll() { - return this.sectionRepository.findAll(); + async update(fieldId: number, dto: UpdateSectionDto) { + + const field = await this.fieldRepository.findOne({ id: fieldId }) + + if (!field) { + throw new NotFoundException('Field not found'); + } + + this.sectionRepository.assign(field, dto) + + this.em.persistAndFlush(field) + + return field + } - async findById(attributeId: number): Promise
{ - const section = await this.sectionRepository.findOne({ id: attributeId }, - { populate: ['fields'] }); + findAll(sectionId: number) { + return this.fieldRepository.find({ section: { id: sectionId } }); + } - if (!section) throw new NotFoundException('Section not found'); - return section; + async findById(fieldId: number): Promise { + const field = await this.fieldRepository.findOne({ id: fieldId }, + { populate: [] }); + + if (!field) throw new NotFoundException('Field not found'); + return field; } async remove(id: number): Promise { - const section = await this.findById(id); + const field = await this.findById(id); - section.deletedAt = new Date(); - return await this.em.persistAndFlush(section); + // field.deletedAt = new Date(); + return await this.em.persistAndFlush(field); } diff --git a/src/modules/product/providers/product.service.ts b/src/modules/product/providers/product.service.ts index 9f22ee5..5ddad25 100644 --- a/src/modules/product/providers/product.service.ts +++ b/src/modules/product/providers/product.service.ts @@ -32,7 +32,9 @@ export class ProductService { // prepareTime: rest.prepareTime ?? 0, quantities: rest.quantities, images: rest.images ?? undefined, + linkUrl: rest.linkUrl, category, + }; const product = this.productRepository.create(data)