Ejemplo n.º 1
0
        public IReadOnlyList <string> GetUsedCategories()
        {
            if (!cache.TryGetValue(CacheKeys.RaffleItemCategories, out List <string> cacheEntry))
            {
                cacheEntry = repository.GetUsedCategories().ToList();

                var cacheEntryOptions = new MemoryCacheEntryOptions()
                                        .SetSlidingExpiration(TimeSpan.FromHours(1));

                cache.Set(CacheKeys.RaffleItemCategories, cacheEntry, cacheEntryOptions);
            }

            return(cacheEntry);
        }
Ejemplo n.º 2
0
        public async Task <IActionResult> Index(string sortBy, string searchFilter, string categoryFilter)
        {
            var raffleEvent = raffleEventRepository.GetById(1);

            if (raffleEvent.CloseDate.HasValue && raffleEvent.CloseDate.Value < DateTime.UtcNow)
            {
                return(View("IndexRaffleClosed"));
            }

            RaffleOrder order = null;

            if (HttpContext.Request.Cookies.ContainsKey(CookieKeys.RaffleOrderId))
            {
                int.TryParse(HttpContext.Request.Cookies[CookieKeys.RaffleOrderId], out int orderId);
                if (!(await mediator.Send(new RaffleOrderExistsQuery {
                    OrderId = orderId
                })))
                {
                    logger.LogInformation("No Order Found");
                    orderId = await mediator.Send(new StartRaffleOrderQuery());

                    HttpContext.Response.Cookies.Append(
                        CookieKeys.RaffleOrderId,
                        orderId.ToString(),
                        new CookieOptions
                    {
                        Secure   = true,
                        SameSite = SameSiteMode.Strict,
                        Expires  = new DateTimeOffset(DateTime.Now.AddDays(7))
                    });
                    logger.LogInformation($"New Order Created (OrderId: {orderId})");
                }

                order = await mediator.Send(new GetRaffleOrderQuery { OrderId = orderId });
            }
            else
            {
                logger.LogInformation("No Order Cookie Found");
                var orderId = await mediator.Send(new StartRaffleOrderQuery());

                HttpContext.Response.Cookies.Append(
                    CookieKeys.RaffleOrderId,
                    orderId.ToString(),
                    new CookieOptions
                {
                    Secure   = true,
                    SameSite = SameSiteMode.Strict,
                    Expires  = new DateTimeOffset(DateTime.Now.AddDays(7))
                });
                logger.LogInformation($"New Order Created (OrderId: {orderId})");

                order = await mediator.Send(new GetRaffleOrderQuery { OrderId = orderId });
            }

            if (HttpContext.Request.Query["emoji"] == "on")
            {
                ViewData["emoji"] = "on";
            }
            if (HttpContext.Request.Query["menu"] == "on")
            {
                ViewData["graph"] = "on";
            }

            ViewData["itemNumberSortParam"] = sortBy == "itemNumber" ? "itemNumber_desc" : "itemNumber";
            ViewData["titleSortParam"]      = sortBy == "title" ? "title_desc" : "title";
            ViewData["categorySortParam"]   = sortBy == "category" ? "category_desc" : "category";
            ViewData["pointsSortParam"]     = sortBy == "points" ? "points_desc" : "points";
            ViewData["currentFilter"]       = searchFilter;
            ViewData["categoryFilter"]      = categoryFilter;

            var raffleItems = raffleItemRepository.GetAll()
                              .Select(x => new RaffleItemModel
            {
                Id                  = x.Id,
                ItemNumber          = x.ItemNumber,
                Title               = x.Title,
                Description         = x.Description,
                Category            = x.Category,
                Sponsor             = x.Sponsor,
                Cost                = x.Cost,
                Value               = x.ItemValue,
                ForOver21           = x.ForOver21,
                LocalPickupOnly     = x.LocalPickupOnly,
                IsAvailable         = x.IsAvailable,
                NumberOfDraws       = x.NumberOfDraws,
                TotalTicketsEntered = x.TotalTicketsEntered,
                Pictures            = x.ImageUrls
            }).ToList();

            foreach (var line in order.Lines)
            {
                raffleItems.First(x => x.Id == line.RaffleItemId).Amount = line.Count;
            }

            if (!string.IsNullOrEmpty(searchFilter))
            {
                raffleItems = raffleItems.Where(x => x.Title.Contains(searchFilter, StringComparison.OrdinalIgnoreCase) ||
                                                x.Description.Contains(searchFilter, StringComparison.OrdinalIgnoreCase) ||
                                                x.Category.Contains(searchFilter, StringComparison.OrdinalIgnoreCase))
                              .ToList();
            }

            if (!string.IsNullOrEmpty(categoryFilter))
            {
                raffleItems = raffleItems.Where(x => x.Category.Equals(categoryFilter, StringComparison.OrdinalIgnoreCase))
                              .ToList();
            }

            switch (sortBy)
            {
            case "title_desc":
                raffleItems = raffleItems.OrderByDescending(x => x.Title).ToList();
                break;

            case "title":
                raffleItems = raffleItems.OrderBy(x => x.Title).ToList();
                break;

            case "category":
                raffleItems = raffleItems.OrderBy(x => x.Category).ToList();
                break;

            case "category_desc":
                raffleItems = raffleItems.OrderByDescending(x => x.Category).ToList();
                break;

            case "itemNumber":
                raffleItems = raffleItems.OrderBy(x => x.ItemNumber).ToList();
                break;

            case "itemNumber_desc":
                raffleItems = raffleItems.OrderByDescending(x => x.ItemNumber).ToList();
                break;

            case "points":
                raffleItems = raffleItems.OrderBy(x => x.Cost).ToList();
                break;

            case "points_desc":
                raffleItems = raffleItems.OrderByDescending(x => x.Cost).ToList();
                break;

            default:
                raffleItems = raffleItems.OrderBy(x => x.ItemNumber).ToList();
                break;
            }

            var model = new RaffleOrderViewModel
            {
                StartDate   = raffleEvent.StartDate,
                CloseDate   = raffleEvent.CloseDate.Value.ToUniversalTime(),
                Categories  = raffleItemRepository.GetUsedCategories().OrderBy(x => x).ToList(),
                RaffleItems = raffleItems
            };

            return(View(model));
        }