класс юзер дто и сервис

This commit is contained in:
ekallin 2024-03-18 12:11:50 +04:00
parent fe92621f14
commit e41cdadd7a
2 changed files with 98 additions and 0 deletions

View File

@ -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;
}
}

View File

@ -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);
}
}