diff --git a/srcs/requirements/nestjs/api_back/src/friendship/friendship.controller.ts b/srcs/requirements/nestjs/api_back/src/friendship/friendship.controller.ts index e03d9f24..da0f656e 100644 --- a/srcs/requirements/nestjs/api_back/src/friendship/friendship.controller.ts +++ b/srcs/requirements/nestjs/api_back/src/friendship/friendship.controller.ts @@ -26,9 +26,10 @@ export class FriendshipController { @UseGuards(AuthenticateGuard) @UseGuards(TwoFactorGuard) findOne(@Query('username') otherUsername: string, @Req() req) { - console.log('GET myfriend') + console.log('GET myfriends') const user = req.user; if (otherUsername !== undefined) { + console.log('my friend: ' + otherUsername) return this.friendshipService.findOneRelationshipByUsername(otherUsername, user.username); } // might change this @@ -48,17 +49,18 @@ export class FriendshipController { // return this.friendshipService.findOneRelationshipByUsername(friendUsername, user.username); // } - - // POST http://transcendance:8080/api/v2/network/relations @Post('relations') @HttpCode(HttpStatus.CREATED) @UseGuards(AuthenticateGuard) @UseGuards(TwoFactorGuard) create(@Body() createFriendshipDto: CreateFriendshipDto, @Req() req) { + console.log('friendship.service create') const user = req.user; - if (user.username !== createFriendshipDto.receiverUsername) + if (user.username !== createFriendshipDto.receiverUsername) { + console.log('friendship.service create have a receiver name') return this.friendshipService.create(createFriendshipDto, user); + } return new HttpException('You can\'t request a frienship to yourself', HttpStatus.BAD_REQUEST); } diff --git a/srcs/requirements/nestjs/api_back/src/friendship/friendship.service.ts b/srcs/requirements/nestjs/api_back/src/friendship/friendship.service.ts index dfb9f5fa..7bd4ed08 100644 --- a/srcs/requirements/nestjs/api_back/src/friendship/friendship.service.ts +++ b/srcs/requirements/nestjs/api_back/src/friendship/friendship.service.ts @@ -129,6 +129,7 @@ export class FriendshipService { const friendship = await this.friendshipRepository .createQueryBuilder('friendship') .leftJoinAndSelect('friendship.sender', 'sender') + .leftJoinAndSelect('friendship.receiver', 'receiver') .where('friendship.id = :id', { id: friendshipId }) .andWhere('sender.id = :requester', { requester: userId }) .andWhere('friendship.status = :status', { status: FriendshipStatus.BLOCKED }) @@ -139,6 +140,7 @@ export class FriendshipService { return new SendableFriendship(friendship); } + async findOneBlockedByUsername(blockedUsername : string, userId : number) { const friendship = await this.friendshipRepository .createQueryBuilder('friendship') @@ -159,6 +161,7 @@ export class FriendshipService { const friendships : Friendship[] = await this.friendshipRepository .createQueryBuilder('friendship') .leftJoinAndSelect('friendship.sender', 'sender') + .leftJoinAndSelect('friendship.receiver', 'receiver') .where('sender.id = :requestee', { requestee: userId }) .andWhere('friendship.status = :status', { status: FriendshipStatus.BLOCKED }) .getMany(); @@ -184,6 +187,7 @@ export class FriendshipService { const friendships = await this.friendshipRepository .createQueryBuilder('friendship') .leftJoinAndSelect('friendship.sender', 'sender') + .leftJoinAndSelect('friendship.receiver', 'receiver') .where('sender.id = :requestee', { requestee: userId }) .andWhere('friendship.status = :status', { status: FriendshipStatus.REQUESTED }) .getMany(); @@ -207,6 +211,7 @@ export class FriendshipService { async findAllReceivedRequestsForFriendship(userId: number) { const friendships = await this.friendshipRepository .createQueryBuilder('friendship') + .leftJoinAndSelect('friendship.sender', 'sender') .leftJoinAndSelect('friendship.receiver', 'receiver') .where('receiver.id = :addressee', { addressee: userId }) .andWhere('friendship.status = :status', { status: FriendshipStatus.REQUESTED }) @@ -231,18 +236,32 @@ export class FriendshipService { async create(createFriendshipDto: CreateFriendshipDto, creator : User) { console.log("DTO : \n") console.log({...createFriendshipDto}) + + // These throws don't seem to work... const receiver = await this.userRepository.findOneBy({username: createFriendshipDto.receiverUsername}); + console.log('receiver: ') + console.log({...receiver}) if (!receiver) throw new HttpException(`The addressee does not exist.`, HttpStatus.NOT_FOUND); 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({ sender: creator, receiver: receiver }); + // const friendship = await this.friendshipRepository.findOneBy({ sender: creator, receiver: receiver }); - console.log('friendship.service create, friendship: ') - console.log({...friendship}) + + const friendship = await this.friendshipRepository + .createQueryBuilder('friendship') + .leftJoinAndSelect('friendship.sender', 'sender') + .leftJoinAndSelect('friendship.receiver', 'receiver') + .where('sender.id = :senderId', {senderId: creator.id}) + .andWhere('receiver.id = :receiverId', {receiverId: receiver.id}) + .getOne(); + + console.log('friendship.service create, friendship: \n\n\n') + // console.log({...friendship}) if (friendship) { + console.log('there is already a friend request') if (friendship.status && friendship.status === FriendshipStatus.ACCEPTED) throw new HttpException(`The friendship request has already been accepted.`, HttpStatus.OK); else if (friendship.status && friendship.status === FriendshipStatus.REQUESTED) @@ -252,6 +271,8 @@ export class FriendshipService { else if (friendship.status && friendship.status === FriendshipStatus.DECLINED) throw new HttpException(`The request has been declined.`, HttpStatus.OK); } + console.log('friendship.service create, we are still saving a new friendship') + const newFriendship = new Friendship(); newFriendship.sender = creator; // newFriendship.senderUsername = creator.username; @@ -279,9 +300,18 @@ export class FriendshipService { } async acceptFriendship(relationshipId: number, user: User) { - const relation = await this.friendshipRepository.findOneBy({id: relationshipId }); - // console.log('.service accept friendship') - // console.log({...relation}) + // const relation = await this.friendshipRepository.findOneBy({id: relationshipId }); + + + const relation = await this.friendshipRepository + .createQueryBuilder('friendship') + .leftJoinAndSelect('friendship.sender', 'sender') + .leftJoinAndSelect('friendship.receiver', 'receiver') + .where('friendship.id = :friendshipId', { friendshipId: relationshipId }) + .getOne(); + + console.log('.service accept friendship') + console.log({...relation}) if (!relation) throw new HttpException(`The requested relationship not found.`, HttpStatus.NOT_FOUND); if (relation.sender.id === user.id) { @@ -304,7 +334,12 @@ export class FriendshipService { } async declineFriendship(relationshipId: number, user: User) { - const relation = await this.friendshipRepository.findOneBy({id: relationshipId }); + const relation = await this.friendshipRepository + .createQueryBuilder('friendship') + .leftJoinAndSelect('friendship.sender', 'sender') + .leftJoinAndSelect('friendship.receiver', 'receiver') + .where('friendship.id = :friendshipId', { friendshipId: relationshipId }) + .getOne(); if (!relation) throw new HttpException(`The requested relationship not found.`, HttpStatus.NOT_FOUND); if (relation.sender.id === user.id) { @@ -325,7 +360,12 @@ export class FriendshipService { } async blockFriendship(relationshipId: number, user: User) { - let relation = await this.friendshipRepository.findOneBy({id: relationshipId }); + const relation = await this.friendshipRepository + .createQueryBuilder('friendship') + .leftJoinAndSelect('friendship.sender', 'sender') + .leftJoinAndSelect('friendship.receiver', 'receiver') + .where('friendship.id = :friendshipId', { friendshipId: relationshipId }) + .getOne(); if (!relation) throw new HttpException(`The requested relationship not found.`, HttpStatus.NOT_FOUND); @@ -365,7 +405,12 @@ export class FriendshipService { async removeFriendship(relationshipId: number, user : User) { // const friendship = await this.friendshipRepository.findOneBy({ id: +relationshipId }); // once again we need find() not findOneBy() so once again have to grab first elem of an array - const friendship = await this.friendshipRepository.findOneBy({id: relationshipId }); + const friendship = await this.friendshipRepository + .createQueryBuilder('friendship') + .leftJoinAndSelect('friendship.sender', 'sender') + .leftJoinAndSelect('friendship.receiver', 'receiver') + .where('friendship.id = :friendshipId', { friendshipId: relationshipId }) + .getOne(); console.log('deleting a friendship .service') console.log({...friendship}) console.log('who is the user') diff --git a/srcs/requirements/nestjs/api_back/src/friendship/sendableFriendship.ts b/srcs/requirements/nestjs/api_back/src/friendship/sendableFriendship.ts index 32d58520..d2c3be30 100644 --- a/srcs/requirements/nestjs/api_back/src/friendship/sendableFriendship.ts +++ b/srcs/requirements/nestjs/api_back/src/friendship/sendableFriendship.ts @@ -10,9 +10,9 @@ export class SendableFriendship { id: number; date: Date; senderUsername: string; - senderId: number; + // senderId: number; receiverUsername: string; - receiverId: number; + // receiverId: number; status: FriendshipStatus; // if my constructor is here won't it get sent all over the place too? @@ -20,9 +20,9 @@ export class SendableFriendship { this.id = friendship.id; this.date = friendship.date this.senderUsername = friendship.sender.username; - this.senderId = friendship.sender.id; + // this.senderId = friendship.sender.id; this.receiverUsername = friendship.receiver.username; - this.receiverId = friendship.receiver.id; + // this.receiverId = friendship.receiver.id; this.status = friendship.status; }; } \ No newline at end of file diff --git a/srcs/requirements/svelte/api_front/src/pages/profile/ProfileFriends.svelte b/srcs/requirements/svelte/api_front/src/pages/profile/ProfileFriends.svelte index fa06c7f6..610a1d97 100644 --- a/srcs/requirements/svelte/api_front/src/pages/profile/ProfileFriends.svelte +++ b/srcs/requirements/svelte/api_front/src/pages/profile/ProfileFriends.svelte @@ -93,23 +93,28 @@ const fetchFriendshipFull = async(aUsername) => { console.log('fetch friendship from a username') console.log(aUsername) - friendshipStatusFull = await fetch(`http://transcendance:8080/api/v2/network/myfriends/${aUsername}`) + friendshipStatusFull = await fetch(`http://transcendance:8080/api/v2/network/myfriends?username=${aUsername}`) .then( x => x.json()); console.log({...friendshipStatusFull}) }; - - const sendFriendRequest = async(aUser) => { + + const sendFriendRequest = async(aUsername) => { + console.log('sending a friend request') + console.log(aUsername) const resp = await fetch("http://transcendance:8080/api/v2/network/relations", { method : "POST", headers: { 'Content-Type': 'application/json'}, body: JSON.stringify({ - "receiverUsername": aUser.username, + "receiverUsername": aUsername, "status": "R" }) }) .then( x => x.json()) - fetchFriendshipFull(aUser.username); + // .catch( x => console.log({...x})) + // "status": "A" + console.log({...resp}) + fetchFriendshipFull(aUsername); }; const viewAUser = async(aUsername) => { @@ -335,6 +340,7 @@ {/if} + {:else}