Files
PIbd-23_Rachek.A.S_ThreeFat…/ThreeFatMenProject/ThreeFatMenContracts/DataModels/SaleDataModel.cs
2025-04-16 00:15:22 +04:00

57 lines
1.9 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ThreeFatMenContracts.Enums;
using ThreeFatMenContracts.Exceptions;
using ThreeFatMenContracts.Extensions;
using ThreeFatMenContracts.Infrastructure;
namespace ThreeFatMenContracts.DataModels;
public class SaleDataModel(string id, string workerId, string? buyerId, double sum, DiscountType discountType, double discount, bool isCancel, List<SaleLunchDataModel> lunches) : IValidation
{
public string Id { get; private set; } = id;
public string WorkerId { get; private set; } = workerId;
public string? BuyerId { get; private set; } = buyerId;
public DateTime SaleDate { get; private set; } = DateTime.UtcNow;
public double Sum { get; private set; } = sum;
public DiscountType DiscountType { get; private set; } = discountType;
public double Discount { get; private set; } = discount;
public bool IsCancel { get; private set; } = isCancel;
public List<SaleLunchDataModel> Lunches { get; private set; } = lunches;
public void Validate()
{
if (Id.IsEmpty())
throw new ValidationException("Field Id is empty");
if (!Id.IsGuid())
throw new ValidationException("The value in the field Id is not a unique identifier");
if (WorkerId.IsEmpty())
throw new ValidationException("Field EmployeeId is empty");
if (!WorkerId.IsGuid())
throw new ValidationException("The value in the field EmployeeId is not a unique identifier");
if (!BuyerId?.IsGuid() ?? !BuyerId?.IsEmpty() ?? false)
throw new ValidationException("The value in the field BuyerId is not a unique identifier");
if (Sum <= 0)
throw new ValidationException("Field Sum is less than or equal to 0");
if ((Lunches?.Count ?? 0) == 0)
throw new ValidationException("The sale must include cocktails");
}
}