davidcr01 / WordlePlus

Repository to store all the documentation, files and structure of my Final Degree Project (TFG in Spanish). The main goal is to develop, as full-stack web developer, a remodel of the Wordle game, including more features and functionalities using Ionic, Django REST Api and PostgreSQL.
1 stars 0 forks source link

Delete friend and friend request (S11) #55

Closed davidcr01 closed 1 year ago

davidcr01 commented 1 year ago

Description

Is necessary to implement a new functionality to remove a friend and reject friend requests.

Tasks

davidcr01 commented 1 year ago

Update Report

Backend

To allow the deletion of friends, is necessary to edit the viewset of the FriendList class.

def destroy(self, request, *args, **kwargs):
        player = getattr(request.user, 'player', None)
        friend_id = kwargs.get('pk')

        if not player:
            return Response({'error': 'Player not found'}, status=404)

        try:
            friend = Player.objects.get(id=friend_id)
        except Player.DoesNotExist:
            return Response({'error': 'Friend not found'}, status=404)

        if not FriendList.objects.filter(Q(sender=player, receiver=friend) | Q(sender=friend, receiver=player)).exists():
            return Response({'error': 'Player is not friends with this user'}, status=403)

        # Delete the friend relationship
        FriendList.objects.filter(Q(sender=player, receiver=friend) | Q(sender=friend, receiver=player)).delete()

        return Response({'message': 'Friend relationship deleted successfully'}, status=204)

Notice that the method checks if both players exist and if the relationship exists too. An example of deleting the relationship between the identified player and the player with ID=1 is the following:

image

Error case (deleting the same friendship again): image

davidcr01 commented 1 year ago

Update Report

Frontend

The friendlistpage has been modified, adding the method confirmDeletePlayer and deletePlayer.

async confirmDeleteFriend(friendId: number) {
    console.log(friendId);
    const alert = await this.alertController.create({
      header: 'Confirm',
      message: 'Are you sure you want to delete this friend?',
      buttons: [
        {
          text: 'Cancel',
          role: 'cancel',
          cssClass: 'secondary',
        },
        {
          text: 'Delete',
          handler: () => {
            this.deleteFriend(friendId);
          },
        },
      ],
    });

    await alert.present();
  }

  async deleteFriend(friendId: number) {
    console.log(friendId);
    (await this.apiService.deleteFriend(friendId)).subscribe(
      async (response) => {
        console.log('Friend request accepted successfully', response);
        this.loadFriendList();
        this.toastService.showToast('Player was deleted succesfully.', 2000, 'top', 'success');
      },
      async (error) => {
        console.error('Error accepting friend request:', error);
        let message = "Error sending request";
        this.toastService.showToast('Player could not be deleted.', 2000, 'top', 'danger');
      }
    );    
  }

Besides, the rejectFriendRequest is also implemented. This method is the same as acceptFriendRequest but using the rejectFriendRequest apiService method:

async rejectFriendRequest(requestId: number) {
    (await this.apiService.rejectFriendRequest(requestId)).subscribe(
      async (response) => {
        console.log('Friend request rejected successfully', response);
        this.loadFriendRequests();
      },
      async (error) => {
        console.error('Error rejecting friend request:', error);
      }
    );
  }

An alert is displayed when the player wants to delete a friend: image

After the confirmation, the friend is deleted and the friend list is reloaded. image