Implementation of the user port for JPA processing.
This commit is contained in:
6
pom.xml
6
pom.xml
@@ -17,6 +17,7 @@
|
|||||||
<maven.compiler.target>21</maven.compiler.target>
|
<maven.compiler.target>21</maven.compiler.target>
|
||||||
<jakarta.servlet-api.version>6.0.0</jakarta.servlet-api.version>
|
<jakarta.servlet-api.version>6.0.0</jakarta.servlet-api.version>
|
||||||
<java-jwt.version>4.4.0</java-jwt.version>
|
<java-jwt.version>4.4.0</java-jwt.version>
|
||||||
|
<postgresql.version>42.7.0</postgresql.version>
|
||||||
</properties>
|
</properties>
|
||||||
|
|
||||||
<modules>
|
<modules>
|
||||||
@@ -66,6 +67,11 @@
|
|||||||
<artifactId>java-jwt</artifactId>
|
<artifactId>java-jwt</artifactId>
|
||||||
<version>${java-jwt.version}</version>
|
<version>${java-jwt.version}</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.postgresql</groupId>
|
||||||
|
<artifactId>postgresql</artifactId>
|
||||||
|
<version>${postgresql.version}</version>
|
||||||
|
</dependency>
|
||||||
</dependencies>
|
</dependencies>
|
||||||
</dependencyManagement>
|
</dependencyManagement>
|
||||||
|
|
||||||
|
|||||||
@@ -10,4 +10,6 @@ public interface UserPort {
|
|||||||
Optional<User> findById(UUID userId);
|
Optional<User> findById(UUID userId);
|
||||||
|
|
||||||
List<User> findAll();
|
List<User> findAll();
|
||||||
|
|
||||||
|
void save(User user);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -25,5 +25,17 @@
|
|||||||
<groupId>org.springframework</groupId>
|
<groupId>org.springframework</groupId>
|
||||||
<artifactId>spring-context</artifactId>
|
<artifactId>spring-context</artifactId>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework.boot</groupId>
|
||||||
|
<artifactId>spring-boot-starter-data-jpa</artifactId>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.projectlombok</groupId>
|
||||||
|
<artifactId>lombok</artifactId>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.postgresql</groupId>
|
||||||
|
<artifactId>postgresql</artifactId>
|
||||||
|
</dependency>
|
||||||
</dependencies>
|
</dependencies>
|
||||||
</project>
|
</project>
|
||||||
|
|||||||
@@ -0,0 +1,11 @@
|
|||||||
|
package org.sportshub.infrastructure.configuration;
|
||||||
|
|
||||||
|
import org.springframework.boot.autoconfigure.domain.EntityScan;
|
||||||
|
import org.springframework.context.annotation.Configuration;
|
||||||
|
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
|
||||||
|
|
||||||
|
@Configuration
|
||||||
|
@EnableJpaRepositories("org.sportshub.infrastructure")
|
||||||
|
@EntityScan("org.sportshub.infrastructure")
|
||||||
|
public class JpaConfiguration {
|
||||||
|
}
|
||||||
@@ -10,7 +10,7 @@ import org.sportshub.domain.user.model.User;
|
|||||||
import org.sportshub.domain.user.port.UserPort;
|
import org.sportshub.domain.user.port.UserPort;
|
||||||
import org.springframework.stereotype.Component;
|
import org.springframework.stereotype.Component;
|
||||||
|
|
||||||
@Component
|
//@Component
|
||||||
public class UserInMemoryAdapter implements UserPort {
|
public class UserInMemoryAdapter implements UserPort {
|
||||||
private static final List<User> users = List.of(
|
private static final List<User> users = List.of(
|
||||||
new User(
|
new User(
|
||||||
@@ -41,4 +41,9 @@ public class UserInMemoryAdapter implements UserPort {
|
|||||||
public List<User> findAll() {
|
public List<User> findAll() {
|
||||||
return users;
|
return users;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void save(final User user) {
|
||||||
|
// Do nothing.
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,46 @@
|
|||||||
|
package org.sportshub.infrastructure.user.adapter;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Optional;
|
||||||
|
import java.util.UUID;
|
||||||
|
|
||||||
|
import org.sportshub.domain.user.model.User;
|
||||||
|
import org.sportshub.domain.user.port.UserPort;
|
||||||
|
import org.sportshub.infrastructure.user.mapper.UserMapper;
|
||||||
|
import org.sportshub.infrastructure.user.model.UserEntity;
|
||||||
|
import org.sportshub.infrastructure.user.repository.UserJpaRepository;
|
||||||
|
import org.springframework.stereotype.Component;
|
||||||
|
|
||||||
|
@Component
|
||||||
|
public class UserJpaAdapter implements UserPort {
|
||||||
|
private final UserJpaRepository userJpaRepository;
|
||||||
|
private final UserMapper userMapper;
|
||||||
|
|
||||||
|
public UserJpaAdapter(
|
||||||
|
UserJpaRepository userJpaRepository,
|
||||||
|
UserMapper userMapper
|
||||||
|
) {
|
||||||
|
this.userJpaRepository = userJpaRepository;
|
||||||
|
this.userMapper = userMapper;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Optional<User> findById(final UUID userId) {
|
||||||
|
return userJpaRepository.findById(userId)
|
||||||
|
.map(userMapper::mapFrom);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public List<User> findAll() {
|
||||||
|
return userJpaRepository.findAll()
|
||||||
|
.stream()
|
||||||
|
.map(userMapper::mapFrom)
|
||||||
|
.toList();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void save(User user) {
|
||||||
|
UserEntity userEntity = userMapper.mapTo(user);
|
||||||
|
userJpaRepository.save(userEntity);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,20 @@
|
|||||||
|
package org.sportshub.infrastructure.user.mapper;
|
||||||
|
|
||||||
|
import org.sportshub.domain.user.model.User;
|
||||||
|
import org.sportshub.infrastructure.user.model.UserEntity;
|
||||||
|
import org.springframework.stereotype.Component;
|
||||||
|
|
||||||
|
@Component
|
||||||
|
public class UserMapper {
|
||||||
|
public User mapFrom(UserEntity userEntity) {
|
||||||
|
return new User(userEntity.getId(), userEntity.getPassword(), userEntity.getRoles());
|
||||||
|
}
|
||||||
|
|
||||||
|
public UserEntity mapTo(User user) {
|
||||||
|
return new UserEntity(
|
||||||
|
user.id(),
|
||||||
|
user.password(),
|
||||||
|
user.roles()
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,40 @@
|
|||||||
|
package org.sportshub.infrastructure.user.model;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.UUID;
|
||||||
|
|
||||||
|
import org.sportshub.domain.user.model.UserRole;
|
||||||
|
|
||||||
|
import jakarta.persistence.CollectionTable;
|
||||||
|
import jakarta.persistence.Column;
|
||||||
|
import jakarta.persistence.ElementCollection;
|
||||||
|
import jakarta.persistence.Entity;
|
||||||
|
import jakarta.persistence.Id;
|
||||||
|
import jakarta.persistence.JoinColumn;
|
||||||
|
import jakarta.persistence.Table;
|
||||||
|
import lombok.AllArgsConstructor;
|
||||||
|
import lombok.Getter;
|
||||||
|
import lombok.NoArgsConstructor;
|
||||||
|
import lombok.Setter;
|
||||||
|
|
||||||
|
@Entity
|
||||||
|
@Table(name = "`user`")
|
||||||
|
@AllArgsConstructor
|
||||||
|
@NoArgsConstructor
|
||||||
|
@Getter
|
||||||
|
@Setter
|
||||||
|
public class UserEntity {
|
||||||
|
@Id
|
||||||
|
private UUID id;
|
||||||
|
|
||||||
|
@Column(nullable = false)
|
||||||
|
private String password;
|
||||||
|
|
||||||
|
@ElementCollection(targetClass = UserRole.class)
|
||||||
|
@CollectionTable(
|
||||||
|
name = "user_role",
|
||||||
|
joinColumns = @JoinColumn(name = "user_id")
|
||||||
|
)
|
||||||
|
@Column(name = "role")
|
||||||
|
private List<UserRole> roles;
|
||||||
|
}
|
||||||
@@ -0,0 +1,20 @@
|
|||||||
|
package org.sportshub.infrastructure.user.repository;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Optional;
|
||||||
|
import java.util.UUID;
|
||||||
|
|
||||||
|
import org.sportshub.infrastructure.user.model.UserEntity;
|
||||||
|
import org.springframework.data.jpa.repository.JpaRepository;
|
||||||
|
import org.springframework.data.jpa.repository.Query;
|
||||||
|
import org.springframework.data.repository.query.Param;
|
||||||
|
import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
|
@Service
|
||||||
|
public interface UserJpaRepository extends JpaRepository<UserEntity, UUID> {
|
||||||
|
@Query("SELECT u FROM UserEntity u JOIN FETCH u.roles WHERE u.id = :userId")
|
||||||
|
Optional<UserEntity> findById(@Param("userId") UUID userId);
|
||||||
|
|
||||||
|
@Query("SELECT u FROM UserEntity u JOIN FETCH u.roles")
|
||||||
|
List<UserEntity> findAll();
|
||||||
|
}
|
||||||
@@ -1 +1,12 @@
|
|||||||
|
\c sportshub_db
|
||||||
|
|
||||||
CREATE EXTENSION IF NOT EXISTS "uuid-ossp";
|
CREATE EXTENSION IF NOT EXISTS "uuid-ossp";
|
||||||
|
|
||||||
|
CREATE USER sportshub_user
|
||||||
|
WITH PASSWORD 'password'
|
||||||
|
NOCREATEDB;
|
||||||
|
|
||||||
|
GRANT SELECT, INSERT, UPDATE, DELETE
|
||||||
|
ON ALL TABLES
|
||||||
|
IN SCHEMA public
|
||||||
|
TO sportshub_user;
|
||||||
@@ -12,3 +12,10 @@ server:
|
|||||||
whitelabel:
|
whitelabel:
|
||||||
enabled: false # Disable html error responses.
|
enabled: false # Disable html error responses.
|
||||||
include-stacktrace: never
|
include-stacktrace: never
|
||||||
|
|
||||||
|
spring:
|
||||||
|
datasource:
|
||||||
|
driverClassName: org.postgresql.Driver
|
||||||
|
url: jdbc:postgresql://localhost:50001/sportshub_db
|
||||||
|
username: sportshub_user
|
||||||
|
password: password
|
||||||
|
|||||||
Reference in New Issue
Block a user