Implement patch, but still a bit ugly

This commit is contained in:
2025-01-31 11:06:33 +01:00
parent 8b64dbac97
commit 12d330fa6f
6 changed files with 67 additions and 20 deletions

View File

@ -90,7 +90,8 @@ paths:
content: content:
application/json: application/json:
schema: schema:
$ref: "#/components/schemas/PatchSigningRequestDocumentRequest" items:
$ref: "#/components/schemas/PatchOperation"
responses: responses:
204: 204:
description: "Signing request document was patched" description: "Signing request document was patched"
@ -216,7 +217,7 @@ components:
confirmed: confirmed:
type: boolean type: boolean
PatchSigningRequestDocumentRequest: PatchOperation:
type: object type: object
required: required:
- op - op

View File

@ -6,13 +6,19 @@ import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ExceptionHandler; import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice; import org.springframework.web.bind.annotation.RestControllerAdvice;
import java.util.NoSuchElementException;
@RestControllerAdvice @RestControllerAdvice
public class ControllerAdvice { public class ControllerAdvice {
@ExceptionHandler(value = {NoSuchElementException.class}) @ExceptionHandler(value = {IllegalArgumentException.class})
public ResponseEntity<ErrorResponse> handleAuthenticationException(Exception e) { public ResponseEntity<ErrorResponse> handleIllegalArgumentException(Exception e) {
var response = new ErrorResponse("TODO", HttpStatus.UNAUTHORIZED.value()); e.printStackTrace();
return new ResponseEntity<>(response, HttpStatus.UNAUTHORIZED); var response = new ErrorResponse(e.getMessage(), HttpStatus.BAD_REQUEST.value());
return new ResponseEntity<>(response, HttpStatus.BAD_REQUEST);
}
@ExceptionHandler(value = {RuntimeException.class})
public ResponseEntity<ErrorResponse> handleRuntimeException(Exception e) {
e.printStackTrace();
var response = new ErrorResponse(e.getMessage(), HttpStatus.INTERNAL_SERVER_ERROR.value());
return new ResponseEntity<>(response, HttpStatus.INTERNAL_SERVER_ERROR);
} }
} }

View File

@ -1,20 +1,53 @@
package ch.dlmw.swisssignchallenge.controllers; package ch.dlmw.swisssignchallenge.controllers;
import ch.dlmw.swisssignchallenge.entities.SigningRequestDocument;
import ch.dlmw.swisssignchallenge.services.SigningRequestDocumentService; import ch.dlmw.swisssignchallenge.services.SigningRequestDocumentService;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.fge.jsonpatch.JsonPatch;
import com.github.fge.jsonpatch.JsonPatchException;
import lombok.AllArgsConstructor; import lombok.AllArgsConstructor;
import org.openapitools.api.SigningRequestDocumentApi; import org.openapitools.api.SigningRequestDocumentApi;
import org.openapitools.model.PatchSigningRequestDocumentRequest; import org.openapitools.model.PatchOperation;
import org.springframework.http.ResponseEntity; import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RestController; import org.springframework.web.bind.annotation.RestController;
import java.io.IOException;
import java.util.List;
@RestController @RestController
@AllArgsConstructor @AllArgsConstructor
public class SigningRequestDocumentController implements SigningRequestDocumentApi { public class SigningRequestDocumentController implements SigningRequestDocumentApi {
private final SigningRequestDocumentService signingRequestDocumentService; private final SigningRequestDocumentService signingRequestDocumentService;
private final ObjectMapper objectMapper;
@Override @Override
public ResponseEntity<Void> patchSigningRequestDocument(String id, PatchSigningRequestDocumentRequest patchSigningRequestDocumentRequest) { public ResponseEntity<Void> patchSigningRequestDocument(String id, List<PatchOperation> patchOperations) {
signingRequestDocumentService.patchSigningRequestDocument(id, patchSigningRequestDocumentRequest); // Fetch the existing document
return ResponseEntity.noContent().build(); var document = signingRequestDocumentService.getSigningRequestDocument(id);
try {
ObjectMapper mapper = new ObjectMapper();
// Serialize patchOperations to JsonNode
JsonNode patchNode = mapper.valueToTree(patchOperations);
// Create JsonPatch from JsonNode
JsonPatch jsonPatch = JsonPatch.fromJson(patchNode);
// Apply patch to the document's JsonNode
JsonNode patchedNode = jsonPatch.apply(mapper.convertValue(document, JsonNode.class));
// Convert the patched JsonNode back to SigningRequestDocument
SigningRequestDocument patchedDocument = mapper.treeToValue(patchedNode, SigningRequestDocument.class);
// Now, update the document in the service layer
signingRequestDocumentService.updateSigningRequestDocument(patchedDocument);
return ResponseEntity.noContent().build();
} catch (IOException | JsonPatchException e) {
// Handle exceptions appropriately
throw new RuntimeException("Failed to apply patch", e);
}
} }
} }

