ApiApplication.cs 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. using System.ComponentModel.DataAnnotations;
  2. using System.ComponentModel.DataAnnotations.Schema;
  3. using Domain.Entities.Developers.ValueObject;
  4. using Domain.Entities.Members;
  5. namespace Domain.Entities.Developers;
  6. /// <summary>
  7. /// 개발자가 등록한 외부 API 앱.
  8. /// OAuth2 Client Credentials 발급 단위 (ApiCredential)
  9. /// </summary>
  10. public class ApiApplication
  11. {
  12. [ForeignKey(nameof(OwnerMemberID))]
  13. public virtual Member? Owner { get; private set; }
  14. public virtual ICollection<ApiCredential> Credentials { get; private set; } = new List<ApiCredential>();
  15. public virtual ICollection<ApiApplicationScope> Scopes { get; private set; } = new List<ApiApplicationScope>();
  16. [Key]
  17. public int ID { get; private set; }
  18. public int OwnerMemberID { get; private set; }
  19. public string Name { get; private set; } = default!;
  20. public string? Description { get; private set; }
  21. public string? HomepageUrl { get; private set; }
  22. public string? LogoPath { get; private set; }
  23. public ApplicationStatus Status { get; private set; } = ApplicationStatus.Pending;
  24. public DateTime CreatedAt { get; private set; } = DateTime.UtcNow;
  25. public DateTime? UpdatedAt { get; private set; }
  26. public DateTime? ApprovedAt { get; private set; }
  27. public DateTime? SuspendedAt { get; private set; }
  28. private ApiApplication() { }
  29. public static ApiApplication Create(int ownerMemberID, string name, string? description, string? homepageUrl)
  30. {
  31. if (string.IsNullOrWhiteSpace(name))
  32. {
  33. throw new ArgumentException("Name is required.", nameof(name));
  34. }
  35. if (name.Length > 100)
  36. {
  37. throw new ArgumentOutOfRangeException(nameof(name));
  38. }
  39. return new ApiApplication
  40. {
  41. OwnerMemberID = ownerMemberID,
  42. Name = name,
  43. Description = description,
  44. HomepageUrl = homepageUrl
  45. };
  46. }
  47. public void UpdateInfo(string name, string? description, string? homepageUrl)
  48. {
  49. Name = name;
  50. Description = description;
  51. HomepageUrl = homepageUrl;
  52. UpdatedAt = DateTime.UtcNow;
  53. }
  54. public void SetLogo(string? logoPath)
  55. {
  56. LogoPath = logoPath;
  57. UpdatedAt = DateTime.UtcNow;
  58. }
  59. public void Approve()
  60. {
  61. Status = ApplicationStatus.Active;
  62. ApprovedAt = DateTime.UtcNow;
  63. UpdatedAt = DateTime.UtcNow;
  64. }
  65. public void Reject()
  66. {
  67. Status = ApplicationStatus.Rejected;
  68. UpdatedAt = DateTime.UtcNow;
  69. }
  70. public void Suspend()
  71. {
  72. Status = ApplicationStatus.Suspended;
  73. SuspendedAt = DateTime.UtcNow;
  74. UpdatedAt = DateTime.UtcNow;
  75. }
  76. public void Reactivate()
  77. {
  78. Status = ApplicationStatus.Active;
  79. SuspendedAt = null;
  80. UpdatedAt = DateTime.UtcNow;
  81. }
  82. }