ref: app structure
This commit is contained in:
@@ -11,16 +11,17 @@
|
|||||||
<relativePath/>
|
<relativePath/>
|
||||||
</parent>
|
</parent>
|
||||||
|
|
||||||
<groupId>com.aplp</groupId>
|
<groupId>aplp.backend</groupId>
|
||||||
<artifactId>web</artifactId>
|
<artifactId>web</artifactId>
|
||||||
<version>0.1.0-SNAPSHOT</version>
|
<version>0.1.0-SNAPSHOT</version>
|
||||||
<name>aplp-backend</name>
|
<name>aplp-backend</name>
|
||||||
<description>APLP — Adaptive Personal Learning Platform (backend)</description>
|
<description>APLP — Adaptive Personal Learning Platform (backend)</description>
|
||||||
|
|
||||||
<properties>
|
<properties>
|
||||||
<java.version>21</java.version>
|
<java.version>17</java.version>
|
||||||
<jjwt.version>0.12.6</jjwt.version>
|
<jjwt.version>0.12.6</jjwt.version>
|
||||||
<springdoc.version>2.8.6</springdoc.version>
|
<springdoc.version>2.8.6</springdoc.version>
|
||||||
|
<mapstruct.version>1.6.3</mapstruct.version>
|
||||||
</properties>
|
</properties>
|
||||||
|
|
||||||
<dependencies>
|
<dependencies>
|
||||||
@@ -50,6 +51,12 @@
|
|||||||
<version>${springdoc.version}</version>
|
<version>${springdoc.version}</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>aplp.backend</groupId>
|
||||||
|
<artifactId>core</artifactId>
|
||||||
|
<version>1.0-SNAPSHOT</version>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.flywaydb</groupId>
|
<groupId>org.flywaydb</groupId>
|
||||||
<artifactId>flyway-core</artifactId>
|
<artifactId>flyway-core</artifactId>
|
||||||
@@ -58,6 +65,10 @@
|
|||||||
<groupId>org.flywaydb</groupId>
|
<groupId>org.flywaydb</groupId>
|
||||||
<artifactId>flyway-database-postgresql</artifactId>
|
<artifactId>flyway-database-postgresql</artifactId>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework.boot</groupId>
|
||||||
|
<artifactId>spring-boot-flyway</artifactId>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>io.jsonwebtoken</groupId>
|
<groupId>io.jsonwebtoken</groupId>
|
||||||
@@ -98,6 +109,11 @@
|
|||||||
<artifactId>spring-security-test</artifactId>
|
<artifactId>spring-security-test</artifactId>
|
||||||
<scope>test</scope>
|
<scope>test</scope>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework.boot</groupId>
|
||||||
|
<artifactId>spring-boot-webmvc-test</artifactId>
|
||||||
|
<scope>test</scope>
|
||||||
|
</dependency>
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.springframework.boot</groupId>
|
<groupId>org.springframework.boot</groupId>
|
||||||
<artifactId>spring-boot-devtools</artifactId>
|
<artifactId>spring-boot-devtools</artifactId>
|
||||||
@@ -108,6 +124,17 @@
|
|||||||
<artifactId>lombok</artifactId>
|
<artifactId>lombok</artifactId>
|
||||||
<scope>provided</scope>
|
<scope>provided</scope>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.mapstruct</groupId>
|
||||||
|
<artifactId>mapstruct</artifactId>
|
||||||
|
<version>${mapstruct.version}</version>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.mapstruct</groupId>
|
||||||
|
<artifactId>mapstruct-processor</artifactId>
|
||||||
|
<version>${mapstruct.version}</version>
|
||||||
|
<scope>provided</scope>
|
||||||
|
</dependency>
|
||||||
</dependencies>
|
</dependencies>
|
||||||
|
|
||||||
<build>
|
<build>
|
||||||
|
|||||||
+3
-3
@@ -1,4 +1,4 @@
|
|||||||
package com.aplp.backend;
|
package aplp.backend.web;
|
||||||
|
|
||||||
import org.springframework.boot.SpringApplication;
|
import org.springframework.boot.SpringApplication;
|
||||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||||
@@ -6,9 +6,9 @@ import org.springframework.boot.context.properties.ConfigurationPropertiesScan;
|
|||||||
|
|
||||||
@SpringBootApplication
|
@SpringBootApplication
|
||||||
@ConfigurationPropertiesScan
|
@ConfigurationPropertiesScan
|
||||||
public class AplpBackendApplication {
|
public class AplpBackendWebApplication {
|
||||||
|
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
SpringApplication.run(AplpBackendApplication.class, args);
|
SpringApplication.run(AplpBackendWebApplication.class, args);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
+5
-3
@@ -1,6 +1,8 @@
|
|||||||
package com.aplp.backend.common.api;
|
package aplp.backend.web.common.api;
|
||||||
|
|
||||||
import com.aplp.backend.common.error.DomainException;
|
import aplp.backend.core.common.exception.DomainException;
|
||||||
|
import aplp.backend.core.common.exception.ErrorCode;
|
||||||
|
import aplp.backend.core.common.response.ApiError;
|
||||||
import jakarta.servlet.http.HttpServletRequest;
|
import jakarta.servlet.http.HttpServletRequest;
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
@@ -26,7 +28,7 @@ public class GlobalExceptionHandler {
|
|||||||
|
|
||||||
@ExceptionHandler(DomainException.class)
|
@ExceptionHandler(DomainException.class)
|
||||||
public ResponseEntity<ApiError> handleDomainException(DomainException ex) {
|
public ResponseEntity<ApiError> handleDomainException(DomainException ex) {
|
||||||
HttpStatus status = ex.code().httpStatus();
|
HttpStatus status = HttpStatus.valueOf(ex.code().status());
|
||||||
return build(status, ex.code().name(), ex.getMessage());
|
return build(status, ex.code().name(), ex.getMessage());
|
||||||
}
|
}
|
||||||
|
|
||||||
+1
-1
@@ -1,4 +1,4 @@
|
|||||||
package com.aplp.backend.common.config;
|
package aplp.backend.web.common.config;
|
||||||
|
|
||||||
import org.springframework.context.annotation.Bean;
|
import org.springframework.context.annotation.Bean;
|
||||||
import org.springframework.context.annotation.Configuration;
|
import org.springframework.context.annotation.Configuration;
|
||||||
+1
-1
@@ -1,4 +1,4 @@
|
|||||||
package com.aplp.backend.common.config;
|
package aplp.backend.web.common.config;
|
||||||
|
|
||||||
import io.swagger.v3.oas.annotations.OpenAPIDefinition;
|
import io.swagger.v3.oas.annotations.OpenAPIDefinition;
|
||||||
import io.swagger.v3.oas.annotations.enums.SecuritySchemeType;
|
import io.swagger.v3.oas.annotations.enums.SecuritySchemeType;
|
||||||
+1
-1
@@ -1,4 +1,4 @@
|
|||||||
package com.aplp.backend.common.security;
|
package aplp.backend.web.common.security;
|
||||||
|
|
||||||
public record AuthenticatedUser(Long userId, String email, String displayName) {
|
public record AuthenticatedUser(Long userId, String email, String displayName) {
|
||||||
}
|
}
|
||||||
+1
-1
@@ -1,4 +1,4 @@
|
|||||||
package com.aplp.backend.common.security;
|
package aplp.backend.web.common.security;
|
||||||
|
|
||||||
import org.springframework.boot.context.properties.ConfigurationProperties;
|
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||||
|
|
||||||
+1
-1
@@ -1,4 +1,4 @@
|
|||||||
package com.aplp.backend.common.security;
|
package aplp.backend.web.common.security;
|
||||||
|
|
||||||
import jakarta.servlet.FilterChain;
|
import jakarta.servlet.FilterChain;
|
||||||
import jakarta.servlet.ServletException;
|
import jakarta.servlet.ServletException;
|
||||||
+1
-1
@@ -1,4 +1,4 @@
|
|||||||
package com.aplp.backend.common.security;
|
package aplp.backend.web.common.security;
|
||||||
|
|
||||||
import java.time.Instant;
|
import java.time.Instant;
|
||||||
|
|
||||||
+1
-1
@@ -1,4 +1,4 @@
|
|||||||
package com.aplp.backend.common.security;
|
package aplp.backend.web.common.security;
|
||||||
|
|
||||||
import org.springframework.boot.context.properties.ConfigurationProperties;
|
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||||
|
|
||||||
+1
-1
@@ -1,4 +1,4 @@
|
|||||||
package com.aplp.backend.common.security;
|
package aplp.backend.web.common.security;
|
||||||
|
|
||||||
import io.jsonwebtoken.Claims;
|
import io.jsonwebtoken.Claims;
|
||||||
import io.jsonwebtoken.JwtException;
|
import io.jsonwebtoken.JwtException;
|
||||||
+1
-1
@@ -1,4 +1,4 @@
|
|||||||
package com.aplp.backend.common.security;
|
package aplp.backend.web.common.security;
|
||||||
|
|
||||||
import org.springframework.boot.security.autoconfigure.web.servlet.PathRequest;
|
import org.springframework.boot.security.autoconfigure.web.servlet.PathRequest;
|
||||||
import org.springframework.context.annotation.Bean;
|
import org.springframework.context.annotation.Bean;
|
||||||
+3
-3
@@ -1,7 +1,7 @@
|
|||||||
package com.aplp.backend.common.security;
|
package aplp.backend.web.common.security;
|
||||||
|
|
||||||
import com.aplp.backend.common.api.ApiError;
|
import aplp.backend.core.common.exception.ErrorCode;
|
||||||
import com.aplp.backend.common.api.ErrorCode;
|
import aplp.backend.core.common.response.ApiError;
|
||||||
import jakarta.servlet.http.HttpServletResponse;
|
import jakarta.servlet.http.HttpServletResponse;
|
||||||
import org.slf4j.MDC;
|
import org.slf4j.MDC;
|
||||||
import org.springframework.http.HttpStatus;
|
import org.springframework.http.HttpStatus;
|
||||||
+3
-3
@@ -1,7 +1,7 @@
|
|||||||
package com.aplp.backend.common.security;
|
package aplp.backend.web.common.security;
|
||||||
|
|
||||||
import com.aplp.backend.common.api.ErrorCode;
|
import aplp.backend.core.common.exception.DomainException;
|
||||||
import com.aplp.backend.common.error.DomainException;
|
import aplp.backend.core.common.exception.ErrorCode;
|
||||||
import org.springframework.security.core.Authentication;
|
import org.springframework.security.core.Authentication;
|
||||||
import org.springframework.security.core.context.SecurityContextHolder;
|
import org.springframework.security.core.context.SecurityContextHolder;
|
||||||
|
|
||||||
+1
-1
@@ -1,4 +1,4 @@
|
|||||||
package com.aplp.backend.common.web;
|
package aplp.backend.web.common.web;
|
||||||
|
|
||||||
import jakarta.servlet.FilterChain;
|
import jakarta.servlet.FilterChain;
|
||||||
import jakarta.servlet.ServletException;
|
import jakarta.servlet.ServletException;
|
||||||
+9
-5
@@ -1,6 +1,10 @@
|
|||||||
package com.aplp.backend.identity.api;
|
package aplp.backend.web.identity.api.controller;
|
||||||
|
|
||||||
import com.aplp.backend.identity.application.AuthService;
|
import aplp.backend.web.identity.application.dtos.AuthResponse;
|
||||||
|
import aplp.backend.web.identity.application.dtos.LoginRequest;
|
||||||
|
import aplp.backend.web.identity.application.dtos.RefreshTokenRequest;
|
||||||
|
import aplp.backend.web.identity.application.dtos.RegisterRequest;
|
||||||
|
import aplp.backend.web.identity.application.services.AuthService;
|
||||||
import jakarta.validation.Valid;
|
import jakarta.validation.Valid;
|
||||||
import org.springframework.http.HttpStatus;
|
import org.springframework.http.HttpStatus;
|
||||||
import org.springframework.web.bind.annotation.PostMapping;
|
import org.springframework.web.bind.annotation.PostMapping;
|
||||||
@@ -22,17 +26,17 @@ public class AuthController {
|
|||||||
@PostMapping("/register")
|
@PostMapping("/register")
|
||||||
@ResponseStatus(HttpStatus.CREATED)
|
@ResponseStatus(HttpStatus.CREATED)
|
||||||
public AuthResponse register(@Valid @RequestBody RegisterRequest request) {
|
public AuthResponse register(@Valid @RequestBody RegisterRequest request) {
|
||||||
return AuthResponse.from(authService.register(request.email(), request.password(), request.displayName()));
|
return authService.register(request.email(), request.password(), request.displayName());
|
||||||
}
|
}
|
||||||
|
|
||||||
@PostMapping("/login")
|
@PostMapping("/login")
|
||||||
public AuthResponse login(@Valid @RequestBody LoginRequest request) {
|
public AuthResponse login(@Valid @RequestBody LoginRequest request) {
|
||||||
return AuthResponse.from(authService.login(request.email(), request.password()));
|
return authService.login(request.email(), request.password());
|
||||||
}
|
}
|
||||||
|
|
||||||
@PostMapping("/refresh")
|
@PostMapping("/refresh")
|
||||||
public AuthResponse refresh(@Valid @RequestBody RefreshTokenRequest request) {
|
public AuthResponse refresh(@Valid @RequestBody RefreshTokenRequest request) {
|
||||||
return AuthResponse.from(authService.refresh(request.refreshToken()));
|
return authService.refresh(request.refreshToken());
|
||||||
}
|
}
|
||||||
|
|
||||||
@PostMapping("/logout")
|
@PostMapping("/logout")
|
||||||
@@ -0,0 +1,13 @@
|
|||||||
|
package aplp.backend.web.identity.application.dtos;
|
||||||
|
|
||||||
|
public record AuthResponse(
|
||||||
|
String accessToken,
|
||||||
|
String refreshToken,
|
||||||
|
String tokenType,
|
||||||
|
long expiresIn,
|
||||||
|
UserDto user
|
||||||
|
) {
|
||||||
|
|
||||||
|
public record UserDto(Long id, String email, String displayName) {
|
||||||
|
}
|
||||||
|
}
|
||||||
+1
-1
@@ -1,4 +1,4 @@
|
|||||||
package com.aplp.backend.identity.api;
|
package aplp.backend.web.identity.application.dtos;
|
||||||
|
|
||||||
import jakarta.validation.constraints.Email;
|
import jakarta.validation.constraints.Email;
|
||||||
import jakarta.validation.constraints.NotBlank;
|
import jakarta.validation.constraints.NotBlank;
|
||||||
+1
-1
@@ -1,4 +1,4 @@
|
|||||||
package com.aplp.backend.identity.api;
|
package aplp.backend.web.identity.application.dtos;
|
||||||
|
|
||||||
import jakarta.validation.constraints.NotBlank;
|
import jakarta.validation.constraints.NotBlank;
|
||||||
|
|
||||||
+1
-1
@@ -1,4 +1,4 @@
|
|||||||
package com.aplp.backend.identity.api;
|
package aplp.backend.web.identity.application.dtos;
|
||||||
|
|
||||||
import jakarta.validation.constraints.Email;
|
import jakarta.validation.constraints.Email;
|
||||||
import jakarta.validation.constraints.NotBlank;
|
import jakarta.validation.constraints.NotBlank;
|
||||||
@@ -0,0 +1,11 @@
|
|||||||
|
package aplp.backend.web.identity.application.mappers;
|
||||||
|
|
||||||
|
import aplp.backend.web.identity.application.dtos.AuthResponse;
|
||||||
|
import aplp.backend.web.identity.domain.entities.User;
|
||||||
|
import org.mapstruct.Mapper;
|
||||||
|
|
||||||
|
@Mapper(componentModel = "spring")
|
||||||
|
public interface AuthMapper {
|
||||||
|
|
||||||
|
AuthResponse.UserDto toUserDto(User user);
|
||||||
|
}
|
||||||
+29
-17
@@ -1,16 +1,20 @@
|
|||||||
package com.aplp.backend.identity.application;
|
package aplp.backend.web.identity.application.services;
|
||||||
|
|
||||||
import com.aplp.backend.common.security.AuthenticatedUser;
|
import aplp.backend.core.common.util.TokenHasher;
|
||||||
import com.aplp.backend.common.security.JwtClaims;
|
import aplp.backend.web.common.security.AuthenticatedUser;
|
||||||
import com.aplp.backend.common.security.JwtProperties;
|
import aplp.backend.web.common.security.JwtClaims;
|
||||||
import com.aplp.backend.common.security.JwtTokenProvider;
|
import aplp.backend.web.common.security.JwtProperties;
|
||||||
import com.aplp.backend.identity.domain.EmailAlreadyExistsException;
|
import aplp.backend.web.common.security.JwtTokenProvider;
|
||||||
import com.aplp.backend.identity.domain.InvalidCredentialsException;
|
import aplp.backend.web.identity.application.dtos.AuthResponse;
|
||||||
import com.aplp.backend.identity.domain.RefreshToken;
|
import aplp.backend.web.identity.application.mappers.AuthMapper;
|
||||||
import com.aplp.backend.identity.domain.RefreshTokenInvalidException;
|
import aplp.backend.web.identity.domain.exceptions.EmailAlreadyExistsException;
|
||||||
import com.aplp.backend.identity.domain.RefreshTokenRepository;
|
import aplp.backend.web.identity.domain.exceptions.InvalidCredentialsException;
|
||||||
import com.aplp.backend.identity.domain.User;
|
import aplp.backend.web.identity.domain.entities.RefreshToken;
|
||||||
import com.aplp.backend.identity.domain.UserRepository;
|
import aplp.backend.web.identity.domain.exceptions.RefreshTokenInvalidException;
|
||||||
|
import aplp.backend.web.identity.domain.repositories.RefreshTokenRepository;
|
||||||
|
import aplp.backend.web.identity.domain.entities.User;
|
||||||
|
import aplp.backend.web.identity.domain.repositories.UserRepository;
|
||||||
|
import aplp.backend.web.learner.application.services.LearnerProvisioner;
|
||||||
import org.springframework.security.crypto.password.PasswordEncoder;
|
import org.springframework.security.crypto.password.PasswordEncoder;
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
import org.springframework.transaction.annotation.Transactional;
|
import org.springframework.transaction.annotation.Transactional;
|
||||||
@@ -27,6 +31,7 @@ public class AuthService {
|
|||||||
private final JwtTokenProvider tokenProvider;
|
private final JwtTokenProvider tokenProvider;
|
||||||
private final JwtProperties jwtProperties;
|
private final JwtProperties jwtProperties;
|
||||||
private final LearnerProvisioner learnerProvisioner;
|
private final LearnerProvisioner learnerProvisioner;
|
||||||
|
private final AuthMapper authMapper;
|
||||||
private final Clock clock;
|
private final Clock clock;
|
||||||
|
|
||||||
public AuthService(UserRepository userRepository,
|
public AuthService(UserRepository userRepository,
|
||||||
@@ -35,6 +40,7 @@ public class AuthService {
|
|||||||
JwtTokenProvider tokenProvider,
|
JwtTokenProvider tokenProvider,
|
||||||
JwtProperties jwtProperties,
|
JwtProperties jwtProperties,
|
||||||
LearnerProvisioner learnerProvisioner,
|
LearnerProvisioner learnerProvisioner,
|
||||||
|
AuthMapper authMapper,
|
||||||
Clock clock) {
|
Clock clock) {
|
||||||
this.userRepository = userRepository;
|
this.userRepository = userRepository;
|
||||||
this.refreshTokenRepository = refreshTokenRepository;
|
this.refreshTokenRepository = refreshTokenRepository;
|
||||||
@@ -42,11 +48,12 @@ public class AuthService {
|
|||||||
this.tokenProvider = tokenProvider;
|
this.tokenProvider = tokenProvider;
|
||||||
this.jwtProperties = jwtProperties;
|
this.jwtProperties = jwtProperties;
|
||||||
this.learnerProvisioner = learnerProvisioner;
|
this.learnerProvisioner = learnerProvisioner;
|
||||||
|
this.authMapper = authMapper;
|
||||||
this.clock = clock;
|
this.clock = clock;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Transactional
|
@Transactional
|
||||||
public AuthResult register(String email, String rawPassword, String displayName) {
|
public AuthResponse register(String email, String rawPassword, String displayName) {
|
||||||
Instant now = clock.instant();
|
Instant now = clock.instant();
|
||||||
String normalizedEmail = email.trim().toLowerCase();
|
String normalizedEmail = email.trim().toLowerCase();
|
||||||
if (userRepository.existsByEmail(normalizedEmail)) {
|
if (userRepository.existsByEmail(normalizedEmail)) {
|
||||||
@@ -59,7 +66,7 @@ public class AuthService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Transactional
|
@Transactional
|
||||||
public AuthResult login(String email, String rawPassword) {
|
public AuthResponse login(String email, String rawPassword) {
|
||||||
String normalizedEmail = email.trim().toLowerCase();
|
String normalizedEmail = email.trim().toLowerCase();
|
||||||
User user = userRepository.findByEmail(normalizedEmail).orElseThrow(InvalidCredentialsException::new);
|
User user = userRepository.findByEmail(normalizedEmail).orElseThrow(InvalidCredentialsException::new);
|
||||||
if (!passwordEncoder.matches(rawPassword, user.passwordHash())) {
|
if (!passwordEncoder.matches(rawPassword, user.passwordHash())) {
|
||||||
@@ -69,7 +76,7 @@ public class AuthService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Transactional
|
@Transactional
|
||||||
public AuthResult refresh(String rawRefreshToken) {
|
public AuthResponse refresh(String rawRefreshToken) {
|
||||||
Instant now = clock.instant();
|
Instant now = clock.instant();
|
||||||
RefreshToken stored = refreshTokenRepository.findByTokenHash(TokenHasher.sha256Hex(rawRefreshToken))
|
RefreshToken stored = refreshTokenRepository.findByTokenHash(TokenHasher.sha256Hex(rawRefreshToken))
|
||||||
.orElseThrow(RefreshTokenInvalidException::new);
|
.orElseThrow(RefreshTokenInvalidException::new);
|
||||||
@@ -112,7 +119,7 @@ public class AuthService {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private AuthResult issueTokens(User user) {
|
private AuthResponse issueTokens(User user) {
|
||||||
Instant now = clock.instant();
|
Instant now = clock.instant();
|
||||||
AuthenticatedUser principal = new AuthenticatedUser(user.id(), user.email(), user.displayName());
|
AuthenticatedUser principal = new AuthenticatedUser(user.id(), user.email(), user.displayName());
|
||||||
String accessToken = tokenProvider.createAccessToken(principal);
|
String accessToken = tokenProvider.createAccessToken(principal);
|
||||||
@@ -123,6 +130,11 @@ public class AuthService {
|
|||||||
now.plus(jwtProperties.refreshTokenTtl()),
|
now.plus(jwtProperties.refreshTokenTtl()),
|
||||||
now);
|
now);
|
||||||
refreshTokenRepository.save(refresh);
|
refreshTokenRepository.save(refresh);
|
||||||
return new AuthResult(accessToken, rawRefreshToken, jwtProperties.accessTokenTtl().toSeconds(), principal);
|
return new AuthResponse(
|
||||||
|
accessToken,
|
||||||
|
rawRefreshToken,
|
||||||
|
"Bearer",
|
||||||
|
jwtProperties.accessTokenTtl().toSeconds(),
|
||||||
|
authMapper.toUserDto(user));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
+31
-5
@@ -1,15 +1,41 @@
|
|||||||
package com.aplp.backend.identity.domain;
|
package aplp.backend.web.identity.domain.entities;
|
||||||
|
|
||||||
|
import jakarta.persistence.Column;
|
||||||
|
import jakarta.persistence.Entity;
|
||||||
|
import jakarta.persistence.GeneratedValue;
|
||||||
|
import jakarta.persistence.GenerationType;
|
||||||
|
import jakarta.persistence.Id;
|
||||||
|
import jakarta.persistence.Table;
|
||||||
|
import lombok.Getter;
|
||||||
|
|
||||||
import java.time.Instant;
|
import java.time.Instant;
|
||||||
|
|
||||||
|
@Getter
|
||||||
|
@Entity
|
||||||
|
@Table(name = "refresh_token")
|
||||||
public class RefreshToken {
|
public class RefreshToken {
|
||||||
|
|
||||||
|
@Id
|
||||||
|
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
||||||
private Long id;
|
private Long id;
|
||||||
private final Long userId;
|
|
||||||
private final String tokenHash;
|
@Column(name = "user_id", nullable = false)
|
||||||
private final Instant expiresAt;
|
private Long userId;
|
||||||
|
|
||||||
|
@Column(name = "token_hash", nullable = false, unique = true, length = 64)
|
||||||
|
private String tokenHash;
|
||||||
|
|
||||||
|
@Column(name = "expires_at", nullable = false)
|
||||||
|
private Instant expiresAt;
|
||||||
|
|
||||||
|
@Column(name = "revoked_at")
|
||||||
private Instant revokedAt;
|
private Instant revokedAt;
|
||||||
private final Instant createdAt;
|
|
||||||
|
@Column(name = "created_at", nullable = false, updatable = false)
|
||||||
|
private Instant createdAt;
|
||||||
|
|
||||||
|
protected RefreshToken() {
|
||||||
|
}
|
||||||
|
|
||||||
private RefreshToken(Long id, Long userId, String tokenHash, Instant expiresAt, Instant revokedAt, Instant createdAt) {
|
private RefreshToken(Long id, Long userId, String tokenHash, Instant expiresAt, Instant revokedAt, Instant createdAt) {
|
||||||
this.id = id;
|
this.id = id;
|
||||||
+34
-3
@@ -1,17 +1,48 @@
|
|||||||
package com.aplp.backend.identity.domain;
|
package aplp.backend.web.identity.domain.entities;
|
||||||
|
|
||||||
|
import jakarta.persistence.Column;
|
||||||
|
import jakarta.persistence.Entity;
|
||||||
|
import jakarta.persistence.EnumType;
|
||||||
|
import jakarta.persistence.Enumerated;
|
||||||
|
import jakarta.persistence.GeneratedValue;
|
||||||
|
import jakarta.persistence.GenerationType;
|
||||||
|
import jakarta.persistence.Id;
|
||||||
|
import jakarta.persistence.Table;
|
||||||
|
import lombok.Getter;
|
||||||
|
|
||||||
import java.time.Instant;
|
import java.time.Instant;
|
||||||
|
|
||||||
|
@Getter
|
||||||
|
@Entity
|
||||||
|
@Table(name = "users")
|
||||||
public class User {
|
public class User {
|
||||||
|
|
||||||
|
@Id
|
||||||
|
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
||||||
private Long id;
|
private Long id;
|
||||||
private final String email;
|
|
||||||
|
@Column(nullable = false, unique = true, length = 320)
|
||||||
|
private String email;
|
||||||
|
|
||||||
|
@Column(name = "password_hash", nullable = false)
|
||||||
private String passwordHash;
|
private String passwordHash;
|
||||||
|
|
||||||
|
@Column(name = "display_name", nullable = false, length = 100)
|
||||||
private String displayName;
|
private String displayName;
|
||||||
|
|
||||||
|
@Enumerated(EnumType.STRING)
|
||||||
|
@Column(nullable = false, length = 20)
|
||||||
private UserStatus status;
|
private UserStatus status;
|
||||||
private final Instant createdAt;
|
|
||||||
|
@Column(name = "created_at", nullable = false, updatable = false)
|
||||||
|
private Instant createdAt;
|
||||||
|
|
||||||
|
@Column(name = "updated_at", nullable = false)
|
||||||
private Instant updatedAt;
|
private Instant updatedAt;
|
||||||
|
|
||||||
|
protected User() {
|
||||||
|
}
|
||||||
|
|
||||||
private User(Long id, String email, String passwordHash, String displayName,
|
private User(Long id, String email, String passwordHash, String displayName,
|
||||||
UserStatus status, Instant createdAt, Instant updatedAt) {
|
UserStatus status, Instant createdAt, Instant updatedAt) {
|
||||||
this.id = id;
|
this.id = id;
|
||||||
+1
-1
@@ -1,4 +1,4 @@
|
|||||||
package com.aplp.backend.identity.domain;
|
package aplp.backend.web.identity.domain.entities;
|
||||||
|
|
||||||
public enum UserStatus {
|
public enum UserStatus {
|
||||||
ACTIVE,
|
ACTIVE,
|
||||||
+3
-3
@@ -1,7 +1,7 @@
|
|||||||
package com.aplp.backend.identity.domain;
|
package aplp.backend.web.identity.domain.exceptions;
|
||||||
|
|
||||||
import com.aplp.backend.common.api.ErrorCode;
|
import aplp.backend.core.common.exception.DomainException;
|
||||||
import com.aplp.backend.common.error.DomainException;
|
import aplp.backend.core.common.exception.ErrorCode;
|
||||||
|
|
||||||
public class EmailAlreadyExistsException extends DomainException {
|
public class EmailAlreadyExistsException extends DomainException {
|
||||||
|
|
||||||
+3
-3
@@ -1,7 +1,7 @@
|
|||||||
package com.aplp.backend.identity.domain;
|
package aplp.backend.web.identity.domain.exceptions;
|
||||||
|
|
||||||
import com.aplp.backend.common.api.ErrorCode;
|
import aplp.backend.core.common.exception.DomainException;
|
||||||
import com.aplp.backend.common.error.DomainException;
|
import aplp.backend.core.common.exception.ErrorCode;
|
||||||
|
|
||||||
public class InvalidCredentialsException extends DomainException {
|
public class InvalidCredentialsException extends DomainException {
|
||||||
|
|
||||||
+3
-3
@@ -1,7 +1,7 @@
|
|||||||
package com.aplp.backend.identity.domain;
|
package aplp.backend.web.identity.domain.exceptions;
|
||||||
|
|
||||||
import com.aplp.backend.common.api.ErrorCode;
|
import aplp.backend.core.common.exception.DomainException;
|
||||||
import com.aplp.backend.common.error.DomainException;
|
import aplp.backend.core.common.exception.ErrorCode;
|
||||||
|
|
||||||
public class RefreshTokenInvalidException extends DomainException {
|
public class RefreshTokenInvalidException extends DomainException {
|
||||||
|
|
||||||
+3
-1
@@ -1,4 +1,6 @@
|
|||||||
package com.aplp.backend.identity.domain;
|
package aplp.backend.web.identity.domain.repositories;
|
||||||
|
|
||||||
|
import aplp.backend.web.identity.domain.entities.RefreshToken;
|
||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Optional;
|
import java.util.Optional;
|
||||||
+3
-1
@@ -1,4 +1,6 @@
|
|||||||
package com.aplp.backend.identity.domain;
|
package aplp.backend.web.identity.domain.repositories;
|
||||||
|
|
||||||
|
import aplp.backend.web.identity.domain.entities.User;
|
||||||
|
|
||||||
import java.util.Optional;
|
import java.util.Optional;
|
||||||
|
|
||||||
+14
@@ -0,0 +1,14 @@
|
|||||||
|
package aplp.backend.web.identity.infrastructure.persistence;
|
||||||
|
|
||||||
|
import aplp.backend.web.identity.domain.entities.RefreshToken;
|
||||||
|
import org.springframework.data.jpa.repository.JpaRepository;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Optional;
|
||||||
|
|
||||||
|
public interface RefreshTokenJpaRepository extends JpaRepository<RefreshToken, Long> {
|
||||||
|
|
||||||
|
Optional<RefreshToken> findByTokenHash(String tokenHash);
|
||||||
|
|
||||||
|
List<RefreshToken> findAllByUserId(Long userId);
|
||||||
|
}
|
||||||
+33
@@ -0,0 +1,33 @@
|
|||||||
|
package aplp.backend.web.identity.infrastructure.persistence;
|
||||||
|
|
||||||
|
import aplp.backend.web.identity.domain.entities.RefreshToken;
|
||||||
|
import aplp.backend.web.identity.domain.repositories.RefreshTokenRepository;
|
||||||
|
import org.springframework.stereotype.Repository;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Optional;
|
||||||
|
|
||||||
|
@Repository
|
||||||
|
public class RefreshTokenRepositoryImpl implements RefreshTokenRepository {
|
||||||
|
|
||||||
|
private final RefreshTokenJpaRepository jpaRepository;
|
||||||
|
|
||||||
|
public RefreshTokenRepositoryImpl(RefreshTokenJpaRepository jpaRepository) {
|
||||||
|
this.jpaRepository = jpaRepository;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Optional<RefreshToken> findByTokenHash(String tokenHash) {
|
||||||
|
return jpaRepository.findByTokenHash(tokenHash);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public List<RefreshToken> findAllByUserId(Long userId) {
|
||||||
|
return jpaRepository.findAllByUserId(userId);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public RefreshToken save(RefreshToken token) {
|
||||||
|
return jpaRepository.save(token);
|
||||||
|
}
|
||||||
|
}
|
||||||
+11
@@ -0,0 +1,11 @@
|
|||||||
|
package aplp.backend.web.identity.infrastructure.persistence;
|
||||||
|
|
||||||
|
import aplp.backend.web.identity.domain.entities.User;
|
||||||
|
import org.springframework.data.jpa.repository.JpaRepository;
|
||||||
|
|
||||||
|
public interface UserJpaRepository extends JpaRepository<User, Long> {
|
||||||
|
|
||||||
|
boolean existsByEmail(String email);
|
||||||
|
|
||||||
|
java.util.Optional<User> findByEmail(String email);
|
||||||
|
}
|
||||||
+37
@@ -0,0 +1,37 @@
|
|||||||
|
package aplp.backend.web.identity.infrastructure.persistence;
|
||||||
|
|
||||||
|
import aplp.backend.web.identity.domain.entities.User;
|
||||||
|
import aplp.backend.web.identity.domain.repositories.UserRepository;
|
||||||
|
import org.springframework.stereotype.Repository;
|
||||||
|
|
||||||
|
import java.util.Optional;
|
||||||
|
|
||||||
|
@Repository
|
||||||
|
public class UserRepositoryImpl implements UserRepository {
|
||||||
|
|
||||||
|
private final UserJpaRepository jpaRepository;
|
||||||
|
|
||||||
|
public UserRepositoryImpl(UserJpaRepository jpaRepository) {
|
||||||
|
this.jpaRepository = jpaRepository;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Optional<User> findByEmail(String email) {
|
||||||
|
return jpaRepository.findByEmail(email);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Optional<User> findById(Long id) {
|
||||||
|
return jpaRepository.findById(id);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean existsByEmail(String email) {
|
||||||
|
return jpaRepository.existsByEmail(email);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public User save(User user) {
|
||||||
|
return jpaRepository.save(user);
|
||||||
|
}
|
||||||
|
}
|
||||||
+11
-6
@@ -1,7 +1,10 @@
|
|||||||
package com.aplp.backend.learner.api;
|
package aplp.backend.web.learner.api.controller;
|
||||||
|
|
||||||
import com.aplp.backend.common.security.SecurityUtils;
|
import aplp.backend.web.common.security.SecurityUtils;
|
||||||
import com.aplp.backend.learner.application.LearnerService;
|
import aplp.backend.web.learner.application.dtos.LearnerProfileResponse;
|
||||||
|
import aplp.backend.web.learner.application.dtos.UpdateLearnerProfileRequest;
|
||||||
|
import aplp.backend.web.learner.application.mappers.LearnerMapper;
|
||||||
|
import aplp.backend.web.learner.application.services.LearnerService;
|
||||||
import jakarta.validation.Valid;
|
import jakarta.validation.Valid;
|
||||||
import org.springframework.web.bind.annotation.GetMapping;
|
import org.springframework.web.bind.annotation.GetMapping;
|
||||||
import org.springframework.web.bind.annotation.PatchMapping;
|
import org.springframework.web.bind.annotation.PatchMapping;
|
||||||
@@ -14,19 +17,21 @@ import org.springframework.web.bind.annotation.RestController;
|
|||||||
public class LearnerController {
|
public class LearnerController {
|
||||||
|
|
||||||
private final LearnerService learnerService;
|
private final LearnerService learnerService;
|
||||||
|
private final LearnerMapper learnerMapper;
|
||||||
|
|
||||||
public LearnerController(LearnerService learnerService) {
|
public LearnerController(LearnerService learnerService, LearnerMapper learnerMapper) {
|
||||||
this.learnerService = learnerService;
|
this.learnerService = learnerService;
|
||||||
|
this.learnerMapper = learnerMapper;
|
||||||
}
|
}
|
||||||
|
|
||||||
@GetMapping
|
@GetMapping
|
||||||
public LearnerProfileResponse getMyProfile() {
|
public LearnerProfileResponse getMyProfile() {
|
||||||
return LearnerProfileResponse.from(learnerService.getProfile(SecurityUtils.currentUserId()));
|
return learnerMapper.toResponse(learnerService.getProfile(SecurityUtils.currentUserId()));
|
||||||
}
|
}
|
||||||
|
|
||||||
@PatchMapping
|
@PatchMapping
|
||||||
public LearnerProfileResponse updateMyProfile(@Valid @RequestBody UpdateLearnerProfileRequest request) {
|
public LearnerProfileResponse updateMyProfile(@Valid @RequestBody UpdateLearnerProfileRequest request) {
|
||||||
return LearnerProfileResponse.from(
|
return learnerMapper.toResponse(
|
||||||
learnerService.updateDisplayName(SecurityUtils.currentUserId(), request.displayName()));
|
learnerService.updateDisplayName(SecurityUtils.currentUserId(), request.displayName()));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -0,0 +1,8 @@
|
|||||||
|
package aplp.backend.web.learner.application.dtos;
|
||||||
|
|
||||||
|
public record LearnerProfileResponse(
|
||||||
|
Long id,
|
||||||
|
Long userId,
|
||||||
|
String displayName
|
||||||
|
) {
|
||||||
|
}
|
||||||
+1
-1
@@ -1,4 +1,4 @@
|
|||||||
package com.aplp.backend.learner.api;
|
package aplp.backend.web.learner.application.dtos;
|
||||||
|
|
||||||
import jakarta.validation.constraints.NotBlank;
|
import jakarta.validation.constraints.NotBlank;
|
||||||
import jakarta.validation.constraints.Size;
|
import jakarta.validation.constraints.Size;
|
||||||
@@ -0,0 +1,11 @@
|
|||||||
|
package aplp.backend.web.learner.application.mappers;
|
||||||
|
|
||||||
|
import aplp.backend.web.learner.application.dtos.LearnerProfileResponse;
|
||||||
|
import aplp.backend.web.learner.domain.entities.Learner;
|
||||||
|
import org.mapstruct.Mapper;
|
||||||
|
|
||||||
|
@Mapper(componentModel = "spring")
|
||||||
|
public interface LearnerMapper {
|
||||||
|
|
||||||
|
LearnerProfileResponse toResponse(Learner learner);
|
||||||
|
}
|
||||||
+5
-7
@@ -1,25 +1,23 @@
|
|||||||
package com.aplp.backend.learner.application;
|
package aplp.backend.web.learner.application.services;
|
||||||
|
|
||||||
import com.aplp.backend.identity.application.LearnerProvisioner;
|
import aplp.backend.web.learner.domain.entities.Learner;
|
||||||
import com.aplp.backend.learner.domain.Learner;
|
import aplp.backend.web.learner.domain.repositories.LearnerRepository;
|
||||||
import com.aplp.backend.learner.domain.LearnerRepository;
|
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
import org.springframework.transaction.annotation.Transactional;
|
import org.springframework.transaction.annotation.Transactional;
|
||||||
|
|
||||||
import java.time.Clock;
|
import java.time.Clock;
|
||||||
|
|
||||||
@Service
|
@Service
|
||||||
public class LearnerProvisionerImpl implements LearnerProvisioner {
|
public class LearnerProvisioner {
|
||||||
|
|
||||||
private final LearnerRepository learnerRepository;
|
private final LearnerRepository learnerRepository;
|
||||||
private final Clock clock;
|
private final Clock clock;
|
||||||
|
|
||||||
public LearnerProvisionerImpl(LearnerRepository learnerRepository, Clock clock) {
|
public LearnerProvisioner(LearnerRepository learnerRepository, Clock clock) {
|
||||||
this.learnerRepository = learnerRepository;
|
this.learnerRepository = learnerRepository;
|
||||||
this.clock = clock;
|
this.clock = clock;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
@Transactional
|
@Transactional
|
||||||
public void provision(Long userId, String displayName) {
|
public void provision(Long userId, String displayName) {
|
||||||
learnerRepository.save(Learner.create(userId, displayName, clock.instant()));
|
learnerRepository.save(Learner.create(userId, displayName, clock.instant()));
|
||||||
+7
-12
@@ -1,8 +1,8 @@
|
|||||||
package com.aplp.backend.learner.application;
|
package aplp.backend.web.learner.application.services;
|
||||||
|
|
||||||
import com.aplp.backend.learner.domain.Learner;
|
import aplp.backend.web.learner.domain.entities.Learner;
|
||||||
import com.aplp.backend.learner.domain.LearnerNotFoundException;
|
import aplp.backend.web.learner.domain.exceptions.LearnerNotFoundException;
|
||||||
import com.aplp.backend.learner.domain.LearnerRepository;
|
import aplp.backend.web.learner.domain.repositories.LearnerRepository;
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
import org.springframework.transaction.annotation.Transactional;
|
import org.springframework.transaction.annotation.Transactional;
|
||||||
|
|
||||||
@@ -20,20 +20,15 @@ public class LearnerService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Transactional(readOnly = true)
|
@Transactional(readOnly = true)
|
||||||
public LearnerProfile getProfile(Long userId) {
|
public Learner getProfile(Long userId) {
|
||||||
return learnerRepository.findByUserId(userId)
|
return learnerRepository.findByUserId(userId)
|
||||||
.map(LearnerService::toProfile)
|
|
||||||
.orElseThrow(LearnerNotFoundException::new);
|
.orElseThrow(LearnerNotFoundException::new);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Transactional
|
@Transactional
|
||||||
public LearnerProfile updateDisplayName(Long userId, String displayName) {
|
public Learner updateDisplayName(Long userId, String displayName) {
|
||||||
Learner learner = learnerRepository.findByUserId(userId).orElseThrow(LearnerNotFoundException::new);
|
Learner learner = learnerRepository.findByUserId(userId).orElseThrow(LearnerNotFoundException::new);
|
||||||
learner.updateDisplayName(displayName, clock.instant());
|
learner.updateDisplayName(displayName, clock.instant());
|
||||||
return toProfile(learnerRepository.save(learner));
|
return learnerRepository.save(learner);
|
||||||
}
|
|
||||||
|
|
||||||
private static LearnerProfile toProfile(Learner learner) {
|
|
||||||
return new LearnerProfile(learner.id(), learner.userId(), learner.displayName());
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
+27
-3
@@ -1,15 +1,39 @@
|
|||||||
package com.aplp.backend.learner.domain;
|
package aplp.backend.web.learner.domain.entities;
|
||||||
|
|
||||||
|
import jakarta.persistence.Column;
|
||||||
|
import jakarta.persistence.Entity;
|
||||||
|
import jakarta.persistence.GeneratedValue;
|
||||||
|
import jakarta.persistence.GenerationType;
|
||||||
|
import jakarta.persistence.Id;
|
||||||
|
import jakarta.persistence.Table;
|
||||||
|
import lombok.Getter;
|
||||||
|
|
||||||
import java.time.Instant;
|
import java.time.Instant;
|
||||||
|
|
||||||
|
@Getter
|
||||||
|
@Entity
|
||||||
|
@Table(name = "learner")
|
||||||
public class Learner {
|
public class Learner {
|
||||||
|
|
||||||
|
@Id
|
||||||
|
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
||||||
private Long id;
|
private Long id;
|
||||||
private final Long userId;
|
|
||||||
|
@Column(name = "user_id", nullable = false, unique = true)
|
||||||
|
private Long userId;
|
||||||
|
|
||||||
|
@Column(name = "display_name", nullable = false, length = 100)
|
||||||
private String displayName;
|
private String displayName;
|
||||||
private final Instant createdAt;
|
|
||||||
|
@Column(name = "created_at", nullable = false, updatable = false)
|
||||||
|
private Instant createdAt;
|
||||||
|
|
||||||
|
@Column(name = "updated_at", nullable = false)
|
||||||
private Instant updatedAt;
|
private Instant updatedAt;
|
||||||
|
|
||||||
|
protected Learner() {
|
||||||
|
}
|
||||||
|
|
||||||
private Learner(Long id, Long userId, String displayName, Instant createdAt, Instant updatedAt) {
|
private Learner(Long id, Long userId, String displayName, Instant createdAt, Instant updatedAt) {
|
||||||
this.id = id;
|
this.id = id;
|
||||||
this.userId = userId;
|
this.userId = userId;
|
||||||
+3
-3
@@ -1,7 +1,7 @@
|
|||||||
package com.aplp.backend.learner.domain;
|
package aplp.backend.web.learner.domain.exceptions;
|
||||||
|
|
||||||
import com.aplp.backend.common.api.ErrorCode;
|
import aplp.backend.core.common.exception.DomainException;
|
||||||
import com.aplp.backend.common.error.DomainException;
|
import aplp.backend.core.common.exception.ErrorCode;
|
||||||
|
|
||||||
public class LearnerNotFoundException extends DomainException {
|
public class LearnerNotFoundException extends DomainException {
|
||||||
|
|
||||||
+3
-1
@@ -1,4 +1,6 @@
|
|||||||
package com.aplp.backend.learner.domain;
|
package aplp.backend.web.learner.domain.repositories;
|
||||||
|
|
||||||
|
import aplp.backend.web.learner.domain.entities.Learner;
|
||||||
|
|
||||||
import java.util.Optional;
|
import java.util.Optional;
|
||||||
|
|
||||||
+11
@@ -0,0 +1,11 @@
|
|||||||
|
package aplp.backend.web.learner.infrastructure.persistence;
|
||||||
|
|
||||||
|
import aplp.backend.web.learner.domain.entities.Learner;
|
||||||
|
import org.springframework.data.jpa.repository.JpaRepository;
|
||||||
|
|
||||||
|
import java.util.Optional;
|
||||||
|
|
||||||
|
public interface LearnerJpaRepository extends JpaRepository<Learner, Long> {
|
||||||
|
|
||||||
|
Optional<Learner> findByUserId(Long userId);
|
||||||
|
}
|
||||||
+27
@@ -0,0 +1,27 @@
|
|||||||
|
package aplp.backend.web.learner.infrastructure.persistence;
|
||||||
|
|
||||||
|
import aplp.backend.web.learner.domain.entities.Learner;
|
||||||
|
import aplp.backend.web.learner.domain.repositories.LearnerRepository;
|
||||||
|
import org.springframework.stereotype.Repository;
|
||||||
|
|
||||||
|
import java.util.Optional;
|
||||||
|
|
||||||
|
@Repository
|
||||||
|
public class LearnerRepositoryImpl implements LearnerRepository {
|
||||||
|
|
||||||
|
private final LearnerJpaRepository jpaRepository;
|
||||||
|
|
||||||
|
public LearnerRepositoryImpl(LearnerJpaRepository jpaRepository) {
|
||||||
|
this.jpaRepository = jpaRepository;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Optional<Learner> findByUserId(Long userId) {
|
||||||
|
return jpaRepository.findByUserId(userId);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Learner save(Learner learner) {
|
||||||
|
return jpaRepository.save(learner);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,17 +0,0 @@
|
|||||||
package com.aplp.backend.common.api;
|
|
||||||
|
|
||||||
import java.time.Instant;
|
|
||||||
|
|
||||||
public record ApiError(
|
|
||||||
Instant timestamp,
|
|
||||||
int status,
|
|
||||||
String error,
|
|
||||||
String code,
|
|
||||||
String message,
|
|
||||||
String traceId
|
|
||||||
) {
|
|
||||||
|
|
||||||
public static ApiError of(int status, String error, String code, String message, String traceId) {
|
|
||||||
return new ApiError(Instant.now(), status, error, code, message, traceId);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,28 +0,0 @@
|
|||||||
package com.aplp.backend.common.api;
|
|
||||||
|
|
||||||
import org.springframework.http.HttpStatus;
|
|
||||||
|
|
||||||
public enum ErrorCode {
|
|
||||||
|
|
||||||
INVALID_ARGUMENT(HttpStatus.BAD_REQUEST),
|
|
||||||
VALIDATION_FAILED(HttpStatus.BAD_REQUEST),
|
|
||||||
MALFORMED_REQUEST(HttpStatus.BAD_REQUEST),
|
|
||||||
UNAUTHENTICATED(HttpStatus.UNAUTHORIZED),
|
|
||||||
INVALID_CREDENTIALS(HttpStatus.UNAUTHORIZED),
|
|
||||||
INVALID_REFRESH_TOKEN(HttpStatus.UNAUTHORIZED),
|
|
||||||
ACCESS_DENIED(HttpStatus.FORBIDDEN),
|
|
||||||
EMAIL_ALREADY_EXISTS(HttpStatus.CONFLICT),
|
|
||||||
USER_NOT_FOUND(HttpStatus.NOT_FOUND),
|
|
||||||
LEARNER_NOT_FOUND(HttpStatus.NOT_FOUND),
|
|
||||||
INTERNAL_ERROR(HttpStatus.INTERNAL_SERVER_ERROR);
|
|
||||||
|
|
||||||
private final HttpStatus httpStatus;
|
|
||||||
|
|
||||||
ErrorCode(HttpStatus httpStatus) {
|
|
||||||
this.httpStatus = httpStatus;
|
|
||||||
}
|
|
||||||
|
|
||||||
public HttpStatus httpStatus() {
|
|
||||||
return httpStatus;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,17 +0,0 @@
|
|||||||
package com.aplp.backend.common.error;
|
|
||||||
|
|
||||||
import com.aplp.backend.common.api.ErrorCode;
|
|
||||||
|
|
||||||
public class DomainException extends RuntimeException {
|
|
||||||
|
|
||||||
private final ErrorCode code;
|
|
||||||
|
|
||||||
public DomainException(ErrorCode code, String message) {
|
|
||||||
super(message);
|
|
||||||
this.code = code;
|
|
||||||
}
|
|
||||||
|
|
||||||
public ErrorCode code() {
|
|
||||||
return code;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,27 +0,0 @@
|
|||||||
package com.aplp.backend.identity.api;
|
|
||||||
|
|
||||||
import com.aplp.backend.identity.application.AuthResult;
|
|
||||||
|
|
||||||
public record AuthResponse(
|
|
||||||
String accessToken,
|
|
||||||
String refreshToken,
|
|
||||||
String tokenType,
|
|
||||||
long expiresIn,
|
|
||||||
UserDto user
|
|
||||||
) {
|
|
||||||
|
|
||||||
public static AuthResponse from(AuthResult result) {
|
|
||||||
return new AuthResponse(
|
|
||||||
result.accessToken(),
|
|
||||||
result.refreshToken(),
|
|
||||||
"Bearer",
|
|
||||||
result.expiresInSeconds(),
|
|
||||||
new UserDto(
|
|
||||||
result.user().userId(),
|
|
||||||
result.user().email(),
|
|
||||||
result.user().displayName()));
|
|
||||||
}
|
|
||||||
|
|
||||||
public record UserDto(Long id, String email, String displayName) {
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,11 +0,0 @@
|
|||||||
package com.aplp.backend.identity.application;
|
|
||||||
|
|
||||||
import com.aplp.backend.common.security.AuthenticatedUser;
|
|
||||||
|
|
||||||
public record AuthResult(
|
|
||||||
String accessToken,
|
|
||||||
String refreshToken,
|
|
||||||
long expiresInSeconds,
|
|
||||||
AuthenticatedUser user
|
|
||||||
) {
|
|
||||||
}
|
|
||||||
@@ -1,6 +0,0 @@
|
|||||||
package com.aplp.backend.identity.application;
|
|
||||||
|
|
||||||
public interface LearnerProvisioner {
|
|
||||||
|
|
||||||
void provision(Long userId, String displayName);
|
|
||||||
}
|
|
||||||
@@ -1,20 +0,0 @@
|
|||||||
package com.aplp.backend.identity.application;
|
|
||||||
|
|
||||||
import java.security.MessageDigest;
|
|
||||||
import java.security.NoSuchAlgorithmException;
|
|
||||||
import java.util.HexFormat;
|
|
||||||
|
|
||||||
final class TokenHasher {
|
|
||||||
|
|
||||||
private TokenHasher() {
|
|
||||||
}
|
|
||||||
|
|
||||||
static String sha256Hex(String value) {
|
|
||||||
try {
|
|
||||||
byte[] digest = MessageDigest.getInstance("SHA-256").digest(value.getBytes(java.nio.charset.StandardCharsets.UTF_8));
|
|
||||||
return HexFormat.of().formatHex(digest);
|
|
||||||
} catch (NoSuchAlgorithmException e) {
|
|
||||||
throw new IllegalStateException("SHA-256 not available", e);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,95 +0,0 @@
|
|||||||
package com.aplp.backend.identity.persistence;
|
|
||||||
|
|
||||||
import jakarta.persistence.Column;
|
|
||||||
import jakarta.persistence.Entity;
|
|
||||||
import jakarta.persistence.GeneratedValue;
|
|
||||||
import jakarta.persistence.GenerationType;
|
|
||||||
import jakarta.persistence.Id;
|
|
||||||
import jakarta.persistence.Table;
|
|
||||||
|
|
||||||
import java.time.Instant;
|
|
||||||
|
|
||||||
@Entity
|
|
||||||
@Table(name = "refresh_token")
|
|
||||||
public class RefreshTokenJpaEntity {
|
|
||||||
|
|
||||||
@Id
|
|
||||||
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
|
||||||
private Long id;
|
|
||||||
|
|
||||||
@Column(name = "user_id", nullable = false)
|
|
||||||
private Long userId;
|
|
||||||
|
|
||||||
@Column(name = "token_hash", nullable = false, unique = true, length = 64)
|
|
||||||
private String tokenHash;
|
|
||||||
|
|
||||||
@Column(name = "expires_at", nullable = false)
|
|
||||||
private Instant expiresAt;
|
|
||||||
|
|
||||||
@Column(name = "revoked_at")
|
|
||||||
private Instant revokedAt;
|
|
||||||
|
|
||||||
@Column(name = "created_at", nullable = false, updatable = false)
|
|
||||||
private Instant createdAt;
|
|
||||||
|
|
||||||
protected RefreshTokenJpaEntity() {
|
|
||||||
}
|
|
||||||
|
|
||||||
public RefreshTokenJpaEntity(Long id, Long userId, String tokenHash, Instant expiresAt,
|
|
||||||
Instant revokedAt, Instant createdAt) {
|
|
||||||
this.id = id;
|
|
||||||
this.userId = userId;
|
|
||||||
this.tokenHash = tokenHash;
|
|
||||||
this.expiresAt = expiresAt;
|
|
||||||
this.revokedAt = revokedAt;
|
|
||||||
this.createdAt = createdAt;
|
|
||||||
}
|
|
||||||
|
|
||||||
public Long getId() {
|
|
||||||
return id;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setId(Long id) {
|
|
||||||
this.id = id;
|
|
||||||
}
|
|
||||||
|
|
||||||
public Long getUserId() {
|
|
||||||
return userId;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setUserId(Long userId) {
|
|
||||||
this.userId = userId;
|
|
||||||
}
|
|
||||||
|
|
||||||
public String getTokenHash() {
|
|
||||||
return tokenHash;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setTokenHash(String tokenHash) {
|
|
||||||
this.tokenHash = tokenHash;
|
|
||||||
}
|
|
||||||
|
|
||||||
public Instant getExpiresAt() {
|
|
||||||
return expiresAt;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setExpiresAt(Instant expiresAt) {
|
|
||||||
this.expiresAt = expiresAt;
|
|
||||||
}
|
|
||||||
|
|
||||||
public Instant getRevokedAt() {
|
|
||||||
return revokedAt;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setRevokedAt(Instant revokedAt) {
|
|
||||||
this.revokedAt = revokedAt;
|
|
||||||
}
|
|
||||||
|
|
||||||
public Instant getCreatedAt() {
|
|
||||||
return createdAt;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setCreatedAt(Instant createdAt) {
|
|
||||||
this.createdAt = createdAt;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,13 +0,0 @@
|
|||||||
package com.aplp.backend.identity.persistence;
|
|
||||||
|
|
||||||
import org.springframework.data.jpa.repository.JpaRepository;
|
|
||||||
|
|
||||||
import java.util.List;
|
|
||||||
import java.util.Optional;
|
|
||||||
|
|
||||||
public interface RefreshTokenJpaRepository extends JpaRepository<RefreshTokenJpaEntity, Long> {
|
|
||||||
|
|
||||||
Optional<RefreshTokenJpaEntity> findByTokenHash(String tokenHash);
|
|
||||||
|
|
||||||
List<RefreshTokenJpaEntity> findAllByUserId(Long userId);
|
|
||||||
}
|
|
||||||
@@ -1,57 +0,0 @@
|
|||||||
package com.aplp.backend.identity.persistence;
|
|
||||||
|
|
||||||
import com.aplp.backend.identity.domain.RefreshToken;
|
|
||||||
import com.aplp.backend.identity.domain.RefreshTokenRepository;
|
|
||||||
import org.springframework.stereotype.Repository;
|
|
||||||
|
|
||||||
import java.util.List;
|
|
||||||
import java.util.Optional;
|
|
||||||
|
|
||||||
@Repository
|
|
||||||
public class RefreshTokenRepositoryImpl implements RefreshTokenRepository {
|
|
||||||
|
|
||||||
private final RefreshTokenJpaRepository jpaRepository;
|
|
||||||
|
|
||||||
public RefreshTokenRepositoryImpl(RefreshTokenJpaRepository jpaRepository) {
|
|
||||||
this.jpaRepository = jpaRepository;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public Optional<RefreshToken> findByTokenHash(String tokenHash) {
|
|
||||||
return jpaRepository.findByTokenHash(tokenHash).map(RefreshTokenRepositoryImpl::toDomain);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public List<RefreshToken> findAllByUserId(Long userId) {
|
|
||||||
return jpaRepository.findAllByUserId(userId).stream()
|
|
||||||
.map(RefreshTokenRepositoryImpl::toDomain)
|
|
||||||
.toList();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public RefreshToken save(RefreshToken token) {
|
|
||||||
RefreshTokenJpaEntity entity = toEntity(token);
|
|
||||||
RefreshTokenJpaEntity saved = jpaRepository.save(entity);
|
|
||||||
return toDomain(saved);
|
|
||||||
}
|
|
||||||
|
|
||||||
private static RefreshTokenJpaEntity toEntity(RefreshToken token) {
|
|
||||||
return new RefreshTokenJpaEntity(
|
|
||||||
token.id(),
|
|
||||||
token.userId(),
|
|
||||||
token.tokenHash(),
|
|
||||||
token.expiresAt(),
|
|
||||||
token.revokedAt(),
|
|
||||||
token.createdAt());
|
|
||||||
}
|
|
||||||
|
|
||||||
private static RefreshToken toDomain(RefreshTokenJpaEntity entity) {
|
|
||||||
return RefreshToken.reconstruct(
|
|
||||||
entity.getId(),
|
|
||||||
entity.getUserId(),
|
|
||||||
entity.getTokenHash(),
|
|
||||||
entity.getExpiresAt(),
|
|
||||||
entity.getRevokedAt(),
|
|
||||||
entity.getCreatedAt());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,111 +0,0 @@
|
|||||||
package com.aplp.backend.identity.persistence;
|
|
||||||
|
|
||||||
import com.aplp.backend.identity.domain.UserStatus;
|
|
||||||
import jakarta.persistence.Column;
|
|
||||||
import jakarta.persistence.Entity;
|
|
||||||
import jakarta.persistence.EnumType;
|
|
||||||
import jakarta.persistence.Enumerated;
|
|
||||||
import jakarta.persistence.GeneratedValue;
|
|
||||||
import jakarta.persistence.GenerationType;
|
|
||||||
import jakarta.persistence.Id;
|
|
||||||
import jakarta.persistence.Table;
|
|
||||||
|
|
||||||
import java.time.Instant;
|
|
||||||
|
|
||||||
@Entity
|
|
||||||
@Table(name = "users")
|
|
||||||
public class UserJpaEntity {
|
|
||||||
|
|
||||||
@Id
|
|
||||||
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
|
||||||
private Long id;
|
|
||||||
|
|
||||||
@Column(nullable = false, unique = true, length = 320)
|
|
||||||
private String email;
|
|
||||||
|
|
||||||
@Column(name = "password_hash", nullable = false)
|
|
||||||
private String passwordHash;
|
|
||||||
|
|
||||||
@Column(name = "display_name", nullable = false, length = 100)
|
|
||||||
private String displayName;
|
|
||||||
|
|
||||||
@Enumerated(EnumType.STRING)
|
|
||||||
@Column(nullable = false, length = 20)
|
|
||||||
private UserStatus status;
|
|
||||||
|
|
||||||
@Column(name = "created_at", nullable = false, updatable = false)
|
|
||||||
private Instant createdAt;
|
|
||||||
|
|
||||||
@Column(name = "updated_at", nullable = false)
|
|
||||||
private Instant updatedAt;
|
|
||||||
|
|
||||||
protected UserJpaEntity() {
|
|
||||||
}
|
|
||||||
|
|
||||||
public UserJpaEntity(Long id, String email, String passwordHash, String displayName,
|
|
||||||
UserStatus status, Instant createdAt, Instant updatedAt) {
|
|
||||||
this.id = id;
|
|
||||||
this.email = email;
|
|
||||||
this.passwordHash = passwordHash;
|
|
||||||
this.displayName = displayName;
|
|
||||||
this.status = status;
|
|
||||||
this.createdAt = createdAt;
|
|
||||||
this.updatedAt = updatedAt;
|
|
||||||
}
|
|
||||||
|
|
||||||
public Long getId() {
|
|
||||||
return id;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setId(Long id) {
|
|
||||||
this.id = id;
|
|
||||||
}
|
|
||||||
|
|
||||||
public String getEmail() {
|
|
||||||
return email;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setEmail(String email) {
|
|
||||||
this.email = email;
|
|
||||||
}
|
|
||||||
|
|
||||||
public String getPasswordHash() {
|
|
||||||
return passwordHash;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setPasswordHash(String passwordHash) {
|
|
||||||
this.passwordHash = passwordHash;
|
|
||||||
}
|
|
||||||
|
|
||||||
public String getDisplayName() {
|
|
||||||
return displayName;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setDisplayName(String displayName) {
|
|
||||||
this.displayName = displayName;
|
|
||||||
}
|
|
||||||
|
|
||||||
public UserStatus getStatus() {
|
|
||||||
return status;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setStatus(UserStatus status) {
|
|
||||||
this.status = status;
|
|
||||||
}
|
|
||||||
|
|
||||||
public Instant getCreatedAt() {
|
|
||||||
return createdAt;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setCreatedAt(Instant createdAt) {
|
|
||||||
this.createdAt = createdAt;
|
|
||||||
}
|
|
||||||
|
|
||||||
public Instant getUpdatedAt() {
|
|
||||||
return updatedAt;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setUpdatedAt(Instant updatedAt) {
|
|
||||||
this.updatedAt = updatedAt;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,10 +0,0 @@
|
|||||||
package com.aplp.backend.identity.persistence;
|
|
||||||
|
|
||||||
import org.springframework.data.jpa.repository.JpaRepository;
|
|
||||||
|
|
||||||
public interface UserJpaRepository extends JpaRepository<UserJpaEntity, Long> {
|
|
||||||
|
|
||||||
boolean existsByEmail(String email);
|
|
||||||
|
|
||||||
java.util.Optional<UserJpaEntity> findByEmail(String email);
|
|
||||||
}
|
|
||||||
@@ -1,61 +0,0 @@
|
|||||||
package com.aplp.backend.identity.persistence;
|
|
||||||
|
|
||||||
import com.aplp.backend.identity.domain.User;
|
|
||||||
import com.aplp.backend.identity.domain.UserRepository;
|
|
||||||
import org.springframework.stereotype.Repository;
|
|
||||||
|
|
||||||
import java.util.Optional;
|
|
||||||
|
|
||||||
@Repository
|
|
||||||
public class UserRepositoryImpl implements UserRepository {
|
|
||||||
|
|
||||||
private final UserJpaRepository jpaRepository;
|
|
||||||
|
|
||||||
public UserRepositoryImpl(UserJpaRepository jpaRepository) {
|
|
||||||
this.jpaRepository = jpaRepository;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public Optional<User> findByEmail(String email) {
|
|
||||||
return jpaRepository.findByEmail(email).map(UserRepositoryImpl::toDomain);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public Optional<User> findById(Long id) {
|
|
||||||
return jpaRepository.findById(id).map(UserRepositoryImpl::toDomain);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public boolean existsByEmail(String email) {
|
|
||||||
return jpaRepository.existsByEmail(email);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public User save(User user) {
|
|
||||||
UserJpaEntity entity = toEntity(user);
|
|
||||||
UserJpaEntity saved = jpaRepository.save(entity);
|
|
||||||
return toDomain(saved);
|
|
||||||
}
|
|
||||||
|
|
||||||
private static UserJpaEntity toEntity(User user) {
|
|
||||||
return new UserJpaEntity(
|
|
||||||
user.id(),
|
|
||||||
user.email(),
|
|
||||||
user.passwordHash(),
|
|
||||||
user.displayName(),
|
|
||||||
user.status(),
|
|
||||||
user.createdAt(),
|
|
||||||
user.updatedAt());
|
|
||||||
}
|
|
||||||
|
|
||||||
private static User toDomain(UserJpaEntity entity) {
|
|
||||||
return User.reconstruct(
|
|
||||||
entity.getId(),
|
|
||||||
entity.getEmail(),
|
|
||||||
entity.getPasswordHash(),
|
|
||||||
entity.getDisplayName(),
|
|
||||||
entity.getStatus(),
|
|
||||||
entity.getCreatedAt(),
|
|
||||||
entity.getUpdatedAt());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,14 +0,0 @@
|
|||||||
package com.aplp.backend.learner.api;
|
|
||||||
|
|
||||||
import com.aplp.backend.learner.application.LearnerProfile;
|
|
||||||
|
|
||||||
public record LearnerProfileResponse(
|
|
||||||
Long id,
|
|
||||||
Long userId,
|
|
||||||
String displayName
|
|
||||||
) {
|
|
||||||
|
|
||||||
public static LearnerProfileResponse from(LearnerProfile profile) {
|
|
||||||
return new LearnerProfileResponse(profile.id(), profile.userId(), profile.displayName());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,8 +0,0 @@
|
|||||||
package com.aplp.backend.learner.application;
|
|
||||||
|
|
||||||
public record LearnerProfile(
|
|
||||||
Long id,
|
|
||||||
Long userId,
|
|
||||||
String displayName
|
|
||||||
) {
|
|
||||||
}
|
|
||||||
@@ -1,82 +0,0 @@
|
|||||||
package com.aplp.backend.learner.persistence;
|
|
||||||
|
|
||||||
import jakarta.persistence.Column;
|
|
||||||
import jakarta.persistence.Entity;
|
|
||||||
import jakarta.persistence.GeneratedValue;
|
|
||||||
import jakarta.persistence.GenerationType;
|
|
||||||
import jakarta.persistence.Id;
|
|
||||||
import jakarta.persistence.Table;
|
|
||||||
|
|
||||||
import java.time.Instant;
|
|
||||||
|
|
||||||
@Entity
|
|
||||||
@Table(name = "learner")
|
|
||||||
public class LearnerJpaEntity {
|
|
||||||
|
|
||||||
@Id
|
|
||||||
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
|
||||||
private Long id;
|
|
||||||
|
|
||||||
@Column(name = "user_id", nullable = false, unique = true)
|
|
||||||
private Long userId;
|
|
||||||
|
|
||||||
@Column(name = "display_name", nullable = false, length = 100)
|
|
||||||
private String displayName;
|
|
||||||
|
|
||||||
@Column(name = "created_at", nullable = false, updatable = false)
|
|
||||||
private Instant createdAt;
|
|
||||||
|
|
||||||
@Column(name = "updated_at", nullable = false)
|
|
||||||
private Instant updatedAt;
|
|
||||||
|
|
||||||
protected LearnerJpaEntity() {
|
|
||||||
}
|
|
||||||
|
|
||||||
public LearnerJpaEntity(Long id, Long userId, String displayName, Instant createdAt, Instant updatedAt) {
|
|
||||||
this.id = id;
|
|
||||||
this.userId = userId;
|
|
||||||
this.displayName = displayName;
|
|
||||||
this.createdAt = createdAt;
|
|
||||||
this.updatedAt = updatedAt;
|
|
||||||
}
|
|
||||||
|
|
||||||
public Long getId() {
|
|
||||||
return id;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setId(Long id) {
|
|
||||||
this.id = id;
|
|
||||||
}
|
|
||||||
|
|
||||||
public Long getUserId() {
|
|
||||||
return userId;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setUserId(Long userId) {
|
|
||||||
this.userId = userId;
|
|
||||||
}
|
|
||||||
|
|
||||||
public String getDisplayName() {
|
|
||||||
return displayName;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setDisplayName(String displayName) {
|
|
||||||
this.displayName = displayName;
|
|
||||||
}
|
|
||||||
|
|
||||||
public Instant getCreatedAt() {
|
|
||||||
return createdAt;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setCreatedAt(Instant createdAt) {
|
|
||||||
this.createdAt = createdAt;
|
|
||||||
}
|
|
||||||
|
|
||||||
public Instant getUpdatedAt() {
|
|
||||||
return updatedAt;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setUpdatedAt(Instant updatedAt) {
|
|
||||||
this.updatedAt = updatedAt;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,10 +0,0 @@
|
|||||||
package com.aplp.backend.learner.persistence;
|
|
||||||
|
|
||||||
import org.springframework.data.jpa.repository.JpaRepository;
|
|
||||||
|
|
||||||
import java.util.Optional;
|
|
||||||
|
|
||||||
public interface LearnerJpaRepository extends JpaRepository<LearnerJpaEntity, Long> {
|
|
||||||
|
|
||||||
Optional<LearnerJpaEntity> findByUserId(Long userId);
|
|
||||||
}
|
|
||||||
@@ -1,46 +0,0 @@
|
|||||||
package com.aplp.backend.learner.persistence;
|
|
||||||
|
|
||||||
import com.aplp.backend.learner.domain.Learner;
|
|
||||||
import com.aplp.backend.learner.domain.LearnerRepository;
|
|
||||||
import org.springframework.stereotype.Repository;
|
|
||||||
|
|
||||||
import java.util.Optional;
|
|
||||||
|
|
||||||
@Repository
|
|
||||||
public class LearnerRepositoryImpl implements LearnerRepository {
|
|
||||||
|
|
||||||
private final LearnerJpaRepository jpaRepository;
|
|
||||||
|
|
||||||
public LearnerRepositoryImpl(LearnerJpaRepository jpaRepository) {
|
|
||||||
this.jpaRepository = jpaRepository;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public Optional<Learner> findByUserId(Long userId) {
|
|
||||||
return jpaRepository.findByUserId(userId).map(LearnerRepositoryImpl::toDomain);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public Learner save(Learner learner) {
|
|
||||||
LearnerJpaEntity entity = toEntity(learner);
|
|
||||||
return toDomain(jpaRepository.save(entity));
|
|
||||||
}
|
|
||||||
|
|
||||||
private static LearnerJpaEntity toEntity(Learner learner) {
|
|
||||||
return new LearnerJpaEntity(
|
|
||||||
learner.id(),
|
|
||||||
learner.userId(),
|
|
||||||
learner.displayName(),
|
|
||||||
learner.createdAt(),
|
|
||||||
learner.updatedAt());
|
|
||||||
}
|
|
||||||
|
|
||||||
private static Learner toDomain(LearnerJpaEntity entity) {
|
|
||||||
return Learner.reconstruct(
|
|
||||||
entity.getId(),
|
|
||||||
entity.getUserId(),
|
|
||||||
entity.getDisplayName(),
|
|
||||||
entity.getCreatedAt(),
|
|
||||||
entity.getUpdatedAt());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -13,4 +13,4 @@ spring:
|
|||||||
|
|
||||||
logging:
|
logging:
|
||||||
level:
|
level:
|
||||||
com.aplp.backend: DEBUG
|
aplp.backend.web: DEBUG
|
||||||
|
|||||||
@@ -8,4 +8,4 @@ spring:
|
|||||||
|
|
||||||
logging:
|
logging:
|
||||||
level:
|
level:
|
||||||
com.aplp.backend: INFO
|
aplp.backend.web: INFO
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
spring:
|
spring:
|
||||||
application:
|
application:
|
||||||
name: aplp-backend
|
name: aplp-backend-web
|
||||||
profiles:
|
profiles:
|
||||||
default: dev
|
default: dev
|
||||||
jpa:
|
jpa:
|
||||||
|
|||||||
+4
-4
@@ -1,10 +1,10 @@
|
|||||||
package com.aplp.backend;
|
package aplp.backend.web;
|
||||||
|
|
||||||
import com.fasterxml.jackson.databind.JsonNode;
|
import tools.jackson.databind.JsonNode;
|
||||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
import tools.jackson.databind.ObjectMapper;
|
||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
|
import org.springframework.boot.webmvc.test.autoconfigure.AutoConfigureMockMvc;
|
||||||
import org.springframework.boot.test.context.SpringBootTest;
|
import org.springframework.boot.test.context.SpringBootTest;
|
||||||
import org.springframework.http.MediaType;
|
import org.springframework.http.MediaType;
|
||||||
import org.springframework.test.context.ActiveProfiles;
|
import org.springframework.test.context.ActiveProfiles;
|
||||||
+3
-3
@@ -1,6 +1,6 @@
|
|||||||
package com.aplp.backend;
|
package aplp.backend.web;
|
||||||
|
|
||||||
import com.aplp.backend.common.security.JwtTokenProvider;
|
import aplp.backend.web.common.security.JwtTokenProvider;
|
||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.boot.test.context.SpringBootTest;
|
import org.springframework.boot.test.context.SpringBootTest;
|
||||||
@@ -17,7 +17,7 @@ class JwtTokenProviderTest {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
void createsAndParsesAccessToken() {
|
void createsAndParsesAccessToken() {
|
||||||
var user = new com.aplp.backend.common.security.AuthenticatedUser(42L, "a@b.c", "Nam");
|
var user = new aplp.backend.web.common.security.AuthenticatedUser(42L, "a@b.c", "Nam");
|
||||||
String token = tokenProvider.createAccessToken(user);
|
String token = tokenProvider.createAccessToken(user);
|
||||||
|
|
||||||
var claims = tokenProvider.parse(token);
|
var claims = tokenProvider.parse(token);
|
||||||
Reference in New Issue
Block a user