Popup.cs 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. using System.ComponentModel.DataAnnotations;
  2. using System.ComponentModel.DataAnnotations.Schema;
  3. namespace Domain.Entities.Page.Popup
  4. {
  5. public class Popup
  6. {
  7. [ForeignKey(nameof(PositionID))]
  8. public virtual PopupPosition PopupPosition { get; private set; } = null!;
  9. [Key]
  10. public int ID { get; private set; }
  11. public int PositionID { get; private set; }
  12. public string Subject { get; private set; } = default!;
  13. public string? Content { get; private set; }
  14. public string? Link { get; private set; }
  15. public DateTime? StartAt { get; private set; }
  16. public DateTime? EndAt { get; private set; }
  17. public short Order { get; private set; } = 0;
  18. public bool IsActive { get; private set; } = false;
  19. public DateTime? UpdatedAt { get; private set; }
  20. public DateTime CreatedAt { get; private set; } = DateTime.UtcNow;
  21. private Popup() { }
  22. private Popup(int positionID, string subject, string? content, string? link, DateTime? startAt, DateTime? endAt, short order, bool isActive)
  23. {
  24. if (positionID <= 0)
  25. {
  26. throw new ArgumentOutOfRangeException(nameof(positionID));
  27. }
  28. if (string.IsNullOrWhiteSpace(subject))
  29. {
  30. throw new ArgumentException("Subject is required.", nameof(subject));
  31. }
  32. if (subject.Length > 255)
  33. {
  34. throw new ArgumentOutOfRangeException(nameof(subject));
  35. }
  36. if (order < 0)
  37. {
  38. throw new ArgumentOutOfRangeException(nameof(order));
  39. }
  40. PositionID = positionID;
  41. Subject = subject;
  42. Content = content;
  43. Link = link;
  44. StartAt = startAt;
  45. EndAt = endAt;
  46. Order = order;
  47. IsActive = isActive;
  48. }
  49. public static Popup Create(int positionID, string subject, string? content = null, string? link = null, DateTime? startAt = null, DateTime? endAt = null, short order = 0, bool isActive = false)
  50. {
  51. return new(positionID, subject, content, link, startAt, endAt, order, isActive);
  52. }
  53. public void Update(int positionID, string subject, string? content, string? link, DateTime? startAt, DateTime? endAt, short order, bool isActive)
  54. {
  55. if (positionID <= 0)
  56. {
  57. throw new ArgumentOutOfRangeException(nameof(positionID));
  58. }
  59. if (string.IsNullOrWhiteSpace(subject))
  60. {
  61. throw new ArgumentException("Subject is required.", nameof(subject));
  62. }
  63. if (subject.Length > 255)
  64. {
  65. throw new ArgumentOutOfRangeException(nameof(subject));
  66. }
  67. if (order < 0)
  68. {
  69. throw new ArgumentOutOfRangeException(nameof(order));
  70. }
  71. PositionID = positionID;
  72. Subject = subject;
  73. Content = content;
  74. Link = link;
  75. StartAt = startAt;
  76. EndAt = endAt;
  77. Order = order;
  78. IsActive = isActive;
  79. UpdatedAt = DateTime.UtcNow;
  80. }
  81. public void SetContent(string? content)
  82. {
  83. Content = content;
  84. UpdatedAt = DateTime.UtcNow;
  85. }
  86. }
  87. }