Skip to content

Commit

Permalink
Merge pull request #441 from bounswe/feature/BE/415/get-user-info
Browse files Browse the repository at this point in the history
feature/BE/415/get-user-info
  • Loading branch information
omersafakbebek authored Nov 15, 2023
2 parents b0b1d8e + d8e980a commit c4203ac
Show file tree
Hide file tree
Showing 6 changed files with 69 additions and 4 deletions.
19 changes: 18 additions & 1 deletion ludos/backend/src/controllers/user.controller.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import {
Body,
Controller,
Get,
HttpCode,
Post,
Put,
Expand Down Expand Up @@ -33,7 +34,7 @@ import { AuthorizedRequest } from '../interfaces/common/authorized-request.inter
@ApiTags('user')
@Controller('user')
export class UserController {
constructor(private readonly userService: UserService) {}
constructor(private readonly userService: UserService) { }
@ApiOkResponse({
description: 'Successful Register',
type: RegisterResponseDto,
Expand Down Expand Up @@ -77,6 +78,7 @@ export class UserController {
@ApiBadRequestResponse({
description: 'Bad Request',
})

@HttpCode(200)
@ApiOperation({ summary: 'Password Reset Request Endpoint' })
@Post('/reset-password')
Expand Down Expand Up @@ -137,4 +139,19 @@ export class UserController {
) {
await this.userService.editInfo(req.user.id, input);
}

@HttpCode(200)
@ApiUnauthorizedResponse({
description: 'Invalid User',
})
@ApiBadRequestResponse({
description: 'Bad Request',
})

@ApiBearerAuth()
@ApiOperation({ summary: 'Get User Info Request Endpoint' })
@Get('/info')
public async getUserInfoById(@Req() req: AuthorizedRequest) {
return await this.userService.getUserInfo(req.user.id);
}
}
10 changes: 10 additions & 0 deletions ludos/backend/src/dtos/user/request/get-user-info.dto.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { ApiProperty } from '@nestjs/swagger';
import { IsString } from 'class-validator';

export class GetUserInfoDto {
@ApiProperty({
example: '1',
})
@IsString()
id: string;
}
11 changes: 11 additions & 0 deletions ludos/backend/src/dtos/user/response/get-user-info-response.dto.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { ApiProperty } from '@nestjs/swagger';
import { Game } from '../../../entities/game.entity';

export class GetUserInfoResponseDto {
@ApiProperty()
username: string;
@ApiProperty()
email: string;
@ApiProperty()
followedGames: Game[];
}
15 changes: 13 additions & 2 deletions ludos/backend/src/repositories/user.repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,25 @@ export class UserRepository extends Repository<User> {
public findUserByEmail(email: string): Promise<User> {
return this.findOneBy({ email: email });
}

public findUserById(id: string): Promise<User> {
return this.findOneBy({ id });
}

public async updateUserPassword(input: Partial<User>, newPassword: string) {
public async updateUserPassword(input: Partial<User>, newPassword: string) {
const user = await this.findUserByUsername(input.username);
user.password = newPassword;
await this.save(user);
}

public async findUserByIdWithRelations(id: string): Promise<User> {
return await this.findOne({
relations: this.getAllRelationsAsList(),
where: { id: id },
});
}

public getAllRelationsAsList() {
return this.metadata.relations.map((relation) => relation.propertyName);
}
}
1 change: 1 addition & 0 deletions ludos/backend/src/services/game.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ export class GameService {
developer: game.developer,
};
} catch (e) {
console.log(e)
if (e.code == '23505') {
throw new ConflictException(e.detail);
}
Expand Down
17 changes: 16 additions & 1 deletion ludos/backend/src/services/user.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import { EditUserInfoDto } from '../dtos/user/request/edit-info.dto';
import { Payload } from '../interfaces/user/payload.interface';
import { ResetPasswordRepository } from '../repositories/reset-password.repository';
import { UserRepository } from '../repositories/user.repository';
import { GetUserInfoResponseDto } from '../dtos/user/response/get-user-info-response.dto';

@Injectable()
export class UserService {
Expand All @@ -31,7 +32,7 @@ export class UserService {
private readonly resetPasswordRepository: ResetPasswordRepository,
private readonly jwtService: JwtService,
private readonly configService: ConfigService,
) {}
) {}

public async register(input: RegisterDto): Promise<RegisterResponseDto> {
try {
Expand Down Expand Up @@ -187,4 +188,18 @@ export class UserService {
let updated = Object.assign(user, editInfoDto);
await this.userRepository.save(updated);
}

public async getUserInfo(userId: string): Promise<GetUserInfoResponseDto> {
const user = await this.userRepository.findUserByIdWithRelations(userId);
if (!user) {
throw new NotFoundException('User not found');
}

const response = new GetUserInfoResponseDto();
response.email = user.email;
response.username = user.username;
response.followedGames = user.followedGames;

return response;
}
}

0 comments on commit c4203ac

Please sign in to comment.