Ignore:
Timestamp:
09/09/26 03:30:35 (3 weeks ago)
Author:
mmilevski <markomilevski3@…>
Branches:
main
Children:
6886d95
Parents:
595d2e5
Message:

Changed controllers to include transactions, Implemented password hashing.

File:
1 edited

Legend:

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

    r595d2e5 r1dcdb2c  
    5151
    5252    [HttpPost]
    53     [ValidateAntiForgeryToken]
    54     public IActionResult Add(long productId)
    55     {
    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);
    74 
    75         if (product == null)
    76             return NotFound();
    77 
    78 
     53[ValidateAntiForgeryToken]
     54public IActionResult Add(long productId)
     55{
     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);
     74
     75    if (product == null)
     76        return NotFound();
     77
     78
     79    using var transaction =
     80        _context.Database.BeginTransaction();
     81
     82    try
     83    {
    7984        var wishlist =
    8085            _context.Wishlists
    … …  
    8287                    w.UserId == userId);
    8388
    84 
    85         // Create wishlist if user doesn't have one yet.
     89       
    8690        if (wishlist == null)
    8791        {
    … …  
    102106        }
    103107
    104 
    105         // Prevent duplicate product.
     108       
    106109        var alreadyExists =
    107110            _context.WishlistProducts
    … …  
    133136
    134137
     138        transaction.Commit();
     139
     140
    135141        TempData["Success"] =
    136142            "Product added to your wishlist.";
    … …  
    140146            nameof(Index));
    141147    }
     148    catch
     149    {
     150        transaction.Rollback();
     151
     152        throw;
     153    }
     154}
    142155
    143156
Note: See TracChangeset for help on using the changeset viewer.