Exemplo n.º 1
0
        public async Task <IActionResult> SendAsync(SendViewModel model)
        {
            if (model.OpportunityIds == null || model.OpportunityIds.Count == 0)
            {
                TempData["Messages"] = "No opportunities selected, no newsletters delivered.";
                return(RedirectToAction(nameof(Index)));
            }

            var opportunities = await MemoryCacheImpl.GetOpportunitiesAcceptingApplications(_memory, _context);

            var html = GenerateSendGridHtmlFromOpportunities(
                opportunities
                .Where(x => model.OpportunityIds.Contains(x.Id))
                .ToList());

            var response = await _sendgridClient.SendNewsletterAsync(html);

            var res = await response.Body.ReadAsStringAsync();

            if (response.StatusCode.Equals(HttpStatusCode.Accepted) ||
                response.StatusCode.Equals(HttpStatusCode.OK))
            {
                TempData["Messages"] = "Newsletter successfully sent";
            }

            return(RedirectToAction(nameof(Index)));
        }
Exemplo n.º 2
0
        private async Task <List <Opportunity> > GetRandomRecentOpportunities(
            int takeFromTopCount,
            int numberOfOpportunitiesToInclude)
        {
            var opportunities = await MemoryCacheImpl.GetOpportunitiesAcceptingApplications(_memory, _context);

            opportunities.Sort();
            opportunities.Reverse();

            if (opportunities.Count > takeFromTopCount)
            {
                opportunities = opportunities.Take(takeFromTopCount).ToList();
            }

            var filteredOpportunities = opportunities;

            if (filteredOpportunities.Count > numberOfOpportunitiesToInclude)
            {
                filteredOpportunities = new List <Opportunity>();
                var    filteredOpportunityIds = new List <int>();
                Random random = new Random();
                while (filteredOpportunities.Count < numberOfOpportunitiesToInclude)
                {
                    var opportunity = opportunities[random.Next(opportunities.Count)];
                    if (!filteredOpportunityIds.Contains(opportunity.Id))
                    {
                        filteredOpportunities.Add(opportunity);
                        filteredOpportunityIds.Add(opportunity.Id);
                    }
                }
            }

            return(filteredOpportunities);
        }
Exemplo n.º 3
0
        public async Task <IActionResult> IndexAsync(int id)
        {
            List <Opportunity> opportunities = await MemoryCacheImpl.GetOpportunitiesAcceptingApplications(_memoryCache, _context);

            OpportunityViewModel opportunityView = opportunities
                                                   .Where(x => x.Id == id)
                                                   .Select(OpportunityViewModel.FromOpportunity)
                                                   .SingleOrDefault();

            if (opportunityView == null)
            {
                return(View(new OpportunityViewModel()));
            }
            return(View(opportunityView));
        }
Exemplo n.º 4
0
        public async Task <IActionResult> Search([FromBody] OpportunitySearch opportunitySearch)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            List <Opportunity> opportunities = await MemoryCacheImpl.GetOpportunitiesAcceptingApplications(_memoryCache, _context);

            List <OpportunityViewModel> opportunityViews = opportunities
                                                           .Where(GetEligibleOpportunityPredicate(opportunitySearch))
                                                           .Select(OpportunityViewModel.FromOpportunity)
                                                           .OrderByDescending(x => x.Id)
                                                           .ToList();

            return(Ok(opportunityViews));
        }