Skip to content

Instantly share code, notes, and snippets.

@jeanpierregomez
jeanpierregomez / UserUseCase.java
Last active March 14, 2024 21:44
User use case
@RequiredArgsConstructor
public class UserUseCase {
private final UserRepository userRepository;
public User saveUser(User user) {
return userRepository.saveUser(user);
}
public User updateUser(User user) {
return userRepository.updateUser(user);
@Entity
@Table(name = "`user`")
@Data
public class UserEntity {
@Id
private String id;
private String firstName;
private String lastName;
private String userName;
private String email;
@Repository
public class JPARepositoryAdapter extends AdapterOperations<User, UserEntity, String, JPARepository>
implements UserRepository
{
public JPARepositoryAdapter(JPARepository repository, ObjectMapper mapper) {
/**
* Could be use mapper.mapBuilder if your domain model implement builder pattern
* super(repository, mapper, d -> mapper.mapBuilder(d,ObjectModel.ObjectModelBuilder.class).build());
* Or using mapper.map with the class of the object model
@RestController
@RequestMapping(value = "/user", produces = MediaType.APPLICATION_JSON_VALUE)
@AllArgsConstructor
public class ApiRest {
private final UserUseCase userUseCase;
@GetMapping(path = "/{id}")
public ResponseEntity<User> getUser(@PathVariable("id") String id) {
return ResponseEntity.ok(userUseCase.getUser(id));
}