public static WallPostsViewModel Create(Post p, ApplicationUser currentUser)
 {
     return new WallPostsViewModel()
     {
         Id = p.Id,
         Author = new UserViewModelMinified()
                      {
                          Id = p.OwnerId,
                          Gender = p.Owner.Gender,
                          Name = p.Owner.Name,
                          ProfileImageData = p.Owner.ProfileImageData,
                          Username = p.Owner.UserName
                      },
         WallOwner = new OwnerViewModel {Id = p.OwnerId, Username = p.Owner.UserName},
         PostContent = p.Content,
         Date = p.CreatedOn,
         LikesCount = p.Likes.Count,
         Liked = p.Likes
             .Any(l => l.UserId == currentUser.Id),
         TotalCommentsCount = p.Comments.Count,
         Comments = p.Comments
             .Take(3)
             .Select(c => CommentViewModel.CreateCommentAndUser(c, currentUser))
     };
 }
        public IHttpActionResult AddPost(int groupId, CreatePostBindingModel post)
        {
            var group = this.Data.Groups.All().SingleOrDefault(g => g.Id == groupId);
            if (group == null)
            {
                return this.BadRequest("Invalid group id");
            }

            if (post == null)
            {
                return this.BadRequest("Post model cannot be null");
            }

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

            var postOwnerId = this.User.Identity.GetUserId();
            var postOwner = this.Data.ApplicationUsers.All().FirstOrDefault(u => u.Id == postOwnerId);

            if (!group.Members.Any(m => m.Id == postOwnerId))
            {
                return this.BadRequest("You are not member of the group");
            }

            var postObj = new Post()
            {
                Content = post.Content,
                CreatedOn = DateTime.Now,
                Owner = postOwner,
                OwnerId = postOwnerId
            };

            group.Posts.Add(postObj);
            this.Data.SaveChanges();

            var viewModel = new PostViewModel()
            {
                Owner = new GroupUserViewModel()
                {
                    Id = postObj.OwnerId,
                    UserName = postObj.Owner.UserName
                },
                Content = postObj.Content
            };

            return this.Ok(viewModel);
        }
        public IHttpActionResult CreatePost(CreatePostBindingModel model)
        {
            if (model == null)
            {
                return this.BadRequest("Model cannot be null");
            }

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

            var wallOwner = this.Data.ApplicationUsers.All().FirstOrDefault(a => a.UserName == model.WallOwnerUsername);

            if (wallOwner == null)
            {
                return this.BadRequest("There is no user with Username " + model.WallOwnerUsername);
            }

            var currentUserId = this.User.Identity.GetUserId();
            var currentUser = this.Data.ApplicationUsers.All().FirstOrDefault(u => u.Id == currentUserId);

            bool isFriend = currentUser.Friends.Any(uf => uf.Id == wallOwner.Id);

            if (!isFriend && currentUserId != wallOwner.Id)
            {
                // cannot post on non friend wall
                return this.Unauthorized();
            }

            var post = new Post { Content = model.Content, OwnerId = currentUserId, WallOwnerId = wallOwner.Id, CreatedOn = DateTime.Now };

            this.Data.Posts.Add(post);
            this.Data.SaveChanges();
            return this.Ok(new { post.Id, post.Content, post.Owner.UserName });
        }