Corrected use of service port

This commit is contained in:
droideparanoico
2022-10-08 12:27:21 +02:00
parent 1a030c75b1
commit 27d64bb8fe
2 changed files with 21 additions and 21 deletions

View File

@@ -4,7 +4,7 @@ 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.domain.ports.primary.CartServicePort;
import com.rviewer.skeletons.application.request.CreateCartReq;
import com.rviewer.skeletons.application.request.UpdateCartReq;
import org.springframework.http.HttpStatus;
@@ -25,23 +25,23 @@ import java.util.UUID;
@RequestMapping("/carts")
public class CartController {
private final CartService cartService;
private final CartServicePort cartServicePort;
public CartController(CartService cartService) {
this.cartService = cartService;
public CartController(CartServicePort cartServicePort) {
this.cartServicePort = cartServicePort;
}
@GetMapping("/{id}")
public ResponseEntity<Cart> getCart(@PathVariable String id) {
return ResponseEntity.status(HttpStatus.OK).body(cartService.get(id).orElseThrow(CartNotFoundException::new));
return ResponseEntity.status(HttpStatus.OK).body(cartServicePort.get(id).orElseThrow(CartNotFoundException::new));
}
@PostMapping("/{id}")
public ResponseEntity<HttpStatus> saveCart(@PathVariable String id, @Valid @RequestBody CreateCartReq createCartReq) {
if (cartService.get(id).isPresent()) {
if (cartServicePort.get(id).isPresent()) {
throw new CartAlreadyExistsException();
} else {
cartService.save(new Cart(UUID.fromString(id), createCartReq.getItems()
cartServicePort.save(new Cart(UUID.fromString(id), createCartReq.getItems()
.stream()
.map(item -> new Item(item.getId(), item.getName(), item.getQuantity(), item.getPrice()))
.toList()));
@@ -51,18 +51,18 @@ public class CartController {
@PatchMapping("/{id}")
public ResponseEntity<HttpStatus> updateCart(@PathVariable String id, @Valid @RequestBody UpdateCartReq updateCartReq) {
Cart cart = cartService.get(id).orElseThrow(CartNotFoundException::new);
Cart cart = cartServicePort.get(id).orElseThrow(CartNotFoundException::new);
cart.setItems(updateCartReq.getItems());
cartService.save(cart);
cartServicePort.save(cart);
return ResponseEntity.status(HttpStatus.OK).build();
}
@DeleteMapping("/{id}")
public ResponseEntity<HttpStatus> deleteCart(@PathVariable String id) {
if (cartService.get(id).isEmpty()) {
if (cartServicePort.get(id).isEmpty()) {
throw new CartNotFoundException();
} else {
cartService.delete(id);
cartServicePort.delete(id);
return ResponseEntity.status(HttpStatus.OK).build();
}
}

View File

@@ -3,7 +3,7 @@ package com.rviewer.skeletons.infrastructure.inbound.api.adapter;
import com.fasterxml.jackson.databind.ObjectMapper;
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.domain.ports.primary.CartServicePort;
import com.rviewer.skeletons.application.exception.CartAlreadyExistsException;
import com.rviewer.skeletons.application.exception.CartNotFoundException;
import org.junit.jupiter.api.Test;
@@ -35,7 +35,7 @@ class CartControllerTest {
@Autowired
private MockMvc mockMvc;
@MockBean
private CartService cartService;
private CartServicePort cartServicePort;
@Test
void should_return_cart() throws Exception {
@@ -43,7 +43,7 @@ class CartControllerTest {
Item item = new Item(UUID.randomUUID(), "item", 1, 100F);
Cart expectedCart = new Cart(id, List.of(item));
when(cartService.get(id.toString())).thenReturn(Optional.of(expectedCart));
when(cartServicePort.get(id.toString())).thenReturn(Optional.of(expectedCart));
mockMvc.perform(MockMvcRequestBuilders.get("/carts/" + id))
.andExpect(status().isOk())
@@ -58,7 +58,7 @@ class CartControllerTest {
void should_not_return_non_existing_cart() throws Exception {
UUID id = UUID.randomUUID();
when(cartService.get(id.toString())).thenReturn(Optional.empty());
when(cartServicePort.get(id.toString())).thenReturn(Optional.empty());
mockMvc.perform(MockMvcRequestBuilders.get("/carts/" + id))
.andExpect(status().isNotFound())
@@ -83,7 +83,7 @@ class CartControllerTest {
Item item = new Item(UUID.randomUUID(), "item", 1, 100F);
Cart oldCart = new Cart(id, List.of(item));
when(cartService.get(id.toString())).thenReturn(Optional.of(oldCart));
when(cartServicePort.get(id.toString())).thenReturn(Optional.of(oldCart));
mockMvc.perform(MockMvcRequestBuilders.post("/carts/" + id)
.content(asJsonString(oldCart))
@@ -126,13 +126,13 @@ class CartControllerTest {
Item item = new Item(UUID.randomUUID(), "item", 1, 100F);
Cart oldCart = new Cart(id, List.of(item));
when(cartService.get(id.toString())).thenReturn(Optional.of(oldCart));
when(cartServicePort.get(id.toString())).thenReturn(Optional.of(oldCart));
mockMvc.perform(MockMvcRequestBuilders.patch("/carts/" + id)
.content(asJsonString(oldCart).replace("100.0", "1000.0"))
.contentType(MediaType.APPLICATION_JSON))
.andExpect(status().isOk())
.andExpect(result -> assertEquals(1000F, cartService.get(id.toString()).orElseThrow().getItems().get(0).getPrice()));
.andExpect(result -> assertEquals(1000F, cartServicePort.get(id.toString()).orElseThrow().getItems().get(0).getPrice()));
}
@Test
@@ -141,7 +141,7 @@ class CartControllerTest {
Item item = new Item(UUID.randomUUID(), "item", 1, 100F);
Cart nonExistingCart = new Cart(id, List.of(item));
when(cartService.get(id.toString())).thenReturn(Optional.empty());
when(cartServicePort.get(id.toString())).thenReturn(Optional.empty());
mockMvc.perform(MockMvcRequestBuilders.patch("/carts/" + id)
.content(asJsonString(nonExistingCart))
@@ -184,7 +184,7 @@ class CartControllerTest {
Item item = new Item(UUID.randomUUID(), "item", 1, 100F);
Cart oldCart = new Cart(id, List.of(item));
when(cartService.get(id.toString())).thenReturn(Optional.of(oldCart));
when(cartServicePort.get(id.toString())).thenReturn(Optional.of(oldCart));
mockMvc.perform(MockMvcRequestBuilders.delete("/carts/" + id)
.content(asJsonString(oldCart))
@@ -198,7 +198,7 @@ class CartControllerTest {
Item item = new Item(UUID.randomUUID(), "item", 1, 100F);
Cart nonExistingCart = new Cart(id, List.of(item));
when(cartService.get(id.toString())).thenReturn(Optional.empty());
when(cartServicePort.get(id.toString())).thenReturn(Optional.empty());
mockMvc.perform(MockMvcRequestBuilders.delete("/carts/" + id)
.content(asJsonString(nonExistingCart))