diff options
| author | Kevin Hoerr <kjhoerr@noreply.cybr.es> | 2022-08-06 01:44:00 +0000 |
|---|---|---|
| committer | Kevin Hoerr <kjhoerr@noreply.cybr.es> | 2022-08-06 01:44:00 +0000 |
| commit | 461b1fa053bcc86d06156574ab59fa7000dbf69e (patch) | |
| tree | 6e0080d057b5015bd92c843481cf45575af462a8 /src/main/java | |
| parent | cdf65b32202746eaffd9e58bf951d1995ab03be3 (diff) | |
| download | pantry-461b1fa053bcc86d06156574ab59fa7000dbf69e.tar.gz pantry-461b1fa053bcc86d06156574ab59fa7000dbf69e.tar.bz2 pantry-461b1fa053bcc86d06156574ab59fa7000dbf69e.zip | |
Quarkus (#2)
Reviewed-on: https://git.submelon.dev/kjhoerr/pantry/pulls/2
Diffstat (limited to 'src/main/java')
8 files changed, 88 insertions, 231 deletions
diff --git a/src/main/java/dev/submelon/pantry/CustomControllerAdvice.java b/src/main/java/dev/submelon/pantry/CustomControllerAdvice.java deleted file mode 100644 index dad961b..0000000 --- a/src/main/java/dev/submelon/pantry/CustomControllerAdvice.java +++ /dev/null @@ -1,46 +0,0 @@ -package dev.submelon.pantry; - -import org.springframework.http.HttpStatus; -import org.springframework.http.ResponseEntity; -import org.springframework.web.bind.annotation.ControllerAdvice; -import org.springframework.web.bind.annotation.ExceptionHandler; -import java.io.PrintWriter; -import java.io.StringWriter; -import java.util.NoSuchElementException; - -@ControllerAdvice -class CustomControllerAdvice { - @ExceptionHandler(NoSuchElementException.class) - public ResponseEntity<ErrorResponse> handleNullPointerExceptions( - NoSuchElementException e - ) { - HttpStatus status = HttpStatus.NOT_FOUND; - - return new ResponseEntity<>( - new ErrorResponse(status, e.getMessage()), - status - ); - } - - @ExceptionHandler(Exception.class) - public ResponseEntity<ErrorResponse> handleExceptions( - Exception e - ) { - HttpStatus status = HttpStatus.INTERNAL_SERVER_ERROR; - - // converting the stack trace to String - StringWriter stringWriter = new StringWriter(); - PrintWriter printWriter = new PrintWriter(stringWriter); - e.printStackTrace(printWriter); - String stackTrace = stringWriter.toString(); - - return new ResponseEntity<>( - new ErrorResponse( - status, - e.getMessage(), - stackTrace // specifying the stack trace in case of 500s - ), - status - ); - } -} diff --git a/src/main/java/dev/submelon/pantry/ErrorResponse.java b/src/main/java/dev/submelon/pantry/ErrorResponse.java deleted file mode 100644 index c5b1dbe..0000000 --- a/src/main/java/dev/submelon/pantry/ErrorResponse.java +++ /dev/null @@ -1,58 +0,0 @@ -package dev.submelon.pantry; - -import com.fasterxml.jackson.annotation.JsonFormat; -import lombok.Getter; -import lombok.Setter; -import org.springframework.http.HttpStatus; -import java.util.Date; - -@Getter -@Setter -public class ErrorResponse { - // customizing timestamp serialization format - @JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "dd-MM-yyyy hh:mm:ss") - private Date timestamp; - - private int code; - - private String status; - - private String message; - - private String stackTrace; - - private Object data; - - public ErrorResponse() { - timestamp = new Date(); - } - - public ErrorResponse(HttpStatus httpStatus, String message) { - this(); - - this.code = httpStatus.value(); - this.status = httpStatus.name(); - this.message = message; - } - - public ErrorResponse( - HttpStatus httpStatus, - String message, - String stackTrace - ) { - this(httpStatus, message); - - this.stackTrace = stackTrace; - } - - public ErrorResponse( - HttpStatus httpStatus, - String message, - String stackTrace, - Object data - ) { - this(httpStatus, message, stackTrace); - - this.data = data; - } -} diff --git a/src/main/java/dev/submelon/pantry/PantryApplication.java b/src/main/java/dev/submelon/pantry/PantryApplication.java deleted file mode 100644 index 0c2a5b5..0000000 --- a/src/main/java/dev/submelon/pantry/PantryApplication.java +++ /dev/null @@ -1,15 +0,0 @@ -package dev.submelon.pantry; - -import org.springframework.boot.SpringApplication; -import org.springframework.boot.autoconfigure.SpringBootApplication; -import org.springframework.web.bind.annotation.RestController; - -@SpringBootApplication -@RestController -public class PantryApplication { - - public static void main(String[] args) { - SpringApplication.run(PantryApplication.class, args); - } - -} diff --git a/src/main/java/dev/submelon/pantry/PantryItem.java b/src/main/java/dev/submelon/pantry/PantryItem.java deleted file mode 100644 index 97ab62d..0000000 --- a/src/main/java/dev/submelon/pantry/PantryItem.java +++ /dev/null @@ -1,40 +0,0 @@ -package dev.submelon.pantry; - -import javax.persistence.Column; -import javax.persistence.Entity; -import javax.persistence.GeneratedValue; -import javax.persistence.GenerationType; -import javax.persistence.Id; - -import lombok.Getter; -import lombok.NoArgsConstructor; -import lombok.Setter; - -@Entity -@Getter -@Setter -@NoArgsConstructor -public class PantryItem { - @Id - @GeneratedValue(strategy=GenerationType.AUTO) - private Long id; - - @Column(nullable=false) - private String name; - - private String description; - - @Column(nullable=false) - private double quantity; - - @Column(nullable=false) - private String quantityUnitType; - - public PantryItem(String name, String description, double quantity, String quantityUnitType) { - this.name = name; - this.description = description; - this.quantity = quantity; - this.quantityUnitType = quantityUnitType; - } - -} diff --git a/src/main/java/dev/submelon/pantry/PantryItemController.java b/src/main/java/dev/submelon/pantry/PantryItemController.java deleted file mode 100644 index d5586e5..0000000 --- a/src/main/java/dev/submelon/pantry/PantryItemController.java +++ /dev/null @@ -1,61 +0,0 @@ -package dev.submelon.pantry; - -import java.util.NoSuchElementException; - -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.stereotype.Controller; -import org.springframework.web.bind.annotation.DeleteMapping; -import org.springframework.web.bind.annotation.GetMapping; -import org.springframework.web.bind.annotation.PathVariable; -import org.springframework.web.bind.annotation.PostMapping; -import org.springframework.web.bind.annotation.PutMapping; -import org.springframework.web.bind.annotation.RequestBody; -import org.springframework.web.bind.annotation.RequestMapping; -import org.springframework.web.bind.annotation.ResponseBody; - -@Controller -@RequestMapping(path="/items") -public class PantryItemController { - @Autowired - private PantryItemRepository itemRepository; - - @GetMapping(path="") - @ResponseBody - Iterable<PantryItem> getAllItems() { - return itemRepository.findAll(); - } - - @PostMapping(path="") - @ResponseBody - PantryItem addNewItem(@RequestBody PantryItem item) { - return itemRepository.save(item); - } - - @PutMapping(path="/{id}") - @ResponseBody - PantryItem addNewItem(@RequestBody PantryItem item, @PathVariable Long id) { - return itemRepository.findById(id) - .map(existingItem -> { - existingItem.setName(item.getName()); - existingItem.setDescription(item.getDescription()); - existingItem.setQuantity(item.getQuantity()); - existingItem.setQuantityUnitType(item.getQuantityUnitType()); - return itemRepository.save(existingItem); - }) - .orElseGet(() -> { - return itemRepository.save(item); - }); - } - - @DeleteMapping(path="/{id}") - @ResponseBody - PantryItem deletePantryItem(@PathVariable Long id) { - return itemRepository.findById(id) - .map(item -> { - itemRepository.delete(item); - return item; - }) - .orElseThrow(NoSuchElementException::new); - } - -} diff --git a/src/main/java/dev/submelon/pantry/PantryItemRepository.java b/src/main/java/dev/submelon/pantry/PantryItemRepository.java deleted file mode 100644 index 947ad6b..0000000 --- a/src/main/java/dev/submelon/pantry/PantryItemRepository.java +++ /dev/null @@ -1,11 +0,0 @@ -package dev.submelon.pantry; - -import java.util.Optional; - -import org.springframework.data.repository.CrudRepository; - -public interface PantryItemRepository extends CrudRepository<PantryItem, Long> { - - Optional<PantryItem> findByName(String name); - -} diff --git a/src/main/java/dev/submelon/rest/json/PantryItem.java b/src/main/java/dev/submelon/rest/json/PantryItem.java new file mode 100644 index 0000000..d476e3c --- /dev/null +++ b/src/main/java/dev/submelon/rest/json/PantryItem.java @@ -0,0 +1,29 @@ +package dev.submelon.rest.json; + +import javax.persistence.Column; +import javax.persistence.Entity; +import javax.persistence.Table; + +import io.quarkus.hibernate.orm.panache.PanacheEntity; +import lombok.Data; +import lombok.EqualsAndHashCode; + +@Entity +@Table(name = "pantryitem") +@Data +@EqualsAndHashCode(callSuper = false) +public class PantryItem extends PanacheEntity { + + @Column(nullable = false) + private String name; + + @Column + private String description; + + @Column(nullable = false) + private double quantity; + + @Column(nullable = false) + private String quantityUnitType; + +} diff --git a/src/main/java/dev/submelon/rest/json/PantryItemResource.java b/src/main/java/dev/submelon/rest/json/PantryItemResource.java new file mode 100644 index 0000000..4bb51c0 --- /dev/null +++ b/src/main/java/dev/submelon/rest/json/PantryItemResource.java @@ -0,0 +1,59 @@ +package dev.submelon.rest.json; + +import java.util.List; + +import javax.transaction.Transactional; +import javax.ws.rs.DELETE; +import javax.ws.rs.GET; +import javax.ws.rs.POST; +import javax.ws.rs.PUT; +import javax.ws.rs.Path; +import javax.ws.rs.PathParam; +import javax.ws.rs.Produces; +import javax.ws.rs.WebApplicationException; +import javax.ws.rs.core.MediaType; +import javax.ws.rs.core.Response; + +@Path("/items") +public class PantryItemResource { + + @GET + @Produces(MediaType.APPLICATION_JSON) + public List<PantryItem> getItems() { + return PantryItem.findAll().list(); + } + + @Transactional + @POST + @Produces(MediaType.APPLICATION_JSON) + public PantryItem postItem(PantryItem item) { + PantryItem.persist(item); + + return item; + } + + @Transactional + @PUT + @Path("/{id}") + @Produces(MediaType.APPLICATION_JSON) + public PantryItem putItem(@PathParam("id") Long id, PantryItem item) { + if (item.id.equals(id)) { + PantryItem.persist(item); + } else { + throw new WebApplicationException(Response.status(400).entity("ID does not match body").build()); + } + + return item; + } + + @Transactional + @DELETE + @Path("/{id}") + public void deleteItem(@PathParam("id") Long id) { + boolean result = PantryItem.deleteById(id); + + if (!result) { + throw new WebApplicationException(Response.status(404).entity("Could not find item").build()); + } + } +} |
