This commit is contained in:
@@ -10,7 +10,7 @@ import { UpdateFieldOptionDto } from '../dto/update-field-option.dto';
|
||||
|
||||
@Controller('admin')
|
||||
@ApiBearerAuth()
|
||||
export class FormBuilderController {
|
||||
export class FormBuilderController {
|
||||
constructor(
|
||||
private readonly fieldService: FieldService,
|
||||
private readonly fieldOptionService: FieldOptionService,
|
||||
@@ -19,36 +19,36 @@ import { UpdateFieldOptionDto } from '../dto/update-field-option.dto';
|
||||
|
||||
/*================================ Field ========================== */
|
||||
|
||||
@Post('section/:id/field')
|
||||
@ApiOperation({ summary: 'Create field for section' })
|
||||
@Post('entity/:id/field')
|
||||
@ApiOperation({ summary: 'Create field for entity' })
|
||||
@UseGuards(AdminAuthGuard)
|
||||
createField(@Body() dto: CreateFieldDto, @Param('id') sectionId: string) {
|
||||
return this.fieldService.create(sectionId,dto);
|
||||
createField(@Body() dto: CreateFieldDto, @Param('id') entityId: string) {
|
||||
return this.fieldService.create(+entityId, dto);
|
||||
}
|
||||
|
||||
@Get('section/:id/field')
|
||||
@Get('entity/:id/field')
|
||||
@ApiOperation({ summary: 'find all secion fields' })
|
||||
@UseGuards(AdminAuthGuard)
|
||||
findAllSectionFields(@Param('id') sectionId: string) {
|
||||
return this.fieldService.findAll(+sectionId);
|
||||
findAllSectionFields(@Param('id') entityId: string) {
|
||||
return this.fieldService.findAll(+entityId);
|
||||
}
|
||||
|
||||
@Get('section/field/:id')
|
||||
@Get('entity/field/:id')
|
||||
@ApiOperation({ summary: 'Find one field' })
|
||||
@UseGuards(AdminAuthGuard)
|
||||
findOneFiled(@Param('id') id: string) {
|
||||
return this.fieldService.findById(+id);
|
||||
}
|
||||
|
||||
@Patch('section/field/:id')
|
||||
@Patch('entity/field/:id')
|
||||
@ApiOperation({ summary: 'Update field' })
|
||||
@UseGuards(AdminAuthGuard)
|
||||
updateField(@Param('id') id: string, @Body() dto: UpdateFieldDto) {
|
||||
return this.fieldService.update(+id, dto);
|
||||
}
|
||||
|
||||
@Delete('section/field/:id')
|
||||
@ApiOperation({ summary: 'Renopve field' })
|
||||
@Delete('entity/field/:id')
|
||||
@ApiOperation({ summary: 'Remove field' })
|
||||
@UseGuards(AdminAuthGuard)
|
||||
removeField(@Param('id') id: string) {
|
||||
return this.fieldService.remove(+id);
|
||||
@@ -56,35 +56,35 @@ import { UpdateFieldOptionDto } from '../dto/update-field-option.dto';
|
||||
|
||||
/*================================ Field Option ========================== */
|
||||
|
||||
@Post('field/:id/option')
|
||||
@Post('entity/field/:id/option')
|
||||
@ApiOperation({ summary: 'Create field Options ' })
|
||||
@UseGuards(AdminAuthGuard)
|
||||
createFieldOption(@Body() dto: CreateFieldOptionDto, @Param('id') id: string) {
|
||||
return this.fieldOptionService.create(+id, dto);
|
||||
}
|
||||
|
||||
@Get('field/:id/option')
|
||||
@Get('entity/field/:id/option')
|
||||
@ApiOperation({ summary: 'find all field Option' })
|
||||
@UseGuards(AdminAuthGuard)
|
||||
findAllFiledOptions(@Param('id') fieldId: string) {
|
||||
return this.fieldOptionService.findAll(+fieldId);
|
||||
}
|
||||
|
||||
@Get('field/option/:id')
|
||||
@Get('entity/field/option/:id')
|
||||
@ApiOperation({ summary: 'Find one field Option' })
|
||||
@UseGuards(AdminAuthGuard)
|
||||
findOneFiledOption(@Param('id') id: string) {
|
||||
return this.fieldOptionService.findById(+id);
|
||||
}
|
||||
|
||||
@Patch('field/option/:id')
|
||||
@Patch('entity/field/option/:id')
|
||||
@ApiOperation({ summary: 'Update field Option' })
|
||||
@UseGuards(AdminAuthGuard)
|
||||
updateFieldOption(@Param('id') id: string, @Body() dto: UpdateFieldOptionDto) {
|
||||
return this.fieldOptionService.update(+id, dto);
|
||||
}
|
||||
|
||||
@Delete('field/option/:id')
|
||||
@Delete('entity/field/option/:id')
|
||||
@ApiOperation({ summary: 'Renopve field Option' })
|
||||
@UseGuards(AdminAuthGuard)
|
||||
removeFieldOption(@Param('id') id: string) {
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
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';
|
||||
import { EntityType, FieldType } from '../interface/print';
|
||||
|
||||
export class CreateFieldDto {
|
||||
@IsString()
|
||||
@@ -23,6 +23,12 @@ export class CreateFieldDto {
|
||||
@ApiProperty({ enum: FieldType, example: FieldType.select })
|
||||
type: FieldType;
|
||||
|
||||
@IsNotEmpty()
|
||||
@IsEnum(EntityType)
|
||||
@ApiProperty({ enum: EntityType, example: EntityType.product })
|
||||
entityType: EntityType
|
||||
|
||||
|
||||
@IsOptional()
|
||||
@IsInt()
|
||||
@Min(0)
|
||||
|
||||
@@ -1,9 +1,7 @@
|
||||
import { Collection, Entity, Enum, ManyToOne, OneToMany, PrimaryKey, Property } from '@mikro-orm/core';
|
||||
import { FieldOption } from './field-option.entity';
|
||||
import { FieldType } from '../interface/print';
|
||||
import { Section } from 'src/modules/print/entities/section.entity';
|
||||
import { EntityType, FieldType } from '../interface/print';
|
||||
import { BaseEntity } from 'src/common/entities/base.entity';
|
||||
import { Product } from 'src/modules/product/entities/product.entity';
|
||||
|
||||
|
||||
@Entity({ tableName: 'field' })
|
||||
@@ -14,11 +12,13 @@ export class Field extends BaseEntity {
|
||||
@OneToMany(() => FieldOption, (attrValue) => attrValue.field)
|
||||
options = new Collection<FieldOption>(this)
|
||||
|
||||
@ManyToOne(() => Section, { nullable: true })
|
||||
section?: Section
|
||||
// Discriminator column
|
||||
@Enum(() => EntityType) // Product , Print
|
||||
entityType: EntityType;
|
||||
|
||||
@ManyToOne(() => Product, { nullable: true })
|
||||
product?: Product
|
||||
// Foreign key based on entityType
|
||||
@Property({ type: 'bigint' })
|
||||
entityId: bigint;
|
||||
|
||||
@Property()
|
||||
name: string;
|
||||
|
||||
@@ -6,4 +6,9 @@ export enum FieldType {
|
||||
radio = 'radio',
|
||||
checkbox = 'checkbox',
|
||||
date = 'date',
|
||||
}
|
||||
|
||||
export enum EntityType {
|
||||
product = 'product',
|
||||
print = 'print',
|
||||
}
|
||||
@@ -2,35 +2,52 @@ import { BadRequestException, Injectable, NotFoundException } from '@nestjs/comm
|
||||
import { EntityManager } from '@mikro-orm/postgresql';
|
||||
import { RequiredEntityData } from '@mikro-orm/core';
|
||||
import { FieldRepository } from '../repository/field.repository';
|
||||
import { SectionRepository } from 'src/modules/print/repository/section.repository';
|
||||
import { PrintRepository } from 'src/modules/print/repository/print.repository';
|
||||
import { Field } from '../entities/field.entity';
|
||||
import { CreateFieldDto } from '../dto/create-field.dto';
|
||||
import { UpdateFieldDto } from '../dto/update-field.dto';
|
||||
import { ProductRepository } from 'src/modules/product/repositories/product.repository';
|
||||
import { EntityType } from '../interface/print';
|
||||
import { Product } from 'src/modules/product/entities/product.entity';
|
||||
import { Print } from 'src/modules/print/entities/print.entity';
|
||||
import { ProductService } from 'src/modules/product/providers/product.service';
|
||||
import { PrintService } from 'src/modules/print/provider/Print.service';
|
||||
|
||||
|
||||
@Injectable()
|
||||
export class FieldService {
|
||||
|
||||
constructor(
|
||||
private readonly sectionRepository: SectionRepository,
|
||||
private readonly sectionRepository: PrintRepository,
|
||||
private readonly fieldRepository: FieldRepository,
|
||||
private readonly productService: ProductService,
|
||||
private readonly printService: PrintService,
|
||||
private readonly em: EntityManager,
|
||||
) { }
|
||||
|
||||
async create(sectionId: string, dto: CreateFieldDto) {
|
||||
const section = await this.sectionRepository.findOne({ id: sectionId })
|
||||
async create(entityId: number, dto: CreateFieldDto) {
|
||||
const { entityType, isRequired, multiple, name, type, order } = dto
|
||||
|
||||
if (!section) {
|
||||
throw new BadRequestException("Section not found !")
|
||||
let entity: null | Product | Print = null
|
||||
|
||||
if (entityType == EntityType.product) {
|
||||
entity = await this.productService.findOneOrFail(entityId)
|
||||
} else if (entityType == EntityType.print) {
|
||||
entity = await this.printService.findOneOrFail(entityId)
|
||||
}
|
||||
|
||||
if(!entity){
|
||||
throw new BadRequestException("Entity not Found")
|
||||
}
|
||||
|
||||
const data: RequiredEntityData<Field> = {
|
||||
name: dto.name,
|
||||
order: dto.order,
|
||||
type: dto.type,
|
||||
section,
|
||||
isRequired: dto.isRequired,
|
||||
multiple: dto.multiple
|
||||
name,
|
||||
order,
|
||||
type,
|
||||
isRequired,
|
||||
multiple,
|
||||
entityId:entity.id,
|
||||
entityType
|
||||
};
|
||||
|
||||
const field = this.fieldRepository.create(data)
|
||||
@@ -57,9 +74,9 @@ export class FieldService {
|
||||
|
||||
}
|
||||
|
||||
findAll(sectionId: number) {
|
||||
findAll(entityId: number) {
|
||||
return this.fieldRepository.find({
|
||||
section: { id: sectionId }
|
||||
entityId
|
||||
},
|
||||
{ populate: ['options', 'options.value'] });
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
import { Controller, Get, Post, Body, Patch, Param, Delete, UseGuards } from '@nestjs/common';
|
||||
import { SectionService } from '../provider/section.service';
|
||||
import { PrintService } from '../provider/Print.service';
|
||||
import { CreateSectionDto } from '../dto/create-section.dto';
|
||||
import { ApiBearerAuth, ApiOperation } from '@nestjs/swagger';
|
||||
import { AdminAuthGuard } from '../../auth/guards/adminAuth.guard';
|
||||
@@ -9,7 +9,7 @@ import { UpdateSectionDto } from '../dto/update-section.dto';
|
||||
@ApiBearerAuth()
|
||||
export class PrintController {
|
||||
constructor(
|
||||
private readonly sectionService: SectionService,
|
||||
private readonly sectionService: PrintService,
|
||||
) { }
|
||||
|
||||
/*================================ Section ========================== */
|
||||
@@ -32,7 +32,7 @@ export class PrintController {
|
||||
@ApiOperation({ summary: 'Find one section' })
|
||||
@UseGuards(AdminAuthGuard)
|
||||
findOne(@Param('id') id: string) {
|
||||
return this.sectionService.findById(+id);
|
||||
return this.sectionService.findOneOrFail(+id);
|
||||
}
|
||||
|
||||
@Patch('section/:id')
|
||||
|
||||
+3
-3
@@ -3,12 +3,12 @@ import { Field } from 'src/modules/form-builder/entities/field.entity';
|
||||
import { BaseEntity } from 'src/common/entities/base.entity';
|
||||
|
||||
|
||||
@Entity({ tableName: 'section' })
|
||||
export class Section extends BaseEntity {
|
||||
@Entity({ tableName: 'print' })
|
||||
export class Print extends BaseEntity {
|
||||
@PrimaryKey({ type: 'bigint', autoincrement: true })
|
||||
id: bigint
|
||||
|
||||
@OneToMany(() => Field, (attrValue) => attrValue.section, { cascade: [Cascade.PERSIST, Cascade.REMOVE] })
|
||||
@OneToMany(() => Field, (attrValue) => attrValue.entityId, { cascade: [Cascade.PERSIST, Cascade.REMOVE] })
|
||||
fields = new Collection<Field>(this)
|
||||
|
||||
@Property()
|
||||
@@ -1,21 +1,21 @@
|
||||
import { Module } from '@nestjs/common';
|
||||
import { SectionService } from './provider/section.service';
|
||||
import { PrintService } from './provider/Print.service';
|
||||
import { PrintController } from './controller/print.controller';
|
||||
import { MikroOrmModule } from '@mikro-orm/nestjs';
|
||||
import { Section } from './entities/section.entity';
|
||||
import { Print } from './entities/print.entity';
|
||||
|
||||
import { SectionRepository } from './repository/section.repository';
|
||||
import { PrintRepository } from './repository/print.repository';
|
||||
import { JwtModule } from '@nestjs/jwt';
|
||||
import { RolesModule } from '../roles/roles.module';
|
||||
|
||||
|
||||
@Module({
|
||||
controllers: [PrintController],
|
||||
providers: [SectionService,
|
||||
SectionRepository],
|
||||
exports: [SectionService,
|
||||
SectionRepository],
|
||||
imports: [MikroOrmModule.forFeature([Section]),
|
||||
providers: [PrintService,
|
||||
PrintRepository],
|
||||
exports: [PrintService,
|
||||
PrintRepository],
|
||||
imports: [MikroOrmModule.forFeature([Print]),
|
||||
RolesModule, JwtModule.register({})]
|
||||
})
|
||||
export class PrintModule { }
|
||||
|
||||
+9
-9
@@ -1,23 +1,23 @@
|
||||
import { Injectable, NotFoundException } from '@nestjs/common';
|
||||
import { EntityManager } from '@mikro-orm/postgresql';
|
||||
import { RequiredEntityData } from '@mikro-orm/core';
|
||||
import { SectionRepository } from '../repository/section.repository';
|
||||
import { PrintRepository } from '../repository/print.repository';
|
||||
import { CreateSectionDto } from '../dto/create-section.dto';
|
||||
import { Section } from '../entities/section.entity';
|
||||
import { Print } from '../entities/print.entity';
|
||||
import { UpdateSectionDto } from '../dto/update-section.dto';
|
||||
|
||||
|
||||
@Injectable()
|
||||
export class SectionService {
|
||||
export class PrintService {
|
||||
|
||||
constructor(
|
||||
private readonly sectionRepository: SectionRepository,
|
||||
private readonly sectionRepository: PrintRepository,
|
||||
private readonly em: EntityManager,
|
||||
) { }
|
||||
|
||||
async create(dto: CreateSectionDto) {
|
||||
|
||||
const data: RequiredEntityData<Section> = {
|
||||
const data: RequiredEntityData<Print> = {
|
||||
title: dto.title,
|
||||
order: dto.order,
|
||||
};
|
||||
@@ -35,7 +35,7 @@ export class SectionService {
|
||||
const section = await this.sectionRepository.findOne({ id: sectionId })
|
||||
|
||||
if (!section) {
|
||||
throw new NotFoundException('Section not found');
|
||||
throw new NotFoundException('Print not found');
|
||||
}
|
||||
|
||||
this.sectionRepository.assign(section, dto)
|
||||
@@ -50,16 +50,16 @@ export class SectionService {
|
||||
return this.sectionRepository.findAll({ populate: ['fields', 'fields.options', 'fields.options.value'] });
|
||||
}
|
||||
|
||||
async findById(attributeId: number): Promise<Section> {
|
||||
async findOneOrFail(attributeId: number): Promise<Print> {
|
||||
const section = await this.sectionRepository.findOne({ id: attributeId },
|
||||
{ populate: ['fields', 'fields.options', 'fields.options.value'] });
|
||||
|
||||
if (!section) throw new NotFoundException('Section not found');
|
||||
if (!section) throw new NotFoundException('Print not found');
|
||||
return section;
|
||||
}
|
||||
|
||||
async remove(id: number): Promise<void> {
|
||||
const section = await this.findById(id);
|
||||
const section = await this.findOneOrFail(id);
|
||||
|
||||
section.deletedAt = new Date();
|
||||
return await this.em.persistAndFlush(section);
|
||||
+3
-3
@@ -1,11 +1,11 @@
|
||||
import { Injectable } from '@nestjs/common';
|
||||
import { EntityManager, EntityRepository } from '@mikro-orm/postgresql';
|
||||
import { Section } from '../entities/section.entity';
|
||||
import { Print } from '../entities/print.entity';
|
||||
|
||||
|
||||
@Injectable()
|
||||
export class SectionRepository extends EntityRepository<Section> {
|
||||
export class PrintRepository extends EntityRepository<Print> {
|
||||
constructor(readonly em: EntityManager) {
|
||||
super(em, Section);
|
||||
super(em, Print);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user