Like functionality in the API


Open IDatingRepository and add new signature for GetLike.

Task<Like> GetLike(int userId, int recipientId);

Open DatingRepository and implement GetLike.

public async Task<Like> GetLike(int userId, int recipientId)
    return await _context.Likes
      .FirstOrDefaultAsync(x => x.LikerId == userId && x.LikeeId == recipientId);


Open UsersController and add a new POST method LikeUser

public async Task<IActionResult> LikeUser(int id, int recipientId) {
    var currentUserId = int.Parse(User.FindFirst(ClaimTypes.NameIdentifier).Value);
    if (id != currentUserId)
        return Unauthorized();

    var existingLike = await _repo.GetLike(id, recipientId);
    if (existingLike != null)
        return BadRequest("You already like this user");

    if(await _repo.GetUser(recipientId) == null) {
        return NotFound();

    var like = new Like { LikerId = id, LikeeId = recipientId};


    if (await _repo.SaveAll())
        return Ok();

    return BadRequest();

Test in Postman

Create a new POST to this url


To create a new Like from the user 1 to the user 12.

The first time we send the request we will receive a 200 and if we send the request again we will receive a 400.