Skip to content

Commit

Permalink
refs(#1) - Ajustes do UpdateUserService
Browse files Browse the repository at this point in the history
  • Loading branch information
ruan-pb committed Feb 22, 2021
1 parent ea649b4 commit c4e8456
Show file tree
Hide file tree
Showing 13 changed files with 40 additions and 33 deletions.
6 changes: 6 additions & 0 deletions .classpath
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,17 @@
<classpathentry kind="src" path="target/generated-sources/annotations">
<attributes>
<attribute name="optional" value="true"/>
<attribute name="maven.pomderived" value="true"/>
<attribute name="ignore_optional_problems" value="true"/>
<attribute name="m2e-apt" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="src" output="target/test-classes" path="target/generated-test-sources/test-annotations">
<attributes>
<attribute name="optional" value="true"/>
<attribute name="maven.pomderived" value="true"/>
<attribute name="ignore_optional_problems" value="true"/>
<attribute name="m2e-apt" value="true"/>
<attribute name="test" value="true"/>
</attributes>
</classpathentry>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ public String generateToken(LoginDTO user) {
Date date = Date.from(instant);
HashMap<String, Object> claim = new HashMap<String, Object>();
Optional<User> userAdmin = userBD.findByLogin(user.getLogin());

claim.put("Login", user.getLogin());
claim.put("Email", userAdmin.get().getEmail());
claim.put("Id", userAdmin.get().getId());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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) {
Expand All @@ -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();
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Original file line number Diff line number Diff line change
Expand Up @@ -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> user = userBD.findById(id);
public UpdateUserDTO getUsersId(Long id) {
Optional<User> user = userDB.findById(id);
if (user.isPresent()) {

return new UpdateUserDTO(user.get());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@
@Service
@RequiredArgsConstructor
public class ListUsersServiceImpl implements ListUsersService{
private final UserRepository userBD;
private final UserRepository userDB;

@Override
public Page<User> listOfRegisteredUsers(Pageable pageable) {
return userBD.findAll(pageable);
return userDB.findAll(pageable);
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,28 +18,26 @@
@Service
public class RegisterUserServiceImpl implements RegisterUserService {

private final UserRepository userBD;
private final UserRepository userDB;

private final PasswordEncoder passwordEncoder;


@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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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> user = userBD.findById(id);
Optional<User> user = userDB.findById(id);
if (user.isPresent()) {
User userUpdate = user.get();
userUpdate.setLogin(update.getLogin());

This comment has been minimized.

Copy link
@alissonvgs

alissonvgs Feb 22, 2021

É melhor tirar a mudança do Login, olha com eduardo aí depois

userUpdate.setEmail(update.getEmail());
userUpdate.setFullname(update.getFullname());
userUpdate.setImage(update.getImage());
userDB.save(userUpdate);
return new UpdateUserDTO(user.get());
} else {
throw new InvalidDataException();
Expand Down

0 comments on commit c4e8456

Please sign in to comment.