Add OrderController
This commit is contained in:
@@ -0,0 +1,97 @@
|
|||||||
|
package de.htwsaar.webshop.controller;
|
||||||
|
|
||||||
|
import de.htwsaar.webshop.repository.entities.Order;
|
||||||
|
import de.htwsaar.webshop.service.OrderService;
|
||||||
|
import de.htwsaar.webshop.service.ValidatorService;
|
||||||
|
import jakarta.servlet.http.HttpServletRequest;
|
||||||
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.http.HttpStatus;
|
||||||
|
import org.springframework.http.ResponseEntity;
|
||||||
|
import org.springframework.web.bind.annotation.*;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
import static de.htwsaar.webshop.config.ControllerPathConfig.ORDER_BASE;
|
||||||
|
import static de.htwsaar.webshop.config.ControllerPathConfig.ORDER_GET_ALL;
|
||||||
|
import static de.htwsaar.webshop.config.ParameterConfig.*;
|
||||||
|
import static de.htwsaar.webshop.util.LoggerUtil.logRequest;
|
||||||
|
|
||||||
|
@RestController
|
||||||
|
@Slf4j
|
||||||
|
public class OrderController {
|
||||||
|
|
||||||
|
private final OrderService orderService;
|
||||||
|
private final ValidatorService validatorService;
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
public OrderController(OrderService orderService, ValidatorService validatorService) {
|
||||||
|
this.orderService = orderService;
|
||||||
|
this.validatorService = validatorService;
|
||||||
|
}
|
||||||
|
|
||||||
|
@RequestMapping(path = ORDER_GET_ALL, method = RequestMethod.GET, produces = "application/json")
|
||||||
|
public ResponseEntity<List<Order>> getAll(HttpServletRequest request,
|
||||||
|
@RequestParam(value = PARAM_CUSTOMER_ID) Long customerId) {
|
||||||
|
logRequest(request);
|
||||||
|
List<Order> orders = orderService.getAllOrders(customerId);
|
||||||
|
if(orders.isEmpty()) {
|
||||||
|
return new ResponseEntity<>(HttpStatus.NO_CONTENT);
|
||||||
|
}
|
||||||
|
return ResponseEntity.ok(orders);
|
||||||
|
}
|
||||||
|
|
||||||
|
@RequestMapping(path = ORDER_BASE, method = RequestMethod.GET, produces = "application/json")
|
||||||
|
public ResponseEntity<Order> get(HttpServletRequest request,
|
||||||
|
@RequestParam(value = PARAM_ID) Long orderId) {
|
||||||
|
logRequest(request);
|
||||||
|
Order image = orderService.getOrderById(orderId);
|
||||||
|
if(image == null) {
|
||||||
|
return new ResponseEntity<>(HttpStatus.NO_CONTENT);
|
||||||
|
}
|
||||||
|
return ResponseEntity.ok(image);
|
||||||
|
}
|
||||||
|
|
||||||
|
@RequestMapping(path = ORDER_BASE, method = RequestMethod.POST, produces = "application/json")
|
||||||
|
public ResponseEntity<Boolean> add(HttpServletRequest request,
|
||||||
|
@RequestBody Order order) {
|
||||||
|
logRequest(request);
|
||||||
|
|
||||||
|
if (validatorService.isInvalid(order)) {
|
||||||
|
log.warn("[{}] failed Validation, sending bad request", request.getRequestURI());
|
||||||
|
return ResponseEntity.badRequest().body(false);
|
||||||
|
}
|
||||||
|
|
||||||
|
Order saved = orderService.save(order);
|
||||||
|
return ResponseEntity.ok(saved != null);
|
||||||
|
}
|
||||||
|
|
||||||
|
//TODO: check whether an order should be updatable
|
||||||
|
@RequestMapping(path = ORDER_BASE, method = RequestMethod.PUT, produces = "application/json")
|
||||||
|
public ResponseEntity<Boolean> update(HttpServletRequest request,
|
||||||
|
@RequestParam(value = PARAM_ID) Long orderId,
|
||||||
|
@RequestBody Order order) {
|
||||||
|
logRequest(request);
|
||||||
|
if (orderId == null || orderService.getOrderById(orderId) == null) {
|
||||||
|
return ResponseEntity.badRequest().body(false);
|
||||||
|
}
|
||||||
|
order.setId(orderService.getOrderById(orderId).getId());
|
||||||
|
return ResponseEntity.ok(orderService.save(order) != null);
|
||||||
|
}
|
||||||
|
|
||||||
|
@RequestMapping(path = ORDER_BASE, method = RequestMethod.DELETE, produces = "application/json")
|
||||||
|
public ResponseEntity<Boolean> delete(HttpServletRequest request,
|
||||||
|
@RequestParam(value = PARAM_ID) Long orderId) {
|
||||||
|
logRequest(request);
|
||||||
|
if (orderId == null) {
|
||||||
|
log.warn("[{}] got invalid orderId", request.getRequestURI());
|
||||||
|
return ResponseEntity.badRequest().body(false);
|
||||||
|
}
|
||||||
|
if (orderService.getOrderById(orderId) != null) {
|
||||||
|
log.warn("[{}] got invalid orderId", request.getRequestURI());
|
||||||
|
return ResponseEntity.badRequest().body(false);
|
||||||
|
}
|
||||||
|
orderService.delete(orderId);
|
||||||
|
return ResponseEntity.ok().build();
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -4,6 +4,9 @@ import de.htwsaar.webshop.repository.entities.Order;
|
|||||||
import org.springframework.data.jpa.repository.JpaRepository;
|
import org.springframework.data.jpa.repository.JpaRepository;
|
||||||
import org.springframework.stereotype.Repository;
|
import org.springframework.stereotype.Repository;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
@Repository
|
@Repository
|
||||||
public interface OrderRepository extends JpaRepository<Order, Long> {
|
public interface OrderRepository extends JpaRepository<Order, Long> {
|
||||||
|
List<Order> getOrdersByCustomer_Id(Long customerId);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,9 +1,17 @@
|
|||||||
package de.htwsaar.webshop.repository.entities;
|
package de.htwsaar.webshop.repository.entities;
|
||||||
|
|
||||||
import jakarta.persistence.*;
|
import jakarta.persistence.*;
|
||||||
|
import lombok.AllArgsConstructor;
|
||||||
|
import lombok.Getter;
|
||||||
|
import lombok.NoArgsConstructor;
|
||||||
|
import lombok.Setter;
|
||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
|
@Getter
|
||||||
|
@Setter
|
||||||
|
@AllArgsConstructor
|
||||||
|
@NoArgsConstructor
|
||||||
@Entity
|
@Entity
|
||||||
@Table(name = "Orders")
|
@Table(name = "Orders")
|
||||||
public class Order {
|
public class Order {
|
||||||
@@ -11,13 +19,13 @@ public class Order {
|
|||||||
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
||||||
private Long id;
|
private Long id;
|
||||||
|
|
||||||
@ManyToOne(fetch = FetchType.LAZY)
|
@ManyToOne(optional = false, fetch = FetchType.LAZY, cascade = CascadeType.ALL, targetEntity = Customer.class)
|
||||||
@JoinColumn(name = "customerId")
|
@JoinColumn(name = "customerId")
|
||||||
private Customer customer;
|
private Customer customer;
|
||||||
|
|
||||||
@Column(name = "time")
|
@Column(name = "time", nullable = false)
|
||||||
private Long time;
|
private Long time;
|
||||||
|
|
||||||
@OneToMany(mappedBy = "order", fetch = FetchType.LAZY)
|
@OneToMany(mappedBy = "order", orphanRemoval = true, fetch = FetchType.LAZY, cascade = CascadeType.ALL, targetEntity = OrderItem.class)
|
||||||
private List<OrderItem> orderItems;
|
private List<OrderItem> orderItems;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,12 @@
|
|||||||
|
package de.htwsaar.webshop.service;
|
||||||
|
|
||||||
|
import de.htwsaar.webshop.repository.entities.Order;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
public interface OrderService {
|
||||||
|
Order save(Order order);
|
||||||
|
void delete(Long orderId);
|
||||||
|
Order getOrderById(Long orderId);
|
||||||
|
List<Order> getAllOrders(Long customerId);
|
||||||
|
}
|
||||||
@@ -0,0 +1,41 @@
|
|||||||
|
package de.htwsaar.webshop.service.impl;
|
||||||
|
|
||||||
|
import de.htwsaar.webshop.repository.OrderRepository;
|
||||||
|
import de.htwsaar.webshop.repository.entities.Order;
|
||||||
|
import de.htwsaar.webshop.service.OrderService;
|
||||||
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
@Service
|
||||||
|
@Slf4j
|
||||||
|
public class OrderServiceImpl implements OrderService {
|
||||||
|
private final OrderRepository orderRepository;
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
public OrderServiceImpl(OrderRepository orderRepository) {
|
||||||
|
this.orderRepository = orderRepository;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Order save(Order order) {
|
||||||
|
return orderRepository.save(order);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void delete(Long orderId) {
|
||||||
|
orderRepository.deleteById(orderId);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Order getOrderById(Long orderId) {
|
||||||
|
return orderRepository.findById(orderId).orElse(null);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public List<Order> getAllOrders(Long customerId) {
|
||||||
|
return orderRepository.getOrdersByCustomer_Id(customerId);
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user