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

This commit is contained in:
Me
2022-12-11 06:04:03 +01:00
parent 61828e5be7
commit 6eeb2b5474
6 changed files with 107 additions and 48 deletions

View File

@@ -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);
}

View File

@@ -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;
}
}

View File

@@ -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) {
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')

View File

@@ -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<User> = {
username: user.username,
image_url: user.image_url,

View File

@@ -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}
<!-- problem, i can't use user.id since Cherif doesn't want to expost that to the front...
is there something else i could use? -->
<!-- add a thing so it doesn't display the current user just all the other users -->
<!-- does this work? -->
<!-- {#each allUsers as aUser (aUser.username)} -->
{#each allUsers as aUser }
<!-- <div class="a-user" on:click={() => displayAUser(aUser.username)}>{aUser.username}</div> -->
<div class="a-user" on:click={() => userToDisplay = aUser.username}>{aUser.username}</div>
@@ -199,22 +204,23 @@ could be a list of friends and if they're active but i can't see that yet
<br> -->
<!-- <Button on:click={displayAllFriends}>Get All Friends</Button>
{#if allFriends !== undefined}
<div>{allFriends}</div>
{#each allFriends as friend}
<!-- <Button on:click={displayMyFriends}>Get All My Friends</Button>
{#if myFriends !== undefined}
<div>{myFriends}</div>
{#each myFriends as friend}
<div>{friend}{friend.username}</div>
{/each}
{/if} -->
<!-- <br>
<br>
<div>Make a Friend!</div>
<form on:submit|preventDefault={() => friendUserByUsername(set.friendUsername)}>
<!-- <form on:submit|preventDefault={() => friendUserByUsername(set.friendUsername)}> -->
<form on:submit|preventDefault={friendUserByUsername}>
<input type="text" placeholder="friend's username" bind:value={set.friendUsername}>
<div class="error">{errors.friendRequest}</div>
<Button type="secondary">Send Friend Request</Button>
</form> -->
</form>
<!-- <br> <br> -->
<!-- here i want to list all the requests you and have an accept and a decline button -->

View File

@@ -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})
})