Код: Выделить всё
@Setter
@Getter
@AllArgsConstructor
@NoArgsConstructor
public class UserDto {
private Long id;
private String username;
private String email;
}
Код: Выделить всё
public UserDto updateUser(UserDto userDto, Long id) {
User user=userRepository.findById(id).orElseThrow(
() -> new ResourceNotFoundException("User of that ID not Found "+id));
if(userRepository.existsByUsername(userDto.getUsername() )){
throw new EmailUsernameAlreadyExist("Username Already Exist ! "+userDto.getUsername());
}
if(userRepository.existsByEmail(userDto.getEmail())){
throw new EmailUsernameAlreadyExist("Email Already Exist ! "+userDto.getEmail());
}
user.setEmail(userDto.getEmail());
user.setUsername(userDto.getUsername());
User user1=userRepository.save(user);
return UserMapper.mapToUserDto(user1);
}
Код: Выделить всё
@PutMapping("{id}")
public ResponseEntity updateUser(@RequestBody UserDto userDto,@PathVariable Long id){
UserDto userDto1=userService.updateUser(userDto,id);
return ResponseEntity.ok(userDto1);
}
Пользовательская служба .
Код: Выделить всё
export const updateUserById=(user,id)=>fetch(API_URL_BASE+'/'+id,
{
method : 'PUT',
headers : {
'Content-Type' : 'application/json'
},
body : JSON.stringify(user)
}
).then(res=>res.json()).catch(error=>console.error(error))
Код: Выделить всё
const saveOrUpdateUser=()=>{
const myUser={username,email}
if(userId){
console.log('update')
console.log(myUser,userId)
updateUserById(myUser,userId)
}
else{
console.log('save')
createUser(myUser).then(res=>{
loadUsers()
}).catch(error=>console.error(error))
}
}
shrong> error.br/>
Подробнее здесь: https://stackoverflow.com/questions/797 ... l-in-react