Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor: Refatoração do controller de usuário #70

Merged
merged 4 commits into from
Mar 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,6 @@ public ResponseEntity<ResponseUserDTO> getUserById(@PathVariable final String us
})
@PostMapping
public ResponseEntity<ResponseUserDTO> createUser(@RequestBody @Valid final RequestUserDTO requestUserDTO) {
if (userService.verifyDuplicateUsername(requestUserDTO.username())) {
return ResponseEntity.status(HttpStatus.CONFLICT).build();
}
ResponseUserDTO createdUser = userService.createUser(requestUserDTO);
return ResponseEntity.status(HttpStatus.CREATED).body(createdUser);
}
Expand Down
10 changes: 7 additions & 3 deletions src/main/java/com/mandacarubroker/service/UserService.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@
import com.mandacarubroker.domain.user.ResponseUserDTO;
import com.mandacarubroker.domain.user.User;
import com.mandacarubroker.domain.user.UserRepository;
import org.springframework.http.HttpStatus;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;
import org.springframework.web.server.ResponseStatusException;

import java.util.ArrayList;
import java.util.List;
Expand Down Expand Up @@ -62,6 +63,9 @@ private User hashPassword(final User user) {
}

public ResponseUserDTO createUser(final RequestUserDTO requestUserDTO) {
if (verifyDuplicateUsername(requestUserDTO.username())) {
throw new ResponseStatusException(HttpStatus.CONFLICT, "this username is not available");
}
validateRequestDTO(requestUserDTO);
User newUser = new User(requestUserDTO);
User hashedPasswordUser = hashPassword(newUser);
Expand Down Expand Up @@ -94,13 +98,13 @@ public void deleteUser(final String id) {

public UserDetails loadUserByUsername(final String username) {
if (username == null || username.isEmpty()) {
throw new UsernameNotFoundException("Username is required");
throw new ResponseStatusException(HttpStatus.BAD_REQUEST, "Username cannot be null or empty");
}

User user = userRepository.findByUsername(username);

if (user == null) {
throw new UsernameNotFoundException("User not found");
throw new ResponseStatusException(HttpStatus.NOT_FOUND, "User not found");
}

return user;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,7 @@ void itShouldReturnCreatedStatusAfterSucessfulPost() throws Exception {
mockMvc.perform(requestBuilder).andExpect(matchStatus);
}

@Test
void itShouldReturnUserDataAfterSucessfulPost() throws Exception {
String userJsonString = objectMapper.writeValueAsString(validUserDTO);
System.out.println(userJsonString);
Expand All @@ -217,6 +218,7 @@ void itShouldReturnUserDataAfterSucessfulPost() throws Exception {
assertResponseUserDTO(validUserDTO, createdUser);
}

@Test
void itShouldReturnUserDataAfterSucessfulPut() throws Exception {
String userJsonString = objectMapper.writeValueAsString(validUserDTO);

Expand Down
Loading