source: KernelRecordsMVC.Web/Controllers/ReleaseController.cs@ fa0fbaf

main
Last change on this file since fa0fbaf was fa0fbaf, checked in by mmilevski <markomilevski3@…>, 4 weeks ago

Added major improvements.

  • Property mode set to 100644
File size: 4.4 KB
RevLine 
[fa0fbaf]1using KernelRecordsMVC.Domain.Enums;
2using KernelRecordsMVC.Infrastructure.Data;
3using Microsoft.AspNetCore.Mvc;
[08aefc6]4using Microsoft.EntityFrameworkCore;
5
[fa0fbaf]6namespace KernelRecordsMVC.Web.Controllers;
[08aefc6]7
8public class ReleaseController : Controller
9{
10 private readonly KernelRecordsContext _context;
11
12 public ReleaseController(KernelRecordsContext context)
13 {
14 _context = context;
15 }
16
[fa0fbaf]17 // =========================================================
18 // BROWSE RELEASES
19 // =========================================================
20
21 public IActionResult Index(
22 string? search,
23 string? genre,
24 string? format,
25 string? type,
26 string? sort)
[08aefc6]27 {
28 var query = _context.Releases
29 .Include(r => r.Products)
30 .Include(r => r.ReleaseArtists)
[fa0fbaf]31 .ThenInclude(ra => ra.Artist)
32 .Include(r => r.Album)
33 .Include(r => r.SingleRelease)
[08aefc6]34 .AsQueryable();
35
[fa0fbaf]36 // SEARCH
[08aefc6]37 if (!string.IsNullOrWhiteSpace(search))
38 {
[fa0fbaf]39 search = search.Trim();
40
[08aefc6]41 query = query.Where(r =>
[fa0fbaf]42 r.Title.ToLower().Contains(search.ToLower()) ||
43 r.Genre.ToLower().Contains(search.ToLower()) ||
44 r.ReleaseArtists.Any(ra =>
45 ra.Artist.ArtistName.ToLower()
46 .Contains(search.ToLower())));
[08aefc6]47 }
48
[fa0fbaf]49 // GENRE
[08aefc6]50 if (!string.IsNullOrWhiteSpace(genre))
51 {
52 query = query.Where(r =>
53 r.Genre.ToLower() == genre.ToLower());
54 }
55
[fa0fbaf]56 // FORMAT
57 if (!string.IsNullOrWhiteSpace(format))
58 {
59 query = query.Where(r =>
60 r.Products.Any(p =>
61 p.Format.ToString() == format));
62 }
63
64 // TYPE
65 if (!string.IsNullOrWhiteSpace(type))
66 {
67 if (type.Equals("Album", StringComparison.OrdinalIgnoreCase))
68 {
69 query = query.Where(r => r.Album != null);
70 }
71 else if (type.Equals("Single", StringComparison.OrdinalIgnoreCase))
72 {
73 query = query.Where(r => r.SingleRelease != null);
74 }
75 }
76
77 // SORT
78 query = sort switch
79 {
80 "titleDesc" =>
81 query.OrderByDescending(r => r.Title),
82
83 "newest" =>
84 query.OrderByDescending(r => r.ReleaseDate),
85
86 "oldest" =>
87 query.OrderBy(r => r.ReleaseDate),
88
89 "format" =>
90 query.OrderBy(r =>
91 r.Products
92 .Select(p => p.Format.ToString())
93 .FirstOrDefault()),
94
95 "priceAsc" =>
96 query.OrderBy(r =>
97 r.Products
98 .Select(p => (decimal?)p.Price)
99 .Min()),
100
101 "priceDesc" =>
102 query.OrderByDescending(r =>
103 r.Products
104 .Select(p => (decimal?)p.Price)
105 .Max()),
106
107 _ =>
108 query.OrderBy(r => r.Title)
109 };
110
111 var releases = query.ToList();
[08aefc6]112
113 ViewBag.Search = search;
114 ViewBag.Genre = genre;
[fa0fbaf]115 ViewBag.Format = format;
116 ViewBag.Type = type;
117 ViewBag.Sort = sort;
[08aefc6]118
119 ViewBag.Genres = _context.Releases
120 .Select(r => r.Genre)
121 .Distinct()
122 .OrderBy(g => g)
123 .ToList();
124
[fa0fbaf]125 ViewBag.Formats = Enum
126 .GetValues<ProductFormat>()
127 .Select(x => x.ToString())
128 .ToList();
129
[08aefc6]130 return View(releases);
131 }
132
133
[fa0fbaf]134 // =========================================================
135 // DETAILS
136 // =========================================================
137
[08aefc6]138 public IActionResult Details(long id)
139 {
140 var release = _context.Releases
141 .Include(r => r.Products)
[fa0fbaf]142
[08aefc6]143 .Include(r => r.ReleaseArtists)
[fa0fbaf]144 .ThenInclude(ra => ra.Artist)
145
[08aefc6]146 .Include(r => r.Album)
[fa0fbaf]147 .ThenInclude(a => a!.AlbumSongs)
148 .ThenInclude(x => x.Song)
149 .ThenInclude(s => s.SongArtists)
150 .ThenInclude(sa => sa.Artist)
151
[08aefc6]152 .Include(r => r.SingleRelease)
[fa0fbaf]153
[08aefc6]154 .FirstOrDefault(r => r.ReleaseId == id);
155
156 if (release == null)
157 return NotFound();
158
159 return View(release);
160 }
161}
Note: See TracBrowser for help on using the repository browser.