Skip to content

Commit

Permalink
Merge pull request #4 from pulnara/dev-pulnara
Browse files Browse the repository at this point in the history
Warm-up improvements
  • Loading branch information
pulnara authored May 20, 2021
2 parents d701540 + 3d47ded commit 8f9c742
Show file tree
Hide file tree
Showing 9 changed files with 169 additions and 59 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Optional;

public class ConsumerService {
Expand All @@ -33,4 +34,9 @@ public ResultWithEvents<Consumer> create(PersonName name) {
public Optional<Consumer> findById(long consumerId) {
return consumerRepository.findById(consumerId);
}


public List<Consumer> getAllConsumers() {
return (List<Consumer>) consumerRepository.findAll();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,26 +7,43 @@
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import static java.util.stream.Collectors.toList;

@RestController
@RequestMapping(path="/consumers")
@RequestMapping(path = "/consumers")
public class ConsumerController {

private ConsumerService consumerService;
private ConsumerService consumerService;

public ConsumerController(ConsumerService consumerService) {
this.consumerService = consumerService;
}

@RequestMapping(method = RequestMethod.POST)
public CreateConsumerResponse create(@RequestBody CreateConsumerRequest request) {
ResultWithEvents<Consumer> result = consumerService.create(request.getName());
return new CreateConsumerResponse(result.result.getId());
}

public ConsumerController(ConsumerService consumerService) {
this.consumerService = consumerService;
}
@RequestMapping(method = RequestMethod.GET, path = "/{consumerId}")
public ResponseEntity<GetConsumerResponse> get(@PathVariable long consumerId) {
return consumerService.findById(consumerId)
.map(consumer -> new ResponseEntity<>(new GetConsumerResponse(consumer.getName(), consumerId), HttpStatus.OK))
.orElseGet(() -> new ResponseEntity<>(HttpStatus.NOT_FOUND));
}

@RequestMapping(method= RequestMethod.POST)
public CreateConsumerResponse create(@RequestBody CreateConsumerRequest request) {
ResultWithEvents<Consumer> result = consumerService.create(request.getName());
return new CreateConsumerResponse(result.result.getId());
}
@RequestMapping(method = RequestMethod.GET)
public ResponseEntity<GetConsumersResponse> get() {
return new ResponseEntity<>(
new GetConsumersResponse(consumerService.getAllConsumers()
.stream()
.map(this::makeGetConsumerResponse)
.collect(toList())),
HttpStatus.OK
);
}

@RequestMapping(method= RequestMethod.GET, path="/{consumerId}")
public ResponseEntity<GetConsumerResponse> get(@PathVariable long consumerId) {
return consumerService.findById(consumerId)
.map(consumer -> new ResponseEntity<>(new GetConsumerResponse(consumer.getName()), HttpStatus.OK))
.orElseGet(() -> new ResponseEntity<>(HttpStatus.NOT_FOUND));
}
private GetConsumerResponse makeGetConsumerResponse(Consumer consumer) {
return new GetConsumerResponse(consumer.getName(), consumer.getId());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,22 @@

public class GetConsumerResponse extends CreateConsumerResponse {
private PersonName name;
private long consumerId;

public PersonName getName() {
return name;
}

public GetConsumerResponse(PersonName name) {
public long getConsumerId() {
return consumerId;
}

public void setConsumerId(long consumerId) {
this.consumerId = consumerId;
}

public GetConsumerResponse(PersonName name, long consumerId) {
this.name = name;
this.consumerId = consumerId;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package net.chrisrichardson.ftgo.consumerservice.web;

import java.util.List;

public class GetConsumersResponse {
private List<GetConsumerResponse> consumers;

public List<GetConsumerResponse> getConsumers() {
return consumers;
}

public void setConsumers(List<GetConsumerResponse> consumers) {
this.consumers = consumers;
}

public GetConsumersResponse(List<GetConsumerResponse> consumers) {
this.consumers = consumers;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package net.chrisrichardson.ftgo.orderservice.web;

import java.util.List;

public class GetOrdersResponse {
private List<GetOrderResponse> orders;

public GetOrdersResponse(List<GetOrderResponse> orders) {
this.orders = orders;
}

public List<GetOrderResponse> getOrders() {
return orders;
}

public void setOrders(List<GetOrderResponse> orders) {
this.orders = orders;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Optional;

import static java.util.stream.Collectors.toList;
Expand All @@ -20,55 +21,64 @@
@RequestMapping(path = "/orders")
public class OrderController {

private OrderService orderService;
private OrderService orderService;

private OrderRepository orderRepository;
private OrderRepository orderRepository;


public OrderController(OrderService orderService, OrderRepository orderRepository) {
this.orderService = orderService;
this.orderRepository = orderRepository;
}
public OrderController(OrderService orderService, OrderRepository orderRepository) {
this.orderService = orderService;
this.orderRepository = orderRepository;
}

@RequestMapping(method = RequestMethod.POST)
public CreateOrderResponse create(@RequestBody CreateOrderRequest request) {
Order order = orderService.createOrder(request.getConsumerId(),
request.getRestaurantId(),
new DeliveryInformation(request.getDeliveryTime(), request.getDeliveryAddress()),
request.getLineItems().stream().map(x -> new MenuItemIdAndQuantity(x.getMenuItemId(), x.getQuantity())).collect(toList())
);
return new CreateOrderResponse(order.getId());
}
@RequestMapping(method = RequestMethod.POST)
public CreateOrderResponse create(@RequestBody CreateOrderRequest request) {
Order order = orderService.createOrder(request.getConsumerId(),
request.getRestaurantId(),
new DeliveryInformation(request.getDeliveryTime(), request.getDeliveryAddress()),
request.getLineItems().stream().map(x -> new MenuItemIdAndQuantity(x.getMenuItemId(), x.getQuantity())).collect(toList())
);
return new CreateOrderResponse(order.getId());
}


@RequestMapping(path = "/{orderId}", method = RequestMethod.GET)
public ResponseEntity<GetOrderResponse> getOrder(@PathVariable long orderId) {
Optional<Order> order = orderRepository.findById(orderId);
return order.map(o -> new ResponseEntity<>(makeGetOrderResponse(o), HttpStatus.OK)).orElseGet(() -> new ResponseEntity<>(HttpStatus.NOT_FOUND));
}
@RequestMapping(path = "/{orderId}", method = RequestMethod.GET)
public ResponseEntity<GetOrderResponse> getOrder(@PathVariable long orderId) {
Optional<Order> order = orderRepository.findById(orderId);
return order.map(o -> new ResponseEntity<>(makeGetOrderResponse(o), HttpStatus.OK)).orElseGet(() -> new ResponseEntity<>(HttpStatus.NOT_FOUND));
}

private GetOrderResponse makeGetOrderResponse(Order order) {
return new GetOrderResponse(order.getId(), order.getState(), order.getOrderTotal());
}
@RequestMapping(method = RequestMethod.GET)
public ResponseEntity<GetOrdersResponse> getAll() {
List<Order> orders = (List<Order>) orderRepository.findAll();
return new ResponseEntity<>(
new GetOrdersResponse(orders.stream().map(this::makeGetOrderResponse).collect(toList())),
HttpStatus.OK
);
}

@RequestMapping(path = "/{orderId}/cancel", method = RequestMethod.POST)
public ResponseEntity<GetOrderResponse> cancel(@PathVariable long orderId) {
try {
Order order = orderService.cancel(orderId);
return new ResponseEntity<>(makeGetOrderResponse(order), HttpStatus.OK);
} catch (OrderNotFoundException e) {
return new ResponseEntity<>(HttpStatus.NOT_FOUND);
private GetOrderResponse makeGetOrderResponse(Order order) {
return new GetOrderResponse(order.getId(), order.getState(), order.getOrderTotal());
}
}

@RequestMapping(path = "/{orderId}/revise", method = RequestMethod.POST)
public ResponseEntity<GetOrderResponse> revise(@PathVariable long orderId, @RequestBody ReviseOrderRequest request) {
try {
Order order = orderService.reviseOrder(orderId, new OrderRevision(Optional.empty(), request.getRevisedOrderLineItems()));
return new ResponseEntity<>(makeGetOrderResponse(order), HttpStatus.OK);
} catch (OrderNotFoundException e) {
return new ResponseEntity<>(HttpStatus.NOT_FOUND);

@RequestMapping(path = "/{orderId}/cancel", method = RequestMethod.POST)
public ResponseEntity<GetOrderResponse> cancel(@PathVariable long orderId) {
try {
Order order = orderService.cancel(orderId);
return new ResponseEntity<>(makeGetOrderResponse(order), HttpStatus.OK);
} catch (OrderNotFoundException e) {
return new ResponseEntity<>(HttpStatus.NOT_FOUND);
}
}

@RequestMapping(path = "/{orderId}/revise", method = RequestMethod.POST)
public ResponseEntity<GetOrderResponse> revise(@PathVariable long orderId, @RequestBody ReviseOrderRequest request) {
try {
Order order = orderService.reviseOrder(orderId, new OrderRevision(Optional.empty(), request.getRevisedOrderLineItems()));
return new ResponseEntity<>(makeGetOrderResponse(order), HttpStatus.OK);
} catch (OrderNotFoundException e) {
return new ResponseEntity<>(HttpStatus.NOT_FOUND);
}
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import org.springframework.transaction.annotation.Transactional;

import java.util.Collections;
import java.util.List;
import java.util.Optional;

@Transactional
Expand All @@ -27,4 +28,8 @@ public Restaurant create(CreateRestaurantRequest request) {
public Optional<Restaurant> findById(long restaurantId) {
return restaurantRepository.findById(restaurantId);
}

public List<Restaurant> getAllRestaurants() {
return (List<Restaurant>) restaurantRepository.findAll();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package net.chrisrichardson.ftgo.restaurantservice.web;

import java.util.List;

public class GetRestaurantsResponse {
private List<GetRestaurantResponse> restaurants;

public GetRestaurantsResponse(List<GetRestaurantResponse> restaurants) {
this.restaurants = restaurants;
}

public List<GetRestaurantResponse> getRestaurants() {
return restaurants;
}

public void setRestaurants(List<GetRestaurantResponse> restaurants) {
this.restaurants = restaurants;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.stream.Collectors;

@RestController
@RequestMapping(path = "/restaurants")
public class RestaurantController {
Expand All @@ -28,9 +30,13 @@ public ResponseEntity<GetRestaurantResponse> get(@PathVariable long restaurantId
.orElseGet(() -> new ResponseEntity<>(HttpStatus.NOT_FOUND));
}

@RequestMapping(method = RequestMethod.GET)
public ResponseEntity<GetRestaurantsResponse> get() {
return new ResponseEntity<>(new GetRestaurantsResponse(restaurantService.getAllRestaurants().stream()
.map(this::makeGetRestaurantResponse).collect(Collectors.toList())), HttpStatus.OK);
}

private GetRestaurantResponse makeGetRestaurantResponse(Restaurant r) {
return new GetRestaurantResponse(r.getId(), r.getName());
}


}

0 comments on commit 8f9c742

Please sign in to comment.