fix bug in seed script and finished the controllers

This commit is contained in:
2026-06-12 17:31:39 +03:00
parent 00e42adcc9
commit 83577bff2d
5 changed files with 149 additions and 4 deletions
@@ -1,10 +1,10 @@
package com.andwati.orders.config;
import com.andwati.orders.model.InventoryItem;
import com.andwati.orders.model.OrderItem;
import com.andwati.orders.model.OrderStatus;
import com.andwati.orders.model.Product;
import com.andwati.orders.model.*;
import com.andwati.orders.repository.InventoryItemRepository;
import com.andwati.orders.repository.OrderRepository;
import com.andwati.orders.repository.ProductRepository;
import jakarta.transaction.Transactional;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.CommandLineRunner;
@@ -0,0 +1,37 @@
package com.andwati.orders.controller;
import com.andwati.orders.dto.response.FareCalculationResponse;
import com.andwati.orders.services.FareCalculationService;
import jakarta.validation.constraints.DecimalMin;
import jakarta.validation.constraints.NotNull;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import java.math.BigDecimal;
@Validated
@RestController
public class FareController {
private final FareCalculationService fareCalculationService;
public FareController(FareCalculationService fareCalculationService) {
this.fareCalculationService = fareCalculationService;
}
@GetMapping("/api/fare/calculate")
public FareCalculationResponse calculateFare(
@RequestParam
@NotNull(message = "distanceKm is required")
@DecimalMin(value = "0.0", inclusive = false, message = "distanceKm must be greater than 0")
BigDecimal distanceKm,
@RequestParam(required = false)
@DecimalMin(value = "1.0", message = "surgeMultiplier must be greater than or equal to 1.0")
BigDecimal surgeMultiplier
) {
return fareCalculationService.calculateFare(distanceKm, surgeMultiplier);
}
}
@@ -0,0 +1,15 @@
package com.andwati.orders.controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.Map;
@RestController
public class HealthController {
@GetMapping("/api/health")
public Map<String, String> health() {
return Map.of("status", "UP");
}
}
@@ -0,0 +1,32 @@
package com.andwati.orders.controller;
import com.andwati.orders.dto.response.InventoryResponse;
import com.andwati.orders.dto.response.LowStockInventoryResponse;
import com.andwati.orders.services.InventoryService;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/api/inventory")
public class InventoryController {
private final InventoryService inventoryService;
public InventoryController(InventoryService inventoryService) {
this.inventoryService = inventoryService;
}
@GetMapping
public InventoryResponse getInventory() {
return inventoryService.getInventory();
}
@GetMapping("/low-stock")
public LowStockInventoryResponse getLowStockItems(
@RequestParam(required = false) Integer threshold
) {
return inventoryService.getLowStockItems(threshold);
}
}
@@ -0,0 +1,61 @@
package com.andwati.orders.controller;
import com.andwati.orders.dto.request.CreateOrderRequest;
import com.andwati.orders.dto.response.CancelOrderResponse;
import com.andwati.orders.dto.response.OrderResponse;
import com.andwati.orders.dto.response.OrderSummaryResponse;
import com.andwati.orders.dto.response.PageResponse;
import com.andwati.orders.model.OrderStatus;
import com.andwati.orders.services.OrderService;
import jakarta.validation.Valid;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;
import java.time.LocalDate;
import java.util.UUID;
@RestController
@RequestMapping("/api/orders")
public class OrderController {
private final OrderService orderService;
public OrderController(OrderService orderService) {
this.orderService = orderService;
}
@PostMapping
@ResponseStatus(HttpStatus.CREATED)
public OrderResponse placeOrder(@Valid @RequestBody CreateOrderRequest request) {
return orderService.placeOrder(request);
}
@GetMapping
public PageResponse<OrderSummaryResponse> listOrders(
@RequestParam(required = false) OrderStatus status,
@RequestParam(required = false)
@DateTimeFormat(iso = DateTimeFormat.ISO.DATE)
LocalDate from,
@RequestParam(required = false)
@DateTimeFormat(iso = DateTimeFormat.ISO.DATE)
LocalDate to,
@RequestParam(defaultValue = "0") int page,
@RequestParam(defaultValue = "20") int size
) {
return orderService.listOrders(status, from, to, page, size);
}
@GetMapping("/{id}")
public OrderResponse getOrder(@PathVariable UUID id) {
return orderService.getOrder(id);
}
@DeleteMapping("/{id}")
public CancelOrderResponse cancelOrder(@PathVariable UUID id) {
return orderService.cancelOrder(id);
}
}