Ejemplo n.º 1
0
        public IHttpActionResult Get(int id)
        {
            var item = _db.Entries.Include(x => x.Votes).Include(x => x.Comments).SingleOrDefault(x => x.Id == id && x.Status != EntryStatus.Trash && x.Visibility != EntryVisibility.Hidden);

            if (item == null)
            {
                return(NotFound());
            }

            var result = new EntryView
            {
                Id         = item.Id,
                Author     = _userContext.GetUser(UserHelper.GetUserNameFromComplexUsername(item.Author)).Name,
                Title      = item.Title,
                Message    = item.Message,
                Vote       = EntryHelper.GetVotes(item.Votes),
                VoteResult = EntryHelper.UserVoteResult(User.Identity, item.Votes),
                Comments   = item.Comments != null ? item.Comments.Count : 0,
                Status     = item.Status.GetEnumDescription(),
                CreateDate = item.CreateDate.ToString("yyyy-MM-dd HH:mm"),
                UpdateDate = item.UpdateDate.HasValue ? item.UpdateDate.Value.ToString("yyyy-MM-dd HH:mm") : ""
            };

            return(Ok(result));
        }
Ejemplo n.º 2
0
        public async Task <IHttpActionResult> PostVote(int id, VoteView voteView)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            var entry = _db.Entries.Include(x => x.Votes).Include(x => x.User).SingleOrDefault(x => x.Id == id);

            if (entry == null)
            {
                return(NotFound());
            }

            var userVoteResult = EntryHelper.UserVoteResult(User.Identity, entry.Votes);

            if (userVoteResult != null)
            {
                return(BadRequest());
            }

            var userName = UserHelper.GetUserNameFromIdentity(User.Identity);
            var user     = _userContext.GetUser(userName);

            var vote = new Vote
            {
                Author     = UserHelper.GetUserNameFromIdentity(User.Identity),
                CreateDate = DateTime.Now,
                Entry      = entry,
                IsPositive = voteView.IsPositive,
                UserId     = user.Id
            };

            _db.Votes.Add(vote);
            await _db.SaveChangesAsync();

            _mailer.InformAboutVote(entry);

            var location = $"api/entry/{vote.Entry.Id}";

            return(Created(location, new VoteView
            {
                Id = vote.Id,
                Author = _userContext.GetUser(UserHelper.GetUserNameFromComplexUsername(vote.Author)).Name,
                CreateDate = vote.CreateDate,
                IsPositive = voteView.IsPositive
            }));
        }
Ejemplo n.º 3
0
        public IHttpActionResult Get()
        {
            List <Entry> entries = _db.Entries.Include(x => x.Votes).Include(x => x.Comments).ToList();

            if (!entries.Any(x => x.Status != EntryStatus.Trash && x.Visibility != EntryVisibility.Hidden))
            {
                return(Ok(new List <int>()));
            }

            var user = _userContext.GetUser(UserHelper.GetUserNameFromIdentity(User.Identity));

            if (!user.IsModerator)
            {
                entries = entries.Where(
                    x =>
                    x.Status != EntryStatus.AwaitingApproval || (x.Status == EntryStatus.AwaitingApproval &&
                                                                 UserHelper.GetUserNameFromComplexUsername(x.Author) ==
                                                                 UserHelper.GetUserNameFromIdentity(User.Identity))).ToList();
            }

            List <EntryView> result = entries
                                      .Where(x => x.Status != EntryStatus.Trash && x.Visibility != EntryVisibility.Hidden)
                                      .Select(x =>
            {
                var author = _userContext.GetUser(UserHelper.GetUserNameFromComplexUsername(x.Author));
                return(new EntryView
                {
                    Id = x.Id,
                    Author = author.Name,
                    Title = x.Title,
                    Message = x.Message,
                    Vote = EntryHelper.GetVotes(x.Votes),
                    VoteResult = EntryHelper.UserVoteResult(User.Identity, x.Votes),
                    Comments = x.Comments != null ? x.Comments.Count : 0,
                    Status = x.Status.GetEnumDescription(),
                    CreateDate = x.CreateDate.ToString("yyyy-MM-dd HH:mm"),
                    UpdateDate = x.UpdateDate.HasValue ? x.UpdateDate.Value.ToString("yyyy-MM-dd HH:mm") : ""
                });
            }).ToList();

            return(Ok(result));
        }