Exemplo n.º 1
0
        public async Task <IActionResult> BefriendUser(int id, int friendId)
        {
            if (id != int.Parse(User.FindFirst(ClaimTypes.NameIdentifier).Value))
            {
                return(Unauthorized());
            }

            var friend = await _repository.GetFriend(id, friendId);

            if (friend != null)
            {
                return(BadRequest("User is already a friend."));
            }

            if (await _repository.GetUser(friendId, false) == null)
            {
                return(NotFound());
            }

            friend = new Friend
            {
                FrienderId = id,
                FriendeeId = friendId
            };

            _repository.Add(friend);

            if (await _repository.SaveAll())
            {
                return(Ok());
            }

            return(BadRequest("Failed to add a friend"));
        }