aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGravatar Vitaly Takmazov2024-07-01 02:46:30 +0300
committerGravatar Vitaly Takmazov2024-07-01 02:46:30 +0300
commit0fe8ba86c60a96b4e67196074ba4ec9944e34d5d (patch)
tree7c1fa51cefe87a286c7dcc465d4baf1254ba4fbe
parent0c00333a7dc86a079182db1578bd0d845cca2b6c (diff)
JPA: map UserEntity to User using `mapstruct` library
-rw-r--r--pom.xml10
-rw-r--r--src/main/java/com/juick/data/UserMapper.java42
-rw-r--r--src/main/java/com/juick/data/UsersRepository.java2
-rw-r--r--src/main/java/com/juick/data/entities/UserEntity.java30
4 files changed, 79 insertions, 5 deletions
diff --git a/pom.xml b/pom.xml
index 7012452a..c2f8ee79 100644
--- a/pom.xml
+++ b/pom.xml
@@ -331,6 +331,16 @@
<artifactId>mapstruct-processor</artifactId>
<version>${org.mapstruct.version}</version>
</path>
+ <path>
+ <groupId>org.projectlombok</groupId>
+ <artifactId>lombok</artifactId>
+ <version>1.18.34</version>
+ </path>
+ <path>
+ <groupId>org.projectlombok</groupId>
+ <artifactId>lombok-mapstruct-binding</artifactId>
+ <version>0.2.0</version>
+ </path>
</annotationProcessorPaths>
</configuration>
</plugin>
diff --git a/src/main/java/com/juick/data/UserMapper.java b/src/main/java/com/juick/data/UserMapper.java
new file mode 100644
index 00000000..ad544116
--- /dev/null
+++ b/src/main/java/com/juick/data/UserMapper.java
@@ -0,0 +1,42 @@
+/*
+ * Copyright (C) 2008-2024, Juick
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License as
+ * published by the Free Software Foundation, either version 3 of the
+ * License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Affero General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+package com.juick.data;
+
+import com.juick.data.entities.UserEntity;
+import com.juick.model.User;
+import org.mapstruct.Mapper;
+import org.mapstruct.Mapping;
+import org.mapstruct.Named;
+
+import java.time.Instant;
+import java.time.OffsetDateTime;
+
+@Mapper(componentModel = "spring")
+public interface UserMapper {
+ @Mapping(source = "id", target = "uid")
+ @Mapping(source = "seen", target = "seen", qualifiedByName = "offsetDateTimeConverter")
+ User userEntityToUser(UserEntity entity);
+
+ @Named("offsetDateTimeConverter")
+ static Instant map(OffsetDateTime offsetDateTime) {
+ if (offsetDateTime != null) {
+ return offsetDateTime.toInstant();
+ }
+ return null;
+ }
+}
diff --git a/src/main/java/com/juick/data/UsersRepository.java b/src/main/java/com/juick/data/UsersRepository.java
index 64e08192..640fd75a 100644
--- a/src/main/java/com/juick/data/UsersRepository.java
+++ b/src/main/java/com/juick/data/UsersRepository.java
@@ -28,7 +28,7 @@ import java.util.stream.Stream;
@Repository
public interface UsersRepository extends CrudRepository<UserEntity, String> {
Optional<UserEntity> findById(Integer uid);
- Optional<UserEntity> findByName(String name);
+ UserEntity findByName(String name);
Optional<UserEntity> findByNameAndPassword(String name, String password);
Optional<UserEntity> findByEmailsEmail(String email);
Stream<UserEntity> findAllByNameIn(Collection<String> names);
diff --git a/src/main/java/com/juick/data/entities/UserEntity.java b/src/main/java/com/juick/data/entities/UserEntity.java
index 1984799e..63edadd9 100644
--- a/src/main/java/com/juick/data/entities/UserEntity.java
+++ b/src/main/java/com/juick/data/entities/UserEntity.java
@@ -18,6 +18,8 @@
package com.juick.data.entities;
import jakarta.persistence.*;
+
+import java.time.OffsetDateTime;
import java.util.HashSet;
import java.util.Set;
@@ -25,18 +27,22 @@ import java.util.Set;
@Table(name = "users")
public class UserEntity {
@Id
- @Column(name = "id", columnDefinition = "int(10) unsigned not null auto_increment")
+ @Column(name = "id")
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer id;
- @Column(name = "nick", columnDefinition = "char(64)")
+ @Column(name = "nick")
private String name;
- @Column(name = "passw", columnDefinition = "char(32)")
+ @Column(name = "passw")
private String password;
- @Column(name = "banned", columnDefinition = "tinyint(1)")
+ @Column(name = "banned")
private boolean banned;
+ @Column(name = "last_seen")
+ private OffsetDateTime seen;
+ @Column
+ private boolean premium;
@OneToMany(fetch = FetchType.LAZY, cascade = CascadeType.ALL, mappedBy = "user")
private Set<EmailEntity> emails = new HashSet<>();
@@ -84,6 +90,14 @@ public class UserEntity {
this.banned = banned;
}
+ public OffsetDateTime getSeen() {
+ return this.seen;
+ }
+
+ public void setSeen(OffsetDateTime seen) {
+ this.seen = seen;
+ }
+
public Set<EmailEntity> getEmails() {
return emails;
}
@@ -123,4 +137,12 @@ public class UserEntity {
public void setTgIds(Set<TelegramEntity> tgIds) {
this.tgIds = tgIds;
}
+
+ public boolean isPremium() {
+ return premium;
+ }
+
+ public void setPremium(boolean premium) {
+ this.premium = premium;
+ }
}