| 1 | using KernelRecordsMVC.Application.ViewModels;
|
|---|
| 2 | using KernelRecordsMVC.Domain.Enums;
|
|---|
| 3 | using KernelRecordsMVC.Infrastructure.Data;
|
|---|
| 4 | using Microsoft.AspNetCore.Mvc;
|
|---|
| 5 | using Microsoft.EntityFrameworkCore;
|
|---|
| 6 |
|
|---|
| 7 | namespace KernelRecordsMVC.Web.Controllers;
|
|---|
| 8 |
|
|---|
| 9 | public class SaleController : Controller
|
|---|
| 10 | {
|
|---|
| 11 | private readonly KernelRecordsContext _context;
|
|---|
| 12 |
|
|---|
| 13 | public SaleController(KernelRecordsContext context)
|
|---|
| 14 | {
|
|---|
| 15 | _context = context;
|
|---|
| 16 | }
|
|---|
| 17 |
|
|---|
| 18 | public IActionResult Index()
|
|---|
| 19 | {
|
|---|
| 20 | var products = _context.Products
|
|---|
| 21 | .Include(p => p.Release)
|
|---|
| 22 | .ThenInclude(r => r.ReleaseArtists)
|
|---|
| 23 | .ThenInclude(ra => ra.Artist)
|
|---|
| 24 | .Include(p => p.ModificationProducts)
|
|---|
| 25 | .ThenInclude(mp => mp.Modification)
|
|---|
| 26 | .ToList();
|
|---|
| 27 |
|
|---|
| 28 |
|
|---|
| 29 | var result = new List<SaleItemViewModel>();
|
|---|
| 30 |
|
|---|
| 31 |
|
|---|
| 32 | foreach (var product in products)
|
|---|
| 33 | {
|
|---|
| 34 | // Get the latest modification for this product.
|
|---|
| 35 | var latestModification = product.ModificationProducts
|
|---|
| 36 | .Select(x => x.Modification)
|
|---|
| 37 | .OrderByDescending(m => m.DateModified)
|
|---|
| 38 | .ThenByDescending(m => m.ModificationId)
|
|---|
| 39 | .FirstOrDefault();
|
|---|
| 40 |
|
|---|
| 41 |
|
|---|
| 42 | // Product is only considered currently on sale
|
|---|
| 43 | // if its latest modification is a discount.
|
|---|
| 44 | if (latestModification == null ||
|
|---|
| 45 | latestModification.TypeOfModification != ModificationType.DISCOUNT ||
|
|---|
| 46 | !latestModification.Discount.HasValue ||
|
|---|
| 47 | latestModification.Discount.Value <= 0)
|
|---|
| 48 | {
|
|---|
| 49 | continue;
|
|---|
| 50 | }
|
|---|
| 51 |
|
|---|
| 52 |
|
|---|
| 53 | var percentage =
|
|---|
| 54 | latestModification.Discount.Value;
|
|---|
| 55 |
|
|---|
| 56 |
|
|---|
| 57 | // IMPORTANT:
|
|---|
| 58 | // Product.Price already contains the discounted price.
|
|---|
| 59 | var salePrice =
|
|---|
| 60 | product.Price;
|
|---|
| 61 |
|
|---|
| 62 |
|
|---|
| 63 | // Reconstruct the price before the discount
|
|---|
| 64 | // so it can be displayed crossed out.
|
|---|
| 65 | var originalPrice =
|
|---|
| 66 | percentage > 0 &&
|
|---|
| 67 | percentage < 100
|
|---|
| 68 | ? Math.Round(
|
|---|
| 69 | salePrice /
|
|---|
| 70 | (1m - percentage / 100m),
|
|---|
| 71 | 2,
|
|---|
| 72 | MidpointRounding.AwayFromZero)
|
|---|
| 73 | : salePrice;
|
|---|
| 74 |
|
|---|
| 75 |
|
|---|
| 76 | result.Add(new SaleItemViewModel
|
|---|
| 77 | {
|
|---|
| 78 | ProductId =
|
|---|
| 79 | product.ProductId,
|
|---|
| 80 |
|
|---|
| 81 | ReleaseId =
|
|---|
| 82 | product.ReleaseId,
|
|---|
| 83 |
|
|---|
| 84 | ReleaseTitle =
|
|---|
| 85 | product.Release.Title,
|
|---|
| 86 |
|
|---|
| 87 | CoverPhoto =
|
|---|
| 88 | product.Release.CoverPhoto,
|
|---|
| 89 |
|
|---|
| 90 | Format =
|
|---|
| 91 | product.Format.ToString(),
|
|---|
| 92 |
|
|---|
| 93 | OriginalPrice =
|
|---|
| 94 | originalPrice,
|
|---|
| 95 |
|
|---|
| 96 | SalePrice =
|
|---|
| 97 | salePrice,
|
|---|
| 98 |
|
|---|
| 99 | DiscountPercentage =
|
|---|
| 100 | percentage,
|
|---|
| 101 |
|
|---|
| 102 | Stock =
|
|---|
| 103 | product.Stock,
|
|---|
| 104 |
|
|---|
| 105 | ArtistName =
|
|---|
| 106 | string.Join(
|
|---|
| 107 | ", ",
|
|---|
| 108 | product.Release.ReleaseArtists
|
|---|
| 109 | .OrderBy(x => x.ReleaseOrdinal)
|
|---|
| 110 | .Select(x => x.Artist.ArtistName))
|
|---|
| 111 | });
|
|---|
| 112 | }
|
|---|
| 113 |
|
|---|
| 114 |
|
|---|
| 115 | result = result
|
|---|
| 116 | .OrderByDescending(x =>
|
|---|
| 117 | x.DiscountPercentage)
|
|---|
| 118 | .ThenBy(x =>
|
|---|
| 119 | x.ReleaseTitle)
|
|---|
| 120 | .ToList();
|
|---|
| 121 |
|
|---|
| 122 |
|
|---|
| 123 | return View(result);
|
|---|
| 124 | }
|
|---|
| 125 | } |
|---|