Skip to content

Commit

Permalink
feat: improve logic for updateEndorsementStatus API
Browse files Browse the repository at this point in the history
  • Loading branch information
Atifsid committed Apr 22, 2024
1 parent 6c68450 commit 60e41c8
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,6 @@ public ResponseEntity<GenericResponse<EndorsementDTO>> postEndorsement(
@PatchMapping(value = "/{id}")
public ResponseEntity<GenericResponse<Void>> updateEndorsementStatus(
@PathVariable(value = "id") String id, @RequestParam String status) {
return endorsementService.updateEndorsementStatus(id, status);
return ResponseEntity.ok().body(endorsementService.updateEndorsementStatus(id, status));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import java.util.UUID;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.http.ResponseEntity;

public interface EndorsementService {
EndorsementDTO getEndorsementById(UUID id);
Expand All @@ -18,5 +17,5 @@ Page<EndorsementModelFromJSON> getEndorsementsFromDummyData(

EndorsementModel createEndorsement(EndorsementDRO endorsementDRO);

ResponseEntity<GenericResponse<Void>> updateEndorsementStatus(String id, String status);
GenericResponse<Void> updateEndorsementStatus(String id, String status);
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,7 @@
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;

Expand Down Expand Up @@ -67,20 +66,21 @@ public EndorsementModel createEndorsement(EndorsementDRO endorsementDRO) {
}

@Override
public ResponseEntity<GenericResponse<Void>> updateEndorsementStatus(String id, String status) {
public GenericResponse<Void> updateEndorsementStatus(String id, String status) {
UserModel user =
(UserModel) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
if (!user.getRole().equals(UserRole.SUPERUSER))
return ResponseEntity.status(HttpStatus.UNAUTHORIZED)
.body(new GenericResponse<>(null, "Unauthorized"));
if (!user.getRole().equals(UserRole.SUPERUSER)) {
throw new AccessDeniedException("Unauthorized access");
}
if (!(Objects.equals(status, EndorsementStatus.APPROVED.name())
|| Objects.equals(status, EndorsementStatus.REJECTED.name()))) {
return ResponseEntity.status(HttpStatus.BAD_REQUEST)
.body(new GenericResponse<>(null, "Invalid Status: " + status));
throw new IllegalArgumentException("Invalid endorsement status: " + status);
}

try {
UUID uuid = UUID.fromString(id);
Optional<EndorsementModel> optionalEndorsementModel = endorsementRepository.findById(uuid);
UUID endorsementId = UUID.fromString(id);
Optional<EndorsementModel> optionalEndorsementModel =
endorsementRepository.findById(endorsementId);
if (optionalEndorsementModel.isPresent()) {
EndorsementModel updatedEndorsementModel =
EndorsementModel.builder()
Expand All @@ -91,20 +91,11 @@ public ResponseEntity<GenericResponse<Void>> updateEndorsementStatus(String id,
.status(EndorsementStatus.valueOf(status))
.build();
endorsementRepository.save(updatedEndorsementModel);
return ResponseEntity.ok()
.body(new GenericResponse<>(null, "Successfully updated endorsement status"));
return new GenericResponse<>(null, "Successfully updated endorsement status");
}
return ResponseEntity.status(HttpStatus.BAD_REQUEST)
.body(new GenericResponse<>(null, "Invalid UUID: " + id));
} catch (IllegalArgumentException e) {
return ResponseEntity.status(HttpStatus.BAD_REQUEST)
.body(new GenericResponse<>(null, "Invalid UUID: " + id));
} catch (EntityNotFoundException e) {
return ResponseEntity.status(HttpStatus.NOT_FOUND)
.body(new GenericResponse<>(null, e.getMessage()));
} catch (Exception e) {
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR)
.body(new GenericResponse<>(null, "Something went wrong. Please contact admin."));
throw new NoEntityException("No endorsement with id " + id + " was found");
} catch (IllegalArgumentException ex) {
throw new IllegalArgumentException("Invalid endorsement id: " + id);
}
}
}

0 comments on commit 60e41c8

Please sign in to comment.