Item.cs 2.9 KB

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