OrderController.java
package cz.vsb.crm.controller;
import cz.vsb.crm.dto.OrderLineResponse;
import cz.vsb.crm.dto.OrderRequest;
import cz.vsb.crm.dto.OrderResponse;
import cz.vsb.crm.service.OrderService;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
@RestController
@RequestMapping("/api/orders")
@RequiredArgsConstructor
public class OrderController {
private final OrderService orderService;
@GetMapping
public List<OrderResponse> list() {
return orderService.findAll();
}
@GetMapping("/{id}")
public OrderResponse get(@PathVariable Long id) {
return orderService.findById(id);
}
@PostMapping
@ResponseStatus(HttpStatus.CREATED)
public OrderResponse create(@Valid @RequestBody OrderRequest request) {
return orderService.create(request);
}
@PutMapping("/{id}")
public OrderResponse update(@PathVariable Long id, @Valid @RequestBody OrderRequest request) {
return orderService.update(id, request);
}
@DeleteMapping("/{id}")
@ResponseStatus(HttpStatus.NO_CONTENT)
public void delete(@PathVariable Long id) {
orderService.delete(id);
}
@GetMapping("/{id}/products")
public List<OrderLineResponse> products(@PathVariable Long id) {
return orderService.getProducts(id);
}
}