View File

@ -1,5 +1,6 @@
package ch.dlmw.swisssignchallenge.entities; package ch.dlmw.swisssignchallenge.entities;
import com.fasterxml.jackson.annotation.JsonIgnore;
import jakarta.persistence.*; import jakarta.persistence.*;
import lombok.Getter; import lombok.Getter;
import lombok.Setter; import lombok.Setter;
@ -18,6 +19,7 @@ public class SigningRequestDocument {
@ManyToOne(fetch = FetchType.EAGER) @ManyToOne(fetch = FetchType.EAGER)
@JoinColumn(name = "signing_request_id", nullable = false) @JoinColumn(name = "signing_request_id", nullable = false)
@JsonIgnore
private SigningRequest signingRequest; private SigningRequest signingRequest;
@Column(name = "name", nullable = false) @Column(name = "name", nullable = false)

View File

@ -1,7 +1,9 @@
package ch.dlmw.swisssignchallenge.services; package ch.dlmw.swisssignchallenge.services;
import org.openapitools.model.PatchSigningRequestDocumentRequest; import ch.dlmw.swisssignchallenge.entities.SigningRequestDocument;
public interface SigningRequestDocumentService { public interface SigningRequestDocumentService {
void patchSigningRequestDocument(String id, PatchSigningRequestDocumentRequest patchSigningRequestDocumentRequest); void updateSigningRequestDocument(SigningRequestDocument signingRequestDocument);
SigningRequestDocument getSigningRequestDocument(String id);
} }

View File

@ -1,10 +1,9 @@
package ch.dlmw.swisssignchallenge.services.impl; package ch.dlmw.swisssignchallenge.services.impl;
import ch.dlmw.swisssignchallenge.entities.SigningRequestDocument;
import ch.dlmw.swisssignchallenge.repositories.SigningRequestDocumentRepository; import ch.dlmw.swisssignchallenge.repositories.SigningRequestDocumentRepository;
import ch.dlmw.swisssignchallenge.services.SigningRequestDocumentService; import ch.dlmw.swisssignchallenge.services.SigningRequestDocumentService;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.AllArgsConstructor; import lombok.AllArgsConstructor;
import org.openapitools.model.PatchSigningRequestDocumentRequest;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import java.util.UUID; import java.util.UUID;
@ -13,12 +12,16 @@ import java.util.UUID;
@AllArgsConstructor @AllArgsConstructor
public class SigningRequestDocumentServiceImpl implements SigningRequestDocumentService { public class SigningRequestDocumentServiceImpl implements SigningRequestDocumentService {
private final SigningRequestDocumentRepository signingRequestDocumentRepository; private final SigningRequestDocumentRepository signingRequestDocumentRepository;
private final ObjectMapper objectMapper;
@Override @Override
public void patchSigningRequestDocument(String id, PatchSigningRequestDocumentRequest patchSigningRequestDocumentRequest) { public void updateSigningRequestDocument(SigningRequestDocument signingRequestDocument) {
var document = signingRequestDocumentRepository.findById(UUID.fromString(id)).orElseThrow(); var document = signingRequestDocumentRepository.findById(signingRequestDocument.getId()).orElseThrow();
signingRequestDocument.setSigningRequest(document.getSigningRequest()); // todo: this is a bit ugly.. due to recursion the relationship is ignored so I set it manually
signingRequestDocumentRepository.save(signingRequestDocument);
}
System.out.println(document.isConfirmed()); @Override
public SigningRequestDocument getSigningRequestDocument(String id) {
return signingRequestDocumentRepository.findById(UUID.fromString(id)).orElseThrow();
} }
} }