adding the ability to add and delete friends, in progress

This commit is contained in:
Me
2022-12-14 06:31:09 +01:00
parent bd4938b64b
commit 3a6729b9a3
3 changed files with 115 additions and 27 deletions

View File

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

View File

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