feat: add admin module
This commit is contained in:
@@ -0,0 +1,48 @@
|
||||
import {
|
||||
IsBoolean,
|
||||
IsEnum,
|
||||
IsNotEmpty,
|
||||
IsOptional,
|
||||
IsString,
|
||||
Matches,
|
||||
MaxLength,
|
||||
} from 'class-validator';
|
||||
import { i18nValidationMessage as t } from 'nestjs-i18n';
|
||||
import { AccessType } from '../enums/access-types.enum';
|
||||
|
||||
export class CreateTestDTO {
|
||||
@IsString()
|
||||
@IsNotEmpty({
|
||||
message: t('validation.requiredField', {
|
||||
field: '$t(tests.fields.title)',
|
||||
}),
|
||||
})
|
||||
@MaxLength(255, {
|
||||
message: t('validation.maxLengthField', {
|
||||
field: '$t(tests.fields.title)',
|
||||
max: 255,
|
||||
}),
|
||||
})
|
||||
title: string;
|
||||
|
||||
@IsString()
|
||||
@IsNotEmpty({
|
||||
message: t('validation.requiredField', {
|
||||
field: '$t(tests.fields.description)',
|
||||
}),
|
||||
})
|
||||
description: string;
|
||||
|
||||
@IsEnum(AccessType)
|
||||
@IsOptional()
|
||||
accessType?: AccessType;
|
||||
|
||||
@IsString()
|
||||
@IsOptional()
|
||||
@Matches(/^\d+$/, { message: t('tests.errors.intPrice') })
|
||||
price?: string;
|
||||
|
||||
@IsBoolean()
|
||||
@IsOptional()
|
||||
isActive?: boolean;
|
||||
}
|
||||
@@ -20,6 +20,7 @@ export class Test {
|
||||
type: 'varchar',
|
||||
length: 255,
|
||||
nullable: false,
|
||||
unique: true,
|
||||
})
|
||||
title: string;
|
||||
|
||||
|
||||
@@ -0,0 +1,57 @@
|
||||
import {
|
||||
HttpStatus,
|
||||
Injectable,
|
||||
RequestTimeoutException,
|
||||
} from '@nestjs/common';
|
||||
import { Repository } from 'typeorm';
|
||||
import { Test } from '../entities/test.entity';
|
||||
import { InjectRepository } from '@nestjs/typeorm';
|
||||
import { CreateTestDTO } from '../dtos/create-test.dto';
|
||||
import { AppException } from '@/common/exceptions/app.exception';
|
||||
import { AppResponse } from '@/common/responses';
|
||||
import { I18nService } from 'nestjs-i18n';
|
||||
|
||||
@Injectable()
|
||||
export class TestsService {
|
||||
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 RequestTimeoutException('common.errors.requestTimeout', {
|
||||
cause: err,
|
||||
description: 'Error connecting to the database',
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,8 +1,11 @@
|
||||
import { Module } from '@nestjs/common';
|
||||
import { TypeOrmModule } from '@nestjs/typeorm';
|
||||
import { Test } from './entities/test.entity';
|
||||
import { TestsService } from './providers/tests.service';
|
||||
|
||||
@Module({
|
||||
imports: [TypeOrmModule.forFeature([Test])],
|
||||
providers: [TestsService],
|
||||
exports: [TestsService],
|
||||
})
|
||||
export class TestsModule {}
|
||||
|
||||
Reference in New Issue
Block a user