Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

67 рядки
2.0KB

  1. using CartWise.Domain.Enums;
  2. namespace CartWise.Domain.Entities;
  3. public class ShoppingList
  4. {
  5. private readonly List<ShoppingListItem> _items = [];
  6. public Guid ShoppingListId { get; private set; }
  7. public Guid HouseholdId { get; private set; }
  8. public string Name { get; private set; } = null!;
  9. public ShoppingListStatus Status { get; private set; }
  10. public DateTime CreatedUtc { get; private set; }
  11. public DateTime? CompletedUtc { get; private set; }
  12. public string CreatedByUserId { get; private set; } = null!;
  13. public IReadOnlyCollection<ShoppingListItem> Items => _items.AsReadOnly();
  14. private ShoppingList()
  15. {
  16. }
  17. public ShoppingList(Guid householdId, string name, string createdByUserId, DateTime createdUtc)
  18. {
  19. if (householdId == Guid.Empty)
  20. {
  21. throw new ArgumentException("HouseholdId is required.", nameof(householdId));
  22. }
  23. if (string.IsNullOrWhiteSpace(name))
  24. {
  25. throw new ArgumentException("Shopping list name is required.", nameof(name));
  26. }
  27. if (string.IsNullOrWhiteSpace(createdByUserId))
  28. {
  29. throw new ArgumentException("CreatedByUserId is required.", nameof(createdByUserId));
  30. }
  31. ShoppingListId = Guid.NewGuid();
  32. HouseholdId = householdId;
  33. Name = name.Trim();
  34. Status = ShoppingListStatus.Active;
  35. CreatedUtc = createdUtc;
  36. CreatedByUserId = createdByUserId;
  37. }
  38. public ShoppingListItem AddItem(string displayName, decimal? quantity, string? unit, string addedByUserId, DateTime addedUtc)
  39. {
  40. if (Status != ShoppingListStatus.Active)
  41. {
  42. throw new InvalidOperationException("Cannot add items to a list that is not active.");
  43. }
  44. var sortOrder = _items.Count == 0 ? 0 : _items.Max(i => i.SortOrder) + 1;
  45. var item = new ShoppingListItem(ShoppingListId, displayName, quantity, unit, sortOrder, addedByUserId, addedUtc);
  46. _items.Add(item);
  47. return item;
  48. }
  49. }

Powered by TurnKey Linux.