예제 #1
0
        public async Task <IActionResult> OnPost()
        {
            if (!ModelState.IsValid)
            {
                return(Page());
            }

            var profile = await _userManager.GetPublicProfileByUserAsync(User);

            if (profile is null)
            {
                return(RedirectToPage("/Account/Login", new { area = "Identity" }));
            }

            var idea = new ProductIdea
            {
                Author      = profile,
                Content     = Input.Content,
                PublishDate = DateTime.Now
            };
            await _context.ProductIdeas.AddAsync(idea);

            await _context.SaveChangesAsync();

            return(RedirectToPage("Index", new { refresh = true }));
        }
예제 #2
0
        public async Task <IActionResult> Submit([FromForm] MessageRequestViewModel messageRequest)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest());
            }

            PublicProfile profile = null;

            if (!messageRequest.IsAnonymous && _signInManager.IsSignedIn(User))
            {
                profile = await _userManager.GetPublicProfileByUserAsync(User);
            }
            var model = new Message
            {
                Author      = profile,
                Content     = messageRequest.Content,
                PublishDate = DateTime.Now
            };

            if (profile != null)
            {
                var baseValue      = ReviewSubmitExperienceReward;
                var recentMessages = await GetRecentMessagesCountAsync(profile) * SubsequentReviewModifier;

                baseValue = Math.Max(baseValue - recentMessages, 0);
                if (baseValue > 0)
                {
                    var realXpGained = profile.AddExperience(baseValue);
                    if (realXpGained > 0)
                    {
                        ViewBag.HasExperience    = true;
                        ViewBag.GainedExperience = baseValue;
                    }
                }
            }
            await _context.WebsiteReviews.AddAsync(model);

            await _context.SaveChangesAsync();

            _cache.Remove(Cache.LastReviews); // refresh the cache
            ViewBag.IsRecent = true;
            return(PartialView("_ReviewPartial", model));
        }
        public async Task <IActionResult> OnGet()
        {
            var profile = await _userManager.GetPublicProfileByUserAsync(User);

            if (profile is null)
            {
                return(NotFound());
            }
            Profile = profile;
            Input   = new InputModel
            {
                DisplayName = profile.DisplayName
            };
            return(Page());
        }
예제 #4
0
        private async Task <IActionResult> CreateVote(int id, int direction)
        {
            if (!_signInManager.IsSignedIn(User))
            {
                return(Unauthorized());
            }

            var profile = await _userManager.GetPublicProfileByUserAsync(User);

            if (profile is null)
            {
                return(BadRequest());
            }
            var idea = await _context.ProductIdeas
                       .Select(i => new
            {
                i.Id,
                Author        = profile.PublicProfileId == i.Author.PublicProfileId ? new PublicProfile() : i.Author, // dummy profile to not self vote
                ExisitingVote = i.Votes.FirstOrDefault(v => v.IssuerPublicProfileId == profile.PublicProfileId),
                VoteCount     = i.Votes.Sum(v => v.Direction)
            })
                       .FirstOrDefaultAsync(i => i.Id == id);

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

            var diff           = idea.ExisitingVote != null && idea.ExisitingVote.Direction != direction ? direction * 2 : idea.ExisitingVote != null ? -direction : direction;
            var isFromDownVote = idea.ExisitingVote?.Direction == -1 || idea.ExisitingVote is null && direction == -1;

            _context.Attach(idea.Author);

            if (idea.VoteCount + diff <= -DownvotesUntilRemoval)
            {
                // bad post è_é
                idea.Author.RemoveExperience(DownvotedExperiencePenalty);
                _context.ProductIdeas.Remove(await _context.ProductIdeas.FindAsync(idea.Id));
                goto confirm; // goto bad? no u
            }

            if (idea.ExisitingVote != null)
            {
                if (!TryReplaceExistingVote(direction, idea.ExisitingVote))
                {
                    profile.ExperienceDebt += VoterExperienceBonus;
                }
            }
            else
            {
                await InsertNewVoteAsync(direction, profile, idea.Id);

                profile.AddExperience(VoterExperienceBonus);
            }
            ProcessExperience(diff, idea.Author, isFromDownVote);

confirm:
            await _context.SaveChangesAsync();

            return(Ok());
        }