-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from Hangar-Tech/feat-users
Feat users
- Loading branch information
Showing
18 changed files
with
494 additions
and
1 deletion.
There are no files selected for viewing
12 changes: 12 additions & 0 deletions
12
src/main/java/com/institutosemprealerta/semprealerta/application/service/UserService.java
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package com.institutosemprealerta.semprealerta.application.service; | ||
|
||
import com.institutosemprealerta.semprealerta.infrastructure.entity.user.User; | ||
|
||
public interface UserService { | ||
void save(User user); | ||
void update(int id, User user); | ||
void delete(int id); | ||
User findByRegistration(String registration); | ||
User findByEmail(String email); | ||
User findById(int id); | ||
} |
49 changes: 49 additions & 0 deletions
49
...java/com/institutosemprealerta/semprealerta/application/service/impl/UserServiceImpl.java
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
package com.institutosemprealerta.semprealerta.application.service.impl; | ||
|
||
import com.institutosemprealerta.semprealerta.application.service.UserService; | ||
import com.institutosemprealerta.semprealerta.infrastructure.entity.user.User; | ||
import com.institutosemprealerta.semprealerta.domain.ports.out.UserRepository; | ||
import org.springframework.stereotype.Service; | ||
|
||
@Service | ||
public class UserServiceImpl implements UserService { | ||
|
||
private final UserRepository userRepository; | ||
|
||
public UserServiceImpl(UserRepository userRepository) { | ||
this.userRepository = userRepository; | ||
} | ||
|
||
@Override | ||
public void save(User user) { | ||
this.userRepository.save(user); | ||
} | ||
|
||
@Override | ||
public void update(int id, User user) { | ||
this.userRepository.update(id, user); | ||
} | ||
|
||
@Override | ||
public void delete(int id) { | ||
this.userRepository.delete(id); | ||
} | ||
|
||
@Override | ||
public User findByRegistration(String registration) { | ||
return this.userRepository.findByRegistration(registration) | ||
.orElseThrow(() -> new RuntimeException("User not found")); | ||
} | ||
|
||
@Override | ||
public User findByEmail(String email) { | ||
return this.userRepository.findByEmail(email) | ||
.orElseThrow(() -> new RuntimeException("User not found")); | ||
} | ||
|
||
@Override | ||
public User findById(int id) { | ||
return this.userRepository.findById(id) | ||
.orElseThrow(() -> new RuntimeException("User not found")); | ||
} | ||
} |
58 changes: 58 additions & 0 deletions
58
src/main/java/com/institutosemprealerta/semprealerta/domain/model/UserDTO.java
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
package com.institutosemprealerta.semprealerta.domain.model; | ||
|
||
|
||
import com.fasterxml.jackson.annotation.JsonFormat; | ||
import com.institutosemprealerta.semprealerta.infrastructure.entity.user.Address; | ||
import com.institutosemprealerta.semprealerta.infrastructure.entity.user.Contact; | ||
import com.institutosemprealerta.semprealerta.infrastructure.entity.user.User; | ||
import com.institutosemprealerta.semprealerta.infrastructure.entity.user.UserRoles; | ||
import com.institutosemprealerta.semprealerta.utils.DateManipulation; | ||
import jakarta.validation.constraints.*; | ||
|
||
import java.time.LocalDate; | ||
import java.time.format.DateTimeFormatter; | ||
|
||
public record UserDTO( | ||
@NotBlank | ||
String name, | ||
String email, | ||
@NotBlank | ||
String password, | ||
@NotBlank | ||
String phone, | ||
String gender, | ||
@PastOrPresent | ||
LocalDate birthDate, | ||
@NotNull | ||
UserRoles roles, | ||
String street, | ||
String number, | ||
String city, | ||
String zipCode | ||
|
||
) { | ||
public User toDomain() { | ||
//LocalDate birth = DateManipulation.stringToLocalDate(birthDate); | ||
Contact contact = new Contact( | ||
email, | ||
phone | ||
); | ||
|
||
Address address = new Address( | ||
street, | ||
number, | ||
city, | ||
zipCode | ||
); | ||
return new User( | ||
name, | ||
password, | ||
gender, | ||
birthDate, | ||
roles, | ||
contact, | ||
address | ||
); | ||
} | ||
} |
15 changes: 15 additions & 0 deletions
15
src/main/java/com/institutosemprealerta/semprealerta/domain/ports/out/UserRepository.java
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package com.institutosemprealerta.semprealerta.domain.ports.out; | ||
|
||
import com.institutosemprealerta.semprealerta.infrastructure.entity.user.User; | ||
|
||
import java.util.Optional; | ||
|
||
public interface UserRepository { | ||
void save(User user); | ||
Optional<User> findById(int id); | ||
void update(int id, User user); | ||
void delete(int id); | ||
Optional<User> findByRegistration(String registration); | ||
Optional<User> findByEmail(String email); | ||
|
||
} |
35 changes: 35 additions & 0 deletions
35
src/main/java/com/institutosemprealerta/semprealerta/domain/ports/out/UserResponse.java
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package com.institutosemprealerta.semprealerta.domain.ports.out; | ||
|
||
import com.institutosemprealerta.semprealerta.infrastructure.entity.user.Address; | ||
import com.institutosemprealerta.semprealerta.infrastructure.entity.user.Contact; | ||
import com.institutosemprealerta.semprealerta.infrastructure.entity.user.User; | ||
import com.institutosemprealerta.semprealerta.infrastructure.entity.user.UserRoles; | ||
import jakarta.persistence.Embedded; | ||
|
||
import java.time.LocalDate; | ||
|
||
public record UserResponse( | ||
String registration, | ||
String name, | ||
|
||
String gender, | ||
LocalDate birthDate, | ||
|
||
UserRoles roles, | ||
|
||
Contact contact, | ||
Address address | ||
) { | ||
|
||
public static UserResponse toResponse(User user) { | ||
return new UserResponse( | ||
user.getRegistration(), | ||
user.getName(), | ||
user.getGender(), | ||
user.getBirthDate(), | ||
user.getRoles(), | ||
user.getContact(), | ||
user.getAddress() | ||
); | ||
} | ||
} |
59 changes: 59 additions & 0 deletions
59
...m/institutosemprealerta/semprealerta/infrastructure/adpters/JpaUserRepositoryAdapter.java
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
package com.institutosemprealerta.semprealerta.infrastructure.adpters; | ||
|
||
import com.institutosemprealerta.semprealerta.domain.ports.out.UserRepository; | ||
import com.institutosemprealerta.semprealerta.infrastructure.entity.user.User; | ||
import com.institutosemprealerta.semprealerta.infrastructure.repositories.JpaUserRepository; | ||
import org.springframework.stereotype.Component; | ||
|
||
import java.util.Optional; | ||
|
||
@Component | ||
public class JpaUserRepositoryAdapter implements UserRepository { | ||
|
||
private final JpaUserRepository userRepository; | ||
|
||
public JpaUserRepositoryAdapter(JpaUserRepository jpaUserRepository) { | ||
this.userRepository = jpaUserRepository; | ||
} | ||
|
||
@Override | ||
public void save(User user) { | ||
this.userRepository.save(user); | ||
} | ||
|
||
@Override | ||
public Optional<User> findById(int id) { | ||
return this.userRepository.findById(id); | ||
} | ||
|
||
@Override | ||
public void update(int id, User user) { | ||
User userToUpdate = this.userRepository.findById(id) | ||
.orElseThrow(() -> new RuntimeException("User not found")); | ||
|
||
user.setId(userToUpdate.getId()); | ||
user.setRegistration(userToUpdate.getRegistration()); | ||
|
||
this.userRepository.save(user); | ||
} | ||
|
||
@Override | ||
public void delete(int id) { | ||
User userToDelete = this.userRepository.findById(id) | ||
.orElseThrow(() -> new RuntimeException("User not found")); | ||
|
||
this.userRepository.delete(userToDelete); | ||
} | ||
|
||
@Override | ||
public Optional<User> findByRegistration(String registration) { | ||
return this.userRepository.findByRegistration(registration); | ||
} | ||
|
||
@Override | ||
public Optional<User> findByEmail(String email) { | ||
return this.userRepository.findByEmail(email); | ||
} | ||
|
||
|
||
} |
51 changes: 51 additions & 0 deletions
51
...ava/com/institutosemprealerta/semprealerta/infrastructure/controllers/UserController.java
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
package com.institutosemprealerta.semprealerta.infrastructure.controllers; | ||
|
||
import com.institutosemprealerta.semprealerta.application.service.UserService; | ||
import com.institutosemprealerta.semprealerta.domain.model.UserDTO; | ||
import com.institutosemprealerta.semprealerta.domain.ports.out.UserResponse; | ||
import com.institutosemprealerta.semprealerta.infrastructure.entity.user.User; | ||
import jakarta.validation.Valid; | ||
import org.apache.catalina.connector.Response; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.web.bind.annotation.*; | ||
|
||
@RestController | ||
@RequestMapping("api/v1/user") | ||
public class UserController { | ||
|
||
private final UserService userService; | ||
|
||
public UserController(UserService userService) { | ||
this.userService = userService; | ||
} | ||
|
||
@PostMapping | ||
public ResponseEntity<?> createUser(@Valid @RequestBody UserDTO user) { | ||
userService.save(user.toDomain()); | ||
return ResponseEntity.status(Response.SC_CREATED).build(); | ||
} | ||
|
||
@GetMapping("/{id}") | ||
public ResponseEntity<UserResponse> findById(@PathVariable int id) { | ||
User userFound = userService.findById(id); | ||
return ResponseEntity.ok().body(UserResponse.toResponse(userFound)); | ||
} | ||
|
||
@GetMapping("/registration/{reg}") | ||
public ResponseEntity<UserResponse> findByRegistration(@PathVariable String reg) { | ||
User userFound = userService.findByRegistration(reg); | ||
return ResponseEntity.ok().body(UserResponse.toResponse(userFound)); | ||
} | ||
|
||
@PutMapping("/{id}") | ||
public ResponseEntity<?> updateUser(@PathVariable int id, @RequestBody UserDTO user) { | ||
userService.update(id, user.toDomain()); | ||
return ResponseEntity.status(Response.SC_NO_CONTENT).build(); | ||
} | ||
|
||
@DeleteMapping("/{id}") | ||
public ResponseEntity<?> deleteUser(@PathVariable int id) { | ||
userService.delete(id); | ||
return ResponseEntity.status(Response.SC_NO_CONTENT).build(); | ||
} | ||
} |
19 changes: 19 additions & 0 deletions
19
src/main/java/com/institutosemprealerta/semprealerta/infrastructure/entity/user/Address.java
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package com.institutosemprealerta.semprealerta.infrastructure.entity.user; | ||
|
||
import jakarta.persistence.Embeddable; | ||
import lombok.AllArgsConstructor; | ||
import lombok.Getter; | ||
import lombok.NoArgsConstructor; | ||
import lombok.Setter; | ||
|
||
@Getter | ||
@Setter | ||
@AllArgsConstructor | ||
@NoArgsConstructor | ||
@Embeddable | ||
public class Address { | ||
private String street; | ||
private String number; | ||
private String city; | ||
private String zipCode; | ||
} |
17 changes: 17 additions & 0 deletions
17
src/main/java/com/institutosemprealerta/semprealerta/infrastructure/entity/user/Contact.java
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package com.institutosemprealerta.semprealerta.infrastructure.entity.user; | ||
|
||
import jakarta.persistence.Embeddable; | ||
import lombok.AllArgsConstructor; | ||
import lombok.Getter; | ||
import lombok.NoArgsConstructor; | ||
import lombok.Setter; | ||
|
||
@Getter | ||
@Setter | ||
@NoArgsConstructor | ||
@AllArgsConstructor | ||
@Embeddable | ||
public class Contact { | ||
private String email; | ||
private String phone; | ||
} |
Oops, something went wrong.