PAN-48 Created the updateDefinition method

This commit is contained in:
2019-02-24 19:17:51 -07:00
parent b77a750c30
commit a3ab3f10bf
@@ -2,6 +2,7 @@ package edu.msudenver.tsp.persistence.controller;
import edu.msudenver.tsp.persistence.dto.DefinitionDto;
import edu.msudenver.tsp.persistence.repository.DefinitionRepository;
import edu.msudenver.tsp.utilities.PersistenceUtilities;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
@@ -102,6 +103,60 @@ public class DefinitionController {
return new ResponseEntity<>(savedDefinition, HttpStatus.CREATED);
}
@PatchMapping("/{id}")
public @ResponseBody ResponseEntity<DefinitionDto> updateAccount(
@PathVariable("id") final Integer id,
@RequestBody final DefinitionDto definitionDto, final BindingResult bindingResult) {
LOG.info("Received request to update an account");
if (bindingResult.hasErrors()) {
LOG.error("Binding result is unprocessable");
return new ResponseEntity<>(HttpStatus.UNPROCESSABLE_ENTITY);
}
if (definitionDto == null) {
LOG.error("Passed entity is null");
return new ResponseEntity<>(HttpStatus.BAD_REQUEST);
}
if (id == null) {
LOG.error("Definition ID must be specified");
return new ResponseEntity<>(HttpStatus.BAD_REQUEST);
}
LOG.debug("Checking for existence of definition with id " + id);
final StopWatch stopWatch = new StopWatch();
stopWatch.start();
final Optional<DefinitionDto> existingDefinition = definitionRepository.findById(id);
stopWatch.stop();
LOG.debug("Received response from server: query took " + stopWatch.getTotalTimeMillis() + "ms to complete");
if (!existingDefinition.isPresent()) {
LOG.error("No definition associated with id " + id);
return new ResponseEntity<>(HttpStatus.BAD_REQUEST);
}
PersistenceUtilities.copyNonNullProperties(definitionDto, existingDefinition.get());
existingDefinition.get().setVersion(existingDefinition.get().getVersion()+ 1);
LOG.info("Updating definition with id " + id);
LOG.debug("Querying for definition with ID " + id);
stopWatch.start();
final DefinitionDto updatedDefinition = definitionRepository.save(existingDefinition.get());
stopWatch.stop();
LOG.debug("Received response from server: query took " + stopWatch.getTotalTimeMillis() + "ms to complete");
return new ResponseEntity<>(updatedDefinition, HttpStatus.OK);
}
@DeleteMapping("/{id}")
public @ResponseBody ResponseEntity<Void> deleteDefinitionById(@PathVariable("id") final Integer id) {
LOG.info("Received request to delete definition with id " + id);