ok gotta test all the paths but we more or less have the friendships and users modules working the new way

This commit is contained in:
Me
2023-01-04 16:04:48 +01:00
parent 000987fbde
commit d3be6e5fe8
4 changed files with 75 additions and 22 deletions

View File

@@ -26,9 +26,10 @@ export class FriendshipController {
@UseGuards(AuthenticateGuard)
@UseGuards(TwoFactorGuard)
findOne(@Query('username') otherUsername: string, @Req() req) {
console.log('GET myfriend')
console.log('GET myfriends')
const user = req.user;
if (otherUsername !== undefined) {
console.log('my friend: ' + otherUsername)
return this.friendshipService.findOneRelationshipByUsername(otherUsername, user.username);
}
// might change this
@@ -48,17 +49,18 @@ export class FriendshipController {
// return this.friendshipService.findOneRelationshipByUsername(friendUsername, user.username);
// }
// POST http://transcendance:8080/api/v2/network/relations
@Post('relations')
@HttpCode(HttpStatus.CREATED)
@UseGuards(AuthenticateGuard)
@UseGuards(TwoFactorGuard)
create(@Body() createFriendshipDto: CreateFriendshipDto, @Req() req) {
console.log('friendship.service create')
const user = req.user;
if (user.username !== createFriendshipDto.receiverUsername)
if (user.username !== createFriendshipDto.receiverUsername) {
console.log('friendship.service create have a receiver name')
return this.friendshipService.create(createFriendshipDto, user);
}
return new HttpException('You can\'t request a frienship to yourself', HttpStatus.BAD_REQUEST);
}

View File

@@ -129,6 +129,7 @@ export class FriendshipService {
const friendship = await this.friendshipRepository
.createQueryBuilder('friendship')
.leftJoinAndSelect('friendship.sender', 'sender')
.leftJoinAndSelect('friendship.receiver', 'receiver')
.where('friendship.id = :id', { id: friendshipId })
.andWhere('sender.id = :requester', { requester: userId })
.andWhere('friendship.status = :status', { status: FriendshipStatus.BLOCKED })
@@ -139,6 +140,7 @@ export class FriendshipService {
return new SendableFriendship(friendship);
}
async findOneBlockedByUsername(blockedUsername : string, userId : number) {
const friendship = await this.friendshipRepository
.createQueryBuilder('friendship')
@@ -159,6 +161,7 @@ export class FriendshipService {
const friendships : Friendship[] = await this.friendshipRepository
.createQueryBuilder('friendship')
.leftJoinAndSelect('friendship.sender', 'sender')
.leftJoinAndSelect('friendship.receiver', 'receiver')
.where('sender.id = :requestee', { requestee: userId })
.andWhere('friendship.status = :status', { status: FriendshipStatus.BLOCKED })
.getMany();
@@ -184,6 +187,7 @@ export class FriendshipService {
const friendships = await this.friendshipRepository
.createQueryBuilder('friendship')
.leftJoinAndSelect('friendship.sender', 'sender')
.leftJoinAndSelect('friendship.receiver', 'receiver')
.where('sender.id = :requestee', { requestee: userId })
.andWhere('friendship.status = :status', { status: FriendshipStatus.REQUESTED })
.getMany();
@@ -207,6 +211,7 @@ export class FriendshipService {
async findAllReceivedRequestsForFriendship(userId: number) {
const friendships = await this.friendshipRepository
.createQueryBuilder('friendship')
.leftJoinAndSelect('friendship.sender', 'sender')
.leftJoinAndSelect('friendship.receiver', 'receiver')
.where('receiver.id = :addressee', { addressee: userId })
.andWhere('friendship.status = :status', { status: FriendshipStatus.REQUESTED })
@@ -231,18 +236,32 @@ export class FriendshipService {
async create(createFriendshipDto: CreateFriendshipDto, creator : User) {
console.log("DTO : \n")
console.log({...createFriendshipDto})
// These throws don't seem to work...
const receiver = await this.userRepository.findOneBy({username: createFriendshipDto.receiverUsername});
console.log('receiver: ')
console.log({...receiver})
if (!receiver)
throw new HttpException(`The addressee does not exist.`, HttpStatus.NOT_FOUND);
if (createFriendshipDto.status !== FriendshipStatus.REQUESTED && createFriendshipDto.status !== FriendshipStatus.BLOCKED)
throw new HttpException(`The status is not valid.`, HttpStatus.NOT_FOUND);
const friendship = await this.friendshipRepository.findOneBy({ sender: creator, receiver: receiver });
// const friendship = await this.friendshipRepository.findOneBy({ sender: creator, receiver: receiver });
console.log('friendship.service create, friendship: ')
console.log({...friendship})
const friendship = await this.friendshipRepository
.createQueryBuilder('friendship')
.leftJoinAndSelect('friendship.sender', 'sender')
.leftJoinAndSelect('friendship.receiver', 'receiver')
.where('sender.id = :senderId', {senderId: creator.id})
.andWhere('receiver.id = :receiverId', {receiverId: receiver.id})
.getOne();
console.log('friendship.service create, friendship: \n\n\n')
// console.log({...friendship})
if (friendship) {
console.log('there is already a friend request')
if (friendship.status && friendship.status === FriendshipStatus.ACCEPTED)
throw new HttpException(`The friendship request has already been accepted.`, HttpStatus.OK);
else if (friendship.status && friendship.status === FriendshipStatus.REQUESTED)
@@ -252,6 +271,8 @@ export class FriendshipService {
else if (friendship.status && friendship.status === FriendshipStatus.DECLINED)
throw new HttpException(`The request has been declined.`, HttpStatus.OK);
}
console.log('friendship.service create, we are still saving a new friendship')
const newFriendship = new Friendship();
newFriendship.sender = creator;
// newFriendship.senderUsername = creator.username;
@@ -279,9 +300,18 @@ export class FriendshipService {
}
async acceptFriendship(relationshipId: number, user: User) {
const relation = await this.friendshipRepository.findOneBy({id: relationshipId });
// console.log('.service accept friendship')
// console.log({...relation})
// const relation = await this.friendshipRepository.findOneBy({id: relationshipId });
const relation = await this.friendshipRepository
.createQueryBuilder('friendship')
.leftJoinAndSelect('friendship.sender', 'sender')
.leftJoinAndSelect('friendship.receiver', 'receiver')
.where('friendship.id = :friendshipId', { friendshipId: relationshipId })
.getOne();
console.log('.service accept friendship')
console.log({...relation})
if (!relation)
throw new HttpException(`The requested relationship not found.`, HttpStatus.NOT_FOUND);
if (relation.sender.id === user.id) {
@@ -304,7 +334,12 @@ export class FriendshipService {
}
async declineFriendship(relationshipId: number, user: User) {
const relation = await this.friendshipRepository.findOneBy({id: relationshipId });
const relation = await this.friendshipRepository
.createQueryBuilder('friendship')
.leftJoinAndSelect('friendship.sender', 'sender')
.leftJoinAndSelect('friendship.receiver', 'receiver')
.where('friendship.id = :friendshipId', { friendshipId: relationshipId })
.getOne();
if (!relation)
throw new HttpException(`The requested relationship not found.`, HttpStatus.NOT_FOUND);
if (relation.sender.id === user.id) {
@@ -325,7 +360,12 @@ export class FriendshipService {
}
async blockFriendship(relationshipId: number, user: User) {
let relation = await this.friendshipRepository.findOneBy({id: relationshipId });
const relation = await this.friendshipRepository
.createQueryBuilder('friendship')
.leftJoinAndSelect('friendship.sender', 'sender')
.leftJoinAndSelect('friendship.receiver', 'receiver')
.where('friendship.id = :friendshipId', { friendshipId: relationshipId })
.getOne();
if (!relation)
throw new HttpException(`The requested relationship not found.`, HttpStatus.NOT_FOUND);
@@ -365,7 +405,12 @@ export class FriendshipService {
async removeFriendship(relationshipId: number, user : User) {
// const friendship = await this.friendshipRepository.findOneBy({ id: +relationshipId });
// once again we need find() not findOneBy() so once again have to grab first elem of an array
const friendship = await this.friendshipRepository.findOneBy({id: relationshipId });
const friendship = await this.friendshipRepository
.createQueryBuilder('friendship')
.leftJoinAndSelect('friendship.sender', 'sender')
.leftJoinAndSelect('friendship.receiver', 'receiver')
.where('friendship.id = :friendshipId', { friendshipId: relationshipId })
.getOne();
console.log('deleting a friendship .service')
console.log({...friendship})
console.log('who is the user')

View File

@@ -10,9 +10,9 @@ export class SendableFriendship {
id: number;
date: Date;
senderUsername: string;
senderId: number;
// senderId: number;
receiverUsername: string;
receiverId: number;
// receiverId: number;
status: FriendshipStatus;
// if my constructor is here won't it get sent all over the place too?
@@ -20,9 +20,9 @@ export class SendableFriendship {
this.id = friendship.id;
this.date = friendship.date
this.senderUsername = friendship.sender.username;
this.senderId = friendship.sender.id;
// this.senderId = friendship.sender.id;
this.receiverUsername = friendship.receiver.username;
this.receiverId = friendship.receiver.id;
// this.receiverId = friendship.receiver.id;
this.status = friendship.status;
};
}

View File

@@ -93,23 +93,28 @@
const fetchFriendshipFull = async(aUsername) => {
console.log('fetch friendship from a username')
console.log(aUsername)
friendshipStatusFull = await fetch(`http://transcendance:8080/api/v2/network/myfriends/${aUsername}`)
friendshipStatusFull = await fetch(`http://transcendance:8080/api/v2/network/myfriends?username=${aUsername}`)
.then( x => x.json());
console.log({...friendshipStatusFull})
};
const sendFriendRequest = async(aUser) => {
const sendFriendRequest = async(aUsername) => {
console.log('sending a friend request')
console.log(aUsername)
const resp = await fetch("http://transcendance:8080/api/v2/network/relations", {
method : "POST",
headers: { 'Content-Type': 'application/json'},
body: JSON.stringify({
"receiverUsername": aUser.username,
"receiverUsername": aUsername,
"status": "R"
})
})
.then( x => x.json())
fetchFriendshipFull(aUser.username);
// .catch( x => console.log({...x}))
// "status": "A"
console.log({...resp})
fetchFriendshipFull(aUsername);
};
const viewAUser = async(aUsername) => {
@@ -335,6 +340,7 @@
<Button type="secondary" on:click={() => sendFriendRequest(usernameBeingViewed)}>Add Friend</Button>
<Button on:click={() => blockANonFriendUser(usernameBeingViewed)}>Block User</Button>
{/if}
<Button type="secondary" on:click={() => sendFriendRequest(usernameBeingViewed)}>Add Friend</Button>
</div>
{:else}
<div class="placeholder">