Public Access
Finished registration Process and added tests
This commit is contained in:
1 parent
c224cbf679
commit
8465524849
14 files changed
+274
-42
No files matched your search
@@ -30,6 +30,6 @@ public class MinimalUserEntity {
|
||||
@Column(nullable = false)
|
||||
private boolean enabled;
|
||||
|
||||
@Column(name = "last_login_date", nullable = false)
|
||||
@Column(name = "last_login_date")
|
||||
private LocalDateTime lastLoginDate;
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
package auth_service.domain.registration.controller;
|
||||
|
||||
import auth_service.domain.registration.controller.dto.mapper.ApiMapper;
|
||||
import auth_service.domain.registration.controller.dto.request.RegistrationRequest;
|
||||
import auth_service.domain.registration.controller.dto.response.RegistrationResponse;
|
||||
import auth_service.domain.registration.service.RegistrationService;
|
||||
import io.micrometer.core.annotation.Timed;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
import org.springframework.web.bind.annotation.RequestBody;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
@RestController
|
||||
@RequestMapping("/api/registration")
|
||||
@RequiredArgsConstructor
|
||||
public class RegistrationController {
|
||||
|
||||
private final RegistrationService registrationService;
|
||||
|
||||
@PostMapping(value = "/new", version = "1.0", produces = "application/json", consumes = "application/json")
|
||||
@Timed(value = "auth.registration.new.requests.time", description = "Time taken to process registration requests")
|
||||
public ResponseEntity<RegistrationResponse> registration(@RequestBody RegistrationRequest request) {
|
||||
var userId = registrationService.register(ApiMapper.mapToModel(request));
|
||||
return ResponseEntity.ok(RegistrationResponse.builder().userId(userId).build());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
package auth_service.domain.registration.controller.dto.mapper;
|
||||
|
||||
import auth_service.domain.registration.controller.dto.request.RegistrationRequest;
|
||||
import auth_service.domain.registration.model.Registration;
|
||||
|
||||
public abstract class ApiMapper {
|
||||
private ApiMapper() {
|
||||
}
|
||||
|
||||
public static Registration mapToModel(RegistrationRequest registrationRequest) {
|
||||
return Registration.builder().
|
||||
email(registrationRequest.email()).
|
||||
rawPassword(registrationRequest.password()).
|
||||
inviteCode(registrationRequest.inviteCode()).
|
||||
steamId(registrationRequest.steamId()).
|
||||
build();
|
||||
}
|
||||
|
||||
}
|
||||
+7
@@ -0,0 +1,7 @@
|
||||
package auth_service.domain.registration.controller.dto.request;
|
||||
|
||||
import lombok.Builder;
|
||||
|
||||
@Builder
|
||||
public record RegistrationRequest(String email, String password, String inviteCode, long steamId) {
|
||||
}
|
||||
+7
@@ -0,0 +1,7 @@
|
||||
package auth_service.domain.registration.controller.dto.response;
|
||||
|
||||
import lombok.Builder;
|
||||
|
||||
@Builder
|
||||
public record RegistrationResponse(long userId) {
|
||||
}
|
||||
@@ -1,7 +0,0 @@
|
||||
package auth_service.domain.registration.model;
|
||||
|
||||
public record RegisteredUser(
|
||||
String id,
|
||||
String email
|
||||
) {
|
||||
}
|
||||
+1
-2
@@ -1,6 +1,5 @@
|
||||
package auth_service.domain.registration.persistence;
|
||||
|
||||
import auth_service.domain.registration.model.RegisteredUser;
|
||||
import auth_service.domain.registration.model.Registration;
|
||||
|
||||
public interface RegisteredUserRepository {
|
||||
@@ -8,5 +7,5 @@ public interface RegisteredUserRepository {
|
||||
|
||||
boolean existsBySteamId(long steamId);
|
||||
|
||||
RegisteredUser save(Registration registration);
|
||||
long save(Registration registration);
|
||||
}
|
||||
+25
-5
@@ -1,23 +1,43 @@
|
||||
package auth_service.domain.registration.persistence;
|
||||
|
||||
import auth_service.domain.registration.model.RegisteredUser;
|
||||
import auth_service.domain.registration.model.Registration;
|
||||
import auth_service.domain.registration.persistence.mapper.EntityMapper;
|
||||
import jakarta.persistence.EntityManager;
|
||||
import jakarta.persistence.PersistenceContext;
|
||||
import jakarta.transaction.Transactional;
|
||||
import org.springframework.stereotype.Repository;
|
||||
|
||||
@Repository
|
||||
@Transactional
|
||||
public class RegisteredUserRepositoryImpl implements RegisteredUserRepository {
|
||||
|
||||
@PersistenceContext
|
||||
private EntityManager entityManager;
|
||||
|
||||
@Override
|
||||
public boolean existsByEmail(String email) {
|
||||
return false;
|
||||
var jpql = "SELECT COUNT(u) FROM UserEntity u WHERE u.email = :email";
|
||||
var count = entityManager.createQuery(jpql, Long.class)
|
||||
.setParameter("email", email)
|
||||
.getSingleResult();
|
||||
|
||||
return count != null && count > 0;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean existsBySteamId(long steamId) {
|
||||
return false;
|
||||
var jpql = "SELECT COUNT(u) FROM UserEntity u WHERE u.steamId = :steamId";
|
||||
var count = entityManager.createQuery(jpql, Long.class)
|
||||
.setParameter("steamId", steamId)
|
||||
.getSingleResult();
|
||||
|
||||
return count != null && count > 0;
|
||||
}
|
||||
|
||||
@Override
|
||||
public RegisteredUser save(Registration registration) {
|
||||
return null;
|
||||
public long save(Registration registration) {
|
||||
var userToRegisterEntity = EntityMapper.mapToPersistence(registration);
|
||||
entityManager.persist(userToRegisterEntity);
|
||||
return userToRegisterEntity.getId();
|
||||
}
|
||||
}
|
||||
+37
@@ -0,0 +1,37 @@
|
||||
package auth_service.domain.registration.persistence.entities;
|
||||
|
||||
import jakarta.persistence.*;
|
||||
import lombok.*;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
|
||||
@Entity
|
||||
@Table(name = "users")
|
||||
@Getter
|
||||
@Setter
|
||||
@NoArgsConstructor
|
||||
@AllArgsConstructor
|
||||
@Builder
|
||||
public class RegisteredUserEntity {
|
||||
@Id
|
||||
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
||||
private Long id;
|
||||
|
||||
@Column(unique = true, nullable = false)
|
||||
private String email;
|
||||
|
||||
@Column(nullable = false)
|
||||
private String password;
|
||||
|
||||
@Column(nullable = false)
|
||||
private boolean enabled;
|
||||
|
||||
@Column(name = "steam_id", unique = true, nullable = false)
|
||||
private Long steamId;
|
||||
|
||||
@Column(name = "registration_date", nullable = false)
|
||||
private LocalDateTime registrationDate;
|
||||
|
||||
@Column(name = "last_login_date")
|
||||
private LocalDateTime lastLoginDate;
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
package auth_service.domain.registration.persistence.mapper;
|
||||
|
||||
import auth_service.domain.registration.model.Registration;
|
||||
import auth_service.domain.registration.persistence.entities.RegisteredUserEntity;
|
||||
|
||||
import java.time.Clock;
|
||||
import java.time.LocalDateTime;
|
||||
|
||||
public abstract class EntityMapper {
|
||||
|
||||
private EntityMapper() {
|
||||
}
|
||||
|
||||
public static RegisteredUserEntity mapToPersistence(Registration userModel) {
|
||||
return RegisteredUserEntity.builder().
|
||||
email(userModel.getEmail()).
|
||||
registrationDate(LocalDateTime.now(Clock.systemUTC())).
|
||||
password(userModel.getEncryptedPassword()).
|
||||
enabled(true).
|
||||
steamId(userModel.getSteamId()).
|
||||
lastLoginDate(null).
|
||||
build();
|
||||
}
|
||||
}
|
||||
@@ -3,7 +3,6 @@ package auth_service.domain.registration.service;
|
||||
import auth_service.domain.registration.exception.EmailAlreadyInUseException;
|
||||
import auth_service.domain.registration.exception.InvalidInvitationCodeException;
|
||||
import auth_service.domain.registration.exception.SteamIdAlreadyRegisteredException;
|
||||
import auth_service.domain.registration.model.RegisteredUser;
|
||||
import auth_service.domain.registration.model.Registration;
|
||||
import auth_service.domain.registration.persistence.RegisteredUserRepository;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
@@ -18,7 +17,7 @@ public class RegistrationService {
|
||||
private final RegisteredUserRepository registeredUserRepository;
|
||||
private final PasswordEncoder passwordEncoder;
|
||||
|
||||
public RegisteredUser register(Registration registration) {
|
||||
public long register(Registration registration) {
|
||||
if (!codeValidationService.isCodeValid(registration.getInviteCode())) {
|
||||
throw new InvalidInvitationCodeException("The provided invite code is invalid or has already been used.");
|
||||
}
|
||||
|
||||
@@ -36,6 +36,6 @@ public class UserEntity {
|
||||
@Column(name = "registration_date", nullable = false)
|
||||
private LocalDateTime registrationDate;
|
||||
|
||||
@Column(name = "last_login_date", nullable = false)
|
||||
@Column(name = "last_login_date")
|
||||
private LocalDateTime lastLoginDate;
|
||||
}
|
||||
Reference in new issue
Block a user