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 4c6417ea..2933aadb 100644 --- a/srcs/requirements/nestjs/api_back/src/friendship/friendship.controller.ts +++ b/srcs/requirements/nestjs/api_back/src/friendship/friendship.controller.ts @@ -14,6 +14,7 @@ export class FriendshipController { @UseGuards(TwoFactorGuard) findEmpty(@Req() req) { const user = req.user; + console.log('GET myfriends') return this.friendshipService.findAllFriends(user.id); } @@ -31,11 +32,12 @@ export class FriendshipController { // return this.friendshipService.findOneFriend(relationshipId, user.username); // } - @Get('myfriend/:friendUsername') + @Get('myfriends/:friendUsername') @UseGuards(AuthenticateGuard) @UseGuards(TwoFactorGuard) findOneRelationByUsername(@Param('friendUsername') friendUsername : string, @Req() req) { - console.log("Username " + friendUsername); + console.log('GET myfriend') + console.log(friendUsername); const user = req.user; return this.friendshipService.findOneFriendByUsername(friendUsername, user.username); } @@ -76,6 +78,7 @@ export class FriendshipController { updateAccept(@Param('relationshipId') relationshipId: string, @Req() req) { const user : User = req.user; + console.log('accepting a friendship') return this.friendshipService.acceptFriendship(relationshipId, user); } @@ -103,6 +106,7 @@ export class FriendshipController { @UseGuards(TwoFactorGuard) remove(@Param('relationshipId') relationshipId: string, @Req() req) { const user : User = req.user; + console.log('deleting a friendship') return this.friendshipService.removeFriendship(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 159b4b49..bec0f497 100644 --- a/srcs/requirements/nestjs/api_back/src/friendship/friendship.service.ts +++ b/srcs/requirements/nestjs/api_back/src/friendship/friendship.service.ts @@ -1,7 +1,7 @@ import { HttpException, HttpStatus, Injectable } from '@nestjs/common'; import { InjectRepository } from '@nestjs/typeorm'; import { User } from 'src/users/entities/user.entity'; -import { Repository } from 'typeorm'; +import { Repository, Brackets } from 'typeorm'; import { CreateFriendshipDto } from './dto/create-friendship.dto'; import { Friendship, FriendshipStatus } from './entities/friendship.entity'; @@ -26,11 +26,32 @@ export class FriendshipService { async findOneFriendByUsername(friendUsername : string, username : string) { const friendship = await this.friendshipRepository .createQueryBuilder('friendship') - .where('friendship.senderUsername = : username', {username : username}) - .andWhere('friendship.receiverUsername = : friendUsername', {friendUsername : friendUsername}) - .andWhere('friendship.status = : status ', {status : FriendshipStatus.REQUESTED}) - .orWhere('friendship.status = : status ', {status : FriendshipStatus.ACCEPTED}) + .where( + new Brackets((qb) => { + qb.where( + new Brackets((subQb) => { + subQb.where('friendship.senderUsername = :username', {username : username}) + .andWhere('friendship.receiverUsername = :friendUsername', {friendUsername : friendUsername}) + }) + ) + .orWhere( + new Brackets((subQb) => { + subQb.where('friendship.senderUsername = :friendUsername', {friendUsername : friendUsername}) + .andWhere('friendship.receiverUsername = :username', {username : username}) + }) + ) + }), + ) + .andWhere( + new Brackets((qb) => { + qb.where('friendship.status = :status', {status : FriendshipStatus.ACCEPTED}) + .orWhere('friendship.status = :status', {status : FriendshipStatus.REQUESTED}) + }), + ) .getOne() + if (!friendship) { + throw new HttpException(`There is no such friendship`, HttpStatus.NOT_FOUND); + } return friendship; } @@ -44,13 +65,17 @@ export class FriendshipService { async findOneBlockedByUsername(blockedUsername : string, username : string) { const friendship = await this.friendshipRepository .createQueryBuilder('friendship') - .where('friendship.senderUsername = : username', {username : username}) - .andWhere('friendship.receiverUsername = : friendUsername', {friendUsername : blockedUsername}) - .andWhere('friendship.status = : status ', {status : FriendshipStatus.BLOCKED}) + .where('friendship.senderUsername = :username', {username : username}) + .andWhere('friendship.receiverUsername = :friendUsername', {friendUsername : blockedUsername}) + .andWhere('friendship.status = :status ', {status : FriendshipStatus.BLOCKED}) .getOne() + if (!friendship) { + throw new HttpException(`The requested blocked not found.`, HttpStatus.NOT_FOUND); + } return friendship; } + // ERIC: test this async findAllFriends(username: string) { const friendship = await this.friendshipRepository .createQueryBuilder('friendship') 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 87eb1716..d3e6f263 100644 --- a/srcs/requirements/svelte/api_front/src/pages/profile/ProfileFriends.svelte +++ b/srcs/requirements/svelte/api_front/src/pages/profile/ProfileFriends.svelte @@ -2,6 +2,7 @@ import { onMount } from "svelte"; import { binding_callbacks } from "svelte/internal"; // WTF is this? + import App from "../../App.svelte"; import Button from "../../pieces/Button.svelte"; import DisplayAUser from "../../pieces/DisplayAUser.svelte"; import Tabs from "../../pieces/Tabs.svelte"; @@ -14,6 +15,8 @@ let myFriends; let requestsMade, requestsRecieved; let usernameBeingViewed; + let friendshipStatusFull; // id, date, senderUsername, reveiverUsername, status + let friendshipFetch; // like a generic var for any friendship fetch actions that might happen /**** Layout variables ****/ let tabItems: string[] = ['All Users', 'My Friends', 'Friend Requests'] @@ -49,10 +52,20 @@ const fetchMyFriends = async() => { myFriends = await fetch('http://transcendance:8080/api/v2/network/myfriends') .then( x => x.json() ); - // console.log('got all friends ') - // console.log({...allFriends}) + console.log('got my friends ') + 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() ); @@ -93,19 +106,47 @@ } }; + const areWeFriends = async(aUsername) => { + console.log("Are We Friends?") + friendshipStatusFull = await fetch(`http://transcendance:8080/api/v2/network/myfriends/${aUsername}`) + .then( x => x.json()); + + }; + const viewAUser = async(aUsername) => { console.log('Profile Friend updating userBeingViewed') usernameBeingViewed = aUsername; + friendshipStatusFull = undefined; + // id, date, senderUsername, reveiverUsername, status + friendshipStatusFull = await fetch(`http://transcendance:8080/api/v2/network/myfriends/${aUsername}`) + .then( x => x.json()); - // sendUsername = userBeingViewed.username; - - // prolly like fetch if you're friends or not? - // GET http://transcendance:8080/api/v2/networks/myfriends?username=aUser but i need to make that as long as Cherif - // doesn't have a better option - // like i want this thing to return the Friendship ID ideally + console.log('Friendship Status Full') + console.log({...friendshipStatusFull}) + }; + + const acceptFriendRequest = async(relationshipId) => { + console.log('accept friend request') + friendshipFetch = undefined; + // PATCH http://transcendance:8080/api/v2/network/myfriends/:relationshipId/accept + friendshipFetch = await fetch(`http://transcendance:8080/api/v2/network/myfriends/${relationshipId}/accept`, { + method: "PATCH"}) + .then( x => x.json()); + }; + + 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()); + }; + + const blockAUser = async(friendshipId) => { }; @@ -143,12 +184,14 @@ {#each allUsers as aUser} -
- - -