public static FriendRequestViewModel Create(FriendRequest request)
 {
     return new FriendRequestViewModel()
     {
         Id = request.Id,
         Status = request.Status,
         User = UserViewModelMinified.Create(request.From)
     };
 }
        public IHttpActionResult SendFriendRequest(string username)
        {
            var recipient = this.SocialNetworkData.Users.All()
                .FirstOrDefault(u => u.UserName == username);
            if (recipient == null)
            {
                return this.NotFound();
            }

            var loggedUserId = this.User.Identity.GetUserId();
            if (loggedUserId == null)
            {
                return this.BadRequest("Invalid session token.");
            }

            var loggedUser = this.SocialNetworkData.Users.GetById(loggedUserId);
            if (username == loggedUser.UserName)
            {
                return this.BadRequest("Cannot send request to self.");
            }

            bool isAlreadyFriend = loggedUser.Friends
                .Any(fr => fr.UserName == recipient.UserName);
            if (isAlreadyFriend)
            {
                return this.BadRequest("User is already in friends.");
            }

            bool hasReceivedRequest = loggedUser.FriendRequests
                .Any(r => r.FromId == recipient.Id && r.Status == FriendRequestStatus.Pending);
            bool hasSentRequest = recipient.FriendRequests
                .Any(r => r.FromId == loggedUser.Id && r.Status == FriendRequestStatus.Pending);
            if (hasSentRequest || hasReceivedRequest)
            {
                return this.BadRequest("A pending request already exists.");
            }

            var friendRequest = new FriendRequest()
            {
                From = loggedUser,
                To = recipient
            };

            recipient.FriendRequests.Add(friendRequest);
            this.SocialNetworkData.SaveChanges();

            return this.Ok(new
            {
                message = string.Format(
                    "Friend request successfully sent to {0}.", recipient.Name)
            });
        }
        public IHttpActionResult SendFriendRequests(string username)
        {
            var currentUserId = this.UserIdProvider.GetUserId();
            var existingUser = this.Data
                .Users
                .All()
                .FirstOrDefault(c => c.Name == username);
            if (existingUser == null)
            {
                return this.NotFound();
            }

            FriendRequest friendRequest = new FriendRequest
            {
                FriendRequestStatus = FriendRequestStatus.AwaitingApproval,
                FromUserId = currentUserId,
                ToUserId = existingUser.Id,
            };

            existingUser.FriendRequests.Add(friendRequest);
            this.Data.SaveChanges();

            return this.Ok();
        }