public static Expression<Func<Comment, CommentViewModel>> Create(ApplicationUser currentUser)
 {
     string currentUserId = currentUser.Id;
     return comment => new CommentViewModel
     {
         Id = comment.Id,
         Author = new UserViewModelMinified
         {
             Id = comment.AuthorId,
             Name = comment.Author.Name,
             Username = comment.Author.UserName,
             IsFriend = comment.Author.Friends.Any(f => f.Id == currentUserId),
             Gender = comment.Author.Gender,
             ProfileImageData = comment.Author.ProfileImageData,
         },
         Date = comment.PostedOn,
         CommentContent = comment.Content,
         LikesCount = comment.Likes.Count,
         Liked = comment.Likes.Any(l => l.UserId == currentUserId),
         CommentReplies = comment.Replies
             .AsQueryable()
             .OrderByDescending(cr => cr.RepliedOn)
             .Select(CommentReplyViewModel.Create(currentUser))
     };
 }
 public static Expression<Func<Post, PostViewModel>> Create(ApplicationUser currentUser)
 {
     string currentUserId = currentUser.Id;
     return post => new PostViewModel
     {
         Id = post.Id,
         Author = new UserViewModelMinified
         {
             Id = post.AuthorId,
             Name = post.Author.Name,
             Username = post.Author.UserName,
             IsFriend = post.Author.Friends.Any(f => f.Id == currentUserId),
             Gender = post.Author.Gender,
             ProfileImageData = post.Author.ProfileImageData,
         },
         WallOwner = new UserViewModelMinified
         {
             Id = post.WallOwnerId,
             Name = post.WallOwner.Name,
             Username = post.WallOwner.UserName,
             IsFriend = post.Author.Friends.Any(f => f.Id == currentUserId),
             Gender = post.WallOwner.Gender,
             ProfileImageData = post.WallOwner.ProfileImageData,
         },
         PostContent = post.Content,
         Date = post.PostedOn,
         LikesCount = post.Likes.Count,
         Liked = post.Likes.Any(l => l.UserId == currentUserId),
         TotalCommentsCount = post.Comments.Count,
         Comments = post.Comments
             .OrderByDescending(c => c.PostedOn)
             .AsQueryable()
             .Select(CommentViewModel.Create(currentUser))
     };
 }
 public static Expression<Func<Photo, PhotoViewModel>> Create(ApplicationUser currentUser)
 {
     string currentUserId = currentUser.Id;
     return photo => new PhotoViewModel
     {
         Id = photo.Id,
         Image = photo.Source,
         Description = photo.Description,
         PostedOn = photo.PostedOn,
         LikesCount = photo.Likes.Count,
         Liked = photo.Likes.Any(l => l.UserId == currentUserId),
         CommentsCount = photo.Comments.Count,
         Owner = new UserViewModelMinified
         {
             Id = photo.PhotoOwner.Id,
             Name = photo.PhotoOwner.Name,
             Username = photo.PhotoOwner.UserName,
             IsFriend = photo.PhotoOwner.Friends.Any(f => f.Id == currentUserId),
             Gender = photo.PhotoOwner.Gender,
             ProfileImageData = photo.PhotoOwner.ProfileImageData,
         },
         Comments = photo.Comments
             .AsQueryable()
             .Select(CommentViewModel.Create(currentUser))
     };
 }
 public FriendViewModel(ApplicationUser friend)
 {
     this.Id = friend.Id;
     this.Username = friend.UserName;
     this.Name = friend.Name;
     this.Image = friend.ProfilePicture;
 }
 public ProfileViewModel(ApplicationUser appUser)
 {
     userName = appUser.UserName;
     name = appUser.Name;
     gender = (int)appUser.Gender;
     email = appUser.Email;
     profileImageData = appUser.ProfilePicture;
     coverImageData = appUser.WallPicture;
     friendsCount = appUser.Friends.Count;
 }
 public static UserViewModelMinified Create(ApplicationUser user)
 {
     return new UserViewModelMinified()
     {
         Id = user.Id,
         Name = user.Name,
         Username = user.UserName,
         Gender = user.Gender,
         ProfileImageData = user.ProfileImageDataMinified
     };
 }
 public static Expression<Func<ApplicationUser, UserViewModelMinified>> Create(ApplicationUser user)
 {
     return userViewModel => new UserViewModelMinified
     {
         Id = userViewModel.Id,
         Name = userViewModel.Name,
         Username = userViewModel.UserName,
         IsFriend = userViewModel.Friends.Any(f => f.Id == user.Id),
         Gender = userViewModel.Gender,
         ProfileImageData = userViewModel.ProfileImageData
     };
 }
 public static CommentViewModel Create(Comment c, ApplicationUser currentUser)
 {
     return new CommentViewModel()
     {
         Id = c.Id,
         Author = UserViewModelMinified.Create(c.Author),
         LikesCount = c.Likes.Count,
         CommentContent = c.Content,
         Date = c.Date,
         Liked = c.Likes
             .Any(l => l.UserId == currentUser.Id)
     };
 }
 public static object Create(ApplicationUser user)
 {
     return new
     {
         totalCount = user.Friends.Count,
         friends = user.Friends.Select(u => new
             {
                 Id = u.Id,
                 UserName = u.UserName,
                 Name = u.Name,
                 Image = u.ProfilePicture
             })
     };
 }
 public static object CreatePreview(ApplicationUser user, Comment comment)
 {
     return new
     {
         Id = comment.Id,
         AuthorId = comment.Author.Id,
         AuthorUsername = comment.Author.UserName,
         AuthorProfileImage = comment.Author.ProfilePicture,
         CommentContent = comment.Content,
         Date = comment.PostedOn,
         LikesCount = comment.Likes.Count,
         Liked = comment.Likes.Any(l => l.Author.Id == user.Id)
     };
 }
 public static PreviewUserDataViewModel GetPreviewUserData(ApplicationUser user, ApplicationUser currentUser)
 {
     return new PreviewUserDataViewModel()
     {
         Id = user.Id,
         UserName = user.UserName,
         Name = user.Name,
         IsFriend = user.Friends.Contains(currentUser).ToString().ToLower(),
         ProfileImageData = user.ProfilePicture,
         HasPendingRequest = user.Requests
             .Any(r => r.Sender == currentUser && r.Status == FriendRequestStatus.Pending)
             .ToString().ToLower()
     };
 }
        protected bool HasAccessToPost(ApplicationUser user, Post post)
        {
            if (post.AuthorId != user.Id && post.WallOwnerId != user.Id)
            {
                bool postAuthorOrWallOwnerIsFriend = user.Friends
                    .Any(fr => fr.Id == post.AuthorId || fr.Id == post.WallOwnerId);

                if (!postAuthorOrWallOwnerIsFriend)
                {
                    return false;
                }
            }

            return true;
        }
 public static UserViewModelPreview Create(ApplicationUser user, ApplicationUser loggedUser)
 {
     return new UserViewModelPreview()
     {
         Id = user.Id,
         Name = user.Name,
         Username = user.UserName,
         Gender = user.Gender,
         ProfileImageData = user.ProfileImageDataMinified,
         IsFriend = user.Friends
            .Any(fr => fr.Id == loggedUser.Id),
         HasPendingRequest = user.FriendRequests
             .Any(r => r.Status == FriendRequestStatus.Pending &&
                 (r.FromId == loggedUser.Id || r.ToId == loggedUser.Id))
     };
 }
        public async Task<IHttpActionResult> RegisterUser(RegisterUserBindingModel model)
        {
            if (this.User.Identity.GetUserId() != null)
            {
                return this.BadRequest("User is already logged in.");
            }

            if (model == null)
            {
                return this.BadRequest("Invalid user data.");
            }

            if (!this.ModelState.IsValid)
            {
                return this.BadRequest(this.ModelState);
            }

            var emailExists = this.SocialNetworkData.Users.All()
                .Any(x => x.Email == model.Email);
            if (emailExists)
            {
                return this.BadRequest("Email is already taken.");
            }

            var user = new ApplicationUser()
            {
                UserName = model.Username,
                Name = model.Name,
                Email = model.Email,
                Gender = model.Gender
            };

            var identityResult = await this.UserManager.CreateAsync(user, model.Password);

            if (!identityResult.Succeeded)
            {
                return this.GetErrorResult(identityResult);
            }

            var loginResult = await this.LoginUser(new LoginUserBindingModel()
            {
                Username = model.Username,
                Password = model.Password
            });

            return loginResult;
        }
 public static Expression<Func<FriendRequest, FriendRequestViewModel>> Create(ApplicationUser currentUser)
 {
     return request =>  new FriendRequestViewModel()
     {
         Id = request.Id,
         Status = request.FriendRequestStatus,
         User = new UserViewModelMinified
         {
             Id = request.FromUserId,
             Name = request.FromUser.Name,
             Username = request.FromUser.UserName,
             IsFriend = request.FromUser.Friends.Any(f => f.Id == currentUser.Id),
             Gender = request.FromUser.Gender,
             ProfileImageData = request.FromUser.ProfileImageData
         }
     };
 }
        //   public bool IsFriend { get; set; }
        //  public UserGender Gender { get; set; }
        //   public string WallPicture { get; set; }
        //  public bool HasPendingRequest { get; set; }
        //  public List<Group> Groups { get; set; }
        public static PreviewUserDataViewModel GetPreviewUserData(ApplicationUser user, ApplicationUser currentUser)
        {
            return new PreviewUserDataViewModel()
            {
                Id = user.Id,
                UserName = user.UserName,
                //  Name = user.Name,
                //  IsFriend = user.Friends.Contains(currentUser),
                ProfileImage = user.ProfilePicture,
                //Gener = user.Gender,
                //WallPicture = user.WallPicture,
                //HasPendingRequest = user.Requests
                //    .Any(r => r.Status == FriendRequestStatus.Pending &&
                //        (r.Sender.Id == currentUser.Id || r.Recipient.Id == currentUser.Id)),
                //GROUPS TO DO WITH GROUPS VIEW MODEL

            };
        }
 public static PostViewModel Create(Post p, ApplicationUser currentUser)
 {
     return new PostViewModel()
     {
         Id = p.Id,
         Author = UserViewModelPreview.Create(p.Author, currentUser),
         WallOwner = UserViewModelPreview.Create(p.WallOwner, currentUser),
         PostContent = p.Content,
         Date = p.Date,
         LikesCount = p.Likes.Count,
         Liked = p.Likes
             .Any(l => l.UserId == currentUser.Id),
         TotalCommentsCount = p.Comments.Count,
         Comments = p.Comments
             .Reverse()
             .Take(3)
             .Select(c => CommentViewModel.Create(c, currentUser))
     };
 }
 public static Expression<Func<Group, GroupViewModel>> Create(ApplicationUser currentUser)
 {
     return group => new GroupViewModel
     {
         Id = group.Id,
         Name = group.Name,
         CoverImageData = group.CoverImageData,
         CreatedOn = group.CreatedOn,
         Description = group.Description,
         Admins = group.Admins
             .AsQueryable()
             .Select(UserViewModelMinified.Create(currentUser)),
         Members = group.Members
             .AsQueryable()
             .Select(UserViewModelMinified.Create(currentUser)),
         Posts = group.Posts
             .AsQueryable()
             .Select(GroupPostViewModel.Create(currentUser))
     };
 }
        public static UserViewModel ConvertTo(ApplicationUser user, ApplicationUser currentUser)
        {
            UserViewModel wantedUser = new UserViewModel
            {
                Id = user.Id,
                Name = user.Name,
                Username = user.UserName,
                Gender = user.Gender,
                ProfileImageData = user.ProfileImageData,
                CoverImageData = user.CoverImageData,
                IsFriend = user.Friends.Any(f => f.Id == currentUser.Id),
                HasPendingRequest = user.FriendRequests.Any(
                    r => r.FriendRequestStatus == FriendRequestStatus.AwaitingApproval &&
                    (r.FromUserId == currentUser.Id)),
                Groups = user.Groups
                    .AsQueryable()
                    .Select(GroupViewModelPreview.Create)
            };

            return wantedUser;
        }
 public static Expression<Func<CommentReply, CommentReplyViewModel>> Create(ApplicationUser currentUser)
 {
     string currentUserId = currentUser.Id;
     return comment => new CommentReplyViewModel
     {
         Id = comment.Id,
         Author = new UserViewModelMinified
         {
             Id = comment.Author.Id,
             Name = comment.Author.Name,
             Username = comment.Author.UserName,
             IsFriend = comment.Author.Friends.Any(f => f.Id == currentUserId),
             Gender = comment.Author.Gender,
             ProfileImageData = comment.Author.ProfileImageData,
         },
         Date = comment.RepliedOn,
         CommentContent = comment.Content,
         LikesCount = comment.Likes.Count,
         Liked = comment.Likes.Any(l => l.UserId == currentUserId)
     };
 }
        public static GroupViewModel ConvertTo(Group group, ApplicationUser currentUser)
        {
            GroupViewModel viewModel = new GroupViewModel
            {
                Id = group.Id,
                Name = group.Name,
                CoverImageData = group.CoverImageData,
                CreatedOn = group.CreatedOn,
                Description = group.Description,
                Admins = group.Admins
                    .AsQueryable()
                    .Select(UserViewModelMinified.Create(currentUser)),
                Members = group.Members
                    .AsQueryable()
                    .Select(UserViewModelMinified.Create(currentUser)),
                Posts = group.Posts
                    .AsQueryable()
                    .Select(GroupPostViewModel.Create(currentUser))
            };

            return viewModel;
        }
        public static AddPostViewModel ConvertTo(Post post, ApplicationUser currentUser)
        {
            AddPostViewModel postViewModel = new AddPostViewModel
            {
                Id = post.Id,
                Author = new UserViewModelMinified
                {
                    Id = post.AuthorId,
                    Name = post.Author.Name,
                    Username = post.Author.UserName,
                    IsFriend = post.Author.Friends.Any(f => f.Id == currentUser.Id),
                    Gender = post.Author.Gender,
                    ProfileImageData = post.Author.ProfileImageData,
                },
                WallOwner = new UserViewModelMinified
                {
                    Id = post.WallOwnerId,
                    Name = post.WallOwner.Name,
                    Username = post.WallOwner.UserName,
                    IsFriend = post.WallOwner.Friends.Any(f => f.Id == currentUser.Id),
                    Gender = post.WallOwner.Gender,
                    ProfileImageData = post.WallOwner.ProfileImageData,
                },
                PostContent = post.Content,
                PostedOn = post.PostedOn,
                LikesCount = post.Likes.Count,
                Liked = post.Likes.Any(l => l.UserId == currentUser.Id),
                TotalCommentsCount = post.Comments.Count,
                Comments = post.Comments
                    .Reverse()
                    .Take(4)
                    .AsQueryable()
                    .Select(CommentViewModel.Create(currentUser))
            };

            return postViewModel;
        }
 public static object CreateGroupPreview(ApplicationUser user, Group group)
 {
     return new
     {
         Id = group.Id,
         Name = group.Name,
         Description = group.Description,
         WallPicture = group.WallPicture,
         Owner = new
         {
             Name = group.Owner.Name,
             Username = group.Owner.UserName,
             Id = group.Owner.Id
         },
         Members = group.Members.Select(m => new
         {
             Name = m.Name,
             Username = m.UserName,
             Id = m.Id
         }),
         IsMember = group.Members.Any(m => m == user),
         IsOwner = group.Owner == user
     };
 }
 private bool HasAuthorization(ApplicationUser currentUser, Post post)
 {
     return !currentUser.Friends.Contains(post.Author) &&
            !currentUser.Friends.Contains(post.WallOwner) &&
            post.WallOwnerId != currentUser.Id;
 }
        private MessageViewModel DenyFriendRequest(FriendshipRequest request, ApplicationUser me)
        {
            request.Status = FriendRequestStatus.Rejected;

            this.Data.SaveChanges();

            return new MessageViewModel("Friend request successfully declined.");
        }
        private MessageViewModel ApproveFriendRequest(FriendshipRequest request, ApplicationUser me)
        {
            request.Status = (FriendRequestStatus)1;
            me.Friends.Add(request.Sender);

            this.Data.SaveChanges();

            return new MessageViewModel("Friend request successfully approved.");
        }
 public static object CreateGroupPostPreview(ApplicationUser currentUser, GroupPost post)
 {
     return new
     {
         Id = post.Id,
         AuthorId = post.Author.Id,
         AuthorUsername = post.Author.UserName,
         AuthorProfileImage = post.Author.ProfilePicture,
         WallOwnerId = post.Owner.Id,
         PostContent = post.Content,
         Date = post.PostedOn,
         LikesCount = post.Likes.Count,
         Liked = post.Likes.Any(l => l.Author == currentUser),
         TotalCommentsCount = post.Comments.Count,
         Comments = post.Comments
             .OrderByDescending(c => c.PostedOn)
             .AsQueryable()
             .Select(c => CommentViewModel.CreatePreview(currentUser, c))
             .ToList()
     };
 }
        public async Task<IHttpActionResult> Register(RegisterBindingModel model)
        {
            if (!ModelState.IsValid)
            {
                return BadRequest(ModelState);
            }

            var user = new ApplicationUser()
            { UserName = model.Username, Email = model.Email };

            IdentityResult result = await UserManager.CreateAsync(user, model.Password);

            if (!result.Succeeded)
            {
                return GetErrorResult(result);
            }

            return Ok();
        }
        public async Task<IHttpActionResult> RegisterExternal(RegisterExternalBindingModel model)
        {
            if (!ModelState.IsValid)
            {
                return BadRequest(ModelState);
            }

            var info = await Authentication.GetExternalLoginInfoAsync();
            if (info == null)
            {
                return InternalServerError();
            }

            var user = new ApplicationUser()
            { UserName = model.Email, Email = model.Email };

            IdentityResult result = await UserManager.CreateAsync(user);
            if (!result.Succeeded)
            {
                return GetErrorResult(result);
            }

            result = await UserManager.AddLoginAsync(user.Id, info.Login);
            if (!result.Succeeded)
            {
                return GetErrorResult(result); 
            }
            return Ok();
        }
        public async Task<HttpResponseMessage> RegisterUser(RegisterBindingModel model)
        {
            if (!ModelState.IsValid)
            {
                return await this.BadRequest(this.ModelState).ExecuteAsync(new CancellationToken());
            }

            var user = new ApplicationUser()
            {
                UserName = model.Username,
                Name = model.Name,
                Gender = model.Gender,
                Email = model.Email
            };

            IdentityResult result = await this.UserManager.CreateAsync(user, model.Password);

            if (!result.Succeeded)
            {
                return await this.GetErrorResult(result).ExecuteAsync(new CancellationToken());
            }

            // Auto login after register (successful user registration should return access_token)
            var loginResult = this.LoginUser(new LoginUserBindingModel()
            {
                Username = model.Username,
                Password = model.Password
            });
            return await loginResult;
        }