Ignore:
Timestamp:
09/01/26 06:11:34 (4 weeks ago)
Author:
mmilevski <markomilevski3@…>
Branches:
main
Children:
d2eccb3
Parents:
08aefc6
Message:

Added major improvements.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • KernelRecordsMVC.Web/Controllers/ReleaseController.cs

    r08aefc6 rfa0fbaf  
    1 using Microsoft.AspNetCore.Mvc;
     1using KernelRecordsMVC.Domain.Enums;
     2using KernelRecordsMVC.Infrastructure.Data;
     3using Microsoft.AspNetCore.Mvc;
    24using Microsoft.EntityFrameworkCore;
    3 using KernelRecordsMVC.Data;
    45
    5 namespace KernelRecordsMVC.Controllers;
     6namespace KernelRecordsMVC.Web.Controllers;
    67
    78public class ReleaseController : Controller
    … …  
    1415    }
    1516
    16     // UC003 - Browse Releases
    17     public IActionResult Index(string? search, string? genre)
     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)
    1827    {
    1928        var query = _context.Releases
    2029            .Include(r => r.Products)
    2130            .Include(r => r.ReleaseArtists)
    22             .ThenInclude(ra => ra.Artist)
     31                .ThenInclude(ra => ra.Artist)
     32            .Include(r => r.Album)
     33            .Include(r => r.SingleRelease)
    2334            .AsQueryable();
    2435
     36        // SEARCH
    2537        if (!string.IsNullOrWhiteSpace(search))
    2638        {
     39            search = search.Trim();
     40
    2741            query = query.Where(r =>
    28                 r.Title.ToLower().Contains(search.ToLower()));
     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())));
    2947        }
    3048
     49        // GENRE
    3150        if (!string.IsNullOrWhiteSpace(genre))
    3251        {
    … …  
    3554        }
    3655
    37         var releases = query
    38             .OrderBy(r => r.Title)
    39             .ToList();
     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();
    40112
    41113        ViewBag.Search = search;
    42114        ViewBag.Genre = genre;
     115        ViewBag.Format = format;
     116        ViewBag.Type = type;
     117        ViewBag.Sort = sort;
    43118
    44119        ViewBag.Genres = _context.Releases
    … …  
    48123            .ToList();
    49124
     125        ViewBag.Formats = Enum
     126            .GetValues<ProductFormat>()
     127            .Select(x => x.ToString())
     128            .ToList();
     129
    50130        return View(releases);
    51131    }
    52132
    53133
    54     // Release details
     134    // =========================================================
     135    // DETAILS
     136    // =========================================================
     137
    55138    public IActionResult Details(long id)
    56139    {
    57140        var release = _context.Releases
    58141            .Include(r => r.Products)
     142
    59143            .Include(r => r.ReleaseArtists)
    60             .ThenInclude(ra => ra.Artist)
     144                .ThenInclude(ra => ra.Artist)
     145
    61146            .Include(r => r.Album)
    62             .ThenInclude(a => a!.AlbumSongs)
    63             .ThenInclude(x => x.Song)
     147                .ThenInclude(a => a!.AlbumSongs)
     148                    .ThenInclude(x => x.Song)
     149                        .ThenInclude(s => s.SongArtists)
     150                            .ThenInclude(sa => sa.Artist)
     151
    64152            .Include(r => r.SingleRelease)
    65             .ThenInclude(s => s!.SingleFeatures)
    66             .ThenInclude(x => x.Song)
     153
    67154            .FirstOrDefault(r => r.ReleaseId == id);
    68155
Note: See TracChangeset for help on using the changeset viewer.