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 { 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<FieldOption>(this)
|
||||
|
||||
@ManyToOne(() => Section)
|
||||
section: Section
|
||||
section!: Section
|
||||
|
||||
@Property()
|
||||
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 { 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<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) {
|
||||
|
||||
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<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() {
|
||||
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<Section> {
|
||||
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<Field> {
|
||||
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> {
|
||||
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);
|
||||
|
||||
}
|
||||
|
||||
|
||||
@@ -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)
|
||||
|
||||
Reference in New Issue
Block a user