From 6eaf28d4d882296b8140100d2bb5742c456b81e2 Mon Sep 17 00:00:00 2001 From: Me Date: Wed, 14 Dec 2022 08:59:00 +0100 Subject: [PATCH] working on accepting friend requests, have to muck about with the DB, i have to learn how to use .find() --- .../src/friendship/friendship.controller.ts | 2 +- .../src/friendship/friendship.service.ts | 16 +++++++++++++++- .../src/pages/profile/ProfileFriends.svelte | 18 ++++-------------- 3 files changed, 20 insertions(+), 16 deletions(-) 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 2933aadb..73c6bddb 100644 --- a/srcs/requirements/nestjs/api_back/src/friendship/friendship.controller.ts +++ b/srcs/requirements/nestjs/api_back/src/friendship/friendship.controller.ts @@ -78,7 +78,7 @@ export class FriendshipController { updateAccept(@Param('relationshipId') relationshipId: string, @Req() req) { const user : User = req.user; - console.log('accepting a friendship') + console.log('accepting a friendship BBBBBBBBBBBBBBBBBBBBBBB') return this.friendshipService.acceptFriendship(relationshipId, user); } 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 bec0f497..74ec28cf 100644 --- a/srcs/requirements/nestjs/api_back/src/friendship/friendship.service.ts +++ b/srcs/requirements/nestjs/api_back/src/friendship/friendship.service.ts @@ -167,9 +167,20 @@ export class FriendshipService { } async acceptFriendship(relationshipId: string, user: User) { - const relation = await this.friendshipRepository.findOneBy({ id: +relationshipId }); + // const relation = await this.friendshipRepository.findOneBy({ id: +relationshipId }); + // ok gotta swap out hte findOneBy for a find, recall what you did in the Nest.js tutorial + // const relation = await this.friendshipRepository.find({where: {id: +relationshipId }, relations: ['sender', 'receiver']} ); + // const relation = await this.friendshipRepository.find({where: {id: +relationshipId } }); + // const relation = await this.friendshipRepository.findOneBy({where: {id: +relationshipId }, relations: ['sender', 'receiver']}); + const relation = await this.friendshipRepository.findOne({where: {id: +relationshipId }, relations: ['sender', 'receiver']}); + + console.log('.service accept friendship') + console.log({...relation}) if (!relation) throw new HttpException(`The requested relationship not found.`, HttpStatus.NOT_FOUND); + // console.log(relation.sender) + // ok so you can't query sender cuz it's not a column in the entity, not sure how you access it then... + // if (relation.sender.id === user.id) { if (relation.sender.id === user.id) { throw new HttpException(`You can't accept your own request.`, HttpStatus.NOT_FOUND); } @@ -181,6 +192,9 @@ export class FriendshipService { receiverUsername: (await savedFriendship).receiverUsername, status : (await savedFriendship).status } + console.log('.service accept friendship END') + console.log({...partialFriendship}) + return partialFriendship; } 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 d3e6f263..95f7d987 100644 --- a/srcs/requirements/svelte/api_front/src/pages/profile/ProfileFriends.svelte +++ b/srcs/requirements/svelte/api_front/src/pages/profile/ProfileFriends.svelte @@ -41,7 +41,6 @@ }); - const fetchAllUsers = async() => { allUsers = await fetch('http://transcendance:8080/api/v2/user/all') .then( x => x.json() ); @@ -56,16 +55,6 @@ console.log({...myFriends}) }; - let cherif; - const fetchAFriend = async() => { - cherif = await fetch('http://transcendance:8080/api/v2/network/myfriends/chbadad') - .then( x => x.json() ); - console.log('got Cherif ') - console.log(cherif) - // console.log({...cherif}) - }; - - const fetchRequestsMade = async() => { requestsMade = await fetch('http://transcendance:8080/api/v2/network/pending') .then( x => x.json() ); @@ -108,9 +97,9 @@ const areWeFriends = async(aUsername) => { console.log("Are We Friends?") + friendshipStatusFull = undefined; friendshipStatusFull = await fetch(`http://transcendance:8080/api/v2/network/myfriends/${aUsername}`) .then( x => x.json()); - }; const viewAUser = async(aUsername) => { @@ -127,7 +116,6 @@ }; - const acceptFriendRequest = async(relationshipId) => { console.log('accept friend request') friendshipFetch = undefined; @@ -135,15 +123,17 @@ friendshipFetch = await fetch(`http://transcendance:8080/api/v2/network/myfriends/${relationshipId}/accept`, { method: "PATCH"}) .then( x => x.json()); + // maybe not the most robust things, not super reusable cuz it depends on outside vars but works for now... + await areWeFriends(usernameBeingViewed); }; const unfriend = async(relationshipId) => { console.log('Unfriend') friendshipFetch = undefined; - // PATCH http://transcendance:8080/api/v2/network/myfriends/:relationshipId/accept friendshipFetch = await fetch(`http://transcendance:8080/api/v2/network/myfriends/${relationshipId}`, { method: "DELETE"}) .then( x => x.json()); + await areWeFriends(usernameBeingViewed); };