Ignore:
Timestamp:
09/01/26 07:17:16 (4 weeks ago)
Author:
mmilevski <markomilevski3@…>
Branches:
main
Children:
595d2e5
Parents:
d2eccb3
Message:

Added few implementaions, minor UI changes.

File:
1 edited

Legend:

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

    rd2eccb3 rd89d25b  
    5454    public IActionResult Add(long productId)
    5555    {
    56         var userId = GetUserId();
    57 
    58         if (userId == null)
    59         {
    60             return RedirectToAction(
    61                 "Login",
    62                 "Account");
    63         }
    64 
    65         var product = _context.Products
    66             .Include(p => p.Release)
    67             .FirstOrDefault(p => p.ProductId == productId);
     56        var sessionUserId =
     57            HttpContext.Session.GetInt32("UserId");
     58
     59        if (!sessionUserId.HasValue)
     60        {
     61            return RedirectToAction(
     62                "Login",
     63                "Account");
     64        }
     65
     66        var userId =
     67            (long)sessionUserId.Value;
     68
     69
     70        var product =
     71            _context.Products
     72                .FirstOrDefault(p =>
     73                    p.ProductId == productId);
    6874
    6975        if (product == null)
    … …  
    7177
    7278
    73         // Find existing wishlist
    74         var wishlist = _context.Wishlists
    75             .Include(w => w.WishlistProducts)
    76             .FirstOrDefault(w =>
    77                 w.UserId == userId.Value);
    78 
    79 
    80         // Create wishlist if user doesn't have one
     79        var wishlist =
     80            _context.Wishlists
     81                .FirstOrDefault(w =>
     82                    w.UserId == userId);
     83
     84
     85        // Create wishlist if user doesn't have one yet.
    8186        if (wishlist == null)
    8287        {
    8388            wishlist = new Wishlist
    8489            {
    85                 WishlistId = GetNextWishlistId(),
    86                 UserId = userId.Value
     90                WishlistId =
     91                    GetNextWishlistId(),
     92
     93                UserId =
     94                    userId
    8795            };
    8896
    89             _context.Wishlists.Add(wishlist);
     97
     98            _context.Wishlists.Add(
     99                wishlist);
     100
    90101            _context.SaveChanges();
    91102        }
    92103
    93104
    94         // Check if product is already there
    95         var alreadyExists = wishlist.WishlistProducts
    96             .Any(x => x.ProductId == productId);
     105        // Prevent duplicate product.
     106        var alreadyExists =
     107            _context.WishlistProducts
     108                .Any(wp =>
     109                    wp.WishlistId ==
     110                    wishlist.WishlistId &&
     111                    wp.ProductId ==
     112                    productId);
     113
    97114
    98115        if (!alreadyExists)
    99116        {
    100             var wishlistProduct = new WishlistProduct
    101             {
    102                 WishlistId = wishlist.WishlistId,
    103                 ProductId = productId,
    104                 AddedAt = DateTime.Today
    105             };
    106 
    107             _context.WishlistProducts.Add(wishlistProduct);
     117            var wishlistProduct =
     118                new WishlistProduct
     119                {
     120                    WishlistId =
     121                        wishlist.WishlistId,
     122
     123                    ProductId =
     124                        productId
     125                };
     126
     127
     128            _context.WishlistProducts.Add(
     129                wishlistProduct);
     130
    108131            _context.SaveChanges();
    109 
    110             TempData["Success"] =
    111                 "Product added to your wishlist.";
    112         }
    113         else
    114         {
    115             TempData["Info"] =
    116                 "This product is already in your wishlist.";
    117         }
     132        }
     133
     134
     135        TempData["Success"] =
     136            "Product added to your wishlist.";
     137
    118138
    119139        return RedirectToAction(
    120             "Details",
    121             "Release",
    122             new { id = product.ReleaseId });
     140            nameof(Index));
    123141    }
    124142
Note: See TracChangeset for help on using the changeset viewer.