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/OrderController.cs

    r08aefc6 rfa0fbaf  
    1 using Microsoft.AspNetCore.Http;
    2 
    3 namespace KernelRecordsMVC.Controllers;
    4 
     1using KernelRecordsMVC.Application.ViewModels;
     2using KernelRecordsMVC.Domain.Enums;
     3using KernelRecordsMVC.Infrastructure.Data;
     4using KernelRecordsMVC.Models;
    55using Microsoft.AspNetCore.Mvc;
    66using Microsoft.EntityFrameworkCore;
    7 using KernelRecordsMVC.Data;
    8 using KernelRecordsMVC.Models;
    9 using KernelRecordsMVC.Domain.Enums;
    10 using KernelRecordsMVC.Application.ViewModels;
     7
     8namespace KernelRecordsMVC.Web.Controllers;
    119
    1210public class OrderController : Controller
    … …  
    6765            order = new Order
    6866            {
    69                 OrderId = GetNextOrderId(),
    7067                UserId = userId.Value,
    71 
    72                 // The actual payment method will be
    73                 // selected during checkout.
    7468                PaymentMethod = PaymentMethodType.CARD,
    75 
    7669                PurchaseDate = DateTime.Today,
    77 
    7870                PointsEarned = 0,
    79 
    8071                PointsUsed = null,
    81 
    8272                Status = OrderStatusType.PENDING
    8373            };
    … …  
    113103        else
    114104        {
     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
    115130            var orderProduct = new OrderProduct
    116131            {
    … …  
    186201
    187202                    Stock =
    188                         op.Product.Stock
     203                        op.Product.Stock,
     204                   
     205                    CoverPhoto = op.Product.Release.CoverPhoto
    189206                })
    190207                .ToList();
    … …  
    294311    private long? GetUserId()
    295312    {
    296         var value =
    297             HttpContext.Session.GetString("UserId");
    298 
    299         if (long.TryParse(value, out var userId))
    300             return userId;
     313        var userId = HttpContext.Session.GetInt32("UserId");
     314
     315        if (userId.HasValue)
     316            return userId.Value;
    301317
    302318        return null;
    303319    }
    304 
    305 
    306     // ==========================================
    307     // ORDER ID
    308     // ==========================================
    309 
    310     private long GetNextOrderId()
    311     {
    312         var maxId = _context.Orders
    313             .Select(x => (long?)x.OrderId)
    314             .Max();
    315 
    316         return (maxId ?? 0) + 1;
    317     }
    318320}
Note: See TracChangeset for help on using the changeset viewer.