From 6eeb2b5474666853768ea0ea348f678a8d0f9a03 Mon Sep 17 00:00:00 2001 From: Me Date: Sun, 11 Dec 2022 06:04:03 +0100 Subject: [PATCH] ok so i fixed the get user by username issue, left all the comments in the back so Cherif and I can go over what i changed just to make sure i didn't break something else, but yea that and my front work just fine --- .../src/friendship/friendship.controller.ts | 6 +- .../src/friendship/friendship.service.ts | 31 +++++++- .../api_back/src/users/users.controller.ts | 21 ++++- .../api_back/src/users/users.service.ts | 16 +++- .../src/pages/profile/ProfileFriends.svelte | 78 ++++++++++--------- .../api_front/src/pieces/DisplayAUser.svelte | 3 +- 6 files changed, 107 insertions(+), 48 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 1441c3b7..57377aa1 100644 --- a/srcs/requirements/nestjs/api_back/src/friendship/friendship.controller.ts +++ b/srcs/requirements/nestjs/api_back/src/friendship/friendship.controller.ts @@ -36,7 +36,9 @@ export class FriendshipController { @UseGuards(TwoFactorGuard) create(@Body() createFriendshipDto: CreateFriendshipDto, @Req() req) { const user = req.user; - console.log(`User id: ${user.id}\nFriend id: ${createFriendshipDto.requesterUsername}\nStatus: ${createFriendshipDto.status}`); + + console.log(`User id: ${user.id}\nUser name: ${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); @@ -77,7 +79,7 @@ export class FriendshipController { findAllPendantFriendshipRequested(@Req() req) { const user = req.user; - console.log("WHAT IS UP MY GUYS IT IS YOUR BOI THAT IDIOT YOU HATE Pending 33333"); + // console.log("WHAT IS UP MY GUYS IT IS YOUR BOI THAT IDIOT YOU HATE Pending 33333"); return this.friendshipService.findAllPendantRequestsForFriendship(user.id); } 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 c2f0a164..5e141f50 100644 --- a/srcs/requirements/nestjs/api_back/src/friendship/friendship.service.ts +++ b/srcs/requirements/nestjs/api_back/src/friendship/friendship.service.ts @@ -69,14 +69,24 @@ export class FriendshipService { //GROS CHANTIER async create(createFriendshipDto: CreateFriendshipDto, creator : User) { const addressee = await this.userRepository.findOneBy({username: createFriendshipDto.addresseeUsername}); + + console.log('made it to create friendship from friendship controller') + if (!addressee) throw new HttpException(`The addressee does not exist.`, HttpStatus.NOT_FOUND); - // console.log('Addressee ID: ' + addressee.id + 'Username: ' + a) + console.log('Addressee ID: ' + addressee.id + ' Username: ' + addressee.username) + console.log('Creator ID: ' + creator.id + ' Username: ' + creator.username) if (creator.id === addressee.id) throw new HttpException(`You can't add yourself.`, HttpStatus.FORBIDDEN); + console.log('test 1') if (createFriendshipDto.status !== FriendshipStatus.REQUESTED && createFriendshipDto.status !== FriendshipStatus.BLOCKED) throw new HttpException(`The status is not valid.`, HttpStatus.NOT_FOUND); + console.log('test 2') + const friendship = await this.friendshipRepository.findOneBy({ requesterUsername: createFriendshipDto.requesterUsername, addresseeUsername: createFriendshipDto.addresseeUsername }); + + console.log('test 3') + if (friendship) { if (friendship.status && friendship.status === FriendshipStatus.ACCEPTED) throw new HttpException(`The friendship request has already been accepted.`, HttpStatus.OK); @@ -87,8 +97,17 @@ export class FriendshipService { else if (friendship.status && friendship.status === FriendshipStatus.DECLINED) throw new HttpException(`The request has been declined.`, HttpStatus.OK); } + console.log('test 7') + const newFriendship = this.friendshipRepository.create(createFriendshipDto); - return this.friendshipRepository.save(newFriendship); + console.log('test 8, New Friendship: ') + console.log({...newFriendship}) + + const tmp = this.friendshipRepository.save(newFriendship); + console.log('tmp: ') + console.log({...tmp}) + return tmp; + // return this.friendshipRepository.save(newFriendship); } async updateFriendship(relationshipId: string, user: User, status: string) { @@ -119,14 +138,20 @@ export class FriendshipService { } async findIfUserIsBlockedOrHasBlocked(userConnectedId: string, userToCheckId: string) { + console.log("finding if user is blocked") const friendship = await this.friendshipRepository .createQueryBuilder('friendship') .where('friendship.requesterUsername = :requestee', { requestee: userConnectedId }) .orWhere('friendship.requesterUsername = :requesteeBis', { requesteeBis: userToCheckId }) .andWhere('friendship.status = :status', { status: FriendshipStatus.BLOCKED }) .getOne(); - if (friendship) + console.log("printing friendship") + console.log(friendship) + // console.log({...friendship}) + if (friendship) { + console.log('we are blocked in friendship.service') return true; + } return false; } } diff --git a/srcs/requirements/nestjs/api_back/src/users/users.controller.ts b/srcs/requirements/nestjs/api_back/src/users/users.controller.ts index 6b4a4b58..181b9ac5 100644 --- a/srcs/requirements/nestjs/api_back/src/users/users.controller.ts +++ b/srcs/requirements/nestjs/api_back/src/users/users.controller.ts @@ -36,12 +36,26 @@ export class UsersController { * car un utilisateur est crée à la première connexion avec l'Oauth de 42. */ + // @UseGuards(AuthenticateGuard) + // @UseGuards(TwoFactorGuard) + // @Get() + // findOne(@Req() req) { + // console.log("Backend Getting current user"); + // return this.usersService.findOne(req.user.id); + // } + @UseGuards(AuthenticateGuard) @UseGuards(TwoFactorGuard) @Get() - findOne(@Req() req) { - console.log("Backend Getting current user"); - return this.usersService.findOne(req.user.id); + findOne(@Query('username') username: string, @Req() req) { + if (username === undefined) { + console.log("Backend Getting current user"); + return this.usersService.findOne(req.user.id); + } else { + const user : User = req.user; + console.log('we have a query: ' + username) + return this.usersService.findOneByUsername(user.id.toString(),username); + } } // GET http://transcendance:8080/user?username=NomDuUser @@ -54,6 +68,7 @@ export class UsersController { return this.usersService.findOneByUsername(user.id.toString(),username); } + @UseGuards(AuthenticateGuard) @UseGuards(TwoFactorGuard) @Get('stats') diff --git a/srcs/requirements/nestjs/api_back/src/users/users.service.ts b/srcs/requirements/nestjs/api_back/src/users/users.service.ts index 0a97fd78..41c9713e 100644 --- a/srcs/requirements/nestjs/api_back/src/users/users.service.ts +++ b/srcs/requirements/nestjs/api_back/src/users/users.service.ts @@ -66,10 +66,20 @@ export class UsersService { .where('user.username = :username', { username: username }) .getOne(); console.log('USERNAME OF FOUND USER : ' + user.username); + // console.log({...user}) + // you can't do that, you need to do user === undefined + // if (user === undefined) if (!user) - throw new HttpException(`The user could not be found.`,HttpStatus.NOT_FOUND); - if (this.friendshipService.findIfUserIsBlockedOrHasBlocked(userConnectedId, user.id.toString())) - throw new HttpException(`The user could not be found.`,HttpStatus.NOT_FOUND); + throw new HttpException(`The user could not be found 1.`,HttpStatus.NOT_FOUND); + + let tmp = await this.friendshipService.findIfUserIsBlockedOrHasBlocked(userConnectedId, user.id.toString()) + console.log('printing return of checking if blocked') + console.log(tmp) + if (await this.friendshipService.findIfUserIsBlockedOrHasBlocked(userConnectedId, user.id.toString())) { + console.log('we are blocked in user.service') + throw new HttpException(`The user could not be found 2.`,HttpStatus.NOT_FOUND); + } + const partialUser : Partial = { username: user.username, image_url: user.image_url, 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 902c51b4..483cae00 100644 --- a/srcs/requirements/svelte/api_front/src/pages/profile/ProfileFriends.svelte +++ b/srcs/requirements/svelte/api_front/src/pages/profile/ProfileFriends.svelte @@ -30,7 +30,6 @@ could be a list of friends and if they're active but i can't see that yet let myFriends; let requestsMade, requestsRecieved; let allUsers; - let allFriends; let userToDisplay; @@ -40,89 +39,91 @@ could be a list of friends and if they're active but i can't see that yet user = await fetch('http://transcendance:8080/api/v2/user') .then( (x) => x.json() ); - console.log('user is ') - console.log(user) - console.log(user.username) + // console.log('user is ') + // console.log(user) + // console.log(user.username) myFriends = await fetch("http://transcendance:8080/api/v2/network/myfriends") .then( (x) => x.json() ); - console.log('my friends') - console.log(myFriends) + // console.log('my friends') + // console.log(myFriends) requestsMade = await fetch('http://transcendance:8080/api/v2/network/pending') .then( x => x.json() ); - console.log('Requests pending '); - console.log(requestsMade); + // console.log('Requests pending '); + // console.log(requestsMade); requestsRecieved = await fetch('http://transcendance:8080/api/v2/network/received') .then( x => x.json() ); - console.log('Requests received '); - console.log(requestsRecieved); + // console.log('Requests received '); + // console.log(requestsRecieved); allUsers = await fetch('http://transcendance:8080/api/v2/user/all') .then( x => x.json() ); - console.log('got all users ' + allUsers) + // console.log('got all users ' + allUsers) - allFriends = await fetch('http://transcendance:8080/api/v2/network/myfriends') - .then( x => x.json() ); - console.log('got all friends ' + allFriends) }) const displayAllUsers = async() => { allUsers = await fetch('http://transcendance:8080/api/v2/user/all') .then( x => x.json() ); - console.log('got all users ' + allUsers) + // console.log('got all users ' + allUsers) }; const displayAllFriends = async() => { - allFriends = await fetch('http://transcendance:8080/api/v2/network/myfriends') + myFriends = await fetch('http://transcendance:8080/api/v2/network/myfriends') .then( x => x.json() ); - console.log('got all friends ' + allFriends) + // console.log('got all friends ' + allFriends) }; const displayRequestsMade = async() => { requestsMade = await fetch('http://transcendance:8080/api/v2/network/pending') .then( x => x.json() ); - console.log('got requests made ' + requestsMade) + // console.log('got requests made ' + requestsMade) }; + // const sendFriendRequest = + + + let sentFriendRequest; - const friendUserByUsername = async(potenticalFriendUsername) => { + // const friendUserByUsername = async(potenticalFriendUsername) => { + const friendUserByUsername = async() => { let valid = false; - if (potenticalFriendUsername === '' || potenticalFriendUsername.trim() === '') { + if (!user || !user.username) { + valid = false; + } + // if (potenticalFriendUsername === '' || potenticalFriendUsername.trim() === '') { + if (set.friendUsername === '' || set.friendUsername.trim() === '') { errors.friendRequest = "You have to put a friend's name in."; valid = false; - } else if (potenticalFriendUsername === user.username) { + } else if (set.friendUsername === user.username) { errors.friendRequest = "You can't friend yourself." valid = false; } else { + valid = true; } - if (!user || !user.username) { - valid = false; - } // should i be checking if a Friend request has already been made ? if (valid) { - let r = "R"; - console.log('user is ') console.log(user) console.log(user.username) console.log('friend is ') console.log(set.friendUsername) - set.friendId = allUsers.find(f => f.username === set.friendUsername).id - console.log('friend found: ' + set.friendId) + // set.friendId = allUsers.find(f => f.username === set.friendUsername).id + // console.log('friend found: ' + set.friendId) // ok this version works // ok not really, requesterId and all that is good but... @@ -137,7 +138,7 @@ could be a list of friends and if they're active but i can't see that yet }) }) .then( x => x.json()) - .then (x => console.log({...x})); + // .then (x => console.log({...x})); // should i then update requtestsMade with another fetch? // maybe also check if the request went through? @@ -159,6 +160,10 @@ could be a list of friends and if they're active but i can't see that yet {:else} + + + + {#each allUsers as aUser }
userToDisplay = aUser.username}>{aUser.username}
@@ -199,22 +204,23 @@ could be a list of friends and if they're active but i can't see that yet
--> - - +
{errors.friendRequest}
-
--> + diff --git a/srcs/requirements/svelte/api_front/src/pieces/DisplayAUser.svelte b/srcs/requirements/svelte/api_front/src/pieces/DisplayAUser.svelte index e729089c..6be789b9 100644 --- a/srcs/requirements/svelte/api_front/src/pieces/DisplayAUser.svelte +++ b/srcs/requirements/svelte/api_front/src/pieces/DisplayAUser.svelte @@ -12,7 +12,8 @@ user = await fetch(`http://transcendance:8080/api/v2/user?username=${aUsername}`) .then( (x) => x.json() ); - console.log('Display a user: ' + user.username) + // console.log('Display a user: ') + // console.log({...user}) })