field
This commit is contained in:
@@ -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;
|
||||||
|
}
|
||||||
@@ -2,10 +2,11 @@ import { Collection, Entity, Enum, ManyToOne, OneToMany, PrimaryKey, Property }
|
|||||||
import { FieldOption } from './field-option.entity';
|
import { FieldOption } from './field-option.entity';
|
||||||
import { FieldType } from '../interface/print';
|
import { FieldType } from '../interface/print';
|
||||||
import { Section } from './section.entity';
|
import { Section } from './section.entity';
|
||||||
|
import { BaseEntity } from 'src/common/entities/base.entity';
|
||||||
|
|
||||||
|
|
||||||
@Entity({ tableName: 'field' })
|
@Entity({ tableName: 'field' })
|
||||||
export class Field {
|
export class Field extends BaseEntity {
|
||||||
@PrimaryKey({ type: 'bigint', autoincrement: true })
|
@PrimaryKey({ type: 'bigint', autoincrement: true })
|
||||||
id: bigint
|
id: bigint
|
||||||
|
|
||||||
@@ -13,7 +14,7 @@ export class Field {
|
|||||||
options = new Collection<FieldOption>(this)
|
options = new Collection<FieldOption>(this)
|
||||||
|
|
||||||
@ManyToOne(() => Section)
|
@ManyToOne(() => Section)
|
||||||
section: Section
|
section!: Section
|
||||||
|
|
||||||
@Property()
|
@Property()
|
||||||
name: string;
|
name: string;
|
||||||
|
|||||||
@@ -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 { EntityManager } from '@mikro-orm/postgresql';
|
||||||
import { RequiredEntityData } from '@mikro-orm/core';
|
import { RequiredEntityData } from '@mikro-orm/core';
|
||||||
import { FieldRepository } from '../repository/field.repository';
|
import { FieldRepository } from '../repository/field.repository';
|
||||||
@@ -6,6 +6,8 @@ import { SectionRepository } from '../repository/section.repository';
|
|||||||
import { CreateSectionDto } from '../dto/create-section.dto';
|
import { CreateSectionDto } from '../dto/create-section.dto';
|
||||||
import { Section } from '../entities/section.entity';
|
import { Section } from '../entities/section.entity';
|
||||||
import { UpdateSectionDto } from '../dto/update-section.dto';
|
import { UpdateSectionDto } from '../dto/update-section.dto';
|
||||||
|
import { Field } from '../entities/field.entity';
|
||||||
|
import { CreateFieldDto } from '../dto/create-field.dto';
|
||||||
|
|
||||||
|
|
||||||
@Injectable()
|
@Injectable()
|
||||||
@@ -17,54 +19,62 @@ export class SectionService {
|
|||||||
private readonly em: EntityManager,
|
private readonly em: EntityManager,
|
||||||
) { }
|
) { }
|
||||||
|
|
||||||
async create(dto: CreateSectionDto) {
|
async create(sectionId: number, dto: CreateFieldDto) {
|
||||||
|
|
||||||
const data: RequiredEntityData<Section> = {
|
|
||||||
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 })
|
const section = await this.sectionRepository.findOne({ id: sectionId })
|
||||||
|
|
||||||
if (!section) {
|
if (!section) {
|
||||||
throw new NotFoundException('Section not found');
|
throw new BadRequestException("Section not found !")
|
||||||
}
|
}
|
||||||
|
|
||||||
this.sectionRepository.assign(section, dto)
|
const data: RequiredEntityData<Field> = {
|
||||||
|
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() {
|
async update(fieldId: number, dto: UpdateSectionDto) {
|
||||||
return this.sectionRepository.findAll();
|
|
||||||
|
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<Section> {
|
findAll(sectionId: number) {
|
||||||
const section = await this.sectionRepository.findOne({ id: attributeId },
|
return this.fieldRepository.find({ section: { id: sectionId } });
|
||||||
{ populate: ['fields'] });
|
}
|
||||||
|
|
||||||
if (!section) throw new NotFoundException('Section not found');
|
async findById(fieldId: number): Promise<Field> {
|
||||||
return section;
|
const field = await this.fieldRepository.findOne({ id: fieldId },
|
||||||
|
{ populate: [] });
|
||||||
|
|
||||||
|
if (!field) throw new NotFoundException('Field not found');
|
||||||
|
return field;
|
||||||
}
|
}
|
||||||
|
|
||||||
async remove(id: number): Promise<void> {
|
async remove(id: number): Promise<void> {
|
||||||
const section = await this.findById(id);
|
const field = await this.findById(id);
|
||||||
|
|
||||||
section.deletedAt = new Date();
|
// field.deletedAt = new Date();
|
||||||
return await this.em.persistAndFlush(section);
|
return await this.em.persistAndFlush(field);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -32,7 +32,9 @@ export class ProductService {
|
|||||||
// prepareTime: rest.prepareTime ?? 0,
|
// prepareTime: rest.prepareTime ?? 0,
|
||||||
quantities: rest.quantities,
|
quantities: rest.quantities,
|
||||||
images: rest.images ?? undefined,
|
images: rest.images ?? undefined,
|
||||||
|
linkUrl: rest.linkUrl,
|
||||||
category,
|
category,
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
const product = this.productRepository.create(data)
|
const product = this.productRepository.create(data)
|
||||||
|
|||||||
Reference in New Issue
Block a user