From d6b18c2279d98b80ca342ffb2d8ffb5a6ac32e11 Mon Sep 17 00:00:00 2001 From: atusa17 Date: Thu, 7 Mar 2019 10:27:51 -0700 Subject: [PATCH] PAN-50 Began creation of the TheoremController --- .../controller/TheoremController.java | 144 +++++++++++++++++- .../repository/TheoremRepository.java | 2 +- 2 files changed, 141 insertions(+), 5 deletions(-) diff --git a/persistence/src/main/java/edu/msudenver/tsp/persistence/controller/TheoremController.java b/persistence/src/main/java/edu/msudenver/tsp/persistence/controller/TheoremController.java index dafd34f..8e86a5b 100644 --- a/persistence/src/main/java/edu/msudenver/tsp/persistence/controller/TheoremController.java +++ b/persistence/src/main/java/edu/msudenver/tsp/persistence/controller/TheoremController.java @@ -2,17 +2,18 @@ package edu.msudenver.tsp.persistence.controller; import edu.msudenver.tsp.persistence.dto.TheoremDto; import edu.msudenver.tsp.persistence.repository.TheoremRepository; +import edu.msudenver.tsp.utilities.PersistenceUtilities; import lombok.AllArgsConstructor; import lombok.extern.slf4j.Slf4j; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; import org.springframework.util.StopWatch; -import org.springframework.web.bind.annotation.GetMapping; -import org.springframework.web.bind.annotation.RequestMapping; -import org.springframework.web.bind.annotation.ResponseBody; -import org.springframework.web.bind.annotation.RestController; +import org.springframework.validation.BindingResult; +import org.springframework.web.bind.annotation.*; +import javax.validation.Valid; import java.util.List; +import java.util.Optional; @Slf4j @RestController @@ -39,4 +40,139 @@ public class TheoremController { return new ResponseEntity<>(listOfTheorems, HttpStatus.OK); } + + @GetMapping("/{id}") + public @ResponseBody + ResponseEntity getTheoremById(@PathVariable("id") final Integer id) { + LOG.info("Received request to query for theorem with id " + id); + if (id == null) { + LOG.error("ERROR: ID was null"); + return new ResponseEntity<>(HttpStatus.BAD_REQUEST); + } + + LOG.debug("Querying for theorem with id " + id); + + final StopWatch stopWatch = new StopWatch(); + stopWatch.start(); + + final Optional theorem = theoremRepository.findById(id); + + stopWatch.stop(); + + LOG.debug("Received response from server: query took " + stopWatch.getTotalTimeMillis() + "ms to complete"); + return theorem.map(theoremDto -> { + LOG.info("Returning theorem with id " + id); + return new ResponseEntity<>(theoremDto, HttpStatus.OK); + }).orElseGet( + () -> { + LOG.warn("No theorem was found with id " + id); + return new ResponseEntity<>(HttpStatus.NOT_FOUND); + }); + + } + + @PostMapping("/") + public @ResponseBody ResponseEntity insertTheorem( + @Valid @RequestBody final TheoremDto theoremDto, + final BindingResult bindingResult) { + LOG.info("Received request to insert a new theorem"); + if (bindingResult.hasErrors()) { + LOG.error("Binding result is unprocessable"); + return new ResponseEntity<>(HttpStatus.UNPROCESSABLE_ENTITY); + } + + if (theoremDto == null) { + LOG.error("Passed entity is unprocessable"); + return new ResponseEntity<>(HttpStatus.BAD_REQUEST); + } + + LOG.debug("Saving new theorem"); + + final StopWatch stopWatch = new StopWatch(); + stopWatch.start(); + + final TheoremDto savedTheorem = theoremRepository.save(theoremDto); + + stopWatch.stop(); + LOG.debug("Received response from server: query took " + stopWatch.getTotalTimeMillis() + "ms to complete"); + + LOG.info("Returning the newly created theorem with id " + savedTheorem.getId()); + return new ResponseEntity<>(savedTheorem, HttpStatus.CREATED); + } + + @PatchMapping("/{id}") + public @ResponseBody ResponseEntity updateTheorem( + @PathVariable("id") final Integer id, + @RequestBody final TheoremDto theoremDto, 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 (theoremDto == null) { + LOG.error("Passed entity is null"); + return new ResponseEntity<>(HttpStatus.BAD_REQUEST); + } + + if (id == null) { + LOG.error("Theorem ID must be specified"); + return new ResponseEntity<>(HttpStatus.BAD_REQUEST); + } + + LOG.debug("Checking for existence of theorem with id " + id); + + final StopWatch stopWatch = new StopWatch(); + stopWatch.start(); + + final Optional existingTheorem = theoremRepository.findById(id); + + stopWatch.stop(); + + LOG.debug("Received response from server: query took " + stopWatch.getTotalTimeMillis() + "ms to complete"); + + if (!existingTheorem.isPresent()) { + LOG.error("No theorem associated with id " + id); + return new ResponseEntity<>(HttpStatus.BAD_REQUEST); + } + + PersistenceUtilities.copyNonNullProperties(theoremDto, existingTheorem.get()); + existingTheorem.get().setVersion(existingTheorem.get().getVersion()+ 1); + + LOG.info("Updating theorem with id " + id); + LOG.debug("Querying for theorem with ID " + id); + + stopWatch.start(); + + final TheoremDto updatedTheorem = theoremRepository.save(existingTheorem.get()); + + stopWatch.stop(); + + LOG.debug("Received response from server: query took " + stopWatch.getTotalTimeMillis() + "ms to complete"); + + return new ResponseEntity<>(updatedTheorem, HttpStatus.OK); + } + + @DeleteMapping("/{id}") + public @ResponseBody ResponseEntity deleteTheoremById(@PathVariable("id") final Integer id) { + LOG.info("Received request to delete theorem with id " + id); + if (id == null) { + LOG.error("Specified id is null"); + return new ResponseEntity<>(HttpStatus.BAD_REQUEST); + } + + LOG.debug("Deleting theorem with id " + id); + + final StopWatch stopWatch = new StopWatch(); + stopWatch.start(); + + theoremRepository.deleteById(id); + + stopWatch.stop(); + + LOG.debug("Received response from server: query took " + stopWatch.getTotalTimeMillis() + "ms to complete"); + + return new ResponseEntity<>(HttpStatus.NO_CONTENT); + } } diff --git a/persistence/src/main/java/edu/msudenver/tsp/persistence/repository/TheoremRepository.java b/persistence/src/main/java/edu/msudenver/tsp/persistence/repository/TheoremRepository.java index 14f6b45..fc1b219 100644 --- a/persistence/src/main/java/edu/msudenver/tsp/persistence/repository/TheoremRepository.java +++ b/persistence/src/main/java/edu/msudenver/tsp/persistence/repository/TheoremRepository.java @@ -5,5 +5,5 @@ import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.stereotype.Repository; @Repository -public interface TheoremRepository extends JpaRepository { +public interface TheoremRepository extends JpaRepository { }