Beispiel #1
0
        private async Task <IEnumerable <ProductIdeaItemViewModel> > GetFeaturedIdeas()
        {
            var profileId = !_signInManager.IsSignedIn(User) ? null : await _manager.GetPublicProfileIdByUserAsync(User);

            var hasUser = profileId.HasValue;
            var ideas   = await _context
                          .ProductIdeas
                          .AsNoTracking()
                          .Select(i => new
            {
                VM = new ProductIdeaItemViewModel
                {
                    Id      = i.Id,
                    Content = i.Content,
                    Author  = new PublicProfileViewModel
                    {
                        DisplayName = i.Author.DisplayName,
                        Id          = i.AuthorPublicProfileId
                    },
                    PublishDate = i.PublishDate,
                    TotalVotes  = i.Votes.Any() ? i.Votes.Sum(v => v.Direction) : 0,    // prevent NULL values
                    IsFromUser  = i.AuthorPublicProfileId == profileId
                },
                TimeBatch       = Math.Floor(EF.Functions.DateDiffHour(i.PublishDate, DateTime.Now) / 25d),
                CurrentUserVote = i.Votes.Where(v => hasUser && v.IssuerPublicProfileId == profileId).Select(v => v.Direction).ToList()
            })
                          .OrderBy(i => i.TimeBatch)
                          .ThenByDescending(i => i.VM.TotalVotes)
                          .ThenByDescending(i => i.VM.PublishDate).ToListAsync();

            if (!hasUser)
            {
                return(ideas.Select(i => i.VM).ToList());
            }
            foreach (var idea in ideas)
            {
                idea.VM.VoteDirection = idea.CurrentUserVote.FirstOrDefault();
            }

            return(ideas.Select(i => i.VM).ToList());
        }
Beispiel #2
0
        public async Task <IActionResult> DeleteIdea(int id)
        {
            if (!_signInManager.IsSignedIn(User))
            {
                return(Unauthorized());
            }

            var idea = await _context.ProductIdeas.FindAsync(id);

            if (idea is null)
            {
                return(NotFound());
            }

            if (idea.AuthorPublicProfileId != await _userManager.GetPublicProfileIdByUserAsync(User))
            {
                return(Unauthorized());
            }

            _context.ProductIdeas.Remove(idea);
            await _context.SaveChangesAsync();

            return(Ok());
        }