0
I’m doing a project and we’re in the API layer with Springboot. We have a problem with the verb PUT. We have the fields Id, Name, password, Tipousuario, password, so far so good, we are doing an If so that it identifies if there is an existing user with that name before updating, but our if does not let change the same person stating that there is already a user with the same name.
EX: I want to change the Tipousuario calling by id without changing the name of the person but he says I can not do because there is already someone with the same name.
@PutMapping("/atualizar")
public ResponseEntity<Usuario> Put(@RequestBody Usuario usuario) {
Optional<Usuario> user = usuarioService.AtualizarUsuario(usuario);
try {
return ResponseEntity.ok(user.get());
} catch (Exception e) {
return ResponseEntity.badRequest().build();
}
}
Here’s my upgrade PUT
I’m sorry if it’s not clear It’s the first time I’ve ever cast a doubt
Here is the user
package com.proagro.madeInRoca.service;
import java.nio.charset.Charset; import java.util.Optional;
import org.apache.Tomcat.util.codec.binary.Base64; import org.springframework.Beans.factory.Annotation.Autowired; import org.springframework.security.crypto.bcrypt.Bcryptpasswordencoder; import org.springframework.stereotype.Service;
import com.proagro.madeInRoca.model.Userlogin; import com.proagro.madeInRoca.model.Usuario; import com.proagro.madeInRoca.repository.Usuariorepository;
@Service public class Usuarioservice {
@Autowired
private UsuarioRepository repository;
public Optional<Usuario> CadastrarUsuario(Usuario usuario){
if(repository.findByUsuario(usuario.getUsuario()).isPresent()) {
return null;
}
BCryptPasswordEncoder encoder = new BCryptPasswordEncoder();
String senhaEconder = encoder.encode(usuario.getSenha());
usuario.setSenha(senhaEconder);
return Optional.of(repository.save(usuario));
}
public Optional<Usuario> AtualizarUsuario(Usuario usuario){
BCryptPasswordEncoder encoder = new BCryptPasswordEncoder();
String senhaEconder = encoder.encode(usuario.getSenha());
usuario.setSenha(senhaEconder);
return Optional.of(repository.save(usuario));
}
public Optional<UserLogin> Logar(Optional <UserLogin> user){
BCryptPasswordEncoder encoder = new BCryptPasswordEncoder();
Optional <Usuario> usuario = repository.findByUsuario(user.get().getUsuario());
if(usuario.isPresent()) {
if(encoder.matches(user.get().getSenha(), usuario.get().getSenha())) {
String auth = user.get().getUsuario()+":"+user.get().getSenha();
byte[]encodedAuth=Base64.encodeBase64(auth.getBytes(Charset.forName("US-ASCII")));
String authHeader="Basic " + new String (encodedAuth);
user.get().setToken(authHeader);
user.get().setNome(usuario.get().getNome());
user.get().setSenha(usuario.get().getSenha());
user.get().setTipoUsuario(usuario.get().getTipoUsuario());
return user;
}
}
return null;
}
}
Cade the user code?
– Franklin Barreto
public Optional<Usuario> AtualizarUsuario(Usuario usuario){

 BCryptPasswordEncoder encoder = new BCryptPasswordEncoder();
 String senhaEconder = encoder.encode(usuario.getSenha());

 usuario.setSenha(senhaEconder);

 return Optional.of(repository.save(usuario));
}
– Heloisa
Post spring error when trying to change
– Guilherme Cesar Medeiros
Edits the post and puts the whole part of the user related to the update. Only with this is not giving to see the problem.
– Franklin Barreto
@Franklinbarreto Alteirei the code, put the user service
– Heloisa
Apparently what is happening is that it is not updating but trying to create a new record. When we are going to do an update, we have to pass the object id and fetch it in the database to be managed by JPA. If it’s not clear, let me know I put a code so you can understand.
– Franklin Barreto
@Franklinbarreto I get it, so it would be a findAll ?? Can you send me the code if possible ? I guess it wasn’t clear
– Heloisa
@Franklinbarreto Thanks for your help, we managed to solve with your code.
– Heloisa
@Heloisa would be nice so mark with useful answer (:
– Franklin Barreto
@Franklinbarreto still can’t move right, just confirm for me if I scored correctly please?
– Heloisa
Yes yes @Heloisa
– Franklin Barreto