класс юзер дто и сервис
This commit is contained in:
parent
fe92621f14
commit
e41cdadd7a
@ -0,0 +1,53 @@
|
||||
package com.example.backend.users.api;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonProperty;
|
||||
|
||||
import jakarta.validation.constraints.NotBlank;
|
||||
import jakarta.validation.constraints.NotNull;
|
||||
|
||||
public class UserDTO {
|
||||
|
||||
private Integer id;
|
||||
|
||||
@NotBlank
|
||||
private String username;
|
||||
|
||||
@NotBlank
|
||||
private String password;
|
||||
|
||||
@NotNull
|
||||
private boolean isAdmin;
|
||||
|
||||
@JsonProperty(access = JsonProperty.Access.READ_ONLY)
|
||||
public Integer getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(Integer id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public String getUsername() {
|
||||
return username;
|
||||
}
|
||||
|
||||
public void setUsername(String username) {
|
||||
this.username = username;
|
||||
}
|
||||
|
||||
public String getPassword() {
|
||||
return password;
|
||||
}
|
||||
|
||||
public void setPassword(String password) {
|
||||
this.password = password;
|
||||
}
|
||||
|
||||
public boolean getIsAdmin() {
|
||||
return isAdmin;
|
||||
}
|
||||
|
||||
public void setIsAdmin(boolean isAdmin) {
|
||||
this.isAdmin = isAdmin;
|
||||
}
|
||||
}
|
@ -0,0 +1,45 @@
|
||||
package com.example.backend.users.service;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import com.example.backend.core.errors.NotFoundException;
|
||||
import com.example.backend.users.model.UserEntity;
|
||||
import com.example.backend.users.repository.UserRepository;
|
||||
|
||||
@Service
|
||||
public class UserService {
|
||||
private final UserRepository repository;
|
||||
|
||||
public UserService(UserRepository repository) {
|
||||
this.repository = repository;
|
||||
}
|
||||
|
||||
public List<UserEntity> getAll() {
|
||||
return repository.getAll();
|
||||
}
|
||||
|
||||
public UserEntity get(Integer id) {
|
||||
return Optional.ofNullable(repository.get(id)).orElseThrow(() -> new NotFoundException(id));
|
||||
}
|
||||
|
||||
public UserEntity create(UserEntity entity) {
|
||||
return repository.create(entity);
|
||||
}
|
||||
|
||||
public UserEntity update(Integer id, UserEntity entity) {
|
||||
final UserEntity existsentity = get(id);
|
||||
existsentity.setUsername(entity.getUsername());
|
||||
existsentity.setPassword(entity.getPassword());
|
||||
existsentity.setIsAdmin(entity.getIsAdmin());
|
||||
return repository.update(existsentity);
|
||||
}
|
||||
|
||||
public UserEntity delete(Integer id) {
|
||||
final UserEntity existsentity = get(id);
|
||||
return repository.delete(existsentity);
|
||||
}
|
||||
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user