Last active
April 19, 2024 14:10
-
-
Save cherifGsoul/d5ac361a2852a66ba8624ef291dc644a to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package dev.cherifbouchelaghem.vodto.model; | |
// INSIED THE HEXAGON | |
public record EmailAddress(String address) { | |
public static EmailAddress fromString(String address) { | |
return new EmailAddress(address); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package dev.cherifbouchelaghem.vodto.model; | |
// INSIDE THE HEXAGON | |
public record FullName (String fistname, String lastname){ | |
static public FullName of (String firstname, String lastname) { | |
return new FullName(firstname, lastname); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package dev.cherifbouchelaghem.vodto.infrastructure.persistence; | |
import dev.cherifbouchelaghem.vodto.model.User; | |
import dev.cherifbouchelaghem.vodto.model.Users; | |
import java.util.ArrayList; | |
import java.util.List; | |
// === OUTSIDE THE HEXAGON ===// | |
/** | |
* PERSISTENCE ADAPTER | |
*/ | |
public class InMemoryUsers implements Users { | |
private List<User> users = new ArrayList<>(); | |
@Override | |
public void add(User user) { | |
this.users.add(user); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package dev.cherifbouchelaghem.vodto.application; | |
import dev.cherifbouchelaghem.vodto.model.EmailAddress; | |
import dev.cherifbouchelaghem.vodto.model.FullName; | |
// ===== INSIDE THE HAXGON ============ // | |
/** | |
* The message/request to the hexagon | |
*/ | |
public class RegistrationCommand { | |
private String firstname; | |
private String lastname; | |
private String emailAddress; | |
public RegistrationCommand(String firstname, String lastname, String emailAddress) { | |
this.firstname = firstname; | |
this.lastname = lastname; | |
this.emailAddress = emailAddress; | |
} | |
public EmailAddress getEmailAddress() { | |
return EmailAddress.fromString(emailAddress); | |
} | |
public FullName getFullName() { | |
return FullName.of(this.firstname, this.lastname); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package dev.cherifbouchelaghem.vodto.model; | |
// INSIDE THE HEXAGON | |
public class User { | |
private FullName fullName; | |
private EmailAddress emailAddress; | |
public User(FullName fullName, EmailAddress emailAddress) { | |
this.fullName = fullName; | |
this.emailAddress = emailAddress; | |
} | |
public static User register(FullName fullName, EmailAddress emailAddress) { | |
return new User(fullName, emailAddress); | |
} | |
public String firstname() { | |
return this.fullName.fistname(); | |
} | |
public String lastname() { | |
return this.fullName.lastname(); | |
} | |
public String emailAddress() { | |
return this.emailAddress.address(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package dev.cherifbouchelaghem.vodto.infrastructure.resource; | |
import dev.cherifbouchelaghem.vodto.application.RegistrationCommand; | |
import dev.cherifbouchelaghem.vodto.application.UserService; | |
import dev.cherifbouchelaghem.vodto.application.UserView; | |
// === OUTSIDE THE HEXAGON ===// | |
/** | |
* HTTP ADAPTER | |
*/ | |
public class UserResource { | |
private UserService userService; | |
public UserResource(UserService userService) { | |
this.userService = userService; | |
} | |
public Response post(String firstname, String lastname, String emailAddress) { | |
UserView user = userService.register(new RegistrationCommand(firstname, lastname, emailAddress)); | |
return Response.created(user); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package dev.cherifbouchelaghem.vodto.model; | |
// INSIDE THE HEXAGON | |
/** | |
* Persistence PORT | |
*/ | |
public interface Users { | |
void add(User user); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package dev.cherifbouchelaghem.vodto.application; | |
import dev.cherifbouchelaghem.vodto.model.User; | |
import dev.cherifbouchelaghem.vodto.model.Users; | |
// ===== INSIDE THE HAXGON ============ // | |
/** | |
* The haxgon entry point | |
* The hexagon PUBLIC API | |
*/ | |
public class UserService { | |
private Users users; | |
public UserService(Users users) { | |
this.users = users; | |
} | |
public UserView register(RegistrationCommand command) { | |
User user = User.register(command.getFullName(), command.getEmailAddress()); | |
users.add(user); | |
return UserView.fromUser(user); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package dev.cherifbouchelaghem.vodto.application; | |
import dev.cherifbouchelaghem.vodto.model.User; | |
// ======= INSIDE THE HEXAGON ===== // | |
/** | |
* the hexgon response | |
* @param firstname | |
* @param lastname | |
* @param emailAddress | |
*/ | |
public record UserView(String firstname, String lastname, String emailAddress) { | |
static public UserView fromUser(User user) { | |
return new UserView(user.firstname(), user.lastname(), user.emailAddress()); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment