Advertisement
K1SR

Order klasa

Jun 18th, 2025
407
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.48 KB | None | 0 0
  1. public class Order // Ovo je AGGREGATE ROOT
  2. {
  3.     public Guid Id { get; private set; } // Identitet agregatnog korena
  4.     public DateTime CreatedOn { get; private set; }
  5.     public Address ShippingAddress { get; private set; } // Value Object
  6.     public OrderStatus Status { get; private set; } // Stanje porudžbine
  7.  
  8.     // Kolekcija OrderItem-a (interni entiteti ili value objekti)
  9.     // Pristup ovoj kolekciji je samo preko metoda Order agregata
  10.     private List<OrderItem> _orderItems;
  11.     public IReadOnlyCollection<OrderItem> OrderItems => _orderItems.AsReadOnly();
  12.  
  13.     public Money TotalPrice { get; private set; } // Izračunata vrednost, Value Object
  14.  
  15.     public Order(Guid id, DateTime createdOn, Address shippingAddress)
  16.     {
  17.         Id = id;
  18.         CreatedOn = createdOn;
  19.         ShippingAddress = shippingAddress ?? throw new ArgumentNullException(nameof(shippingAddress));
  20.         Status = OrderStatus.Created;
  21.         _orderItems = new List<OrderItem>();
  22.         TotalPrice = new Money(0, "EUR"); // Inicijalna vrednost
  23.     }
  24.  
  25.     // Sva interakcija sa agregatom ide kroz koren (Order klasu)
  26.     public void AddItem(Product product, int quantity)
  27.     {
  28.         // Poslovno pravilo: ne dozvoliti negativnu količinu
  29.         if (quantity <= 0)
  30.         {
  31.             throw new ArgumentException("Quantity must be positive.");
  32.         }
  33.  
  34.         // Provera da li proizvod već postoji u porudžbini
  35.         var existingItem = _orderItems.FirstOrDefault(item => item.ProductId == product.Id);
  36.         if (existingItem != null)
  37.         {
  38.             // Ažuriranje postojeće stavke
  39.             existingItem.IncreaseQuantity(quantity); // Ova metoda bi bila definisana unutar OrderItem
  40.         }
  41.         else
  42.         {
  43.             // Dodavanje nove stavke
  44.             _orderItems.Add(new OrderItem(Guid.NewGuid(), product.Id, product.Name, product.Price, quantity));
  45.         }
  46.  
  47.         // Izračunavanje ukupne cene (poslovno pravilo, konzistentnost agregata)
  48.         CalculateTotalPrice();
  49.     }
  50.  
  51.     public void RemoveItem(Guid orderItemId)
  52.     {
  53.         var itemToRemove = _orderItems.FirstOrDefault(item => item.Id == orderItemId);
  54.         if (itemToRemove == null)
  55.         {
  56.             throw new InvalidOperationException("Order item not found.");
  57.         }
  58.         _orderItems.Remove(itemToRemove);
  59.         CalculateTotalPrice();
  60.     }
  61.  
  62.     public void UpdateShippingAddress(Address newAddress)
  63.     {
  64.         // Validacija za promenu adrese ako je potrebno (npr. samo pre isporuke)
  65.         if (Status != OrderStatus.Created)
  66.         {
  67.             throw new InvalidOperationException("Shipping address can only be updated for new orders.");
  68.         }
  69.         ShippingAddress = newAddress;
  70.     }
  71.  
  72.     private void CalculateTotalPrice()
  73.     {
  74.         decimal totalAmount = 0;
  75.         string currency = "EUR"; // Pretpostavka jedne valute za TotalPrice
  76.  
  77.         foreach (var item in _orderItems)
  78.         {
  79.             totalAmount += item.ProductPrice.Amount * item.Quantity;
  80.             // Provera valute ako su stavke u različitim valutama
  81.             if (currency != item.ProductPrice.Currency)
  82.             {
  83.                 // Možda kompleksnija logika za konverziju ili bacanje izuzetka
  84.                 throw new InvalidOperationException("Mixed currencies in order items.");
  85.             }
  86.         }
  87.         TotalPrice = new Money(totalAmount, currency);
  88.     }
  89.  
  90.     // Mogući unutrašnji entitet (OrderItem)
  91.     public class OrderItem // Ovo je entitet unutar agregata, nema sopstveni repozitorijum
  92.     {
  93.         public Guid Id { get; private set; } // Identitet unutar agregata
  94.         public Guid ProductId { get; private set; }
  95.         public string ProductName { get; private set; }
  96.         public Money ProductPrice { get; private set; } // Value Object
  97.         public int Quantity { get; private set; }
  98.  
  99.         public OrderItem(Guid id, Guid productId, string productName, Money productPrice, int quantity)
  100.         {
  101.             Id = id;
  102.             ProductId = productId;
  103.             ProductName = productName;
  104.             ProductPrice = productPrice;
  105.             Quantity = quantity;
  106.         }
  107.  
  108.         public void IncreaseQuantity(int amount)
  109.         {
  110.             if (amount <= 0)
  111.             {
  112.                 throw new ArgumentException("Amount must be positive.");
  113.             }
  114.             Quantity += amount;
  115.         }
  116.     }
  117. }
  118.  
  119. public enum OrderStatus
  120. {
  121.     Created,
  122.     Paid,
  123.     Shipped,
  124.     Delivered,
  125.     Cancelled
  126. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement