Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions scripts/orders.sh
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
#!/usr/bin/env bash

# create new order
# create new order linked to the restaurant
curl -X POST http://localhost:8080/api/orders \
-H "Content-Type: application/json" \
-d '{"customerName": "John Doe", "dishName": "Margherita Pizza", "status": "Pending"}'
-d '{"customerName": "John Doe", "dishName": "Margherita Pizza", "status": "Pending", "restaurantId": 1}'


# list all orders
curl http://localhost:8080/api/orders
Expand Down
3 changes: 2 additions & 1 deletion services/order-service/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ dependencies {
implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
implementation 'org.springframework.boot:spring-boot-starter-actuator'
implementation 'org.postgresql:postgresql'

implementation 'org.springframework.boot:spring-boot-starter-webflux'

compileOnly 'org.projectlombok:lombok:1.18.28'
annotationProcessor 'org.projectlombok:lombok:1.18.28'

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,29 @@
import com.ofds.order.model.Order;
import com.ofds.order.repository.OrderRepository;
import org.springframework.web.bind.annotation.*;

import org.springframework.web.client.RestTemplate;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api/orders")
public class OrderController {

private final OrderRepository orderRepository;
private final RestTemplate restTemplate;

public OrderController(OrderRepository orderRepository) {
this.orderRepository = orderRepository;
this.restTemplate = new RestTemplate();
}

@PostMapping
public Order createOrder(@RequestBody Order order) {
order.setStatus("Pending");
public Order createOrder(@RequestBody Map<String, Object> request) {
Order order = new Order();
order.setCustomerName((String) request.get("customerName"));
order.setDishName((String) request.get("dishName"));
order.setStatus((String) request.get("status"));
order.setRestaurantId(Long.parseLong(request.get("restaurantId").toString())); // Link to restaurant
return orderRepository.save(order);
}

Expand All @@ -28,14 +35,25 @@ public List<Order> listOrders() {
}

@GetMapping("/{id}")
public Order getOrderById(@PathVariable Long id) {
return orderRepository.findById(id).orElse(null);
public Map<String, Object> getOrderWithRestaurant(@PathVariable Long id) {
return orderRepository.findById(id).map(order -> {
String restaurantUrl = "http://gateway:8080/api/restaurants/" + order.getRestaurantId();
Map<String, Object> restaurant = restTemplate.getForObject(restaurantUrl, Map.class);

return Map.of(
"id", order.getId(),
"customerName", order.getCustomerName(),
"dishName", order.getDishName(),
"status", order.getStatus(),
"restaurant", restaurant
);
}).orElse(null);
}

@PutMapping("/{id}/status")
public Order updateOrderStatus(@PathVariable Long id, @RequestBody String status) {
return orderRepository.findById(id).map(order -> {
order.setStatus(status.replaceAll("\"", "")); // Remove extra quotes from JSON
order.setStatus(status.replaceAll("\"", ""));
return orderRepository.save(order);
}).orElse(null);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ public class Order {
@Id
@GeneratedValue
private Long id;
private Long restaurantId;
private String customerName;
private String dishName;
private String status;
Expand Down