From 0fe8ba86c60a96b4e67196074ba4ec9944e34d5d Mon Sep 17 00:00:00 2001 From: Vitaly Takmazov Date: Mon, 1 Jul 2024 02:46:30 +0300 Subject: JPA: map UserEntity to User using `mapstruct` library --- src/main/java/com/juick/data/UserMapper.java | 42 ++++++++++++++++++++++ src/main/java/com/juick/data/UsersRepository.java | 2 +- .../java/com/juick/data/entities/UserEntity.java | 30 +++++++++++++--- 3 files changed, 69 insertions(+), 5 deletions(-) create mode 100644 src/main/java/com/juick/data/UserMapper.java (limited to 'src/main') 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 . + */ + +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 { Optional findById(Integer uid); - Optional findByName(String name); + UserEntity findByName(String name); Optional findByNameAndPassword(String name, String password); Optional findByEmailsEmail(String email); Stream findAllByNameIn(Collection 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 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 getEmails() { return emails; } @@ -123,4 +137,12 @@ public class UserEntity { public void setTgIds(Set tgIds) { this.tgIds = tgIds; } + + public boolean isPremium() { + return premium; + } + + public void setPremium(boolean premium) { + this.premium = premium; + } } -- cgit v1.2.3