Skip to content

Commit

Permalink
refactor: Update UserController to handle user profile updates and im…
Browse files Browse the repository at this point in the history
…age uploads
  • Loading branch information
suk-6 committed Jul 13, 2024
1 parent 7252802 commit 0776a76
Showing 1 changed file with 25 additions and 10 deletions.
35 changes: 25 additions & 10 deletions src/modules/user/user.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { Body, Controller, Delete, Get, Param, Patch, Put, Req, UseGuards } from
import { AuthGuard } from '@nestjs/passport';
import { ApiBearerAuth, ApiBody, ApiOkResponse, ApiOperation, ApiTags, ApiUnauthorizedResponse } from '@nestjs/swagger';

import { CurrentUser } from 'src/common';
import { CurrentUser, ResponseDTO } from 'src/common';

import { UpdateUserDTO } from './dto/update-user.dto';
import { UploadProfileImageDTO } from './dto/upload-profile-image.dto';
Expand All @@ -22,7 +22,7 @@ export class UserController {
@ApiOperation({ summary: 'Get user information' })
@ApiOkResponse({ description: 'User information', type: UserResponseDTO })
@ApiUnauthorizedResponse({ description: 'Unauthorized' })
async getUser(@Req() req: any) {
async getUser(@Req() req: any): Promise<UserResponseDTO> {
return await this.userService.findUserById(req.user.id);
}

Expand All @@ -32,7 +32,7 @@ export class UserController {
@ApiOperation({ summary: 'Get user information by id' })
@ApiOkResponse({ description: 'User information', type: UserResponseDTO })
@ApiUnauthorizedResponse({ description: 'Unauthorized' })
async getUserById(@Param('userId') userId: string) {
async getUserById(@Param('userId') userId: string): Promise<UserResponseDTO> {
return await this.userService.findUserById(userId);
}

Expand All @@ -45,30 +45,45 @@ export class UserController {
@ApiOperation({ summary: 'Update user information' })
@ApiOkResponse({
description: 'User information updated',
type: UserResponseDTO,
type: ResponseDTO<null>,
})
@ApiUnauthorizedResponse({ description: 'Unauthorized' })
async updateUser(@CurrentUser() user: User, @Body() data: Prisma.UserUpdateInput) {
return await this.userService.updateUserById(user.id, data);
async updateUser(@CurrentUser() user: User, @Body() data: Prisma.UserUpdateInput): Promise<ResponseDTO<null>> {
try {
await this.userService.updateUserById(user.id, data);
return { status: 'success', data: null };
} catch (error) {
return { status: 'error', data: null };
}
}

@Put('/profile-image')
@UseGuards(AuthGuard('access'))
@ApiBearerAuth()
@ApiOperation({ summary: 'Update user profile image' })
@ApiOkResponse({ description: 'User profile image updated' })
@ApiOkResponse({ description: 'User profile image updated', type: ResponseDTO<null> })
@ApiUnauthorizedResponse({ description: 'Unauthorized' })
async updateUserProfileImage(@CurrentUser() user: User, @Body() dto: UploadProfileImageDTO) {
return await this.userService.updateUserProfileImageById(user.id, dto.profileImage);
try {
await this.userService.updateUserProfileImageById(user.id, dto.profileImage);
return { status: 'success', data: null };
} catch (error) {
return { status: 'error', data: null };
}
}

@Delete('/')
@UseGuards(AuthGuard('access'))
@ApiBearerAuth()
@ApiOperation({ summary: 'Delete user' })
@ApiOkResponse({ description: 'User deleted', type: UserResponseDTO })
@ApiOkResponse({ description: 'User deleted', type: ResponseDTO<null> })
@ApiUnauthorizedResponse({ description: 'Unauthorized' })
async deleteUser(@CurrentUser() user: User) {
return await this.userService.deleteUserById(user.id);
try {
await this.userService.deleteUserById(user.id);
return { status: 'success', data: null };
} catch (error) {
return { status: 'error', data: null };
}
}
}

0 comments on commit 0776a76

Please sign in to comment.