From c4e84567facc0aa7c3e32c0e74a9c2753827cf94 Mon Sep 17 00:00:00 2001 From: ruan-pb Date: Mon, 22 Feb 2021 12:11:47 -0300 Subject: [PATCH] refs(#1) - Ajustes do UpdateUserService --- .classpath | 6 ++++++ .../org/ayty/hatcher/api/v1/security/JwtService.java | 1 - .../ayty/hatcher/api/v1/security/SecurityConfig.java | 2 +- .../api/v1/user/controller/UserController.java | 11 ++++++----- .../hatcher/api/v1/user/dto/RegisterUserDTO.java | 2 +- .../org/ayty/hatcher/api/v1/user/entity/User.java | 2 +- .../v1/user/service/AuthenticateUserServiceImpl.java | 9 +++++---- ...istUsersIdService.java => GetUsersIdService.java} | 4 ++-- ...IdServiceImpl.java => GetUsersIdServiceImpl.java} | 8 ++++---- .../api/v1/user/service/ListUsersServiceImpl.java | 4 ++-- .../api/v1/user/service/RegisterUserServiceImpl.java | 12 +++++------- .../api/v1/user/service/RemoveUserServiceImpl.java | 6 +++--- .../api/v1/user/service/UpdateUserServiceImpl.java | 6 ++++-- 13 files changed, 40 insertions(+), 33 deletions(-) rename src/main/java/org/ayty/hatcher/api/v1/user/service/{ListUsersIdService.java => GetUsersIdService.java} (63%) rename src/main/java/org/ayty/hatcher/api/v1/user/service/{ListUsersIdServiceImpl.java => GetUsersIdServiceImpl.java} (74%) diff --git a/.classpath b/.classpath index 66499b2..39abf1c 100644 --- a/.classpath +++ b/.classpath @@ -31,11 +31,17 @@ + + + + + + diff --git a/src/main/java/org/ayty/hatcher/api/v1/security/JwtService.java b/src/main/java/org/ayty/hatcher/api/v1/security/JwtService.java index 0e381fe..e6415ca 100644 --- a/src/main/java/org/ayty/hatcher/api/v1/security/JwtService.java +++ b/src/main/java/org/ayty/hatcher/api/v1/security/JwtService.java @@ -38,7 +38,6 @@ public String generateToken(LoginDTO user) { Date date = Date.from(instant); HashMap claim = new HashMap(); Optional userAdmin = userBD.findByLogin(user.getLogin()); - claim.put("Login", user.getLogin()); claim.put("Email", userAdmin.get().getEmail()); claim.put("Id", userAdmin.get().getId()); diff --git a/src/main/java/org/ayty/hatcher/api/v1/security/SecurityConfig.java b/src/main/java/org/ayty/hatcher/api/v1/security/SecurityConfig.java index 64f9c5c..ddb8756 100644 --- a/src/main/java/org/ayty/hatcher/api/v1/security/SecurityConfig.java +++ b/src/main/java/org/ayty/hatcher/api/v1/security/SecurityConfig.java @@ -60,7 +60,7 @@ private void permitAll(HttpSecurity http) throws Exception{ .permitAll() .antMatchers(HttpMethod.PUT,"/api/v1/hatcher/update/**") .permitAll() - .antMatchers(HttpMethod.GET,"/api/v1/hatcher/listUsersId/**") + .antMatchers(HttpMethod.GET,"/api/v1/hatcher/getById/**") .permitAll() .antMatchers(HttpMethod.GET,"/api/v1/hatcher/listUsers") .permitAll() diff --git a/src/main/java/org/ayty/hatcher/api/v1/user/controller/UserController.java b/src/main/java/org/ayty/hatcher/api/v1/user/controller/UserController.java index 1d449f4..7a43cfa 100644 --- a/src/main/java/org/ayty/hatcher/api/v1/user/controller/UserController.java +++ b/src/main/java/org/ayty/hatcher/api/v1/user/controller/UserController.java @@ -3,6 +3,7 @@ import javax.validation.Valid; + import org.ayty.hatcher.api.v1.user.dto.LoginDTO; import org.ayty.hatcher.api.v1.user.dto.OutRegisterDTO; import org.ayty.hatcher.api.v1.user.dto.RegisterUserDTO; @@ -11,7 +12,7 @@ import org.ayty.hatcher.api.v1.user.dto.UserListDTO; import org.ayty.hatcher.api.v1.user.entity.User; import org.ayty.hatcher.api.v1.user.exception.InvalidDataException; -import org.ayty.hatcher.api.v1.user.service.ListUsersIdServiceImpl; +import org.ayty.hatcher.api.v1.user.service.GetUsersIdServiceImpl; import org.ayty.hatcher.api.v1.user.service.ListUsersServiceImpl; import org.ayty.hatcher.api.v1.user.service.RegisterUserServiceImpl; import org.ayty.hatcher.api.v1.user.service.RemoveUserServiceImpl; @@ -49,7 +50,7 @@ public class UserController { private final RemoveUserServiceImpl removeUserService; private final ReturnsLoginAndTokenService authenticationReturn; private final UpdateUserServiceImpl updateUserService; - private final ListUsersIdServiceImpl listUsersId; + private final GetUsersIdServiceImpl getUserService; @ResponseStatus(HttpStatus.CREATED) @@ -98,8 +99,8 @@ public UpdateUserDTO updateUser( @PathVariable Long id, @RequestBody User user) } } - @GetMapping(value = "/listUsersId/{id}", produces = MediaType.APPLICATION_JSON_VALUE) - public UpdateUserDTO listUsersId(@PathVariable Long id) { - return listUsersId.listUsersId(id); + @GetMapping(value = "/getById/{id}", produces = MediaType.APPLICATION_JSON_VALUE) + public UpdateUserDTO getByUsersId(@PathVariable Long id) { + return getUserService.getUsersId(id); } } diff --git a/src/main/java/org/ayty/hatcher/api/v1/user/dto/RegisterUserDTO.java b/src/main/java/org/ayty/hatcher/api/v1/user/dto/RegisterUserDTO.java index 525f349..e84c0c3 100644 --- a/src/main/java/org/ayty/hatcher/api/v1/user/dto/RegisterUserDTO.java +++ b/src/main/java/org/ayty/hatcher/api/v1/user/dto/RegisterUserDTO.java @@ -37,7 +37,7 @@ public class RegisterUserDTO { @Email(message = "field.email.invalid") private String email; @Length(min = 3 ,max = 255) - private String fullName; + private String fullname; private String image; @Enumerated(EnumType.STRING) private Profile profile; diff --git a/src/main/java/org/ayty/hatcher/api/v1/user/entity/User.java b/src/main/java/org/ayty/hatcher/api/v1/user/entity/User.java index ebbd831..068373b 100644 --- a/src/main/java/org/ayty/hatcher/api/v1/user/entity/User.java +++ b/src/main/java/org/ayty/hatcher/api/v1/user/entity/User.java @@ -70,7 +70,7 @@ public static User to(RegisterUserDTO dto) { return User.builder() .login(dto.getLogin()) .password(dto.getPassword()) - .fullname(dto.getFullName()) + .fullname(dto.getFullname()) .email(dto.getEmail()) .image(dto.getImage()) .profile(dto.getProfile()) diff --git a/src/main/java/org/ayty/hatcher/api/v1/user/service/AuthenticateUserServiceImpl.java b/src/main/java/org/ayty/hatcher/api/v1/user/service/AuthenticateUserServiceImpl.java index 896680a..02d0c04 100644 --- a/src/main/java/org/ayty/hatcher/api/v1/user/service/AuthenticateUserServiceImpl.java +++ b/src/main/java/org/ayty/hatcher/api/v1/user/service/AuthenticateUserServiceImpl.java @@ -1,6 +1,5 @@ package org.ayty.hatcher.api.v1.user.service; -import org.ayty.hatcher.api.v1.user.dto.TokenDTO; import org.ayty.hatcher.api.v1.user.entity.User; import org.ayty.hatcher.api.v1.user.exception.IncorrectUserOrPasswordException; import org.ayty.hatcher.api.v1.user.exception.LoginNotFoundException; @@ -17,7 +16,7 @@ public class AuthenticateUserServiceImpl implements AuthenticateUserService{ private final PasswordEncoder encoder; - private final UserRepository userBD; + private final UserRepository userDB; private final LoadUserByUsarname load; public UserDetails authenticate(User user) { @@ -27,12 +26,14 @@ public UserDetails authenticate(User user) { if (userDetails.getUsername() == null) { throw new LoginNotFoundException(); } - User usuario = userBD.findByLogin(user.getLogin()).orElseThrow(() -> new UserDoesNotExistException()); + + User usuario = userDB.findByLogin(user.getLogin()).orElseThrow(() -> new UserDoesNotExistException()); boolean PasswordsMatch = encoder.matches(user.getPassword(), userDetails.getPassword()); if (PasswordsMatch) { return userDetails; - } else { + } + else { throw new IncorrectUserOrPasswordException(); } } diff --git a/src/main/java/org/ayty/hatcher/api/v1/user/service/ListUsersIdService.java b/src/main/java/org/ayty/hatcher/api/v1/user/service/GetUsersIdService.java similarity index 63% rename from src/main/java/org/ayty/hatcher/api/v1/user/service/ListUsersIdService.java rename to src/main/java/org/ayty/hatcher/api/v1/user/service/GetUsersIdService.java index 355fb9e..b63b04b 100644 --- a/src/main/java/org/ayty/hatcher/api/v1/user/service/ListUsersIdService.java +++ b/src/main/java/org/ayty/hatcher/api/v1/user/service/GetUsersIdService.java @@ -3,7 +3,7 @@ import org.ayty.hatcher.api.v1.user.dto.UpdateUserDTO; @FunctionalInterface -public interface ListUsersIdService { +public interface GetUsersIdService { - UpdateUserDTO listUsersId(Long id); + UpdateUserDTO getUsersId(Long id); } diff --git a/src/main/java/org/ayty/hatcher/api/v1/user/service/ListUsersIdServiceImpl.java b/src/main/java/org/ayty/hatcher/api/v1/user/service/GetUsersIdServiceImpl.java similarity index 74% rename from src/main/java/org/ayty/hatcher/api/v1/user/service/ListUsersIdServiceImpl.java rename to src/main/java/org/ayty/hatcher/api/v1/user/service/GetUsersIdServiceImpl.java index 6078cc0..f41a634 100644 --- a/src/main/java/org/ayty/hatcher/api/v1/user/service/ListUsersIdServiceImpl.java +++ b/src/main/java/org/ayty/hatcher/api/v1/user/service/GetUsersIdServiceImpl.java @@ -12,12 +12,12 @@ @Service @RequiredArgsConstructor -public class ListUsersIdServiceImpl implements ListUsersIdService{ - private final UserRepository userBD; +public class GetUsersIdServiceImpl implements GetUsersIdService{ + private final UserRepository userDB; @Override - public UpdateUserDTO listUsersId(Long id) { - Optional user = userBD.findById(id); + public UpdateUserDTO getUsersId(Long id) { + Optional user = userDB.findById(id); if (user.isPresent()) { return new UpdateUserDTO(user.get()); diff --git a/src/main/java/org/ayty/hatcher/api/v1/user/service/ListUsersServiceImpl.java b/src/main/java/org/ayty/hatcher/api/v1/user/service/ListUsersServiceImpl.java index 79fbe02..c3269dd 100644 --- a/src/main/java/org/ayty/hatcher/api/v1/user/service/ListUsersServiceImpl.java +++ b/src/main/java/org/ayty/hatcher/api/v1/user/service/ListUsersServiceImpl.java @@ -12,11 +12,11 @@ @Service @RequiredArgsConstructor public class ListUsersServiceImpl implements ListUsersService{ - private final UserRepository userBD; + private final UserRepository userDB; @Override public Page listOfRegisteredUsers(Pageable pageable) { - return userBD.findAll(pageable); + return userDB.findAll(pageable); } diff --git a/src/main/java/org/ayty/hatcher/api/v1/user/service/RegisterUserServiceImpl.java b/src/main/java/org/ayty/hatcher/api/v1/user/service/RegisterUserServiceImpl.java index 1006a77..b3d7a11 100644 --- a/src/main/java/org/ayty/hatcher/api/v1/user/service/RegisterUserServiceImpl.java +++ b/src/main/java/org/ayty/hatcher/api/v1/user/service/RegisterUserServiceImpl.java @@ -18,7 +18,7 @@ @Service public class RegisterUserServiceImpl implements RegisterUserService { - private final UserRepository userBD; + private final UserRepository userDB; private final PasswordEncoder passwordEncoder; @@ -26,20 +26,18 @@ public class RegisterUserServiceImpl implements RegisterUserService { @Transactional public OutRegisterDTO save(RegisterUserDTO user) { - if(userBD.existsByLogin(user.getLogin())==true){ + if(userDB.existsByLogin(user.getLogin())==true){ throw new UserAlreadyExistsException(); } - if(userBD.existsByEmail(user.getEmail())==true) { + if(userDB.existsByEmail(user.getEmail())==true) { throw new UserAlreadyExistsException(); } if(user.getLogin().matches("[a-zA-Z.]*")) { String EncryptedPassword = passwordEncoder.encode(user.getPassword()); user.setPassword(EncryptedPassword); User userImpl = User.to(user); - userBD.save(userImpl); - - - return new OutRegisterDTO(userImpl); + userDB.save(userImpl); + return new OutRegisterDTO(userImpl); } else { diff --git a/src/main/java/org/ayty/hatcher/api/v1/user/service/RemoveUserServiceImpl.java b/src/main/java/org/ayty/hatcher/api/v1/user/service/RemoveUserServiceImpl.java index f73fa15..f2dffb4 100644 --- a/src/main/java/org/ayty/hatcher/api/v1/user/service/RemoveUserServiceImpl.java +++ b/src/main/java/org/ayty/hatcher/api/v1/user/service/RemoveUserServiceImpl.java @@ -9,13 +9,13 @@ @RequiredArgsConstructor @Service public class RemoveUserServiceImpl implements RemoveUserService { - private final UserRepository userBD; + private final UserRepository userDB; @Override public void removeUser(Long id) { - if (userBD.existsById(id)==true) { - userBD.deleteById(id); + if (userDB.existsById(id)==true) { + userDB.deleteById(id); } else { throw new UsernameNotFoundException(); diff --git a/src/main/java/org/ayty/hatcher/api/v1/user/service/UpdateUserServiceImpl.java b/src/main/java/org/ayty/hatcher/api/v1/user/service/UpdateUserServiceImpl.java index 9b96892..bff6edd 100644 --- a/src/main/java/org/ayty/hatcher/api/v1/user/service/UpdateUserServiceImpl.java +++ b/src/main/java/org/ayty/hatcher/api/v1/user/service/UpdateUserServiceImpl.java @@ -16,15 +16,17 @@ public class UpdateUserServiceImpl implements UpdateUserService{ - private final UserRepository userBD; + private final UserRepository userDB; public UpdateUserDTO updateUser(Long id, User update) { - Optional user = userBD.findById(id); + Optional user = userDB.findById(id); if (user.isPresent()) { User userUpdate = user.get(); + userUpdate.setLogin(update.getLogin()); userUpdate.setEmail(update.getEmail()); userUpdate.setFullname(update.getFullname()); userUpdate.setImage(update.getImage()); + userDB.save(userUpdate); return new UpdateUserDTO(user.get()); } else { throw new InvalidDataException();