[JAVA-14174] Renamed paterns to paterns-module (#12718)
* [JAVA-14174] Renamed paterns to paterns-module * [JAVA-14174] naming fixes Co-authored-by: panagiotiskakos <panagiotis.kakos@libra-is.com>
This commit is contained in:
+40
@@ -0,0 +1,40 @@
|
||||
package com.baeldung.pattern.cleanarchitecture;
|
||||
|
||||
import org.springframework.beans.factory.config.BeanFactoryPostProcessor;
|
||||
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.boot.web.servlet.context.AnnotationConfigServletWebServerApplicationContext;
|
||||
import org.springframework.context.ApplicationContext;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.ClassPathBeanDefinitionScanner;
|
||||
import org.springframework.core.type.classreading.MetadataReader;
|
||||
import org.springframework.core.type.classreading.MetadataReaderFactory;
|
||||
import org.springframework.core.type.filter.TypeFilter;
|
||||
|
||||
@SpringBootApplication
|
||||
public class CleanArchitectureApplication {
|
||||
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(CleanArchitectureApplication.class);
|
||||
}
|
||||
|
||||
@Bean
|
||||
BeanFactoryPostProcessor beanFactoryPostProcessor(ApplicationContext beanRegistry) {
|
||||
return beanFactory -> {
|
||||
genericApplicationContext((BeanDefinitionRegistry) ((AnnotationConfigServletWebServerApplicationContext) beanRegistry).getBeanFactory());
|
||||
};
|
||||
}
|
||||
|
||||
void genericApplicationContext(BeanDefinitionRegistry beanRegistry) {
|
||||
ClassPathBeanDefinitionScanner beanDefinitionScanner = new ClassPathBeanDefinitionScanner(beanRegistry);
|
||||
beanDefinitionScanner.addIncludeFilter(removeModelAndEntitiesFilter());
|
||||
beanDefinitionScanner.scan("com.baeldung.pattern.cleanarchitecture");
|
||||
}
|
||||
|
||||
static TypeFilter removeModelAndEntitiesFilter() {
|
||||
return (MetadataReader mr, MetadataReaderFactory mrf) -> !mr.getClassMetadata()
|
||||
.getClassName()
|
||||
.endsWith("Model");
|
||||
}
|
||||
}
|
||||
+30
@@ -0,0 +1,30 @@
|
||||
package com.baeldung.pattern.cleanarchitecture.usercreation;
|
||||
|
||||
class CommonUser implements User {
|
||||
|
||||
String name;
|
||||
String password;
|
||||
|
||||
CommonUser(String name, String password) {
|
||||
this.name = name;
|
||||
this.password = password;
|
||||
}
|
||||
|
||||
CommonUser() {
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean passwordIsValid() {
|
||||
return password != null && password.length() > 5;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getPassword() {
|
||||
return password;
|
||||
}
|
||||
}
|
||||
+8
@@ -0,0 +1,8 @@
|
||||
package com.baeldung.pattern.cleanarchitecture.usercreation;
|
||||
|
||||
class CommonUserFactory implements UserFactory {
|
||||
@Override
|
||||
public User create(String name, String password) {
|
||||
return new CommonUser(name, password);
|
||||
}
|
||||
}
|
||||
+21
@@ -0,0 +1,21 @@
|
||||
package com.baeldung.pattern.cleanarchitecture.usercreation;
|
||||
|
||||
class JpaUser implements UserRegisterDsGateway {
|
||||
|
||||
final JpaUserRepository repository;
|
||||
|
||||
JpaUser(JpaUserRepository repository) {
|
||||
this.repository = repository;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean existsByName(String name) {
|
||||
return repository.existsById(name);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void save(UserDsRequestModel requestModel) {
|
||||
UserDataMapper accountDataMapper = new UserDataMapper(requestModel.getName(), requestModel.getPassword(), requestModel.getCreationTime());
|
||||
repository.save(accountDataMapper);
|
||||
}
|
||||
}
|
||||
+8
@@ -0,0 +1,8 @@
|
||||
package com.baeldung.pattern.cleanarchitecture.usercreation;
|
||||
|
||||
import org.springframework.data.jpa.repository.JpaRepository;
|
||||
import org.springframework.stereotype.Repository;
|
||||
|
||||
@Repository
|
||||
interface JpaUserRepository extends JpaRepository<UserDataMapper, String> {
|
||||
}
|
||||
+9
@@ -0,0 +1,9 @@
|
||||
package com.baeldung.pattern.cleanarchitecture.usercreation;
|
||||
|
||||
interface User {
|
||||
boolean passwordIsValid();
|
||||
|
||||
String getName();
|
||||
|
||||
String getPassword();
|
||||
}
|
||||
+53
@@ -0,0 +1,53 @@
|
||||
package com.baeldung.pattern.cleanarchitecture.usercreation;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.Id;
|
||||
import javax.persistence.Table;
|
||||
|
||||
@Entity
|
||||
@Table(name = "user")
|
||||
class UserDataMapper {
|
||||
|
||||
@Id
|
||||
String name;
|
||||
|
||||
String password;
|
||||
|
||||
LocalDateTime creationTime;
|
||||
|
||||
public UserDataMapper() {
|
||||
}
|
||||
|
||||
public UserDataMapper(String name, String password, LocalDateTime creationTime) {
|
||||
super();
|
||||
this.name = name;
|
||||
this.password = password;
|
||||
this.creationTime = creationTime;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public String getPassword() {
|
||||
return password;
|
||||
}
|
||||
|
||||
public void setPassword(String password) {
|
||||
this.password = password;
|
||||
}
|
||||
|
||||
public LocalDateTime getCreationTime() {
|
||||
return creationTime;
|
||||
}
|
||||
|
||||
public void setCreationTime(LocalDateTime creationTime) {
|
||||
this.creationTime = creationTime;
|
||||
}
|
||||
}
|
||||
+41
@@ -0,0 +1,41 @@
|
||||
package com.baeldung.pattern.cleanarchitecture.usercreation;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
|
||||
class UserDsRequestModel {
|
||||
|
||||
String name;
|
||||
String password;
|
||||
LocalDateTime creationTime;
|
||||
|
||||
public UserDsRequestModel(String name, String password, LocalDateTime creationTime) {
|
||||
this.name = name;
|
||||
this.password = password;
|
||||
this.creationTime = creationTime;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public String getPassword() {
|
||||
return password;
|
||||
}
|
||||
|
||||
public void setPassword(String password) {
|
||||
this.password = password;
|
||||
}
|
||||
|
||||
public LocalDateTime getCreationTime() {
|
||||
return creationTime;
|
||||
}
|
||||
|
||||
public void setCreationTime(LocalDateTime creationTime) {
|
||||
this.creationTime = creationTime;
|
||||
}
|
||||
|
||||
}
|
||||
+5
@@ -0,0 +1,5 @@
|
||||
package com.baeldung.pattern.cleanarchitecture.usercreation;
|
||||
|
||||
interface UserFactory {
|
||||
User create(String name, String password);
|
||||
}
|
||||
+5
@@ -0,0 +1,5 @@
|
||||
package com.baeldung.pattern.cleanarchitecture.usercreation;
|
||||
|
||||
public interface UserInputBoundary {
|
||||
UserResponseModel create(UserRequestModel requestModel);
|
||||
}
|
||||
+7
@@ -0,0 +1,7 @@
|
||||
package com.baeldung.pattern.cleanarchitecture.usercreation;
|
||||
|
||||
interface UserPresenter {
|
||||
UserResponseModel prepareSuccessView(UserResponseModel user);
|
||||
|
||||
UserResponseModel prepareFailView(String error);
|
||||
}
|
||||
+20
@@ -0,0 +1,20 @@
|
||||
package com.baeldung.pattern.cleanarchitecture.usercreation;
|
||||
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
import org.springframework.web.bind.annotation.RequestBody;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
@RestController
|
||||
class UserRegisterController {
|
||||
|
||||
final UserInputBoundary userInput;
|
||||
|
||||
UserRegisterController(UserInputBoundary accountGateway) {
|
||||
this.userInput = accountGateway;
|
||||
}
|
||||
|
||||
@PostMapping("/user")
|
||||
UserResponseModel create(@RequestBody UserRequestModel requestModel) {
|
||||
return userInput.create(requestModel);
|
||||
}
|
||||
}
|
||||
+7
@@ -0,0 +1,7 @@
|
||||
package com.baeldung.pattern.cleanarchitecture.usercreation;
|
||||
|
||||
interface UserRegisterDsGateway {
|
||||
boolean existsByName(String identifier);
|
||||
|
||||
void save(UserDsRequestModel requestModel);
|
||||
}
|
||||
+35
@@ -0,0 +1,35 @@
|
||||
package com.baeldung.pattern.cleanarchitecture.usercreation;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
|
||||
class UserRegisterInteractor implements UserInputBoundary {
|
||||
|
||||
final UserRegisterDsGateway userDsGateway;
|
||||
final UserPresenter userPresenter;
|
||||
final UserFactory userFactory;
|
||||
|
||||
UserRegisterInteractor(UserRegisterDsGateway userRegisterDfGateway, UserPresenter userPresenter,
|
||||
UserFactory userFactory) {
|
||||
this.userDsGateway = userRegisterDfGateway;
|
||||
this.userPresenter = userPresenter;
|
||||
this.userFactory = userFactory;
|
||||
}
|
||||
|
||||
@Override
|
||||
public UserResponseModel create(UserRequestModel requestModel) {
|
||||
if (userDsGateway.existsByName(requestModel.getName())) {
|
||||
return userPresenter.prepareFailView("User already exists.");
|
||||
}
|
||||
User user = userFactory.create(requestModel.getName(), requestModel.getPassword());
|
||||
if (!user.passwordIsValid()) {
|
||||
return userPresenter.prepareFailView("User password must have more than 5 characters.");
|
||||
}
|
||||
LocalDateTime now = LocalDateTime.now();
|
||||
UserDsRequestModel userDsModel = new UserDsRequestModel(user.getName(), user.getPassword(), now);
|
||||
|
||||
userDsGateway.save(userDsModel);
|
||||
|
||||
UserResponseModel accountResponseModel = new UserResponseModel(user.getName(), now.toString());
|
||||
return userPresenter.prepareSuccessView(accountResponseModel);
|
||||
}
|
||||
}
|
||||
+33
@@ -0,0 +1,33 @@
|
||||
package com.baeldung.pattern.cleanarchitecture.usercreation;
|
||||
|
||||
class UserRequestModel {
|
||||
|
||||
String name;
|
||||
String password;
|
||||
|
||||
public UserRequestModel() {
|
||||
super();
|
||||
}
|
||||
|
||||
UserRequestModel(String name, String password) {
|
||||
super();
|
||||
this.name = name;
|
||||
this.password = password;
|
||||
}
|
||||
|
||||
String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
String getPassword() {
|
||||
return password;
|
||||
}
|
||||
|
||||
void setPassword(String password) {
|
||||
this.password = password;
|
||||
}
|
||||
}
|
||||
+22
@@ -0,0 +1,22 @@
|
||||
package com.baeldung.pattern.cleanarchitecture.usercreation;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
import java.time.format.DateTimeFormatter;
|
||||
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.web.server.ResponseStatusException;
|
||||
|
||||
class UserResponseFormatter implements UserPresenter {
|
||||
|
||||
@Override
|
||||
public UserResponseModel prepareSuccessView(UserResponseModel response) {
|
||||
LocalDateTime responseTime = LocalDateTime.parse(response.getCreationTime());
|
||||
response.setCreationTime(responseTime.format(DateTimeFormatter.ofPattern("hh:mm:ss")));
|
||||
return response;
|
||||
}
|
||||
|
||||
@Override
|
||||
public UserResponseModel prepareFailView(String error) {
|
||||
throw new ResponseStatusException(HttpStatus.CONFLICT, error);
|
||||
}
|
||||
}
|
||||
+29
@@ -0,0 +1,29 @@
|
||||
package com.baeldung.pattern.cleanarchitecture.usercreation;
|
||||
|
||||
public class UserResponseModel {
|
||||
|
||||
String login;
|
||||
String creationTime;
|
||||
|
||||
public UserResponseModel(String login, String creationTime) {
|
||||
this.login = login;
|
||||
this.creationTime = creationTime;
|
||||
}
|
||||
|
||||
public String getLogin() {
|
||||
return login;
|
||||
}
|
||||
|
||||
public void setLogin(String login) {
|
||||
this.login = login;
|
||||
}
|
||||
|
||||
public String getCreationTime() {
|
||||
return creationTime;
|
||||
}
|
||||
|
||||
public void setCreationTime(String creationTime) {
|
||||
this.creationTime = creationTime;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,2 @@
|
||||
server.port=8080
|
||||
server.error.include-message=always
|
||||
Reference in New Issue
Block a user