Added application layer and blockchain implementation

This commit is contained in:
droideparanoico
2022-10-08 12:27:21 +02:00
parent 63fb2fd1c6
commit 1a030c75b1
18 changed files with 140 additions and 41 deletions

View File

@@ -0,0 +1,70 @@
package com.rviewer.skeletons.application.adapter;
import com.rviewer.skeletons.application.exception.CartAlreadyExistsException;
import com.rviewer.skeletons.application.exception.CartNotFoundException;
import com.rviewer.skeletons.domain.model.Cart;
import com.rviewer.skeletons.domain.model.Item;
import com.rviewer.skeletons.domain.service.CartService;
import com.rviewer.skeletons.application.request.CreateCartReq;
import com.rviewer.skeletons.application.request.UpdateCartReq;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PatchMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.validation.Valid;
import java.util.UUID;
@RestController
@RequestMapping("/carts")
public class CartController {
private final CartService cartService;
public CartController(CartService cartService) {
this.cartService = cartService;
}
@GetMapping("/{id}")
public ResponseEntity<Cart> getCart(@PathVariable String id) {
return ResponseEntity.status(HttpStatus.OK).body(cartService.get(id).orElseThrow(CartNotFoundException::new));
}
@PostMapping("/{id}")
public ResponseEntity<HttpStatus> saveCart(@PathVariable String id, @Valid @RequestBody CreateCartReq createCartReq) {
if (cartService.get(id).isPresent()) {
throw new CartAlreadyExistsException();
} else {
cartService.save(new Cart(UUID.fromString(id), createCartReq.getItems()
.stream()
.map(item -> new Item(item.getId(), item.getName(), item.getQuantity(), item.getPrice()))
.toList()));
return ResponseEntity.status(HttpStatus.CREATED).build();
}
}
@PatchMapping("/{id}")
public ResponseEntity<HttpStatus> updateCart(@PathVariable String id, @Valid @RequestBody UpdateCartReq updateCartReq) {
Cart cart = cartService.get(id).orElseThrow(CartNotFoundException::new);
cart.setItems(updateCartReq.getItems());
cartService.save(cart);
return ResponseEntity.status(HttpStatus.OK).build();
}
@DeleteMapping("/{id}")
public ResponseEntity<HttpStatus> deleteCart(@PathVariable String id) {
if (cartService.get(id).isEmpty()) {
throw new CartNotFoundException();
} else {
cartService.delete(id);
return ResponseEntity.status(HttpStatus.OK).build();
}
}
}

View File

@@ -0,0 +1,13 @@
package com.rviewer.skeletons.application.exception;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ResponseStatus;
@ResponseStatus(HttpStatus.CONFLICT)
public class CartAlreadyExistsException extends RuntimeException {
private static final String CART_ALREADY_EXISTS = "Invalid identifier.";
public CartAlreadyExistsException() {
super(CART_ALREADY_EXISTS);
}
}

View File

@@ -0,0 +1,13 @@
package com.rviewer.skeletons.application.exception;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ResponseStatus;
@ResponseStatus(HttpStatus.NOT_FOUND)
public class CartNotFoundException extends RuntimeException {
private static final String CART_NOT_FOUND = "Cart not found for the given ID";
public CartNotFoundException() {
super(CART_NOT_FOUND);
}
}

View File

@@ -0,0 +1,32 @@
package com.rviewer.skeletons.application.exception;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.http.converter.HttpMessageNotReadableException;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseStatus;
@ControllerAdvice
public class ControllerExceptionHandler {
public static final String INVALID_BODY = "Invalid body provided, check the payload.";
@ExceptionHandler(CartNotFoundException.class)
public ResponseEntity<Object> handleCartNotFound(CartNotFoundException e) {
return new ResponseEntity<>(e.getMessage(), HttpStatus.NOT_FOUND);
}
@ExceptionHandler(CartAlreadyExistsException.class)
public ResponseEntity<Object> handleCartAlreadyExists(CartAlreadyExistsException e) {
return new ResponseEntity<>(e.getMessage(), HttpStatus.CONFLICT);
}
@ExceptionHandler({MethodArgumentNotValidException.class, HttpMessageNotReadableException.class})
@ResponseStatus(HttpStatus.BAD_REQUEST)
public ResponseEntity<Object> handleInvalidBody() {
return new ResponseEntity<>(INVALID_BODY, HttpStatus.BAD_REQUEST);
}
}

View File

@@ -0,0 +1,13 @@
package com.rviewer.skeletons.application.request;
import com.rviewer.skeletons.domain.model.Item;
import lombok.Getter;
import javax.validation.constraints.NotNull;
import java.util.List;
@Getter
public class CreateCartReq {
@NotNull
private List<Item> items;
}

View File

@@ -0,0 +1,13 @@
package com.rviewer.skeletons.application.request;
import com.rviewer.skeletons.domain.model.Item;
import lombok.Getter;
import javax.validation.constraints.NotNull;
import java.util.List;
@Getter
public class UpdateCartReq {
@NotNull
private List<Item> items;
}