Add Region module

This commit is contained in:
faris Aljohari
2024-07-15 15:46:32 +03:00
parent 8c2647c1f0
commit 31a6377fa6
5 changed files with 73 additions and 0 deletions

View File

@ -0,0 +1 @@
export * from './region.controller';

View 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,
);
}
}
}

View 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 {}

View File

@ -0,0 +1 @@
export * from './region.service';

View 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);
}
}
}
}