Build a Twitter Clone With Adonis and Vue

Unfavoriting a Tweet

For one reason or the other, users might want to unfavorite a particular tweet they had favorited before. Let's give them the ability to do just that.

Add the code below to start/routes.js within the favorites routes group:

// start/routes.js

Route.delete('/destroy/:id', 'FavoriteController.unFavorite');

The route takes a tweet ID as a parameter.

Next, let's add the unFavorite method to FavoriteController:

// app/Controllers/Http/FavoriteController.js

async unFavorite ({ params, auth, response }) {
    // get currently authenticated user
    const user = auth.current.user

    // fetch favorite
    await Favorite.query()
        .where('user_id', user.id)
        .where('tweet_id', params.id)
        .delete()

    return response.json({
        status: 'success',
        data: null
    })
}

This simply does the opposite of the favorite method by deleting a record from the favorites table which matches the user's ID and specified Tweet ID.