add: add modules entity
This commit is contained in:
20
src/configurable/configurable.controller.spec.ts
Normal file
20
src/configurable/configurable.controller.spec.ts
Normal file
@@ -0,0 +1,20 @@
|
||||
import { Test, TestingModule } from '@nestjs/testing';
|
||||
import { ConfigurableController } from './configurable.controller';
|
||||
import { ConfigurableService } from './configurable.service';
|
||||
|
||||
describe('ConfigurableController', () => {
|
||||
let controller: ConfigurableController;
|
||||
|
||||
beforeEach(async () => {
|
||||
const module: TestingModule = await Test.createTestingModule({
|
||||
controllers: [ConfigurableController],
|
||||
providers: [ConfigurableService],
|
||||
}).compile();
|
||||
|
||||
controller = module.get<ConfigurableController>(ConfigurableController);
|
||||
});
|
||||
|
||||
it('should be defined', () => {
|
||||
expect(controller).toBeDefined();
|
||||
});
|
||||
});
|
||||
42
src/configurable/configurable.controller.ts
Normal file
42
src/configurable/configurable.controller.ts
Normal file
@@ -0,0 +1,42 @@
|
||||
import {
|
||||
Controller,
|
||||
Get,
|
||||
Post,
|
||||
Body,
|
||||
Put,
|
||||
Param,
|
||||
Delete,
|
||||
ParseUUIDPipe,
|
||||
HttpStatus,
|
||||
} from '@nestjs/common';
|
||||
import { ConfigurableService } from './configurable.service';
|
||||
|
||||
@Controller({
|
||||
path: 'config',
|
||||
version: '1',
|
||||
})
|
||||
export class ConfigurableController {
|
||||
constructor(private readonly usersService: ConfigurableService) {}
|
||||
|
||||
@Get()
|
||||
async findAll() {
|
||||
const [data, count] = await this.usersService.findAll();
|
||||
|
||||
return {
|
||||
data,
|
||||
count,
|
||||
statusCode: HttpStatus.OK,
|
||||
message: 'success',
|
||||
};
|
||||
}
|
||||
|
||||
@Get(':id')
|
||||
async findOne(@Param('id', ParseUUIDPipe) id: string) {
|
||||
return {
|
||||
data: await this.usersService.findOne(id),
|
||||
statusCode: HttpStatus.OK,
|
||||
message: 'success',
|
||||
};
|
||||
}
|
||||
|
||||
}
|
||||
12
src/configurable/configurable.module.ts
Normal file
12
src/configurable/configurable.module.ts
Normal file
@@ -0,0 +1,12 @@
|
||||
import { Module } from '@nestjs/common';
|
||||
import { TypeOrmModule } from '@nestjs/typeorm';
|
||||
import { Roles } from './entities/roles.entity';
|
||||
import { ConfigurableController } from './configurable.controller';
|
||||
import { ConfigurableService } from './configurable.service';
|
||||
|
||||
@Module({
|
||||
imports: [TypeOrmModule.forFeature([Roles])],
|
||||
controllers: [ConfigurableController],
|
||||
providers: [ConfigurableService],
|
||||
})
|
||||
export class ConfigurableModule {}
|
||||
18
src/configurable/configurable.service.spec.ts
Normal file
18
src/configurable/configurable.service.spec.ts
Normal file
@@ -0,0 +1,18 @@
|
||||
import { Test, TestingModule } from '@nestjs/testing';
|
||||
import { ConfigurableService } from './configurable.service';
|
||||
|
||||
describe('ConfigurableService', () => {
|
||||
let service: ConfigurableService;
|
||||
|
||||
beforeEach(async () => {
|
||||
const module: TestingModule = await Test.createTestingModule({
|
||||
providers: [ConfigurableService],
|
||||
}).compile();
|
||||
|
||||
service = module.get<ConfigurableService>(ConfigurableService);
|
||||
});
|
||||
|
||||
it('should be defined', () => {
|
||||
expect(service).toBeDefined();
|
||||
});
|
||||
});
|
||||
34
src/configurable/configurable.service.ts
Normal file
34
src/configurable/configurable.service.ts
Normal file
@@ -0,0 +1,34 @@
|
||||
import { HttpException, HttpStatus, Injectable } from '@nestjs/common';
|
||||
import { EntityNotFoundError, Repository } from 'typeorm';
|
||||
import { Roles } from './entities/roles.entity';
|
||||
import { InjectRepository } from '@nestjs/typeorm';
|
||||
|
||||
@Injectable()
|
||||
export class ConfigurableService {
|
||||
constructor(
|
||||
@InjectRepository(Roles)
|
||||
private rolesRepository: Repository<Roles>,
|
||||
) {}
|
||||
|
||||
findAll() {
|
||||
return this.rolesRepository.findAndCount();
|
||||
}
|
||||
|
||||
async findOne(id: string) {
|
||||
try {
|
||||
return await this.rolesRepository.findOneOrFail(id);
|
||||
} catch (e) {
|
||||
if (e instanceof EntityNotFoundError) {
|
||||
throw new HttpException(
|
||||
{
|
||||
statusCode: HttpStatus.NOT_FOUND,
|
||||
error: 'Data not found',
|
||||
},
|
||||
HttpStatus.NOT_FOUND,
|
||||
);
|
||||
} else {
|
||||
throw e;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
8
src/configurable/entities/roles.entity.ts
Normal file
8
src/configurable/entities/roles.entity.ts
Normal file
@@ -0,0 +1,8 @@
|
||||
import { Entity, Column } from 'typeorm';
|
||||
import { BaseModel } from '../../config/basemodel.entity';
|
||||
|
||||
@Entity()
|
||||
export class Roles extends BaseModel {
|
||||
@Column()
|
||||
name: string;
|
||||
}
|
||||
Reference in New Issue
Block a user