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 {
@IsString()
readonly requesterId: string;
readonly requesterUsername: string;
@IsString()
readonly addresseeId: string;
readonly addresseeUsername: string;
@IsEnum(FriendshipStatus)
readonly status: FriendshipStatus;
}

View File

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

View File

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

View File

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