diff --git a/backend/src/main/java/com/example/backend/users/api/UserDTO.java b/backend/src/main/java/com/example/backend/users/api/UserDTO.java new file mode 100644 index 0000000..7acdef3 --- /dev/null +++ b/backend/src/main/java/com/example/backend/users/api/UserDTO.java @@ -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; + } +} diff --git a/backend/src/main/java/com/example/backend/users/service/UserService.java b/backend/src/main/java/com/example/backend/users/service/UserService.java new file mode 100644 index 0000000..4eda0d2 --- /dev/null +++ b/backend/src/main/java/com/example/backend/users/service/UserService.java @@ -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 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); + } + +}