Document.cs 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. using System.ComponentModel.DataAnnotations;
  2. namespace Domain.Entities.Page;
  3. public class Document
  4. {
  5. [Key]
  6. public int ID { get; private set; }
  7. public string Code { get; private set; } = default!;
  8. public string Subject { get; private set; } = default!;
  9. public string? Content { get; private set; }
  10. public bool IsActive { get; private set; } = false;
  11. public int Views { get; private set; } = 0;
  12. public DateTime? UpdatedAt { get; private set; }
  13. public DateTime CreatedAt { get; private set; } = DateTime.UtcNow;
  14. private Document() { }
  15. private Document(string code, string subject, string? content, 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 > 120)
  30. {
  31. throw new ArgumentOutOfRangeException(nameof(subject));
  32. }
  33. Code = code;
  34. Subject = subject;
  35. Content = content;
  36. IsActive = isActive;
  37. }
  38. public static Document Create(string code, string subject, string? content = null, bool isActive = false)
  39. {
  40. return new(code, subject, content, isActive);
  41. }
  42. public void Update(string code, string subject, string? content, bool isActive)
  43. {
  44. if (string.IsNullOrWhiteSpace(subject))
  45. {
  46. throw new ArgumentException("Subject is required.", nameof(subject));
  47. }
  48. if (subject.Length > 120)
  49. {
  50. throw new ArgumentOutOfRangeException(nameof(subject));
  51. }
  52. Code = code;
  53. Subject = subject;
  54. Content = content;
  55. IsActive = isActive;
  56. UpdatedAt = DateTime.UtcNow;
  57. }
  58. public void SetContent(string? content)
  59. {
  60. Content = content;
  61. UpdatedAt = DateTime.UtcNow;
  62. }
  63. public void IncreaseViews()
  64. {
  65. Views++;
  66. }
  67. }