form builder module
This commit is contained in:
@@ -0,0 +1,83 @@
|
||||
import { BadRequestException, 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 'src/modules/print/repository/section.repository';
|
||||
import { Field } from '../entities/field.entity';
|
||||
import { CreateFieldDto } from '../dto/create-field.dto';
|
||||
import { UpdateFieldDto } from '../dto/update-field.dto';
|
||||
|
||||
|
||||
@Injectable()
|
||||
export class FieldService {
|
||||
|
||||
constructor(
|
||||
private readonly sectionRepository: SectionRepository,
|
||||
private readonly fieldRepository: FieldRepository,
|
||||
private readonly em: EntityManager,
|
||||
) { }
|
||||
|
||||
async create(sectionId: string, dto: CreateFieldDto) {
|
||||
const section = await this.sectionRepository.findOne({ id: sectionId })
|
||||
|
||||
if (!section) {
|
||||
throw new BadRequestException("Section not found !")
|
||||
}
|
||||
|
||||
const data: RequiredEntityData<Field> = {
|
||||
name: dto.name,
|
||||
order: dto.order,
|
||||
type: dto.type,
|
||||
section,
|
||||
isRequired: dto.isRequired,
|
||||
multiple: dto.multiple
|
||||
};
|
||||
|
||||
const field = this.fieldRepository.create(data)
|
||||
|
||||
await this.em.persistAndFlush(field)
|
||||
|
||||
return field
|
||||
|
||||
}
|
||||
|
||||
async update(fieldId: number, dto: UpdateFieldDto) {
|
||||
|
||||
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
|
||||
|
||||
}
|
||||
|
||||
findAll(sectionId: number) {
|
||||
return this.fieldRepository.find({
|
||||
section: { id: sectionId }
|
||||
},
|
||||
{ populate: ['options', 'options.value'] });
|
||||
}
|
||||
|
||||
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 field = await this.findById(id);
|
||||
|
||||
field.deletedAt = new Date();
|
||||
return await this.em.persistAndFlush(field);
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user