Skip to content

Commit

Permalink
Merge pull request #207 from skni-kod/issue-206
Browse files Browse the repository at this point in the history
feat: #206 add endpoint to evaluate statuses
  • Loading branch information
marcinbator authored Dec 4, 2024
2 parents e3ee211 + a88017a commit 9c474e5
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import pl.sknikod.kodemybackend.infrastructure.database.Material;
import pl.sknikod.kodemybackend.infrastructure.module.material.model.MaterialPageable;
import pl.sknikod.kodemybackend.infrastructure.module.material.model.SingleMaterialResponse;
import pl.sknikod.kodemybackend.infrastructure.module.material.model.StatusesToChangeResponse;
import pl.sknikod.kodemybackend.infrastructure.rest.MaterialControllerDefinition;

import java.net.URI;
Expand Down Expand Up @@ -60,6 +61,12 @@ public ResponseEntity<SingleMaterialResponse> showDetails(Long materialId) {
.body(materialGetByIdService.showDetails(materialId));
}

@Override
public ResponseEntity<StatusesToChangeResponse> showStatusesToChange(Long materialId) {
return ResponseEntity.status(HttpStatus.OK)
.body(materialStatusService.showStatusesToChange(materialId));
}

@Override
public ResponseEntity<Page<MaterialPageable>> manage(int size, int page, MaterialSortField sortField, Sort.Direction sortDirection, MaterialFilterSearchParams filterSearchParams) {
return ResponseEntity.status(HttpStatus.OK)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.stereotype.Service;
import pl.sknikod.kodemybackend.infrastructure.database.Material;
import pl.sknikod.kodemybackend.infrastructure.module.material.model.StatusesToChangeResponse;
import pl.sknikod.kodemybackend.infrastructure.store.MaterialStore;
import pl.sknikod.kodemycommons.exception.Validation400Exception;
import pl.sknikod.kodemycommons.exception.content.ExceptionUtil;
Expand Down Expand Up @@ -32,6 +33,14 @@ public Material.MaterialStatus update(Long materialId, Material.MaterialStatus n
.getOrElseThrow(ExceptionUtil::throwIfFailure);
}

StatusesToChangeResponse showStatusesToChange(Long materialId) {
return materialStore.findById(materialId, true)
.map(MaterialStore.FindByIdObject::getMaterial)
.map(material -> getPossibleStatuses(material.getStatus()))
.map(StatusesToChangeResponse::new)
.getOrElseThrow(ExceptionUtil::throwIfFailure);
}

private boolean canUserUpdateStatus(SimpleGrantedAuthority neededAuthority, Material material) {
UserPrincipal userPrincipal = AuthFacade.getCurrentUserPrincipal().get();
return userPrincipal.getAuthorities().contains(neededAuthority)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package pl.sknikod.kodemybackend.infrastructure.module.material.model;

import jakarta.persistence.EnumType;
import jakarta.persistence.Enumerated;
import pl.sknikod.kodemybackend.infrastructure.database.Material;

import java.util.List;

public record StatusesToChangeResponse(
@Enumerated(EnumType.STRING) List<Material.MaterialStatus> statuses) {
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import pl.sknikod.kodemybackend.infrastructure.module.material.MaterialUpdateService;
import pl.sknikod.kodemybackend.infrastructure.module.material.model.MaterialPageable;
import pl.sknikod.kodemybackend.infrastructure.module.material.model.SingleMaterialResponse;
import pl.sknikod.kodemybackend.infrastructure.module.material.model.StatusesToChangeResponse;
import pl.sknikod.kodemycommons.doc.SwaggerResponse;

import java.time.Instant;
Expand Down Expand Up @@ -85,6 +86,11 @@ ResponseEntity<Void> reindex(
@GetMapping("/{materialId}")
ResponseEntity<SingleMaterialResponse> showDetails(@PathVariable Long materialId);

@Operation(summary = "Show all possible statuses to change")
@SwaggerResponse.SuccessCode200
@GetMapping("/{materialId}/status/evaluate")
ResponseEntity<StatusesToChangeResponse> showStatusesToChange(@PathVariable Long materialId);

@Operation(summary = "Show all materials")
@SwaggerResponse.SuccessCode200
@SwaggerResponse.UnauthorizedCode401
Expand Down

0 comments on commit 9c474e5

Please sign in to comment.