Skip to content

Commit

Permalink
Merge pull request #1340 from ricky-ultimate/dev
Browse files Browse the repository at this point in the history
feat(language): Implement endpoint to fetch user languages
  • Loading branch information
incredible-phoenix246 authored Mar 1, 2025
2 parents 06283ce + 92c3b41 commit fad19fc
Show file tree
Hide file tree
Showing 4 changed files with 263 additions and 429 deletions.
117 changes: 70 additions & 47 deletions src/modules/languages/languages.controller.ts
Original file line number Diff line number Diff line change
@@ -1,55 +1,78 @@
import { Controller, Post, Body, Get, Patch, Param, Res, Request, Response } from '@nestjs/common';
import { Controller, Post, Body, Get, Patch, Param, Res, Request, Response, UseGuards } from '@nestjs/common';
import { ApiTags, ApiOperation, ApiResponse, ApiBody, ApiBearerAuth } from '@nestjs/swagger';
import { CreateLanguageDto, UpdateLanguageDto } from './dto/create-language.dto';
import { LanguagesService } from './languages.service';
import { AuthGuard } from '@guards/auth.guard';
import { NotFoundException, UnauthorizedException } from '@nestjs/common';

@ApiTags('Languages')
@Controller('languages')
export class LanguagesController {
constructor(private readonly languagesService: LanguagesService) {}

@Post()
@ApiBearerAuth()
@ApiOperation({ summary: 'Create a new language' })
@ApiBody({ type: CreateLanguageDto })
@ApiResponse({ status: 201, description: 'Language successfully created.' })
@ApiResponse({ status: 409, description: 'Language already exists.' })
@ApiResponse({ status: 401, description: 'Unauthorized' })
async createLanguage(@Body() createLanguageDto: CreateLanguageDto) {
return this.languagesService.createLanguage(createLanguageDto);
}

@Get()
@ApiBearerAuth()
@ApiOperation({ summary: 'Get all supported languages' })
@ApiResponse({ status: 200, description: 'List of supported languages.' })
@ApiResponse({ status: 401, description: 'Unauthorized' })
async getLanguages(@Response() response): Promise<any> {
const result = await this.languagesService.getSupportedLanguages();
return response.status(result.status_code).json(result);
}

@Get(':id')
@ApiBearerAuth()
@ApiOperation({ summary: 'Get all languages from a userId' })
@ApiResponse({ status: 200, description: 'List of languages by the user.' })
@ApiResponse({ status: 401, description: 'Unauthorized' })
@ApiResponse({ status: 400, description: 'Invalid UserId' })
@ApiResponse({ status: 404, description: 'No languages found for the specified user' })
@ApiResponse({ status: 500, description: 'Internal server error' })
async getLanguagesByUserId(@Param('id') id: string, @Request() req, @Response() response): Promise<any> {
const result = await this.languagesService.getLanguagesById(id, req.user);
return response.status(result.status_code).json(result);
}

@Patch(':id')
@ApiBearerAuth()
@ApiOperation({ summary: 'Update a language' })
@ApiBody({ type: UpdateLanguageDto })
@ApiResponse({ status: 200, description: 'Language successfully updated.' })
@ApiResponse({ status: 404, description: 'Language not found.' })
@ApiResponse({ status: 401, description: 'Unauthorized' })
async updateLanguage(@Param('id') id: string, @Body() updateLanguageDto: UpdateLanguageDto) {
return this.languagesService.updateLanguage(id, updateLanguageDto);
}
constructor(private readonly languagesService: LanguagesService) { }

@Post()
@ApiBearerAuth()
@ApiOperation({ summary: 'Create a new language' })
@ApiBody({ type: CreateLanguageDto })
@ApiResponse({ status: 201, description: 'Language successfully created.' })
@ApiResponse({ status: 409, description: 'Language already exists.' })
@ApiResponse({ status: 401, description: 'Unauthorized' })
async createLanguage(@Body() createLanguageDto: CreateLanguageDto) {
return this.languagesService.createLanguage(createLanguageDto);
}

@Get()
@ApiBearerAuth()
@ApiOperation({ summary: 'Get all supported languages' })
@ApiResponse({ status: 200, description: 'List of supported languages.' })
@ApiResponse({ status: 401, description: 'Unauthorized' })
async getLanguages(@Response() response): Promise<any> {
const result = await this.languagesService.getSupportedLanguages();
return response.status(result.status_code).json(result);
}

@Get(':id')
@ApiBearerAuth()
@ApiOperation({ summary: 'Get all languages from a userId' })
@ApiResponse({ status: 200, description: 'List of languages by the user.' })
@ApiResponse({ status: 401, description: 'Unauthorized' })
@ApiResponse({ status: 400, description: 'Invalid UserId' })
@ApiResponse({ status: 404, description: 'No languages found for the specified user' })
@ApiResponse({ status: 500, description: 'Internal server error' })
async getLanguagesByUserId(@Param('id') id: string, @Request() req, @Response() response): Promise<any> {
const result = await this.languagesService.getLanguagesById(id, req.user);
return response.status(result.status_code).json(result);
}

@Patch(':id')
@ApiBearerAuth()
@ApiOperation({ summary: 'Update a language' })
@ApiBody({ type: UpdateLanguageDto })
@ApiResponse({ status: 200, description: 'Language successfully updated.' })
@ApiResponse({ status: 404, description: 'Language not found.' })
@ApiResponse({ status: 401, description: 'Unauthorized' })
async updateLanguage(@Param('id') id: string, @Body() updateLanguageDto: UpdateLanguageDto) {
return this.languagesService.updateLanguage(id, updateLanguageDto);
}

@ApiBearerAuth()
@UseGuards(AuthGuard)
@ApiOperation({ summary: 'Get all languages by User ID' })
@ApiResponse({ status: 200, description: 'Returns languages associated with user.' })
@ApiResponse({ status: 404, description: 'User not found or no languages available.' })
@ApiResponse({ status: 401, description: 'Unauthorized access.' })
@Get(':id/languages')
async getUserLanguages(@Param('id') userId: string) {
if (!userId) {
throw new UnauthorizedException('Invalid user ID.');
}

const languages = await this.languagesService.getUserLanguages(userId);

if (!languages.length) {
throw new NotFoundException('No languages found for this user.');
}

return { status: 200, data: languages };
}
}
Loading

0 comments on commit fad19fc

Please sign in to comment.