diff options
Diffstat (limited to 'src/main/java/dev/submelon/pantry')
6 files changed, 0 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); - -} |
