Category.cs 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. using System.ComponentModel.DataAnnotations;
  2. namespace Domain.Entities.Page.Faq;
  3. public class FaqCategory
  4. {
  5. public virtual List<FaqItem> FaqItems { get; private set; } = [];
  6. [Key]
  7. public int ID { get; private set; }
  8. public string Code { get; private set; } = default!;
  9. public string Subject { get; private set; } = default!;
  10. public short Order { get; private set; } = 0;
  11. public bool IsActive { get; private set; } = false;
  12. public DateTime? UpdatedAt { get; private set; }
  13. public DateTime CreatedAt { get; private set; } = DateTime.UtcNow;
  14. private FaqCategory() { }
  15. private FaqCategory(string code, string subject, short order, bool isActive)
  16. {
  17. if (string.IsNullOrWhiteSpace(code))
  18. {
  19. throw new ArgumentException("Code is required.", nameof(code));
  20. }
  21. if (code.Length > 30)
  22. {
  23. throw new ArgumentOutOfRangeException(nameof(code));
  24. }
  25. if (string.IsNullOrWhiteSpace(subject))
  26. {
  27. throw new ArgumentException("Subject is required.", nameof(subject));
  28. }
  29. if (subject.Length > 255)
  30. {
  31. throw new ArgumentOutOfRangeException(nameof(subject));
  32. }
  33. if (order < 0)
  34. {
  35. throw new ArgumentOutOfRangeException(nameof(order));
  36. }
  37. Code = code;
  38. Subject = subject;
  39. Order = order;
  40. IsActive = isActive;
  41. }
  42. public static FaqCategory Create(string code, string subject, short order = 0, bool isActive = false)
  43. {
  44. return new(code, subject, order, isActive);
  45. }
  46. public void Update(string code, string subject, short order, bool isActive)
  47. {
  48. if (string.IsNullOrWhiteSpace(code))
  49. {
  50. throw new ArgumentException("Code is required.", nameof(code));
  51. }
  52. if (string.IsNullOrWhiteSpace(subject))
  53. {
  54. throw new ArgumentException("Subject is required.", nameof(subject));
  55. }
  56. if (subject.Length > 255)
  57. {
  58. throw new ArgumentOutOfRangeException(nameof(subject));
  59. }
  60. if (order < 0)
  61. {
  62. throw new ArgumentOutOfRangeException(nameof(order));
  63. }
  64. Code = code;
  65. Subject = subject;
  66. Order = order;
  67. IsActive = isActive;
  68. UpdatedAt = DateTime.UtcNow;
  69. }
  70. }