2025-02-12 22:16:56 +04:00

48 lines
1.9 KiB
C#

using System;
using System.Collections.Generic;
using CandyHouseBase.Exceptions;
using CandyHouseBase.Extensions;
using CandyHouseBase.Infrastructure;
namespace CandyHouseBase.DataModels
{
public class OrderDataModel : IValidation
{
public string Id { get; private set; }
public string CustomerId { get; private set; } // Может быть null, если клиент разовый
public DateTime OrderDate { get; private set; }
public decimal TotalAmount { get; private set; }
public decimal DiscountAmount { get; private set; }
public List<OrderItemDataModel> OrderItems { get; private set; }
public bool IsCompleted { get; private set; }
public OrderDataModel(string id, string customerId, DateTime orderDate, decimal totalAmount,
decimal discountAmount, List<OrderItemDataModel> orderItems, bool isCompleted)
{
Id = id;
CustomerId = customerId;
OrderDate = orderDate;
TotalAmount = totalAmount;
DiscountAmount = discountAmount;
OrderItems = orderItems;
IsCompleted = isCompleted;
}
public void Validate()
{
if (Id.IsEmpty()) throw new ValidationException("Field Id is empty");
if (!Id.IsGuid()) throw new ValidationException("Id must be a GUID");
if (!CustomerId.IsEmpty() && !CustomerId.IsGuid())
throw new ValidationException("CustomerId must be a GUID or empty");
if (OrderItems == null || OrderItems.Count == 0)
throw new ValidationException("Order must contain at least one product");
if (TotalAmount < 0) throw new ValidationException("TotalAmount cannot be negative");
if (DiscountAmount < 0) throw new ValidationException("DiscountAmount cannot be negative");
}
public void MarkAsCompleted()
{
IsCompleted = true;
}
}
}