Merge branch 'master' of bitbucket.org:LuckyLaszlo/ft_transcendence into inclusion_jeu_a_partir_de_master

This commit is contained in:
batche
2022-12-09 11:29:38 +01:00
4 changed files with 25 additions and 26 deletions

View File

@@ -3,9 +3,9 @@ import { FriendshipStatus } from '../entities/friendship.entity';
export class CreateFriendshipDto { export class CreateFriendshipDto {
@IsString() @IsString()
readonly requesterId: string; readonly requesterUsername: string;
@IsString() @IsString()
readonly addresseeId: string; readonly addresseeUsername: string;
@IsEnum(FriendshipStatus) @IsEnum(FriendshipStatus)
readonly status: FriendshipStatus; readonly status: FriendshipStatus;
} }

View File

@@ -18,12 +18,12 @@ export class Friendship {
date : Date; date : Date;
@Column() @Column()
@ManyToOne(type => User, user => user.requesterId) @ManyToOne(type => User, user => user.username)
requesterId: string; requesterUsername: string;
@Column() @Column()
@ManyToOne(type => User, user => user.addresseeId) @ManyToOne(type => User, user => user.username)
addresseeId: string; addresseeUsername: string;
@Column({ type: 'enum', enum: FriendshipStatus, default: FriendshipStatus.REQUESTED}) @Column({ type: 'enum', enum: FriendshipStatus, default: FriendshipStatus.REQUESTED})
status: FriendshipStatus; status: FriendshipStatus;

View File

@@ -33,8 +33,8 @@ export class FriendshipController {
@UseGuards(TwoFactorGuard) @UseGuards(TwoFactorGuard)
create(@Body() createFriendshipDto: CreateFriendshipDto, @Req() req) { create(@Body() createFriendshipDto: CreateFriendshipDto, @Req() req) {
const user = req.user; const user = req.user;
console.log(`User id: ${user.id}\nFriend id: ${createFriendshipDto.requesterId}\nStatus: ${createFriendshipDto.status}`); console.log(`User id: ${user.id}\nFriend id: ${createFriendshipDto.requesterUsername}\nStatus: ${createFriendshipDto.status}`);
if (user.id === +createFriendshipDto.requesterId) if (user.username === createFriendshipDto.requesterUsername)
return this.friendshipService.create(createFriendshipDto, user); return this.friendshipService.create(createFriendshipDto, user);
return new HttpException('You can\'t request a frienship for another user', HttpStatus.FORBIDDEN); return new HttpException('You can\'t request a frienship for another user', HttpStatus.FORBIDDEN);
} }
@@ -64,7 +64,7 @@ export class FriendshipController {
@UseGuards(TwoFactorGuard) @UseGuards(TwoFactorGuard)
findAllBlocked(@Req() req) { findAllBlocked(@Req() req) {
const user = req.user; const user = req.user;
return this.friendshipService.findAllBlockedFriends(user.id); return this.friendshipService.findAllBlockedFriends(user.username);
} }
// GET http://transcendance:8080/api/v2/network/pending // GET http://transcendance:8080/api/v2/network/pending

View File

@@ -17,8 +17,8 @@ export class FriendshipService {
) { } ) { }
async findOneFriend(friendshipId: string, userId: string) { async findOneFriend(friendshipId: string, username: string) {
const friendship = await this.friendshipRepository.find({ where: { id: +friendshipId, requesterId: userId, status: FriendshipStatus.ACCEPTED } }); const friendship = await this.friendshipRepository.find({ where: { id: +friendshipId, requesterUsername: username, status: FriendshipStatus.ACCEPTED } });
if (!friendship) if (!friendship)
throw new HttpException(`The requested friend not found.`, HttpStatus.NOT_FOUND); throw new HttpException(`The requested friend not found.`, HttpStatus.NOT_FOUND);
return friendship; return friendship;
@@ -31,12 +31,12 @@ export class FriendshipService {
return friendship; return friendship;
} }
async findAllFriends(userId: string) { async findAllFriends(username: string) {
const friendship = await this.friendshipRepository const friendship = await this.friendshipRepository
.createQueryBuilder('friendship') .createQueryBuilder('friendship')
.where('friendship.status = :status', { status: FriendshipStatus.ACCEPTED }) .where('friendship.status = :status', { status: FriendshipStatus.ACCEPTED })
.andWhere('friendship.addresseeId = :addressee', { addressee: userId }) .andWhere('friendship.addresseeUsername = :addressee', { addressee: username })
.orWhere('friendship.requesterId = :requester', { requester: userId }) .orWhere('friendship.requesterUsername = :requester', { requester: username })
.andWhere('friendship.status = :status', { status: FriendshipStatus.ACCEPTED }) .andWhere('friendship.status = :status', { status: FriendshipStatus.ACCEPTED })
.getMany(); .getMany();
for (const friend of friendship) for (const friend of friendship)
@@ -44,40 +44,39 @@ export class FriendshipService {
return friendship; return friendship;
} }
async findAllBlockedFriends(userId: string) { async findAllBlockedFriends(username: string) {
return await this.friendshipRepository return await this.friendshipRepository
.createQueryBuilder('friendship') .createQueryBuilder('friendship')
.where('friendship.requesterId = :requestee', { requestee: userId }) .where('friendship.requesterUsername = :requestee', { requestee: username })
.orWhere('friendship.addresseeId = :addressee', { addressee: userId })
.andWhere('friendship.status = :status', { status: FriendshipStatus.BLOCKED }) .andWhere('friendship.status = :status', { status: FriendshipStatus.BLOCKED })
.getMany(); .getMany();
} }
async findAllPendantRequestsForFriendship(userId: string) { async findAllPendantRequestsForFriendship(username: string) {
return await this.friendshipRepository return await this.friendshipRepository
.createQueryBuilder('friendship') .createQueryBuilder('friendship')
.where('friendship.requesterId = :requestee', { requestee: userId }) .where('friendship.requesterUsername = :requestee', { requestee: username })
.andWhere('friendship.status = :status', { status: FriendshipStatus.REQUESTED }) .andWhere('friendship.status = :status', { status: FriendshipStatus.REQUESTED })
.getMany(); .getMany();
} }
async findAllReceivedRequestsForFriendship(userId: string) { async findAllReceivedRequestsForFriendship(username: string) {
return await this.friendshipRepository return await this.friendshipRepository
.createQueryBuilder('friendship') .createQueryBuilder('friendship')
.where('friendship.addresseeId = :addressee', { addressee: userId }) .where('friendship.addresseeUsername = :addressee', { addressee: username })
.andWhere('friendship.status = :status', { status: FriendshipStatus.REQUESTED }) .andWhere('friendship.status = :status', { status: FriendshipStatus.REQUESTED })
.getMany(); .getMany();
} }
//GROS CHANTIER //GROS CHANTIER
async create(createFriendshipDto: CreateFriendshipDto, creator : User) { async create(createFriendshipDto: CreateFriendshipDto, creator : User) {
const addressee = await this.userRepository.findOneBy({ id: +createFriendshipDto.addresseeId }); const addressee = await ;
if (!addressee) if (!addressee)
throw new HttpException(`The addressee does not exist.`, HttpStatus.NOT_FOUND); throw new HttpException(`The addressee does not exist.`, HttpStatus.NOT_FOUND);
if (creator.id === addressee.id) if (creator.id === addressee.id)
throw new HttpException(`You can't add yourself.`, HttpStatus.FORBIDDEN); throw new HttpException(`You can't add yourself.`, HttpStatus.FORBIDDEN);
if (createFriendshipDto.status !== FriendshipStatus.REQUESTED && createFriendshipDto.status !== FriendshipStatus.BLOCKED) if (createFriendshipDto.status !== FriendshipStatus.REQUESTED && createFriendshipDto.status !== FriendshipStatus.BLOCKED)
throw new HttpException(`The status is not valid.`, HttpStatus.NOT_FOUND); throw new HttpException(`The status is not valid.`, HttpStatus.NOT_FOUND);
const friendship = await this.friendshipRepository.findOneBy({ requesterId: createFriendshipDto.requesterId, addresseeId: createFriendshipDto.addresseeId }); const friendship = await this.friendshipRepository.findOneBy({ requesterUsername: createFriendshipDto.requesterUsername, addresseeUsername: createFriendshipDto.addresseeUsername });
if (friendship) { if (friendship) {
if (friendship.status && friendship.status === FriendshipStatus.ACCEPTED) if (friendship.status && friendship.status === FriendshipStatus.ACCEPTED)
throw new HttpException(`The friendship request has already been accepted.`, HttpStatus.OK); throw new HttpException(`The friendship request has already been accepted.`, HttpStatus.OK);
@@ -96,7 +95,7 @@ export class FriendshipService {
const relation = await this.friendshipRepository.findOneBy({ id: +relationshipId }); const relation = await this.friendshipRepository.findOneBy({ id: +relationshipId });
if (!relation) if (!relation)
throw new HttpException(`The requested relationship not found.`, HttpStatus.NOT_FOUND); throw new HttpException(`The requested relationship not found.`, HttpStatus.NOT_FOUND);
if (+relation.requesterId === user.id) { if (+relation.requesterUsername === user.id) {
throw new HttpException(`You can't accept your own request.`, HttpStatus.NOT_FOUND); throw new HttpException(`You can't accept your own request.`, HttpStatus.NOT_FOUND);
} }
if (status === FriendshipStatus.ACCEPTED) if (status === FriendshipStatus.ACCEPTED)
@@ -122,8 +121,8 @@ export class FriendshipService {
async findIfUserIsBlockedOrHasBlocked(userConnectedId: string, userToCheckId: string) { async findIfUserIsBlockedOrHasBlocked(userConnectedId: string, userToCheckId: string) {
const friendship = await this.friendshipRepository const friendship = await this.friendshipRepository
.createQueryBuilder('friendship') .createQueryBuilder('friendship')
.where('friendship.requesterId = :requestee', { requestee: userConnectedId }) .where('friendship.requesterUsername = :requestee', { requestee: userConnectedId })
.orWhere('friendship.requesterId = :requesteeBis', { requesteeBis: userToCheckId }) .orWhere('friendship.requesterUsername = :requesteeBis', { requesteeBis: userToCheckId })
.andWhere('friendship.status = :status', { status: FriendshipStatus.BLOCKED }) .andWhere('friendship.status = :status', { status: FriendshipStatus.BLOCKED })
.getOne(); .getOne();
if (friendship) if (friendship)