MemberLoginLog.cs 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. using System.ComponentModel.DataAnnotations;
  2. using System.ComponentModel.DataAnnotations.Schema;
  3. namespace Domain.Entities.Members.Logs
  4. {
  5. /// <summary>
  6. /// 로그인 기록
  7. /// </summary>
  8. public class MemberLoginLog
  9. {
  10. [ForeignKey(nameof(MemberID))]
  11. public virtual Member Member { get; private set; } = null!;
  12. [Key]
  13. public int ID { get; private set; }
  14. public int? MemberID { get; private set; }
  15. public bool Success { get; private set; } = false;
  16. public string Account { get; private set; } = default!;
  17. public string? Reason { get; private set; }
  18. public string? Referer { get; private set; }
  19. public string? Url { get; private set; }
  20. public string? IpAddress { get; private set; }
  21. public string? UserAgent { get; private set; }
  22. public DateTime CreatedAt { get; private set; } = DateTime.UtcNow;
  23. private MemberLoginLog() { }
  24. private MemberLoginLog(int? memberID, bool success, string account, string? reason, string? referer, string? url, string? ipAddress, string? userAgent)
  25. {
  26. if (string.IsNullOrWhiteSpace(account))
  27. {
  28. throw new ArgumentException("Account is required.", nameof(account));
  29. }
  30. if (account.Length > 120)
  31. {
  32. throw new ArgumentOutOfRangeException(nameof(account));
  33. }
  34. if (reason is not null && reason.Length > 225)
  35. {
  36. throw new ArgumentOutOfRangeException(nameof(reason));
  37. }
  38. if (url is not null && url.Length > 500)
  39. {
  40. throw new ArgumentOutOfRangeException(nameof(url));
  41. }
  42. if (ipAddress is not null && ipAddress.Length > 45)
  43. {
  44. throw new ArgumentOutOfRangeException(nameof(ipAddress));
  45. }
  46. if (userAgent is not null && userAgent.Length > 512)
  47. {
  48. throw new ArgumentOutOfRangeException(nameof(userAgent));
  49. }
  50. MemberID = memberID;
  51. Success = success;
  52. Account = account;
  53. Reason = reason;
  54. Referer = referer;
  55. Url = url;
  56. IpAddress = ipAddress;
  57. UserAgent = userAgent;
  58. CreatedAt = DateTime.UtcNow;
  59. }
  60. public static MemberLoginLog Create(int? memberID, bool success, string account, string? reason = null, string? referer = null, string? url = null, string? ipAddress = null, string? userAgent = null)
  61. {
  62. return new(memberID, success, account, reason, referer, url, ipAddress, userAgent);
  63. }
  64. }
  65. }