Item.cs 2.6 KB

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