Changeset 1dcdb2c


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.

Files:
57 edited

Legend:

Unmodified
Added
Removed
  • KernelRecordsMVC.Application/obj/Debug/net8.0/KernelRecordsMVC.Application.AssemblyInfo.cs

    r595d2e5 r1dcdb2c  
    1414[assembly: System.Reflection.AssemblyConfigurationAttribute("Debug")]
    1515[assembly: System.Reflection.AssemblyFileVersionAttribute("1.0.0.0")]
    16 [assembly: System.Reflection.AssemblyInformationalVersionAttribute("1.0.0+d89d25b3a7f785676877cf198c9cc706308ab4a3")]
     16[assembly: System.Reflection.AssemblyInformationalVersionAttribute("1.0.0+595d2e58cf322a776ba1d3cedcb86e9bb2ee8093")]
    1717[assembly: System.Reflection.AssemblyProductAttribute("KernelRecordsMVC.Application")]
    1818[assembly: System.Reflection.AssemblyTitleAttribute("KernelRecordsMVC.Application")]
  • KernelRecordsMVC.Application/obj/Debug/net8.0/KernelRecordsMVC.Application.AssemblyInfoInputs.cache

    r595d2e5 r1dcdb2c  
    1 1de1e67b21e6555460bba54dec4600f29627ec11612fbabaf75aead1668384fb
     12b95f46ca6e7a7b44d1bea5cf3bcb61e1e18ef9f205b6731124e61118243093c
  • KernelRecordsMVC.Application/obj/Debug/net8.0/KernelRecordsMVC.Application.sourcelink.json

    r595d2e5 r1dcdb2c  
    1 {"documents":{"C:\\Users\\Marko\\RiderProjects\\KernelRecordsMVC\\*":"https://raw.githubusercontent.com/mmilevski/kernelrecords/d89d25b3a7f785676877cf198c9cc706308ab4a3/*"}}
     1{"documents":{"C:\\Users\\Marko\\RiderProjects\\KernelRecordsMVC\\*":"https://raw.githubusercontent.com/mmilevski/kernelrecords/595d2e58cf322a776ba1d3cedcb86e9bb2ee8093/*"}}
  • KernelRecordsMVC.Domain/obj/Debug/net8.0/KernelRecordsMVC.Domain.AssemblyInfo.cs

    r595d2e5 r1dcdb2c  
    1414[assembly: System.Reflection.AssemblyConfigurationAttribute("Debug")]
    1515[assembly: System.Reflection.AssemblyFileVersionAttribute("1.0.0.0")]
    16 [assembly: System.Reflection.AssemblyInformationalVersionAttribute("1.0.0+d89d25b3a7f785676877cf198c9cc706308ab4a3")]
     16[assembly: System.Reflection.AssemblyInformationalVersionAttribute("1.0.0+595d2e58cf322a776ba1d3cedcb86e9bb2ee8093")]
    1717[assembly: System.Reflection.AssemblyProductAttribute("KernelRecordsMVC.Domain")]
    1818[assembly: System.Reflection.AssemblyTitleAttribute("KernelRecordsMVC.Domain")]
  • KernelRecordsMVC.Domain/obj/Debug/net8.0/KernelRecordsMVC.Domain.AssemblyInfoInputs.cache

    r595d2e5 r1dcdb2c  
    1 2c5c6269aac41449bcb50d10a12b287cb457ef35a5d93683092ccb6ff4c72e0c
     10efe0787d662b9426c568107579e3e84ab30d64c136c14f64cb8dcc29bea5998
  • KernelRecordsMVC.Domain/obj/Debug/net8.0/KernelRecordsMVC.Domain.sourcelink.json

    r595d2e5 r1dcdb2c  
    1 {"documents":{"C:\\Users\\Marko\\RiderProjects\\KernelRecordsMVC\\*":"https://raw.githubusercontent.com/mmilevski/kernelrecords/d89d25b3a7f785676877cf198c9cc706308ab4a3/*"}}
     1{"documents":{"C:\\Users\\Marko\\RiderProjects\\KernelRecordsMVC\\*":"https://raw.githubusercontent.com/mmilevski/kernelrecords/595d2e58cf322a776ba1d3cedcb86e9bb2ee8093/*"}}
  • KernelRecordsMVC.Infrastructure/obj/Debug/net8.0/KernelRecordsMVC.Infrastructure.AssemblyInfo.cs

    r595d2e5 r1dcdb2c  
    1414[assembly: System.Reflection.AssemblyConfigurationAttribute("Debug")]
    1515[assembly: System.Reflection.AssemblyFileVersionAttribute("1.0.0.0")]
    16 [assembly: System.Reflection.AssemblyInformationalVersionAttribute("1.0.0+d89d25b3a7f785676877cf198c9cc706308ab4a3")]
     16[assembly: System.Reflection.AssemblyInformationalVersionAttribute("1.0.0+595d2e58cf322a776ba1d3cedcb86e9bb2ee8093")]
    1717[assembly: System.Reflection.AssemblyProductAttribute("KernelRecordsMVC.Infrastructure")]
    1818[assembly: System.Reflection.AssemblyTitleAttribute("KernelRecordsMVC.Infrastructure")]
  • KernelRecordsMVC.Infrastructure/obj/Debug/net8.0/KernelRecordsMVC.Infrastructure.AssemblyInfoInputs.cache

    r595d2e5 r1dcdb2c  
    1 5ec5e89a0b05a9fba0312a8736adff239c38327d3e73e894fad14b10b6985769
     14045a2d37b7dc6d9739414ba672768dbe0fe8eae8e3c23bace485112222ed00e
  • KernelRecordsMVC.Infrastructure/obj/Debug/net8.0/KernelRecordsMVC.Infrastructure.sourcelink.json

    r595d2e5 r1dcdb2c  
    1 {"documents":{"C:\\Users\\Marko\\RiderProjects\\KernelRecordsMVC\\*":"https://raw.githubusercontent.com/mmilevski/kernelrecords/d89d25b3a7f785676877cf198c9cc706308ab4a3/*"}}
     1{"documents":{"C:\\Users\\Marko\\RiderProjects\\KernelRecordsMVC\\*":"https://raw.githubusercontent.com/mmilevski/kernelrecords/595d2e58cf322a776ba1d3cedcb86e9bb2ee8093/*"}}
  • KernelRecordsMVC.Web/Controllers/AccountController.cs

    r595d2e5 r1dcdb2c  
    66using Microsoft.AspNetCore.Mvc;
    77using Microsoft.EntityFrameworkCore;
     8using Microsoft.AspNetCore.Identity;
    89
    910namespace KernelRecordsMVC.Web.Controllers;
    … …  
    1213{
    1314    private readonly KernelRecordsContext _context;
    14 
    15     public AccountController(KernelRecordsContext context)
     15    private readonly IPasswordHasher<User> _passwordHasher;
     16
     17    public AccountController(KernelRecordsContext context,IPasswordHasher<User> passwordHasher)
    1618    {
    1719        _context = context;
     20        _passwordHasher = passwordHasher;
    1821    }
    1922
    … …  
    5962        }
    6063
    61         var user = new User
    62         {
    63             Email = model.Email,
    64             Username = model.Username,
    65 
    66             // TODO:
    67             // Replace with password hashing later.
    68             Password = model.Password,
    69 
    70             DateCreated = DateTime.Today,
    71             ShippingAddress = model.ShippingAddress,
    72             TelephoneNumber = model.TelephoneNumber
    73         };
    74 
    75         _context.Users.Add(user);
    76 
    77         _context.SaveChanges();
    78 
    79 
    80         // Every normal registered user is a Consumer.
    81         var consumer = new Consumer
    82         {
    83             UserId = user.UserId,
    84             PointsCollected = 0
    85         };
    86 
    87         _context.Consumers.Add(consumer);
    88 
    89         _context.SaveChanges();
    90 
    91         return RedirectToAction(nameof(Login));
     64        using var transaction =
     65            _context.Database.BeginTransaction();
     66
     67        try
     68        {
     69            var user = new User
     70            {
     71                Email = model.Email,
     72                Username = model.Username,
     73                DateCreated = DateTime.Today,
     74                ShippingAddress = model.ShippingAddress,
     75                TelephoneNumber = model.TelephoneNumber
     76            };
     77
     78            user.Password =
     79                _passwordHasher.HashPassword(
     80                    user,
     81                    model.Password);
     82
     83            _context.Users.Add(user);
     84            _context.SaveChanges();
     85
     86            var consumer = new Consumer
     87            {
     88                UserId = user.UserId,
     89                PointsCollected = 0
     90            };
     91
     92            _context.Consumers.Add(consumer);
     93            _context.SaveChanges();
     94
     95            transaction.Commit();
     96
     97            return RedirectToAction(nameof(Login));
     98        }
     99        catch
     100        {
     101            transaction.Rollback();
     102            throw;
     103        }
    92104    }
    93105
    … …  
    118130        var user = _context.Users
    119131            .FirstOrDefault(x =>
    120                 x.Username == model.Username &&
    121                 x.Password == model.Password);
     132                x.Username == model.Username);
    122133
    123134        if (user == null)
     135        {
     136            ModelState.AddModelError(
     137                "",
     138                "Invalid username or password.");
     139
     140            return View(model);
     141        }
     142
     143        var result =
     144            _passwordHasher.VerifyHashedPassword(
     145                user,
     146                user.Password,
     147                model.Password);
     148
     149        if (result == PasswordVerificationResult.Failed)
    124150        {
    125151            ModelState.AddModelError(
  • KernelRecordsMVC.Web/Controllers/OrderController.cs

    r595d2e5 r1dcdb2c  
    5252
    5353
    54         // Find an existing pending order.
    55         var order = _context.Orders
    56             .Include(o => o.OrderProducts)
    57             .FirstOrDefault(o =>
    58                 o.UserId == userId.Value &&
    59                 o.Status == OrderStatusType.PENDING);
    60 
    61 
    62         // If there is no pending order, create one.
    63         if (order == null)
    64         {
    65             order = new Order
     54        using var transaction =
     55            _context.Database.BeginTransaction();
     56
     57        try
     58        {
     59            var order = _context.Orders
     60                .Include(o => o.OrderProducts)
     61                .FirstOrDefault(o =>
     62                    o.UserId == userId.Value &&
     63                    o.Status == OrderStatusType.PENDING);
     64           
     65            if (order == null)
    6666            {
    67                 UserId = userId.Value,
    68                 PaymentMethod = PaymentMethodType.CARD,
    69                 PurchaseDate = DateTime.Today,
    70                 PointsEarned = 0,
    71                 PointsUsed = null,
    72                 Status = OrderStatusType.PENDING
    73             };
    74 
    75             _context.Orders.Add(order);
     67                order = new Order
     68                {
     69                    UserId = userId.Value,
     70                    PaymentMethod = PaymentMethodType.CARD,
     71                    PurchaseDate = DateTime.Today,
     72                    PointsEarned = 0,
     73                    PointsUsed = null,
     74                    Status = OrderStatusType.PENDING
     75                };
     76
     77                _context.Orders.Add(order);
     78
     79                _context.SaveChanges();
     80            }
     81
     82           
     83            var existingItem = order.OrderProducts
     84                .FirstOrDefault(x =>
     85                    x.ProductId == product.ProductId);
     86
     87
     88            if (existingItem != null)
     89            {
     90                if (existingItem.Quantity + 1 >
     91                    product.Stock)
     92                {
     93                    TempData["Error"] =
     94                        "There is not enough stock available.";
     95
     96                    transaction.Rollback();
     97
     98                    return RedirectToAction(
     99                        "Details",
     100                        "Release",
     101                        new { id = product.ReleaseId });
     102                }
     103
     104                existingItem.Quantity++;
     105            }
     106            else
     107            {
     108                var discount = _context.ModificationProducts
     109                    .Include(mp => mp.Modification)
     110                    .Where(mp =>
     111                        mp.ProductId == product.ProductId &&
     112                        mp.Modification.TypeOfModification ==
     113                        ModificationType.DISCOUNT &&
     114                        mp.Modification.Discount.HasValue &&
     115                        mp.Modification.Discount.Value > 0)
     116                    .Select(mp => mp.Modification)
     117                    .OrderByDescending(m => m.DateModified)
     118                    .FirstOrDefault();
     119
     120
     121                var priceAtPurchase = product.Price;
     122
     123                if (discount != null)
     124                {
     125                    priceAtPurchase =
     126                        product.Price -
     127                        (
     128                            product.Price *
     129                            discount.Discount!.Value /
     130                            100m
     131                        );
     132
     133                    priceAtPurchase =
     134                        Math.Round(
     135                            priceAtPurchase,
     136                            2);
     137                }
     138
     139
     140                var orderProduct = new OrderProduct
     141                {
     142                    OrderId = order.OrderId,
     143
     144                    ProductId =
     145                        product.ProductId,
     146
     147                    PriceAtPurchase =
     148                        priceAtPurchase,
     149
     150                    Quantity = 1
     151                };
     152
     153                _context.OrderProducts.Add(orderProduct);
     154            }
     155
    76156
    77157            _context.SaveChanges();
    78         }
    79 
    80 
    81         // Check whether product is already in cart.
    82         var existingItem = order.OrderProducts
    83             .FirstOrDefault(x =>
    84                 x.ProductId == product.ProductId);
    85 
    86 
    87         if (existingItem != null)
    88         {
    89             if (existingItem.Quantity + 1 >
    90                 product.Stock)
    91             {
    92                 TempData["Error"] =
    93                     "There is not enough stock available.";
    94 
    95                 return RedirectToAction(
    96                     "Details",
    97                     "Release",
    98                     new { id = product.ReleaseId });
    99             }
    100 
    101             existingItem.Quantity++;
    102         }
    103         else
    104         {
    105             var discount = _context.ModificationProducts
    106                 .Include(mp => mp.Modification)
    107                 .Where(mp =>
    108                     mp.ProductId == product.ProductId &&
    109                     mp.Modification.TypeOfModification ==
    110                     ModificationType.DISCOUNT &&
    111                     mp.Modification.Discount.HasValue &&
    112                     mp.Modification.Discount.Value > 0)
    113                 .Select(mp => mp.Modification)
    114                 .OrderByDescending(m => m.DateModified)
    115                 .FirstOrDefault();
    116 
    117             var priceAtPurchase = product.Price;
    118 
    119             if (discount != null)
    120             {
    121                 priceAtPurchase =
    122                     product.Price -
    123                     (product.Price * discount.Discount!.Value / 100m);
    124 
    125                 priceAtPurchase = Math.Round(
    126                     priceAtPurchase,
    127                     2);
    128             }
    129 
    130             var orderProduct = new OrderProduct
    131             {
    132                 OrderId = order.OrderId,
    133 
    134                 ProductId = product.ProductId,
    135 
    136                 PriceAtPurchase = product.Price,
    137 
    138                 Quantity = 1
    139             };
    140 
    141             _context.OrderProducts.Add(orderProduct);
    142         }
    143 
    144         _context.SaveChanges();
    145 
    146 
    147         return RedirectToAction(nameof(Cart));
     158
     159            transaction.Commit();
     160
     161
     162            return RedirectToAction(nameof(Cart));
     163        }
     164        catch
     165        {
     166            transaction.Rollback();
     167            throw;
     168        }
    148169    }
    149170
    … …  
    202223                    Stock =
    203224                        op.Product.Stock,
    204                    
    205                     CoverPhoto = op.Product.Release.CoverPhoto
     225
     226                    CoverPhoto =
     227                        op.Product.Release.CoverPhoto
    206228                })
    207229                .ToList();
    … …  
    241263        {
    242264            _context.OrderProducts.Remove(item);
     265
    243266            _context.SaveChanges();
    244267        }
    … …  
    311334    private long? GetUserId()
    312335    {
    313         var userId = HttpContext.Session.GetInt32("UserId");
     336        var userId =
     337            HttpContext.Session.GetInt32("UserId");
    314338
    315339        if (userId.HasValue)
  • 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
  • KernelRecordsMVC.Web/Program.cs

    r595d2e5 r1dcdb2c  
    44using KernelRecordsMVC.Domain.Enums;
    55using KernelRecordsMVC.Infrastructure.Data;
     6using Microsoft.AspNetCore.Identity;
     7using KernelRecordsMVC.Models;
    68
    79var builder = WebApplication.CreateBuilder(args);
    … …  
    2426
    2527builder.Services.AddControllersWithViews();
     28
     29builder.Services.AddScoped<IPasswordHasher<User>, PasswordHasher<User>>();
    2630
    2731// ==========================================
  • KernelRecordsMVC.Web/obj/Debug/net8.0/KernelRecordsMVC.Web.AssemblyInfo.cs

    r595d2e5 r1dcdb2c  
    1515[assembly: System.Reflection.AssemblyConfigurationAttribute("Debug")]
    1616[assembly: System.Reflection.AssemblyFileVersionAttribute("1.0.0.0")]
    17 [assembly: System.Reflection.AssemblyInformationalVersionAttribute("1.0.0+d89d25b3a7f785676877cf198c9cc706308ab4a3")]
     17[assembly: System.Reflection.AssemblyInformationalVersionAttribute("1.0.0+595d2e58cf322a776ba1d3cedcb86e9bb2ee8093")]
    1818[assembly: System.Reflection.AssemblyProductAttribute("KernelRecordsMVC.Web")]
    1919[assembly: System.Reflection.AssemblyTitleAttribute("KernelRecordsMVC.Web")]
  • KernelRecordsMVC.Web/obj/Debug/net8.0/KernelRecordsMVC.Web.AssemblyInfoInputs.cache

    r595d2e5 r1dcdb2c  
    1 5c4ef787946962959a0ba14b7ffc3cd723d328624707161e0bc37e6293c6f25c
     1c4091efdfb2ea8a6937ee7bd732f81d58fc7a86e988c5b54f31b2dcc4f2f33d7
  • KernelRecordsMVC.Web/obj/Debug/net8.0/KernelRecordsMVC.Web.sourcelink.json

    r595d2e5 r1dcdb2c  
    1 {"documents":{"C:\\Users\\Marko\\RiderProjects\\KernelRecordsMVC\\*":"https://raw.githubusercontent.com/mmilevski/kernelrecords/d89d25b3a7f785676877cf198c9cc706308ab4a3/*"}}
     1{"documents":{"C:\\Users\\Marko\\RiderProjects\\KernelRecordsMVC\\*":"https://raw.githubusercontent.com/mmilevski/kernelrecords/595d2e58cf322a776ba1d3cedcb86e9bb2ee8093/*"}}
Note: See TracChangeset for help on using the changeset viewer.