amélioration des requêtes d'amitié, on peut maintenant directement bloquer une personne.

This commit is contained in:
batche
2022-11-21 12:39:41 +01:00
parent 452acd5146
commit 5efea64ca1
3 changed files with 17 additions and 17 deletions

View File

@@ -1,4 +1,4 @@
import { IsEnum, IsInt, IsNumber, IsOptional, IsPositive, IsString } from 'class-validator';
import { IsEnum, IsString } from 'class-validator';
import { FriendshipStatus } from '../entities/friendship.entity';
export class CreateFriendshipDto {
@@ -7,6 +7,5 @@ export class CreateFriendshipDto {
@IsString()
readonly addresseeId: string;
@IsEnum(FriendshipStatus)
@IsOptional()
readonly status: FriendshipStatus;
}

View File

@@ -8,7 +8,7 @@ import { FriendshipService } from './friendship.service';
export class FriendshipController {
constructor(private readonly friendshipService: FriendshipService) { }
// GET http://127.0.0.1:3000/api/v2/network/myfriends
// GET http://transcendance:8080/api/v2/network/myfriends
@Get('myfriends')
@UseGuards(AuthenticateGuard)
@UseGuards(TwoFactorGuard)
@@ -17,7 +17,7 @@ export class FriendshipController {
return this.friendshipService.findAllFriends(user.id);
}
// GET http://127.0.0.1:3000/api/v2/network/myfriends/relationshipId
// GET http://transcendance:8080/api/v2/network/myfriends/relationshipId
@Get('myfriends/:relationshipId')
@UseGuards(AuthenticateGuard)
@UseGuards(TwoFactorGuard)
@@ -26,20 +26,20 @@ export class FriendshipController {
return this.friendshipService.findOneFriend(relationshipId, user.id);
}
// POST http://127.0.0.1:3000/api/v2/network/myfriends
// POST http://transcendance:8080/api/v2/network/myfriends
@Post('myfriends')
@HttpCode(HttpStatus.CREATED)
@UseGuards(AuthenticateGuard)
@UseGuards(TwoFactorGuard)
create(@Body() createFriendshipDto: CreateFriendshipDto, @Req() req) {
const user = req.user;
console.log(`User id: ${user.id}\n Friend id: ${createFriendshipDto.requesterId}`);
console.log(`User id: ${user.id}\nFriend id: ${createFriendshipDto.requesterId}\nStatus: ${createFriendshipDto.status}`);
if (user.id === +createFriendshipDto.requesterId)
return this.friendshipService.create(createFriendshipDto, user);
return new HttpException('You can\'t request a frienship for another user', HttpStatus.FORBIDDEN);
}
// PATCH http://127.0.0.1:3000/api/v2/network/myfriends/relationshipId?status=A
// PATCH http://transcendance:8080/api/v2/network/myfriends/relationshipId?status=A
@Patch('myfriends/:relationshipId')
@UseGuards(AuthenticateGuard)
@UseGuards(TwoFactorGuard)
@@ -49,7 +49,7 @@ export class FriendshipController {
return this.friendshipService.updateFriendship(relationshipId, user, status);
}
// DELETE http://127.0.0.1:3000/api/v2/network/myfriends/relationshipId
// DELETE http://transcendance:8080/api/v2/network/myfriends/relationshipId
@Delete('myfriends/:relationshipId')
@UseGuards(AuthenticateGuard)
@UseGuards(TwoFactorGuard)
@@ -58,7 +58,7 @@ export class FriendshipController {
}
// GET http://127.0.0.1:3000/api/v2/network/blocked
// GET http://transcendance:8080/api/v2/network/blocked
@Get('blocked')
@UseGuards(AuthenticateGuard)
@UseGuards(TwoFactorGuard)
@@ -67,7 +67,7 @@ export class FriendshipController {
return this.friendshipService.findAllBlockedFriends(user.id);
}
// GET http://127.0.0.1:3000/api/v2/network/pending
// GET http://transcendance:8080/api/v2/network/pending
@Get('pending')
@UseGuards(AuthenticateGuard)
@UseGuards(TwoFactorGuard)
@@ -76,7 +76,7 @@ export class FriendshipController {
return this.friendshipService.findAllPendantRequestsForFriendship(user.id);
}
// GET http://127.0.0.1:3000/api/v2/network/received
// GET http://transcendance:8080/api/v2/network/received
@Get('received')
@UseGuards(AuthenticateGuard)
@UseGuards(TwoFactorGuard)

View File

@@ -74,7 +74,9 @@ export class FriendshipService {
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.NOT_FOUND);
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 });
if (friendship) {
if (friendship.status && friendship.status === FriendshipStatus.ACCEPTED)
@@ -86,8 +88,7 @@ export class FriendshipService {
else if (friendship.status && friendship.status === FriendshipStatus.DECLINED)
throw new HttpException(`The request has been declined.`, HttpStatus.OK);
}
const newFriendship = this.friendshipRepository.create(
{ requesterId: createFriendshipDto.requesterId, addresseeId: createFriendshipDto.addresseeId, status: FriendshipStatus.REQUESTED });
const newFriendship = this.friendshipRepository.create(createFriendshipDto);
return this.friendshipRepository.save(newFriendship);
}
@@ -95,9 +96,9 @@ 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) {
// throw new HttpException(`You can't accept your own request.`, HttpStatus.NOT_FOUND);
// }
if (+relation.requesterId === user.id) {
throw new HttpException(`You can't accept your own request.`, HttpStatus.NOT_FOUND);
}
if (status === FriendshipStatus.ACCEPTED)
relation.status = FriendshipStatus.ACCEPTED;
else if (status === FriendshipStatus.DECLINED)