From 9eb54aa700a857ce0e1bf9d5fbba27e0dc042865 Mon Sep 17 00:00:00 2001 From: MatheusVict Date: Tue, 20 Feb 2024 12:35:40 -0300 Subject: [PATCH 01/14] feat: add user entity Signed-off-by: MatheusVict --- .../semprealerta/domain/user/Address.java | 19 ++++++ .../semprealerta/domain/user/Contact.java | 17 ++++++ .../semprealerta/domain/user/User.java | 59 +++++++++++++++++++ .../semprealerta/domain/user/UserRoles.java | 16 +++++ 4 files changed, 111 insertions(+) create mode 100644 src/main/java/com/institutosemprealerta/semprealerta/domain/user/Address.java create mode 100644 src/main/java/com/institutosemprealerta/semprealerta/domain/user/Contact.java create mode 100644 src/main/java/com/institutosemprealerta/semprealerta/domain/user/User.java create mode 100644 src/main/java/com/institutosemprealerta/semprealerta/domain/user/UserRoles.java diff --git a/src/main/java/com/institutosemprealerta/semprealerta/domain/user/Address.java b/src/main/java/com/institutosemprealerta/semprealerta/domain/user/Address.java new file mode 100644 index 0000000..a281468 --- /dev/null +++ b/src/main/java/com/institutosemprealerta/semprealerta/domain/user/Address.java @@ -0,0 +1,19 @@ +package com.institutosemprealerta.semprealerta.domain.user; + +import jakarta.persistence.Embeddable; +import lombok.AllArgsConstructor; +import lombok.Getter; +import lombok.NoArgsConstructor; +import lombok.Setter; + +@Getter +@Setter +@AllArgsConstructor +@NoArgsConstructor +@Embeddable +public class Address { + private String street; + private String number; + private String city; + private String zipCode; +} diff --git a/src/main/java/com/institutosemprealerta/semprealerta/domain/user/Contact.java b/src/main/java/com/institutosemprealerta/semprealerta/domain/user/Contact.java new file mode 100644 index 0000000..bd0b82e --- /dev/null +++ b/src/main/java/com/institutosemprealerta/semprealerta/domain/user/Contact.java @@ -0,0 +1,17 @@ +package com.institutosemprealerta.semprealerta.domain.user; + +import jakarta.persistence.Embeddable; +import lombok.AllArgsConstructor; +import lombok.Getter; +import lombok.NoArgsConstructor; +import lombok.Setter; + +@Getter +@Setter +@NoArgsConstructor +@AllArgsConstructor +@Embeddable +public class Contact { + private String email; + private String phone; +} diff --git a/src/main/java/com/institutosemprealerta/semprealerta/domain/user/User.java b/src/main/java/com/institutosemprealerta/semprealerta/domain/user/User.java new file mode 100644 index 0000000..acf22b5 --- /dev/null +++ b/src/main/java/com/institutosemprealerta/semprealerta/domain/user/User.java @@ -0,0 +1,59 @@ +package com.institutosemprealerta.semprealerta.domain.user; + + +import jakarta.persistence.Embedded; +import jakarta.persistence.Entity; +import jakarta.persistence.Id; +import jakarta.persistence.Table; +import lombok.Getter; +import lombok.Setter; + +import java.time.LocalDate; +import java.util.HashSet; +import java.util.Set; + +@Entity +@Table(name = "users") +@Getter +@Setter +public class User { + @Id + private int registration; + private String name; + private String password; + private String gender; + private LocalDate birthDate; + + private UserRoles roles; + @Embedded + private Contact contact; + + @Embedded + private Address address; + + public User() { + this.registration = generateRegistration(); + } + + public User(String name, String password, String gender, LocalDate birthDate, UserRoles roles, Contact contact, Address address) { + this.registration = this.generateRegistration(); + this.name = name; + this.password = password; + this.gender = gender; + this.birthDate = birthDate; + this.roles = roles; + this.contact = contact; + this.address = address; + } + + private int generateRegistration() { + Set registrations = new HashSet<>(8); + + while (registrations.size() < 8) { + for (int i = 0; i < 8; i++) { + registrations.add((int) (Math.random() * 10)); + } + } + return Integer.parseInt(registrations.toString()); + } +} diff --git a/src/main/java/com/institutosemprealerta/semprealerta/domain/user/UserRoles.java b/src/main/java/com/institutosemprealerta/semprealerta/domain/user/UserRoles.java new file mode 100644 index 0000000..b3e1b1e --- /dev/null +++ b/src/main/java/com/institutosemprealerta/semprealerta/domain/user/UserRoles.java @@ -0,0 +1,16 @@ +package com.institutosemprealerta.semprealerta.domain.user; + +public enum UserRoles { + ADMIN("ADMIN"), + USER("USER"); + + private final String role; + + UserRoles(String role) { + this.role = role; + } + + public String getRole() { + return role; + } +} From f5fbef3cffe27eaaa82714489d3c8fea2137b793 Mon Sep 17 00:00:00 2001 From: MatheusVict Date: Tue, 20 Feb 2024 12:50:35 -0300 Subject: [PATCH 02/14] feat: add migrations Signed-off-by: MatheusVict --- src/main/resources/application-dev.yml | 16 ++++++++++++++++ src/main/resources/application-prod.yml | 0 src/main/resources/application.properties | 1 - .../db/migration/V1__create-user-table.sql | 16 ++++++++++++++++ 4 files changed, 32 insertions(+), 1 deletion(-) create mode 100644 src/main/resources/application-dev.yml create mode 100644 src/main/resources/application-prod.yml delete mode 100644 src/main/resources/application.properties create mode 100644 src/main/resources/db/migration/V1__create-user-table.sql diff --git a/src/main/resources/application-dev.yml b/src/main/resources/application-dev.yml new file mode 100644 index 0000000..3d8bee0 --- /dev/null +++ b/src/main/resources/application-dev.yml @@ -0,0 +1,16 @@ +spring: + datasource: + url: jdbc:postgresql://localhost:5432/postgres?serverTimezone=UTC + username: postgres + password: 123 + jpa: + hibernate: + ddl-auto: update + show-sql: true + properties: + hibernate: + dialect: org.hibernate.dialect.PostgreSQLDialect + flyway: + enabled: true + locations: classpath:db/migration + baseline-on-migrate: true \ No newline at end of file diff --git a/src/main/resources/application-prod.yml b/src/main/resources/application-prod.yml new file mode 100644 index 0000000..e69de29 diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties deleted file mode 100644 index 8b13789..0000000 --- a/src/main/resources/application.properties +++ /dev/null @@ -1 +0,0 @@ - diff --git a/src/main/resources/db/migration/V1__create-user-table.sql b/src/main/resources/db/migration/V1__create-user-table.sql new file mode 100644 index 0000000..df12367 --- /dev/null +++ b/src/main/resources/db/migration/V1__create-user-table.sql @@ -0,0 +1,16 @@ +CREATE TABLE users +( + registration INTEGER NOT NULL, + name VARCHAR(255), + password VARCHAR(255), + gender VARCHAR(255), + birth_date date, + roles SMALLINT, + email VARCHAR(255), + phone VARCHAR(255), + street VARCHAR(255), + number VARCHAR(255), + city VARCHAR(255), + zip_code VARCHAR(255), + CONSTRAINT pk_users PRIMARY KEY (registration) +); \ No newline at end of file From 4982e86ca1dbd2d3ea809339ca2e481d06da0ebb Mon Sep 17 00:00:00 2001 From: MatheusVict Date: Tue, 20 Feb 2024 13:00:51 -0300 Subject: [PATCH 03/14] repository Signed-off-by: MatheusVict --- .../semprealerta/repository/UserRepository.java | 7 +++++++ 1 file changed, 7 insertions(+) create mode 100644 src/main/java/com/institutosemprealerta/semprealerta/repository/UserRepository.java diff --git a/src/main/java/com/institutosemprealerta/semprealerta/repository/UserRepository.java b/src/main/java/com/institutosemprealerta/semprealerta/repository/UserRepository.java new file mode 100644 index 0000000..7a8d0e9 --- /dev/null +++ b/src/main/java/com/institutosemprealerta/semprealerta/repository/UserRepository.java @@ -0,0 +1,7 @@ +package com.institutosemprealerta.semprealerta.repository; + +import com.institutosemprealerta.semprealerta.domain.user.User; +import org.springframework.data.jpa.repository.JpaRepository; + +public interface UserRepository extends JpaRepository { +} From 63e36c20cb8943733f28a01ca769d258d1dc2f95 Mon Sep 17 00:00:00 2001 From: MatheusVict Date: Tue, 20 Feb 2024 13:58:12 -0300 Subject: [PATCH 04/14] feat: add repository hexagonal --- .../domain/ports/out/UserRepository.java | 14 ++++++ .../semprealerta/domain/user/User.java | 2 + .../adpters/JpaUserRepositoryAdapter.java | 47 +++++++++++++++++++ .../repositories/JpaUserRepository.java | 9 ++++ .../repository/UserRepository.java | 7 --- 5 files changed, 72 insertions(+), 7 deletions(-) create mode 100644 src/main/java/com/institutosemprealerta/semprealerta/domain/ports/out/UserRepository.java create mode 100644 src/main/java/com/institutosemprealerta/semprealerta/infrastructure/adpters/JpaUserRepositoryAdapter.java create mode 100644 src/main/java/com/institutosemprealerta/semprealerta/infrastructure/repositories/JpaUserRepository.java delete mode 100644 src/main/java/com/institutosemprealerta/semprealerta/repository/UserRepository.java diff --git a/src/main/java/com/institutosemprealerta/semprealerta/domain/ports/out/UserRepository.java b/src/main/java/com/institutosemprealerta/semprealerta/domain/ports/out/UserRepository.java new file mode 100644 index 0000000..d365ca8 --- /dev/null +++ b/src/main/java/com/institutosemprealerta/semprealerta/domain/ports/out/UserRepository.java @@ -0,0 +1,14 @@ +package com.institutosemprealerta.semprealerta.domain.ports.out; + +import com.institutosemprealerta.semprealerta.domain.user.User; +import org.springframework.data.jpa.repository.JpaRepository; + +import java.util.Optional; + +public interface UserRepository { + void save(User user); + Optional findById(int id); + void update(int id, User user); + void delete(int id); + +} diff --git a/src/main/java/com/institutosemprealerta/semprealerta/domain/user/User.java b/src/main/java/com/institutosemprealerta/semprealerta/domain/user/User.java index acf22b5..61765fc 100644 --- a/src/main/java/com/institutosemprealerta/semprealerta/domain/user/User.java +++ b/src/main/java/com/institutosemprealerta/semprealerta/domain/user/User.java @@ -7,6 +7,7 @@ import jakarta.persistence.Table; import lombok.Getter; import lombok.Setter; +import org.hibernate.annotations.DynamicUpdate; import java.time.LocalDate; import java.util.HashSet; @@ -14,6 +15,7 @@ @Entity @Table(name = "users") +@DynamicUpdate @Getter @Setter public class User { diff --git a/src/main/java/com/institutosemprealerta/semprealerta/infrastructure/adpters/JpaUserRepositoryAdapter.java b/src/main/java/com/institutosemprealerta/semprealerta/infrastructure/adpters/JpaUserRepositoryAdapter.java new file mode 100644 index 0000000..22cfa7b --- /dev/null +++ b/src/main/java/com/institutosemprealerta/semprealerta/infrastructure/adpters/JpaUserRepositoryAdapter.java @@ -0,0 +1,47 @@ +package com.institutosemprealerta.semprealerta.infrastructure.adpters; + +import com.institutosemprealerta.semprealerta.domain.ports.out.UserRepository; +import com.institutosemprealerta.semprealerta.domain.user.User; +import com.institutosemprealerta.semprealerta.infrastructure.repositories.JpaUserRepository; +import org.springframework.beans.BeanUtils; +import org.springframework.stereotype.Component; + +import java.util.Optional; + +@Component +public class JpaUserRepositoryAdapter implements UserRepository { + + private final JpaUserRepository userRepository; + + public JpaUserRepositoryAdapter(JpaUserRepository jpaUserRepository) { + this.userRepository = jpaUserRepository; + } + + @Override + public void save(User user) { + this.userRepository.save(user); + } + + @Override + public Optional findById(int id) { + return this.userRepository.findById(id); + } + + @Override + public void update(int id, User user) { + User userToUpdate = this.userRepository.findById(id) + .orElseThrow(() -> new RuntimeException("User not found")); + + user.setRegistration(userToUpdate.getRegistration()); + + this.userRepository.save(user); + } + + @Override + public void delete(int id) { + User userToDelete = this.userRepository.findById(id) + .orElseThrow(() -> new RuntimeException("User not found")); + + this.userRepository.delete(userToDelete); + } +} diff --git a/src/main/java/com/institutosemprealerta/semprealerta/infrastructure/repositories/JpaUserRepository.java b/src/main/java/com/institutosemprealerta/semprealerta/infrastructure/repositories/JpaUserRepository.java new file mode 100644 index 0000000..5994731 --- /dev/null +++ b/src/main/java/com/institutosemprealerta/semprealerta/infrastructure/repositories/JpaUserRepository.java @@ -0,0 +1,9 @@ +package com.institutosemprealerta.semprealerta.infrastructure.repositories; + +import com.institutosemprealerta.semprealerta.domain.user.User; +import org.springframework.data.jpa.repository.JpaRepository; +import org.springframework.stereotype.Repository; + +@Repository +public interface JpaUserRepository extends JpaRepository { +} diff --git a/src/main/java/com/institutosemprealerta/semprealerta/repository/UserRepository.java b/src/main/java/com/institutosemprealerta/semprealerta/repository/UserRepository.java deleted file mode 100644 index 7a8d0e9..0000000 --- a/src/main/java/com/institutosemprealerta/semprealerta/repository/UserRepository.java +++ /dev/null @@ -1,7 +0,0 @@ -package com.institutosemprealerta.semprealerta.repository; - -import com.institutosemprealerta.semprealerta.domain.user.User; -import org.springframework.data.jpa.repository.JpaRepository; - -public interface UserRepository extends JpaRepository { -} From 652112f795fee36bb91c5698ecfd6c4183cdbd34 Mon Sep 17 00:00:00 2001 From: MatheusVict Date: Tue, 20 Feb 2024 14:14:06 -0300 Subject: [PATCH 05/14] feat: add find by email and registration --- .../semprealerta/domain/ports/out/UserRepository.java | 2 ++ .../adpters/JpaUserRepositoryAdapter.java | 10 ++++++++++ .../infrastructure/repositories/JpaUserRepository.java | 5 +++++ 3 files changed, 17 insertions(+) diff --git a/src/main/java/com/institutosemprealerta/semprealerta/domain/ports/out/UserRepository.java b/src/main/java/com/institutosemprealerta/semprealerta/domain/ports/out/UserRepository.java index d365ca8..de6adee 100644 --- a/src/main/java/com/institutosemprealerta/semprealerta/domain/ports/out/UserRepository.java +++ b/src/main/java/com/institutosemprealerta/semprealerta/domain/ports/out/UserRepository.java @@ -10,5 +10,7 @@ public interface UserRepository { Optional findById(int id); void update(int id, User user); void delete(int id); + Optional findByRegistration(int registration); + Optional findByEmail(String email); } diff --git a/src/main/java/com/institutosemprealerta/semprealerta/infrastructure/adpters/JpaUserRepositoryAdapter.java b/src/main/java/com/institutosemprealerta/semprealerta/infrastructure/adpters/JpaUserRepositoryAdapter.java index 22cfa7b..aed38f8 100644 --- a/src/main/java/com/institutosemprealerta/semprealerta/infrastructure/adpters/JpaUserRepositoryAdapter.java +++ b/src/main/java/com/institutosemprealerta/semprealerta/infrastructure/adpters/JpaUserRepositoryAdapter.java @@ -44,4 +44,14 @@ public void delete(int id) { this.userRepository.delete(userToDelete); } + + @Override + public Optional findByRegistration(int registration) { + return this.userRepository.findById(registration); + } + + @Override + public Optional findByEmail(String email) { + return this.userRepository.findByEmail(email); + } } diff --git a/src/main/java/com/institutosemprealerta/semprealerta/infrastructure/repositories/JpaUserRepository.java b/src/main/java/com/institutosemprealerta/semprealerta/infrastructure/repositories/JpaUserRepository.java index 5994731..1b7211f 100644 --- a/src/main/java/com/institutosemprealerta/semprealerta/infrastructure/repositories/JpaUserRepository.java +++ b/src/main/java/com/institutosemprealerta/semprealerta/infrastructure/repositories/JpaUserRepository.java @@ -2,8 +2,13 @@ import com.institutosemprealerta.semprealerta.domain.user.User; import org.springframework.data.jpa.repository.JpaRepository; +import org.springframework.data.jpa.repository.Query; import org.springframework.stereotype.Repository; +import java.util.Optional; + @Repository public interface JpaUserRepository extends JpaRepository { + @Query("SELECT u FROM User u WHERE u.contact.email = ?1") + Optional findByEmail(String email); } From a1cce5b751c5bbdcaafb7c296729a8257078a6e9 Mon Sep 17 00:00:00 2001 From: MatheusVict Date: Tue, 20 Feb 2024 14:14:36 -0300 Subject: [PATCH 06/14] feat: add user service Signed-off-by: MatheusVict --- .../application/service/UserService.java | 11 +++++ .../service/impl/UserServiceImpl.java | 43 +++++++++++++++++++ 2 files changed, 54 insertions(+) create mode 100644 src/main/java/com/institutosemprealerta/semprealerta/application/service/UserService.java create mode 100644 src/main/java/com/institutosemprealerta/semprealerta/application/service/impl/UserServiceImpl.java diff --git a/src/main/java/com/institutosemprealerta/semprealerta/application/service/UserService.java b/src/main/java/com/institutosemprealerta/semprealerta/application/service/UserService.java new file mode 100644 index 0000000..842c8d6 --- /dev/null +++ b/src/main/java/com/institutosemprealerta/semprealerta/application/service/UserService.java @@ -0,0 +1,11 @@ +package com.institutosemprealerta.semprealerta.application.service; + +import com.institutosemprealerta.semprealerta.domain.user.User; + +public interface UserService { + void save(User user); + void update(int id, User user); + void delete(int id); + User findByRegistration(int registration); + User findByEmail(String email); +} diff --git a/src/main/java/com/institutosemprealerta/semprealerta/application/service/impl/UserServiceImpl.java b/src/main/java/com/institutosemprealerta/semprealerta/application/service/impl/UserServiceImpl.java new file mode 100644 index 0000000..f09d3b3 --- /dev/null +++ b/src/main/java/com/institutosemprealerta/semprealerta/application/service/impl/UserServiceImpl.java @@ -0,0 +1,43 @@ +package com.institutosemprealerta.semprealerta.application.service.impl; + +import com.institutosemprealerta.semprealerta.application.service.UserService; +import com.institutosemprealerta.semprealerta.domain.user.User; +import com.institutosemprealerta.semprealerta.domain.ports.out.UserRepository; +import org.springframework.stereotype.Service; + +@Service +public class UserServiceImpl implements UserService { + + private final UserRepository userRepository; + + public UserServiceImpl(UserRepository userRepository) { + this.userRepository = userRepository; + } + + @Override + public void save(User user) { + this.userRepository.save(user); + } + + @Override + public void update(int id, User user) { + this.userRepository.update(id, user); + } + + @Override + public void delete(int id) { + this.userRepository.delete(id); + } + + @Override + public User findByRegistration(int registration) { + return this.userRepository.findByRegistration(registration) + .orElseThrow(() -> new RuntimeException("User not found")); + } + + @Override + public User findByEmail(String email) { + return this.userRepository.findByEmail(email) + .orElseThrow(() -> new RuntimeException("User not found")); + } +} From 02bd8413786d3304399ffaefc54a0299896ed512 Mon Sep 17 00:00:00 2001 From: MatheusVict Date: Tue, 20 Feb 2024 16:50:18 -0300 Subject: [PATCH 07/14] refactor: user directory --- .../domain/ports/model/UserDTO.java | 23 +++++++++++++++ .../entity}/user/Address.java | 2 +- .../entity}/user/Contact.java | 2 +- .../entity}/user/User.java | 28 +++++++++++++++---- .../entity}/user/UserRoles.java | 2 +- .../db/migration/V1__create-user-table.sql | 1 + 6 files changed, 50 insertions(+), 8 deletions(-) create mode 100644 src/main/java/com/institutosemprealerta/semprealerta/domain/ports/model/UserDTO.java rename src/main/java/com/institutosemprealerta/semprealerta/{domain => infrastructure/entity}/user/Address.java (82%) rename src/main/java/com/institutosemprealerta/semprealerta/{domain => infrastructure/entity}/user/Contact.java (79%) rename src/main/java/com/institutosemprealerta/semprealerta/{domain => infrastructure/entity}/user/User.java (62%) rename src/main/java/com/institutosemprealerta/semprealerta/{domain => infrastructure/entity}/user/UserRoles.java (74%) diff --git a/src/main/java/com/institutosemprealerta/semprealerta/domain/ports/model/UserDTO.java b/src/main/java/com/institutosemprealerta/semprealerta/domain/ports/model/UserDTO.java new file mode 100644 index 0000000..3217d07 --- /dev/null +++ b/src/main/java/com/institutosemprealerta/semprealerta/domain/ports/model/UserDTO.java @@ -0,0 +1,23 @@ +package com.institutosemprealerta.semprealerta.domain.ports.model; + + +import com.institutosemprealerta.semprealerta.infrastructure.entity.user.UserRoles; + +import java.time.LocalDate; + +public record UserDTO( + String name, + String email, + String password, + String phone, + String gender, + LocalDate birthDate, + UserRoles roles, + String street, + String number, + String city, + String zipCode + +) { + +} diff --git a/src/main/java/com/institutosemprealerta/semprealerta/domain/user/Address.java b/src/main/java/com/institutosemprealerta/semprealerta/infrastructure/entity/user/Address.java similarity index 82% rename from src/main/java/com/institutosemprealerta/semprealerta/domain/user/Address.java rename to src/main/java/com/institutosemprealerta/semprealerta/infrastructure/entity/user/Address.java index a281468..9f09f59 100644 --- a/src/main/java/com/institutosemprealerta/semprealerta/domain/user/Address.java +++ b/src/main/java/com/institutosemprealerta/semprealerta/infrastructure/entity/user/Address.java @@ -1,4 +1,4 @@ -package com.institutosemprealerta.semprealerta.domain.user; +package com.institutosemprealerta.semprealerta.infrastructure.entity.user; import jakarta.persistence.Embeddable; import lombok.AllArgsConstructor; diff --git a/src/main/java/com/institutosemprealerta/semprealerta/domain/user/Contact.java b/src/main/java/com/institutosemprealerta/semprealerta/infrastructure/entity/user/Contact.java similarity index 79% rename from src/main/java/com/institutosemprealerta/semprealerta/domain/user/Contact.java rename to src/main/java/com/institutosemprealerta/semprealerta/infrastructure/entity/user/Contact.java index bd0b82e..5c48e24 100644 --- a/src/main/java/com/institutosemprealerta/semprealerta/domain/user/Contact.java +++ b/src/main/java/com/institutosemprealerta/semprealerta/infrastructure/entity/user/Contact.java @@ -1,4 +1,4 @@ -package com.institutosemprealerta.semprealerta.domain.user; +package com.institutosemprealerta.semprealerta.infrastructure.entity.user; import jakarta.persistence.Embeddable; import lombok.AllArgsConstructor; diff --git a/src/main/java/com/institutosemprealerta/semprealerta/domain/user/User.java b/src/main/java/com/institutosemprealerta/semprealerta/infrastructure/entity/user/User.java similarity index 62% rename from src/main/java/com/institutosemprealerta/semprealerta/domain/user/User.java rename to src/main/java/com/institutosemprealerta/semprealerta/infrastructure/entity/user/User.java index 61765fc..67dc39a 100644 --- a/src/main/java/com/institutosemprealerta/semprealerta/domain/user/User.java +++ b/src/main/java/com/institutosemprealerta/semprealerta/infrastructure/entity/user/User.java @@ -1,15 +1,15 @@ -package com.institutosemprealerta.semprealerta.domain.user; +package com.institutosemprealerta.semprealerta.infrastructure.entity.user; -import jakarta.persistence.Embedded; -import jakarta.persistence.Entity; -import jakarta.persistence.Id; -import jakarta.persistence.Table; +import com.institutosemprealerta.semprealerta.domain.ports.model.UserDTO; +import jakarta.persistence.*; import lombok.Getter; import lombok.Setter; +import org.hibernate.annotations.CreationTimestamp; import org.hibernate.annotations.DynamicUpdate; import java.time.LocalDate; +import java.time.LocalDateTime; import java.util.HashSet; import java.util.Set; @@ -33,6 +33,10 @@ public class User { @Embedded private Address address; + @CreationTimestamp + @Column(name = "created_at", updatable = false, columnDefinition = "TIMESTAMP DEFAULT CURRENT_TIMESTAMP") + private LocalDateTime createdAt; + public User() { this.registration = generateRegistration(); } @@ -48,6 +52,20 @@ public User(String name, String password, String gender, LocalDate birthDate, Us this.address = address; } + public User fromModelToDomain(UserDTO dto) { + return new User( + dto.name(), + dto.password(), + dto.gender(), + dto.birthDate(), + dto.roles(), + new Contact(dto.email(), dto.phone()), + new Address(dto.street(), dto.number(), dto.city(), dto.zipCode()) + ); + } + + + private int generateRegistration() { Set registrations = new HashSet<>(8); diff --git a/src/main/java/com/institutosemprealerta/semprealerta/domain/user/UserRoles.java b/src/main/java/com/institutosemprealerta/semprealerta/infrastructure/entity/user/UserRoles.java similarity index 74% rename from src/main/java/com/institutosemprealerta/semprealerta/domain/user/UserRoles.java rename to src/main/java/com/institutosemprealerta/semprealerta/infrastructure/entity/user/UserRoles.java index b3e1b1e..330bb8f 100644 --- a/src/main/java/com/institutosemprealerta/semprealerta/domain/user/UserRoles.java +++ b/src/main/java/com/institutosemprealerta/semprealerta/infrastructure/entity/user/UserRoles.java @@ -1,4 +1,4 @@ -package com.institutosemprealerta.semprealerta.domain.user; +package com.institutosemprealerta.semprealerta.infrastructure.entity.user; public enum UserRoles { ADMIN("ADMIN"), diff --git a/src/main/resources/db/migration/V1__create-user-table.sql b/src/main/resources/db/migration/V1__create-user-table.sql index df12367..69b1979 100644 --- a/src/main/resources/db/migration/V1__create-user-table.sql +++ b/src/main/resources/db/migration/V1__create-user-table.sql @@ -6,6 +6,7 @@ CREATE TABLE users gender VARCHAR(255), birth_date date, roles SMALLINT, + created_at TIMESTAMP WITHOUT TIME ZONE DEFAULT NOW(), email VARCHAR(255), phone VARCHAR(255), street VARCHAR(255), From d2053870d6ed40b7fc4c32073bc14825bca8eb00 Mon Sep 17 00:00:00 2001 From: MatheusVict Date: Tue, 20 Feb 2024 16:51:09 -0300 Subject: [PATCH 08/14] refactor: user repository to use adapter --- .../semprealerta/domain/ports/out/UserRepository.java | 3 +-- .../infrastructure/adpters/JpaUserRepositoryAdapter.java | 3 +-- .../infrastructure/repositories/JpaUserRepository.java | 2 +- 3 files changed, 3 insertions(+), 5 deletions(-) diff --git a/src/main/java/com/institutosemprealerta/semprealerta/domain/ports/out/UserRepository.java b/src/main/java/com/institutosemprealerta/semprealerta/domain/ports/out/UserRepository.java index de6adee..c28d170 100644 --- a/src/main/java/com/institutosemprealerta/semprealerta/domain/ports/out/UserRepository.java +++ b/src/main/java/com/institutosemprealerta/semprealerta/domain/ports/out/UserRepository.java @@ -1,7 +1,6 @@ package com.institutosemprealerta.semprealerta.domain.ports.out; -import com.institutosemprealerta.semprealerta.domain.user.User; -import org.springframework.data.jpa.repository.JpaRepository; +import com.institutosemprealerta.semprealerta.infrastructure.entity.user.User; import java.util.Optional; diff --git a/src/main/java/com/institutosemprealerta/semprealerta/infrastructure/adpters/JpaUserRepositoryAdapter.java b/src/main/java/com/institutosemprealerta/semprealerta/infrastructure/adpters/JpaUserRepositoryAdapter.java index aed38f8..6d06612 100644 --- a/src/main/java/com/institutosemprealerta/semprealerta/infrastructure/adpters/JpaUserRepositoryAdapter.java +++ b/src/main/java/com/institutosemprealerta/semprealerta/infrastructure/adpters/JpaUserRepositoryAdapter.java @@ -1,9 +1,8 @@ package com.institutosemprealerta.semprealerta.infrastructure.adpters; import com.institutosemprealerta.semprealerta.domain.ports.out.UserRepository; -import com.institutosemprealerta.semprealerta.domain.user.User; +import com.institutosemprealerta.semprealerta.infrastructure.entity.user.User; import com.institutosemprealerta.semprealerta.infrastructure.repositories.JpaUserRepository; -import org.springframework.beans.BeanUtils; import org.springframework.stereotype.Component; import java.util.Optional; diff --git a/src/main/java/com/institutosemprealerta/semprealerta/infrastructure/repositories/JpaUserRepository.java b/src/main/java/com/institutosemprealerta/semprealerta/infrastructure/repositories/JpaUserRepository.java index 1b7211f..84cf8d5 100644 --- a/src/main/java/com/institutosemprealerta/semprealerta/infrastructure/repositories/JpaUserRepository.java +++ b/src/main/java/com/institutosemprealerta/semprealerta/infrastructure/repositories/JpaUserRepository.java @@ -1,6 +1,6 @@ package com.institutosemprealerta.semprealerta.infrastructure.repositories; -import com.institutosemprealerta.semprealerta.domain.user.User; +import com.institutosemprealerta.semprealerta.infrastructure.entity.user.User; import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.data.jpa.repository.Query; import org.springframework.stereotype.Repository; From 673d8d6372b78f0544f354f938fa1f24d69d65a8 Mon Sep 17 00:00:00 2001 From: MatheusVict Date: Tue, 20 Feb 2024 16:51:33 -0300 Subject: [PATCH 09/14] feat: user service Signed-off-by: MatheusVict --- .../semprealerta/application/service/UserService.java | 2 +- .../semprealerta/application/service/impl/UserServiceImpl.java | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/main/java/com/institutosemprealerta/semprealerta/application/service/UserService.java b/src/main/java/com/institutosemprealerta/semprealerta/application/service/UserService.java index 842c8d6..97594ef 100644 --- a/src/main/java/com/institutosemprealerta/semprealerta/application/service/UserService.java +++ b/src/main/java/com/institutosemprealerta/semprealerta/application/service/UserService.java @@ -1,6 +1,6 @@ package com.institutosemprealerta.semprealerta.application.service; -import com.institutosemprealerta.semprealerta.domain.user.User; +import com.institutosemprealerta.semprealerta.infrastructure.entity.user.User; public interface UserService { void save(User user); diff --git a/src/main/java/com/institutosemprealerta/semprealerta/application/service/impl/UserServiceImpl.java b/src/main/java/com/institutosemprealerta/semprealerta/application/service/impl/UserServiceImpl.java index f09d3b3..201253e 100644 --- a/src/main/java/com/institutosemprealerta/semprealerta/application/service/impl/UserServiceImpl.java +++ b/src/main/java/com/institutosemprealerta/semprealerta/application/service/impl/UserServiceImpl.java @@ -1,7 +1,7 @@ package com.institutosemprealerta.semprealerta.application.service.impl; import com.institutosemprealerta.semprealerta.application.service.UserService; -import com.institutosemprealerta.semprealerta.domain.user.User; +import com.institutosemprealerta.semprealerta.infrastructure.entity.user.User; import com.institutosemprealerta.semprealerta.domain.ports.out.UserRepository; import org.springframework.stereotype.Service; From e6cb55f90f60966270e3423a8300ed5144421363 Mon Sep 17 00:00:00 2001 From: MatheusVict Date: Tue, 20 Feb 2024 20:03:47 -0300 Subject: [PATCH 10/14] feat: controller --- .../semprealerta/domain/model/UserDTO.java | 51 +++++++++++++++++++ .../domain/ports/model/UserDTO.java | 23 --------- .../domain/ports/out/UserResponse.java | 35 +++++++++++++ .../controllers/UserController.java | 44 ++++++++++++++++ .../semprealerta/utils/DateManipulation.java | 16 ++++++ 5 files changed, 146 insertions(+), 23 deletions(-) create mode 100644 src/main/java/com/institutosemprealerta/semprealerta/domain/model/UserDTO.java delete mode 100644 src/main/java/com/institutosemprealerta/semprealerta/domain/ports/model/UserDTO.java create mode 100644 src/main/java/com/institutosemprealerta/semprealerta/domain/ports/out/UserResponse.java create mode 100644 src/main/java/com/institutosemprealerta/semprealerta/infrastructure/controllers/UserController.java create mode 100644 src/main/java/com/institutosemprealerta/semprealerta/utils/DateManipulation.java diff --git a/src/main/java/com/institutosemprealerta/semprealerta/domain/model/UserDTO.java b/src/main/java/com/institutosemprealerta/semprealerta/domain/model/UserDTO.java new file mode 100644 index 0000000..0c10584 --- /dev/null +++ b/src/main/java/com/institutosemprealerta/semprealerta/domain/model/UserDTO.java @@ -0,0 +1,51 @@ +package com.institutosemprealerta.semprealerta.domain.model; + + +import com.fasterxml.jackson.annotation.JsonFormat; +import com.institutosemprealerta.semprealerta.infrastructure.entity.user.Address; +import com.institutosemprealerta.semprealerta.infrastructure.entity.user.Contact; +import com.institutosemprealerta.semprealerta.infrastructure.entity.user.User; +import com.institutosemprealerta.semprealerta.infrastructure.entity.user.UserRoles; +import com.institutosemprealerta.semprealerta.utils.DateManipulation; + +import java.time.LocalDate; +import java.time.format.DateTimeFormatter; + +public record UserDTO( + String name, + String email, + String password, + String phone, + String gender, + String birthDate, + UserRoles roles, + String street, + String number, + String city, + String zipCode + +) { + public User toDomain() { + LocalDate birth = DateManipulation.stringToLocalDate(birthDate); + Contact contact = new Contact( + email, + phone + ); + + Address address = new Address( + street, + number, + city, + zipCode + ); + return new User( + name, + password, + gender, + birth, + roles, + contact, + address + ); + } +} diff --git a/src/main/java/com/institutosemprealerta/semprealerta/domain/ports/model/UserDTO.java b/src/main/java/com/institutosemprealerta/semprealerta/domain/ports/model/UserDTO.java deleted file mode 100644 index 3217d07..0000000 --- a/src/main/java/com/institutosemprealerta/semprealerta/domain/ports/model/UserDTO.java +++ /dev/null @@ -1,23 +0,0 @@ -package com.institutosemprealerta.semprealerta.domain.ports.model; - - -import com.institutosemprealerta.semprealerta.infrastructure.entity.user.UserRoles; - -import java.time.LocalDate; - -public record UserDTO( - String name, - String email, - String password, - String phone, - String gender, - LocalDate birthDate, - UserRoles roles, - String street, - String number, - String city, - String zipCode - -) { - -} diff --git a/src/main/java/com/institutosemprealerta/semprealerta/domain/ports/out/UserResponse.java b/src/main/java/com/institutosemprealerta/semprealerta/domain/ports/out/UserResponse.java new file mode 100644 index 0000000..aad8f91 --- /dev/null +++ b/src/main/java/com/institutosemprealerta/semprealerta/domain/ports/out/UserResponse.java @@ -0,0 +1,35 @@ +package com.institutosemprealerta.semprealerta.domain.ports.out; + +import com.institutosemprealerta.semprealerta.infrastructure.entity.user.Address; +import com.institutosemprealerta.semprealerta.infrastructure.entity.user.Contact; +import com.institutosemprealerta.semprealerta.infrastructure.entity.user.User; +import com.institutosemprealerta.semprealerta.infrastructure.entity.user.UserRoles; +import jakarta.persistence.Embedded; + +import java.time.LocalDate; + +public record UserResponse( + int registration, + String name, + + String gender, + LocalDate birthDate, + + UserRoles roles, + + Contact contact, + Address address +) { + + public static UserResponse toResponse(User user) { + return new UserResponse( + user.getRegistration(), + user.getName(), + user.getGender(), + user.getBirthDate(), + user.getRoles(), + user.getContact(), + user.getAddress() + ); + } +} diff --git a/src/main/java/com/institutosemprealerta/semprealerta/infrastructure/controllers/UserController.java b/src/main/java/com/institutosemprealerta/semprealerta/infrastructure/controllers/UserController.java new file mode 100644 index 0000000..10626eb --- /dev/null +++ b/src/main/java/com/institutosemprealerta/semprealerta/infrastructure/controllers/UserController.java @@ -0,0 +1,44 @@ +package com.institutosemprealerta.semprealerta.infrastructure.controllers; + +import com.institutosemprealerta.semprealerta.application.service.UserService; +import com.institutosemprealerta.semprealerta.domain.model.UserDTO; +import com.institutosemprealerta.semprealerta.domain.ports.out.UserResponse; +import com.institutosemprealerta.semprealerta.infrastructure.entity.user.User; +import org.apache.catalina.connector.Response; +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.*; + +@RestController +@RequestMapping("api/v1/user") +public class UserController { + + private final UserService userService; + + public UserController(UserService userService) { + this.userService = userService; + } + + @PostMapping + public ResponseEntity createUser(@RequestBody UserDTO user) { + userService.save(user.toDomain()); + return ResponseEntity.status(Response.SC_CREATED).build(); + } + + @GetMapping("/{id}") + public ResponseEntity findById(@PathVariable int id) { + User userFound = userService.findByRegistration(id); + return ResponseEntity.ok().body(UserResponse.toResponse(userFound)); + } + + @PutMapping("/{id}") + public ResponseEntity updateUser(@PathVariable int id, @RequestBody UserDTO user) { + userService.update(id, user.toDomain()); + return ResponseEntity.status(Response.SC_NO_CONTENT).build(); + } + + @DeleteMapping("/{id}") + public ResponseEntity deleteUser(@PathVariable int id) { + userService.delete(id); + return ResponseEntity.status(Response.SC_NO_CONTENT).build(); + } +} diff --git a/src/main/java/com/institutosemprealerta/semprealerta/utils/DateManipulation.java b/src/main/java/com/institutosemprealerta/semprealerta/utils/DateManipulation.java new file mode 100644 index 0000000..9a4e128 --- /dev/null +++ b/src/main/java/com/institutosemprealerta/semprealerta/utils/DateManipulation.java @@ -0,0 +1,16 @@ +package com.institutosemprealerta.semprealerta.utils; + +import java.time.LocalDate; +import java.time.format.DateTimeFormatter; + +public abstract class DateManipulation { + public static LocalDate stringToLocalDate(String date) { + DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd"); + return LocalDate.parse(date, formatter); + } + + public static String localDateToString(LocalDate date) { + DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd"); + return date.format(formatter); + } +} From 996b705f919111aa6ed29fbe27815407abf34350 Mon Sep 17 00:00:00 2001 From: MatheusVict Date: Tue, 20 Feb 2024 20:12:43 -0300 Subject: [PATCH 11/14] feat: find by registration Signed-off-by: MatheusVict --- .../application/service/UserService.java | 1 + .../service/impl/UserServiceImpl.java | 6 ++++++ .../adpters/JpaUserRepositoryAdapter.java | 2 +- .../controllers/UserController.java | 8 +++++++- .../infrastructure/entity/user/User.java | 18 +++++++++++++----- .../repositories/JpaUserRepository.java | 2 ++ .../db/migration/V1__create-user-table.sql | 10 +++++++--- 7 files changed, 37 insertions(+), 10 deletions(-) diff --git a/src/main/java/com/institutosemprealerta/semprealerta/application/service/UserService.java b/src/main/java/com/institutosemprealerta/semprealerta/application/service/UserService.java index 97594ef..faebd2a 100644 --- a/src/main/java/com/institutosemprealerta/semprealerta/application/service/UserService.java +++ b/src/main/java/com/institutosemprealerta/semprealerta/application/service/UserService.java @@ -8,4 +8,5 @@ public interface UserService { void delete(int id); User findByRegistration(int registration); User findByEmail(String email); + User findById(int id); } diff --git a/src/main/java/com/institutosemprealerta/semprealerta/application/service/impl/UserServiceImpl.java b/src/main/java/com/institutosemprealerta/semprealerta/application/service/impl/UserServiceImpl.java index 201253e..56ea915 100644 --- a/src/main/java/com/institutosemprealerta/semprealerta/application/service/impl/UserServiceImpl.java +++ b/src/main/java/com/institutosemprealerta/semprealerta/application/service/impl/UserServiceImpl.java @@ -40,4 +40,10 @@ public User findByEmail(String email) { return this.userRepository.findByEmail(email) .orElseThrow(() -> new RuntimeException("User not found")); } + + @Override + public User findById(int id) { + return this.userRepository.findById(id) + .orElseThrow(() -> new RuntimeException("User not found")); + } } diff --git a/src/main/java/com/institutosemprealerta/semprealerta/infrastructure/adpters/JpaUserRepositoryAdapter.java b/src/main/java/com/institutosemprealerta/semprealerta/infrastructure/adpters/JpaUserRepositoryAdapter.java index 6d06612..a6b47e4 100644 --- a/src/main/java/com/institutosemprealerta/semprealerta/infrastructure/adpters/JpaUserRepositoryAdapter.java +++ b/src/main/java/com/institutosemprealerta/semprealerta/infrastructure/adpters/JpaUserRepositoryAdapter.java @@ -46,7 +46,7 @@ public void delete(int id) { @Override public Optional findByRegistration(int registration) { - return this.userRepository.findById(registration); + return this.userRepository.findByRegistration(registration); } @Override diff --git a/src/main/java/com/institutosemprealerta/semprealerta/infrastructure/controllers/UserController.java b/src/main/java/com/institutosemprealerta/semprealerta/infrastructure/controllers/UserController.java index 10626eb..2860848 100644 --- a/src/main/java/com/institutosemprealerta/semprealerta/infrastructure/controllers/UserController.java +++ b/src/main/java/com/institutosemprealerta/semprealerta/infrastructure/controllers/UserController.java @@ -26,7 +26,13 @@ public ResponseEntity createUser(@RequestBody UserDTO user) { @GetMapping("/{id}") public ResponseEntity findById(@PathVariable int id) { - User userFound = userService.findByRegistration(id); + User userFound = userService.findById(id); + return ResponseEntity.ok().body(UserResponse.toResponse(userFound)); + } + + @GetMapping("/registration/{reg}") + public ResponseEntity findByRegistration(@PathVariable int reg) { + User userFound = userService.findByRegistration(reg); return ResponseEntity.ok().body(UserResponse.toResponse(userFound)); } diff --git a/src/main/java/com/institutosemprealerta/semprealerta/infrastructure/entity/user/User.java b/src/main/java/com/institutosemprealerta/semprealerta/infrastructure/entity/user/User.java index 67dc39a..d56cd29 100644 --- a/src/main/java/com/institutosemprealerta/semprealerta/infrastructure/entity/user/User.java +++ b/src/main/java/com/institutosemprealerta/semprealerta/infrastructure/entity/user/User.java @@ -1,7 +1,8 @@ package com.institutosemprealerta.semprealerta.infrastructure.entity.user; -import com.institutosemprealerta.semprealerta.domain.ports.model.UserDTO; +import com.institutosemprealerta.semprealerta.domain.model.UserDTO; +import com.institutosemprealerta.semprealerta.utils.DateManipulation; import jakarta.persistence.*; import lombok.Getter; import lombok.Setter; @@ -10,6 +11,7 @@ import java.time.LocalDate; import java.time.LocalDateTime; +import java.util.Arrays; import java.util.HashSet; import java.util.Set; @@ -20,6 +22,9 @@ @Setter public class User { @Id + @GeneratedValue(strategy = GenerationType.IDENTITY) + private Long id; + @Column(unique = true) private int registration; private String name; private String password; @@ -53,11 +58,12 @@ public User(String name, String password, String gender, LocalDate birthDate, Us } public User fromModelToDomain(UserDTO dto) { + LocalDate birth = DateManipulation.stringToLocalDate(dto.birthDate()); return new User( dto.name(), dto.password(), dto.gender(), - dto.birthDate(), + birth, dto.roles(), new Contact(dto.email(), dto.phone()), new Address(dto.street(), dto.number(), dto.city(), dto.zipCode()) @@ -65,15 +71,17 @@ public User fromModelToDomain(UserDTO dto) { } - private int generateRegistration() { Set registrations = new HashSet<>(8); while (registrations.size() < 8) { for (int i = 0; i < 8; i++) { - registrations.add((int) (Math.random() * 10)); + int digit = (int) (Math.random() * 9) + 1; + registrations.add(digit); } } - return Integer.parseInt(registrations.toString()); + StringBuilder builder = new StringBuilder(); + registrations.forEach(builder::append); + return Integer.parseInt(builder.toString()); } } diff --git a/src/main/java/com/institutosemprealerta/semprealerta/infrastructure/repositories/JpaUserRepository.java b/src/main/java/com/institutosemprealerta/semprealerta/infrastructure/repositories/JpaUserRepository.java index 84cf8d5..ec8fcca 100644 --- a/src/main/java/com/institutosemprealerta/semprealerta/infrastructure/repositories/JpaUserRepository.java +++ b/src/main/java/com/institutosemprealerta/semprealerta/infrastructure/repositories/JpaUserRepository.java @@ -11,4 +11,6 @@ public interface JpaUserRepository extends JpaRepository { @Query("SELECT u FROM User u WHERE u.contact.email = ?1") Optional findByEmail(String email); + + Optional findByRegistration(int registration); } diff --git a/src/main/resources/db/migration/V1__create-user-table.sql b/src/main/resources/db/migration/V1__create-user-table.sql index 69b1979..0f68b6c 100644 --- a/src/main/resources/db/migration/V1__create-user-table.sql +++ b/src/main/resources/db/migration/V1__create-user-table.sql @@ -1,6 +1,7 @@ CREATE TABLE users ( - registration INTEGER NOT NULL, + id BIGINT GENERATED BY DEFAULT AS IDENTITY NOT NULL, + registration INTEGER, name VARCHAR(255), password VARCHAR(255), gender VARCHAR(255), @@ -13,5 +14,8 @@ CREATE TABLE users number VARCHAR(255), city VARCHAR(255), zip_code VARCHAR(255), - CONSTRAINT pk_users PRIMARY KEY (registration) -); \ No newline at end of file + CONSTRAINT pk_users PRIMARY KEY (id) +); + +ALTER TABLE users + ADD CONSTRAINT uc_users_registration UNIQUE (registration); \ No newline at end of file From c6447ee01f74f5c40d5f99fd110dda60904f19a4 Mon Sep 17 00:00:00 2001 From: MatheusVict Date: Tue, 20 Feb 2024 20:17:57 -0300 Subject: [PATCH 12/14] fix: update user by id dupicitly Signed-off-by: MatheusVict --- .../infrastructure/adpters/JpaUserRepositoryAdapter.java | 1 + .../semprealerta/infrastructure/entity/user/User.java | 1 - 2 files changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/com/institutosemprealerta/semprealerta/infrastructure/adpters/JpaUserRepositoryAdapter.java b/src/main/java/com/institutosemprealerta/semprealerta/infrastructure/adpters/JpaUserRepositoryAdapter.java index a6b47e4..47d79cf 100644 --- a/src/main/java/com/institutosemprealerta/semprealerta/infrastructure/adpters/JpaUserRepositoryAdapter.java +++ b/src/main/java/com/institutosemprealerta/semprealerta/infrastructure/adpters/JpaUserRepositoryAdapter.java @@ -31,6 +31,7 @@ public void update(int id, User user) { User userToUpdate = this.userRepository.findById(id) .orElseThrow(() -> new RuntimeException("User not found")); + user.setId(userToUpdate.getId()); user.setRegistration(userToUpdate.getRegistration()); this.userRepository.save(user); diff --git a/src/main/java/com/institutosemprealerta/semprealerta/infrastructure/entity/user/User.java b/src/main/java/com/institutosemprealerta/semprealerta/infrastructure/entity/user/User.java index d56cd29..893f4bc 100644 --- a/src/main/java/com/institutosemprealerta/semprealerta/infrastructure/entity/user/User.java +++ b/src/main/java/com/institutosemprealerta/semprealerta/infrastructure/entity/user/User.java @@ -17,7 +17,6 @@ @Entity @Table(name = "users") -@DynamicUpdate @Getter @Setter public class User { From 47cf7dd6c72f8deec4871eccb2ee0806d09adf9a Mon Sep 17 00:00:00 2001 From: MatheusVict Date: Tue, 20 Feb 2024 20:56:52 -0300 Subject: [PATCH 13/14] feat: add unique registration Signed-off-by: MatheusVict --- .../application/service/UserService.java | 2 +- .../service/impl/UserServiceImpl.java | 2 +- .../domain/ports/out/UserRepository.java | 2 +- .../domain/ports/out/UserResponse.java | 2 +- .../adpters/JpaUserRepositoryAdapter.java | 4 +- .../controllers/UserController.java | 2 +- .../infrastructure/entity/user/User.java | 44 +++++++++---------- .../repositories/JpaUserRepository.java | 2 +- .../semprealerta/utils/HashGeneration.java | 10 +++++ .../db/migration/V1__create-user-table.sql | 2 +- 10 files changed, 42 insertions(+), 30 deletions(-) create mode 100644 src/main/java/com/institutosemprealerta/semprealerta/utils/HashGeneration.java diff --git a/src/main/java/com/institutosemprealerta/semprealerta/application/service/UserService.java b/src/main/java/com/institutosemprealerta/semprealerta/application/service/UserService.java index faebd2a..f84b57d 100644 --- a/src/main/java/com/institutosemprealerta/semprealerta/application/service/UserService.java +++ b/src/main/java/com/institutosemprealerta/semprealerta/application/service/UserService.java @@ -6,7 +6,7 @@ public interface UserService { void save(User user); void update(int id, User user); void delete(int id); - User findByRegistration(int registration); + User findByRegistration(String registration); User findByEmail(String email); User findById(int id); } diff --git a/src/main/java/com/institutosemprealerta/semprealerta/application/service/impl/UserServiceImpl.java b/src/main/java/com/institutosemprealerta/semprealerta/application/service/impl/UserServiceImpl.java index 56ea915..732d5f3 100644 --- a/src/main/java/com/institutosemprealerta/semprealerta/application/service/impl/UserServiceImpl.java +++ b/src/main/java/com/institutosemprealerta/semprealerta/application/service/impl/UserServiceImpl.java @@ -30,7 +30,7 @@ public void delete(int id) { } @Override - public User findByRegistration(int registration) { + public User findByRegistration(String registration) { return this.userRepository.findByRegistration(registration) .orElseThrow(() -> new RuntimeException("User not found")); } diff --git a/src/main/java/com/institutosemprealerta/semprealerta/domain/ports/out/UserRepository.java b/src/main/java/com/institutosemprealerta/semprealerta/domain/ports/out/UserRepository.java index c28d170..42c6d51 100644 --- a/src/main/java/com/institutosemprealerta/semprealerta/domain/ports/out/UserRepository.java +++ b/src/main/java/com/institutosemprealerta/semprealerta/domain/ports/out/UserRepository.java @@ -9,7 +9,7 @@ public interface UserRepository { Optional findById(int id); void update(int id, User user); void delete(int id); - Optional findByRegistration(int registration); + Optional findByRegistration(String registration); Optional findByEmail(String email); } diff --git a/src/main/java/com/institutosemprealerta/semprealerta/domain/ports/out/UserResponse.java b/src/main/java/com/institutosemprealerta/semprealerta/domain/ports/out/UserResponse.java index aad8f91..d931ef0 100644 --- a/src/main/java/com/institutosemprealerta/semprealerta/domain/ports/out/UserResponse.java +++ b/src/main/java/com/institutosemprealerta/semprealerta/domain/ports/out/UserResponse.java @@ -9,7 +9,7 @@ import java.time.LocalDate; public record UserResponse( - int registration, + String registration, String name, String gender, diff --git a/src/main/java/com/institutosemprealerta/semprealerta/infrastructure/adpters/JpaUserRepositoryAdapter.java b/src/main/java/com/institutosemprealerta/semprealerta/infrastructure/adpters/JpaUserRepositoryAdapter.java index 47d79cf..f7e27a1 100644 --- a/src/main/java/com/institutosemprealerta/semprealerta/infrastructure/adpters/JpaUserRepositoryAdapter.java +++ b/src/main/java/com/institutosemprealerta/semprealerta/infrastructure/adpters/JpaUserRepositoryAdapter.java @@ -46,7 +46,7 @@ public void delete(int id) { } @Override - public Optional findByRegistration(int registration) { + public Optional findByRegistration(String registration) { return this.userRepository.findByRegistration(registration); } @@ -54,4 +54,6 @@ public Optional findByRegistration(int registration) { public Optional findByEmail(String email) { return this.userRepository.findByEmail(email); } + + } diff --git a/src/main/java/com/institutosemprealerta/semprealerta/infrastructure/controllers/UserController.java b/src/main/java/com/institutosemprealerta/semprealerta/infrastructure/controllers/UserController.java index 2860848..20855bb 100644 --- a/src/main/java/com/institutosemprealerta/semprealerta/infrastructure/controllers/UserController.java +++ b/src/main/java/com/institutosemprealerta/semprealerta/infrastructure/controllers/UserController.java @@ -31,7 +31,7 @@ public ResponseEntity findById(@PathVariable int id) { } @GetMapping("/registration/{reg}") - public ResponseEntity findByRegistration(@PathVariable int reg) { + public ResponseEntity findByRegistration(@PathVariable String reg) { User userFound = userService.findByRegistration(reg); return ResponseEntity.ok().body(UserResponse.toResponse(userFound)); } diff --git a/src/main/java/com/institutosemprealerta/semprealerta/infrastructure/entity/user/User.java b/src/main/java/com/institutosemprealerta/semprealerta/infrastructure/entity/user/User.java index 893f4bc..591c8a3 100644 --- a/src/main/java/com/institutosemprealerta/semprealerta/infrastructure/entity/user/User.java +++ b/src/main/java/com/institutosemprealerta/semprealerta/infrastructure/entity/user/User.java @@ -7,13 +7,11 @@ import lombok.Getter; import lombok.Setter; import org.hibernate.annotations.CreationTimestamp; -import org.hibernate.annotations.DynamicUpdate; import java.time.LocalDate; import java.time.LocalDateTime; -import java.util.Arrays; -import java.util.HashSet; -import java.util.Set; +import java.util.Random; + @Entity @Table(name = "users") @@ -24,7 +22,7 @@ public class User { @GeneratedValue(strategy = GenerationType.IDENTITY) private Long id; @Column(unique = true) - private int registration; + private String registration; private String name; private String password; private String gender; @@ -42,11 +40,10 @@ public class User { private LocalDateTime createdAt; public User() { - this.registration = generateRegistration(); } + public User(String name, String password, String gender, LocalDate birthDate, UserRoles roles, Contact contact, Address address) { - this.registration = this.generateRegistration(); this.name = name; this.password = password; this.gender = gender; @@ -56,6 +53,24 @@ public User(String name, String password, String gender, LocalDate birthDate, Us this.address = address; } + @PrePersist + public void generateRegistration() { + String prefix = this.name.substring(0, 3).toLowerCase() + "-"; + this.registration = generateRegistration(prefix); + } + + private String generateRegistration(String prefix) { + StringBuilder registration = new StringBuilder(prefix); + Random random = new Random(); + + for (int i = 0; i < 8; i++) { + int digit = random.nextInt(10); + registration.append(digit); + } + + return registration.toString(); + } + public User fromModelToDomain(UserDTO dto) { LocalDate birth = DateManipulation.stringToLocalDate(dto.birthDate()); return new User( @@ -68,19 +83,4 @@ public User fromModelToDomain(UserDTO dto) { new Address(dto.street(), dto.number(), dto.city(), dto.zipCode()) ); } - - - private int generateRegistration() { - Set registrations = new HashSet<>(8); - - while (registrations.size() < 8) { - for (int i = 0; i < 8; i++) { - int digit = (int) (Math.random() * 9) + 1; - registrations.add(digit); - } - } - StringBuilder builder = new StringBuilder(); - registrations.forEach(builder::append); - return Integer.parseInt(builder.toString()); - } } diff --git a/src/main/java/com/institutosemprealerta/semprealerta/infrastructure/repositories/JpaUserRepository.java b/src/main/java/com/institutosemprealerta/semprealerta/infrastructure/repositories/JpaUserRepository.java index ec8fcca..fd4967e 100644 --- a/src/main/java/com/institutosemprealerta/semprealerta/infrastructure/repositories/JpaUserRepository.java +++ b/src/main/java/com/institutosemprealerta/semprealerta/infrastructure/repositories/JpaUserRepository.java @@ -12,5 +12,5 @@ public interface JpaUserRepository extends JpaRepository { @Query("SELECT u FROM User u WHERE u.contact.email = ?1") Optional findByEmail(String email); - Optional findByRegistration(int registration); + Optional findByRegistration(String registration); } diff --git a/src/main/java/com/institutosemprealerta/semprealerta/utils/HashGeneration.java b/src/main/java/com/institutosemprealerta/semprealerta/utils/HashGeneration.java new file mode 100644 index 0000000..a884f31 --- /dev/null +++ b/src/main/java/com/institutosemprealerta/semprealerta/utils/HashGeneration.java @@ -0,0 +1,10 @@ +package com.institutosemprealerta.semprealerta.utils; + +import java.util.HashSet; +import java.util.Random; +import java.util.Set; + +public abstract class HashGeneration { + + +} diff --git a/src/main/resources/db/migration/V1__create-user-table.sql b/src/main/resources/db/migration/V1__create-user-table.sql index 0f68b6c..46d9da6 100644 --- a/src/main/resources/db/migration/V1__create-user-table.sql +++ b/src/main/resources/db/migration/V1__create-user-table.sql @@ -1,7 +1,7 @@ CREATE TABLE users ( id BIGINT GENERATED BY DEFAULT AS IDENTITY NOT NULL, - registration INTEGER, + registration VARCHAR(255), name VARCHAR(255), password VARCHAR(255), gender VARCHAR(255), From ea36a40f680ee91892e2db8fec36328c76701bbe Mon Sep 17 00:00:00 2001 From: MatheusVict Date: Tue, 20 Feb 2024 21:17:55 -0300 Subject: [PATCH 14/14] feat: validation dto Signed-off-by: MatheusVict --- .../semprealerta/domain/model/UserDTO.java | 13 ++++++++++--- .../infrastructure/controllers/UserController.java | 3 ++- .../infrastructure/entity/user/User.java | 4 ++-- src/main/resources/application-dev.yml | 4 +--- 4 files changed, 15 insertions(+), 9 deletions(-) diff --git a/src/main/java/com/institutosemprealerta/semprealerta/domain/model/UserDTO.java b/src/main/java/com/institutosemprealerta/semprealerta/domain/model/UserDTO.java index 0c10584..bb70fb5 100644 --- a/src/main/java/com/institutosemprealerta/semprealerta/domain/model/UserDTO.java +++ b/src/main/java/com/institutosemprealerta/semprealerta/domain/model/UserDTO.java @@ -7,17 +7,24 @@ import com.institutosemprealerta.semprealerta.infrastructure.entity.user.User; import com.institutosemprealerta.semprealerta.infrastructure.entity.user.UserRoles; import com.institutosemprealerta.semprealerta.utils.DateManipulation; +import jakarta.validation.constraints.*; import java.time.LocalDate; import java.time.format.DateTimeFormatter; public record UserDTO( + @NotBlank String name, + @Email String email, + @NotBlank String password, + @NotBlank String phone, String gender, - String birthDate, + @PastOrPresent + LocalDate birthDate, + @NotNull UserRoles roles, String street, String number, @@ -26,7 +33,7 @@ public record UserDTO( ) { public User toDomain() { - LocalDate birth = DateManipulation.stringToLocalDate(birthDate); + //LocalDate birth = DateManipulation.stringToLocalDate(birthDate); Contact contact = new Contact( email, phone @@ -42,7 +49,7 @@ public User toDomain() { name, password, gender, - birth, + birthDate, roles, contact, address diff --git a/src/main/java/com/institutosemprealerta/semprealerta/infrastructure/controllers/UserController.java b/src/main/java/com/institutosemprealerta/semprealerta/infrastructure/controllers/UserController.java index 20855bb..0f2fe1e 100644 --- a/src/main/java/com/institutosemprealerta/semprealerta/infrastructure/controllers/UserController.java +++ b/src/main/java/com/institutosemprealerta/semprealerta/infrastructure/controllers/UserController.java @@ -4,6 +4,7 @@ import com.institutosemprealerta.semprealerta.domain.model.UserDTO; import com.institutosemprealerta.semprealerta.domain.ports.out.UserResponse; import com.institutosemprealerta.semprealerta.infrastructure.entity.user.User; +import jakarta.validation.Valid; import org.apache.catalina.connector.Response; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.*; @@ -19,7 +20,7 @@ public UserController(UserService userService) { } @PostMapping - public ResponseEntity createUser(@RequestBody UserDTO user) { + public ResponseEntity createUser(@Valid @RequestBody UserDTO user) { userService.save(user.toDomain()); return ResponseEntity.status(Response.SC_CREATED).build(); } diff --git a/src/main/java/com/institutosemprealerta/semprealerta/infrastructure/entity/user/User.java b/src/main/java/com/institutosemprealerta/semprealerta/infrastructure/entity/user/User.java index 591c8a3..76cd971 100644 --- a/src/main/java/com/institutosemprealerta/semprealerta/infrastructure/entity/user/User.java +++ b/src/main/java/com/institutosemprealerta/semprealerta/infrastructure/entity/user/User.java @@ -72,12 +72,12 @@ private String generateRegistration(String prefix) { } public User fromModelToDomain(UserDTO dto) { - LocalDate birth = DateManipulation.stringToLocalDate(dto.birthDate()); + //LocalDate birth = DateManipulation.stringToLocalDate(dto.birthDate()); return new User( dto.name(), dto.password(), dto.gender(), - birth, + dto.birthDate(), dto.roles(), new Contact(dto.email(), dto.phone()), new Address(dto.street(), dto.number(), dto.city(), dto.zipCode()) diff --git a/src/main/resources/application-dev.yml b/src/main/resources/application-dev.yml index 3d8bee0..ace88a2 100644 --- a/src/main/resources/application-dev.yml +++ b/src/main/resources/application-dev.yml @@ -7,9 +7,7 @@ spring: hibernate: ddl-auto: update show-sql: true - properties: - hibernate: - dialect: org.hibernate.dialect.PostgreSQLDialect + flyway: enabled: true locations: classpath:db/migration