Document.cs 2.4 KB

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