Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feature/BE/415/get-user-info #441

Merged
merged 7 commits into from
Nov 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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> {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be better if the user with all relations is fetched in one query since we will have additional relations. You can check this

const user = await this.userRepository.findUserByIdWithRelations(userId);
if (!user) {
throw new NotFoundException('User not found');
}

const response = new GetUserInfoResponseDto();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Recently added user fields should be added also. These are not enough.

response.email = user.email;
response.username = user.username;
response.followedGames = user.followedGames;

return response;
}
}
Loading