feat: add pagination, sorting and filtering
This commit is contained in:
@@ -0,0 +1,104 @@
|
||||
import {
|
||||
BadRequestException,
|
||||
HttpStatus,
|
||||
Injectable,
|
||||
NotFoundException,
|
||||
} from '@nestjs/common';
|
||||
import { CreateTestDTO } from '../dtos/create-test.dto';
|
||||
import { InjectRepository } from '@nestjs/typeorm';
|
||||
import { Test } from '../entities/test.entity';
|
||||
import { Repository } from 'typeorm';
|
||||
import { I18nService } from 'nestjs-i18n';
|
||||
import { AppException } from '@/common/exceptions/app.exception';
|
||||
import { AppResponse } from '@/common/responses';
|
||||
import { AppTimeoutException } from '@/common/exceptions/app-timeout.exception';
|
||||
import { UpdateTestDTO } from '../dtos/update-test.dto';
|
||||
import { AccessType } from '../enums/access-types.enum';
|
||||
|
||||
@Injectable()
|
||||
export class TestsCommandProvider {
|
||||
constructor(
|
||||
/**
|
||||
* Inject Tests Repository
|
||||
*/
|
||||
@InjectRepository(Test)
|
||||
private readonly testRepository: Repository<Test>,
|
||||
|
||||
/**
|
||||
* Inject I18n Service
|
||||
*/
|
||||
private readonly i18nService: I18nService,
|
||||
) {}
|
||||
|
||||
public async createOne(createTestDto: CreateTestDTO) {
|
||||
const titleExists = await this.testRepository.findOne({
|
||||
where: {
|
||||
title: createTestDto.title,
|
||||
},
|
||||
});
|
||||
|
||||
if (titleExists) {
|
||||
throw new AppException('validation.duplicateField', HttpStatus.CONFLICT, {
|
||||
field: '$t(tests.fields.title)',
|
||||
});
|
||||
}
|
||||
|
||||
const newTest = this.testRepository.create(createTestDto);
|
||||
|
||||
try {
|
||||
await this.testRepository.save(newTest);
|
||||
return new AppResponse(
|
||||
newTest,
|
||||
this.i18nService.translate('tests.messages.createSuccess'),
|
||||
);
|
||||
} catch (err) {
|
||||
throw new AppTimeoutException(err);
|
||||
}
|
||||
}
|
||||
|
||||
public async updateOne(id: number, updateTestDto: UpdateTestDTO) {
|
||||
const test = await this.testRepository.findOneBy({ id });
|
||||
|
||||
if (!test) {
|
||||
throw new NotFoundException('tests.errors.testNotFound');
|
||||
}
|
||||
|
||||
const accessType = updateTestDto.accessType ?? test.accessType;
|
||||
const price = updateTestDto.price ?? test.price;
|
||||
|
||||
if (
|
||||
(accessType === AccessType.PAID && !price) ||
|
||||
(accessType !== AccessType.PAID && price)
|
||||
) {
|
||||
throw new BadRequestException(
|
||||
this.i18nService.translate('tests.errors.priceAccessConstraint'),
|
||||
);
|
||||
}
|
||||
|
||||
Object.assign(test, updateTestDto);
|
||||
|
||||
try {
|
||||
return await this.testRepository.save(test);
|
||||
} catch (err) {
|
||||
throw new AppTimeoutException(err);
|
||||
}
|
||||
}
|
||||
|
||||
public async deleteOne(id: number) {
|
||||
let test: Test | null = null;
|
||||
|
||||
try {
|
||||
test = await this.testRepository.findOneBy({ id });
|
||||
} catch (err) {
|
||||
throw new AppTimeoutException(err);
|
||||
}
|
||||
|
||||
if (!test) throw new NotFoundException('tests.errors.testNotFound');
|
||||
|
||||
try {
|
||||
await this.testRepository.delete(test.id);
|
||||
} catch (err) {
|
||||
throw new AppTimeoutException(err);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user