Skip to content

Commit

Permalink
Configure API to allow favoriting other users
Browse files Browse the repository at this point in the history
  • Loading branch information
marvac committed Feb 22, 2019
1 parent 51c3a4b commit f6b5fb7
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 2 deletions.
33 changes: 33 additions & 0 deletions Controllers/UsersController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -75,5 +75,38 @@ public async Task<IActionResult> UpdateUser(int userId, UpdateUserResource userR

throw new Exception($"Updating user ID {userId} failed to save");
}

[HttpPost("{userId}/like/{recipientId}")]
public async Task<IActionResult> LikeUser(int userId, int recipientId)
{
int id = int.Parse(User.FindFirst(ClaimTypes.NameIdentifier).Value);

var like = await _repo.GetLike(userId, recipientId);

if (like != null)
{
return BadRequest("This person was already liked by you");
}

if (_repo.GetUser(recipientId) == null)
{
return NotFound("The user you tried to like doesn't exist");
}

like = new Like
{
LikerId = userId,
LikeeId = recipientId
};

_repo.Add(like);

if (await _repo.SaveChangesAsync())
{
return Ok(like);
}

return BadRequest("Failed to like this person");
}
}
}
7 changes: 5 additions & 2 deletions Data/FriendRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -75,11 +75,14 @@ public async Task<PagedList<User>> GetUsers(UserParameters parameters)
return await PagedList<User>.CreateAsync(users, parameters.PageNumber, parameters.PageSize);
}

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

public async Task<bool> SaveChangesAsync()
{
return await _context.SaveChangesAsync() > 0;
}


}
}
2 changes: 2 additions & 0 deletions Data/IFriendRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,7 @@ public interface IFriendRepository
Task<User> GetUser(int userId);
Task<Photo> GetPhoto(int photoId);
Task<Photo> GetMainPhoto(int userId);
Task<Like> GetLike(int userId, int recipientId);

}
}

0 comments on commit f6b5fb7

Please sign in to comment.