mirror of
https://github.com/SyncrowIOT/backend.git
synced 2025-07-17 03:05:13 +00:00
Add Region module
This commit is contained in:
1
src/region/controllers/index.ts
Normal file
1
src/region/controllers/index.ts
Normal file
@ -0,0 +1 @@
|
|||||||
|
export * from './region.controller';
|
33
src/region/controllers/region.controller.ts
Normal file
33
src/region/controllers/region.controller.ts
Normal file
@ -0,0 +1,33 @@
|
|||||||
|
import {
|
||||||
|
Controller,
|
||||||
|
Get,
|
||||||
|
HttpException,
|
||||||
|
HttpStatus,
|
||||||
|
UseGuards,
|
||||||
|
} from '@nestjs/common';
|
||||||
|
import { RegionService } from '../services/region.service';
|
||||||
|
import { ApiTags, ApiBearerAuth } from '@nestjs/swagger';
|
||||||
|
import { JwtAuthGuard } from '../../../libs/common/src/guards/jwt.auth.guard';
|
||||||
|
|
||||||
|
@ApiTags('Region Module')
|
||||||
|
@Controller({
|
||||||
|
version: '1',
|
||||||
|
path: 'region',
|
||||||
|
})
|
||||||
|
export class RegionController {
|
||||||
|
constructor(private readonly regionService: RegionService) {}
|
||||||
|
|
||||||
|
@ApiBearerAuth()
|
||||||
|
@UseGuards(JwtAuthGuard)
|
||||||
|
@Get()
|
||||||
|
async getAllRegions() {
|
||||||
|
try {
|
||||||
|
return await this.regionService.getAllRegions();
|
||||||
|
} catch (error) {
|
||||||
|
throw new HttpException(
|
||||||
|
error.message || 'Internal server error',
|
||||||
|
error.status || HttpStatus.INTERNAL_SERVER_ERROR,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
13
src/region/region.module.ts
Normal file
13
src/region/region.module.ts
Normal file
@ -0,0 +1,13 @@
|
|||||||
|
import { Module } from '@nestjs/common';
|
||||||
|
import { RegionService } from './services/region.service';
|
||||||
|
import { RegionController } from './controllers/region.controller';
|
||||||
|
import { ConfigModule } from '@nestjs/config';
|
||||||
|
import { RegionRepository } from '@app/common/modules/region/repositories';
|
||||||
|
|
||||||
|
@Module({
|
||||||
|
imports: [ConfigModule],
|
||||||
|
controllers: [RegionController],
|
||||||
|
providers: [RegionService, RegionRepository],
|
||||||
|
exports: [RegionService],
|
||||||
|
})
|
||||||
|
export class RegionModule {}
|
1
src/region/services/index.ts
Normal file
1
src/region/services/index.ts
Normal file
@ -0,0 +1 @@
|
|||||||
|
export * from './region.service';
|
25
src/region/services/region.service.ts
Normal file
25
src/region/services/region.service.ts
Normal file
@ -0,0 +1,25 @@
|
|||||||
|
import {
|
||||||
|
BadRequestException,
|
||||||
|
HttpException,
|
||||||
|
HttpStatus,
|
||||||
|
Injectable,
|
||||||
|
} from '@nestjs/common';
|
||||||
|
import { RegionRepository } from '@app/common/modules/region/repositories';
|
||||||
|
|
||||||
|
@Injectable()
|
||||||
|
export class RegionService {
|
||||||
|
constructor(private readonly regionRepository: RegionRepository) {}
|
||||||
|
async getAllRegions() {
|
||||||
|
try {
|
||||||
|
const regions = await this.regionRepository.find();
|
||||||
|
|
||||||
|
return regions;
|
||||||
|
} catch (err) {
|
||||||
|
if (err instanceof BadRequestException) {
|
||||||
|
throw err; // Re-throw BadRequestException
|
||||||
|
} else {
|
||||||
|
throw new HttpException('Regions found', HttpStatus.NOT_FOUND);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Reference in New Issue
Block a user