89 lines
2.4 KiB
TypeScript
89 lines
2.4 KiB
TypeScript
import { Injectable, NotFoundException } from '@nestjs/common';
|
|
import { InjectRepository } from '@nestjs/typeorm';
|
|
import { Repository } from 'typeorm';
|
|
import { FormResult } from './entity/formResult.entity';
|
|
import { CreateFormResultDto } from './dto/create-formResult.dto';
|
|
import { UpdateFormResultDto } from './dto/update-formResult.dto';
|
|
|
|
@Injectable()
|
|
export class FormResultService {
|
|
constructor(
|
|
@InjectRepository(FormResult)
|
|
private readonly formResultRepo: Repository<FormResult>,
|
|
) {}
|
|
|
|
async create(data: CreateFormResultDto): Promise<FormResult> {
|
|
const formResult = this.formResultRepo.create({
|
|
...data,
|
|
});
|
|
return await this.formResultRepo.save(formResult);
|
|
}
|
|
|
|
async findAll(
|
|
page = 1,
|
|
limit = 10,
|
|
): Promise<{
|
|
docs: FormResult[];
|
|
totalDocs: number;
|
|
limit: number;
|
|
page: number;
|
|
totalPages: number;
|
|
hasNextPage: boolean;
|
|
hasPrevPage: boolean;
|
|
nextPage: number | null;
|
|
prevPage: number | null;
|
|
}> {
|
|
const [docs, totalDocs] = await this.formResultRepo.findAndCount({
|
|
skip: (page - 1) * limit,
|
|
take: limit,
|
|
// No specific relations needed here unless FormResult links to other entities
|
|
});
|
|
|
|
const totalPages = Math.ceil(totalDocs / limit);
|
|
return {
|
|
docs,
|
|
totalDocs,
|
|
limit,
|
|
page,
|
|
totalPages,
|
|
hasNextPage: page < totalPages,
|
|
hasPrevPage: page > 1,
|
|
nextPage: page < totalPages ? page + 1 : null,
|
|
prevPage: page > 1 ? page - 1 : null,
|
|
};
|
|
}
|
|
|
|
async findById(id: string): Promise<FormResult | null> {
|
|
const formResult = await this.formResultRepo.findOne({
|
|
where: { id },
|
|
// No specific relations needed here unless FormResult links to other entities
|
|
});
|
|
if (!formResult) {
|
|
throw new NotFoundException(`FormResult with ID "${id}" not found`);
|
|
}
|
|
return formResult;
|
|
}
|
|
|
|
async update(
|
|
id: string,
|
|
data: UpdateFormResultDto,
|
|
): Promise<FormResult | null> {
|
|
const result = await this.formResultRepo.update(
|
|
{ id },
|
|
{ ...data, updatedAt: new Date() },
|
|
);
|
|
|
|
if (result.affected === 0) {
|
|
throw new NotFoundException(`FormResult with ID "${id}" not found`);
|
|
}
|
|
return await this.findById(id);
|
|
}
|
|
|
|
async remove(id: string): Promise<void> {
|
|
const result = await this.formResultRepo.delete({ id });
|
|
if (result.affected === 0) {
|
|
throw new NotFoundException(`FormResult with ID "${id}" not found`);
|
|
}
|
|
}
|
|
}
|