Esempio n. 1
0
        public IEnumerable<PostViewModel> BuildFeed()
        {
            if (null == _postViews)
            {
                _postViews = new List<PostViewModel>();
                var feeders = _user.Friends;
                feeders.Add(_user.Id);

                var posts = new List<Post>();
                foreach (var feeder in feeders)
                {
                    posts.AddRange(_postRepository.ByUser(feeder));
                }

                var users = ParseUniqueUser(posts);

                foreach (var post in posts)
                {
                    var view = new PostViewModel
                    {
                        Id = post.Id,
                        Message = post.Message,
                        PostedBy = users[post.PostedBy],
                        PostedOn = post.PostedOn,
                        LikedBy = post.LikedBy.Select(s => users[s]).ToList()
                    };
                    view.LikeCount = view.LikedBy.Count;

                    var vComments = post.Comments.Select(comment => new CommentViewModel
                    {
                        CommentedOn = comment.CommentedOn.ToLocalTime(),
                        Description = comment.Description,
                        Id = comment.Id,
                        CommentedBy = users[comment.CommentedBy]
                    }).ToList();
                    view.Comments = vComments;

                    view.LikedByMe = post.LikedBy.Any(u => u.Equals(_user.Id));

                    _postViews.Add(view);
                }

                _postViews = _postViews.OrderByDescending(model => model.PostedOn).Take(30).ToList();
            }

            return _postViews;
        }
Esempio n. 2
0
        // POST api/post
        public IHttpActionResult Post(PostBindingModel post)
        {
            if (!ModelState.IsValid)
            {
                return BadRequest(ModelState);
            }

            var postedBy = _userRepository.FindById(User.Identity.GetUserId());

            var model = new Post()
            {
                Message = post.Message,
                PostedBy = postedBy.Id,
                PostedOn = DateTime.UtcNow,
                LikedBy = new List<string>(),
                Comments = new List<Comment>()
            };
            model.PostedBy = User.Identity.GetUserId();
            _postRepository.Insert(model);

            var vModel = new PostViewModel
            {
                Id = model.Id,
                Message = post.Message,
                PostedBy = AutoMapper.Mapper.Map<ApplicationUser, ApplicationUserViewModel>(postedBy),
                LikeCount = model.LikeCount,
                LikedBy = new List<ApplicationUserViewModel>(),
                Comments = new List<CommentViewModel>()
            };

            return Ok(vModel);
        }