Add TimeZone module with controller and service

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

View File

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

View File

@ -0,0 +1,33 @@
import {
Controller,
Get,
HttpException,
HttpStatus,
UseGuards,
} from '@nestjs/common';
import { TimeZoneService } from '../services/timezone.service';
import { ApiTags, ApiBearerAuth } from '@nestjs/swagger';
import { JwtAuthGuard } from '../../../libs/common/src/guards/jwt.auth.guard';
@ApiTags('TimeZone Module')
@Controller({
version: '1',
path: 'timezone',
})
export class TimeZoneController {
constructor(private readonly timeZoneService: TimeZoneService) {}
@ApiBearerAuth()
@UseGuards(JwtAuthGuard)
@Get()
async getAllTimeZones() {
try {
return await this.timeZoneService.getAllTimeZones();
} catch (error) {
throw new HttpException(
error.message || 'Internal server error',
error.status || HttpStatus.INTERNAL_SERVER_ERROR,
);
}
}
}

View File

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

View File

@ -0,0 +1,25 @@
import { TimeZoneRepository } from '@app/common/modules/timezone/repositories';
import {
BadRequestException,
HttpException,
HttpStatus,
Injectable,
} from '@nestjs/common';
@Injectable()
export class TimeZoneService {
constructor(private readonly timeZoneRepository: TimeZoneRepository) {}
async getAllTimeZones() {
try {
const timeZones = await this.timeZoneRepository.find();
return timeZones;
} catch (err) {
if (err instanceof BadRequestException) {
throw err; // Re-throw BadRequestException
} else {
throw new HttpException('TimeZones found', HttpStatus.NOT_FOUND);
}
}
}
}

View File

@ -0,0 +1,13 @@
import { Module } from '@nestjs/common';
import { TimeZoneService } from './services/timezone.service';
import { TimeZoneController } from './controllers/timezone.controller';
import { ConfigModule } from '@nestjs/config';
import { TimeZoneRepository } from '@app/common/modules/timezone/repositories';
@Module({
imports: [ConfigModule],
controllers: [TimeZoneController],
providers: [TimeZoneService, TimeZoneRepository],
exports: [TimeZoneService],
})
export class TimeZoneModule {}