-
Notifications
You must be signed in to change notification settings - Fork 192
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1340 from ricky-ultimate/dev
feat(language): Implement endpoint to fetch user languages
- Loading branch information
Showing
4 changed files
with
263 additions
and
429 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }; | ||
} | ||
} |
Oops, something went wrong.