Account.java
3,155 bytes
| 1 | package com.tasteprint.account; |
|---|---|
| 2 | |
| 3 | import java.time.Instant; |
| 4 | import java.util.Locale; |
| 5 | import java.util.UUID; |
| 6 | |
| 7 | import jakarta.persistence.Column; |
| 8 | import jakarta.persistence.Entity; |
| 9 | import jakarta.persistence.Id; |
| 10 | import jakarta.persistence.Table; |
| 11 | |
| 12 | @Entity |
| 13 | @Table(name = "app_user") |
| 14 | class Account { |
| 15 | |
| 16 | @Id |
| 17 | private UUID id; |
| 18 | |
| 19 | @Column(name = "display_name", nullable = false, length = 80) |
| 20 | private String displayName; |
| 21 | |
| 22 | @Column(nullable = false, length = 254, unique = true) |
| 23 | private String email; |
| 24 | |
| 25 | @Column(name = "password_hash", nullable = false, length = 100) |
| 26 | private String passwordHash; |
| 27 | |
| 28 | @Column(name = "share_slug", nullable = false, length = 100, unique = true) |
| 29 | private String shareSlug; |
| 30 | |
| 31 | @Column(name = "home_city", length = 100) |
| 32 | private String homeCity; |
| 33 | |
| 34 | @Column(name = "home_country_code", length = 2) |
| 35 | private String homeCountryCode; |
| 36 | |
| 37 | @Column(length = 280) |
| 38 | private String bio; |
| 39 | |
| 40 | @Column(name = "avatar_url", length = 500) |
| 41 | private String avatarUrl; |
| 42 | |
| 43 | @Column(name = "profile_public", nullable = false) |
| 44 | private boolean profilePublic; |
| 45 | |
| 46 | @Column(name = "created_at", nullable = false) |
| 47 | private Instant createdAt; |
| 48 | |
| 49 | @Column(name = "updated_at", nullable = false) |
| 50 | private Instant updatedAt; |
| 51 | |
| 52 | protected Account() { |
| 53 | } |
| 54 | |
| 55 | Account(UUID id, String displayName, String email, String passwordHash, String shareSlug, Instant now) { |
| 56 | this.id = id; |
| 57 | this.displayName = displayName.trim(); |
| 58 | this.email = email.trim().toLowerCase(Locale.ROOT); |
| 59 | this.passwordHash = passwordHash; |
| 60 | this.shareSlug = shareSlug; |
| 61 | this.profilePublic = false; |
| 62 | this.createdAt = now; |
| 63 | this.updatedAt = now; |
| 64 | } |
| 65 | |
| 66 | void updateProfile(String displayName, String homeCity, String homeCountryCode, String bio, |
| 67 | String avatarUrl, boolean profilePublic, Instant now) { |
| 68 | this.displayName = displayName.trim(); |
| 69 | this.homeCity = clean(homeCity); |
| 70 | this.homeCountryCode = clean(homeCountryCode) == null |
| 71 | ? null |
| 72 | : homeCountryCode.trim().toUpperCase(Locale.ROOT); |
| 73 | this.bio = clean(bio); |
| 74 | this.avatarUrl = clean(avatarUrl); |
| 75 | this.profilePublic = profilePublic; |
| 76 | this.updatedAt = now; |
| 77 | } |
| 78 | |
| 79 | void makePublic(Instant now) { |
| 80 | this.profilePublic = true; |
| 81 | this.updatedAt = now; |
| 82 | } |
| 83 | |
| 84 | private String clean(String value) { |
| 85 | return value == null || value.isBlank() ? null : value.trim(); |
| 86 | } |
| 87 | |
| 88 | UUID id() { |
| 89 | return id; |
| 90 | } |
| 91 | |
| 92 | String displayName() { |
| 93 | return displayName; |
| 94 | } |
| 95 | |
| 96 | String email() { |
| 97 | return email; |
| 98 | } |
| 99 | |
| 100 | String passwordHash() { |
| 101 | return passwordHash; |
| 102 | } |
| 103 | |
| 104 | String shareSlug() { |
| 105 | return shareSlug; |
| 106 | } |
| 107 | |
| 108 | String homeCity() { |
| 109 | return homeCity; |
| 110 | } |
| 111 | |
| 112 | String homeCountryCode() { |
| 113 | return homeCountryCode; |
| 114 | } |
| 115 | |
| 116 | String bio() { |
| 117 | return bio; |
| 118 | } |
| 119 | |
| 120 | String avatarUrl() { |
| 121 | return avatarUrl; |
| 122 | } |
| 123 | |
| 124 | boolean profilePublic() { |
| 125 | return profilePublic; |
| 126 | } |
| 127 | |
| 128 | Instant createdAt() { |
| 129 | return createdAt; |
| 130 | } |
| 131 | } |
| 132 | |