Skip to content

Commit

Permalink
Merge pull request #72 from pawl1n/feature-add-orders
Browse files Browse the repository at this point in the history
Feature add orders
  • Loading branch information
pawl1n authored Jun 14, 2023
2 parents 1f05e71 + 3071f63 commit c7ca528
Show file tree
Hide file tree
Showing 33 changed files with 1,119 additions and 12 deletions.
53 changes: 53 additions & 0 deletions src/main/java/ua/kishkastrybaie/order/Order.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package ua.kishkastrybaie.order;

import jakarta.persistence.*;
import java.util.Set;
import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.Setter;
import ua.kishkastrybaie.order.item.OrderItem;
import ua.kishkastrybaie.order.payment.type.PaymentType;
import ua.kishkastrybaie.order.shipping.method.ShippingMethod;
import ua.kishkastrybaie.order.status.OrderStatus;
import ua.kishkastrybaie.order.status.OrderStatusHistory;

@Entity
@Table(name = "`order`", schema = "main")
@Getter
@Setter
@EqualsAndHashCode
public class Order {
@Id
@GeneratedValue(generator = "order_seq")
@SequenceGenerator(
name = "order_seq",
sequenceName = "order_seq",
schema = "main",
allocationSize = 1)
private Long id;

private String userEmail;

private Double totalPrice;

private String address;

@Enumerated(EnumType.STRING)
private OrderStatus status;

@OneToMany(mappedBy = "order", cascade = CascadeType.ALL)
private Set<OrderStatusHistory> statusHistory;

@Enumerated(EnumType.STRING)
private PaymentType paymentType;

private String phoneNumber;

private String customerFullName;

@Enumerated(EnumType.STRING)
private ShippingMethod shippingMethod;

@OneToMany(mappedBy = "order", cascade = CascadeType.ALL)
private Set<OrderItem> items;
}
51 changes: 51 additions & 0 deletions src/main/java/ua/kishkastrybaie/order/OrderController.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package ua.kishkastrybaie.order;

import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.domain.Pageable;
import org.springframework.data.web.PageableDefault;
import org.springframework.hateoas.CollectionModel;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import ua.kishkastrybaie.order.status.OrderStatus;

@RestController
@RequestMapping("/api/orders")
@RequiredArgsConstructor
@Slf4j
public class OrderController {
private final OrderService orderService;

@GetMapping
public ResponseEntity<CollectionModel<OrderDto>> all(@PageableDefault Pageable pageable) {
log.info("Get all orders");
return ResponseEntity.ok(orderService.findAll(pageable));
}

@GetMapping("/{id}")
public ResponseEntity<OrderDto> one(@PathVariable Long id) {
log.info("Get order by id {}", id);
return ResponseEntity.ok(orderService.findById(id));
}

@PostMapping
public ResponseEntity<OrderDto> save(@RequestBody OrderRequestDto orderRequest) {
log.info("Save order {}", orderRequest);
return ResponseEntity.ok(orderService.save(orderRequest));
}

@PutMapping("/{id}")
public ResponseEntity<OrderDto> replace(
@PathVariable Long id, @Valid @RequestBody OrderRequestDto orderRequest) {
log.info("Replace order {}", orderRequest);
return ResponseEntity.ok(orderService.replace(id, orderRequest));
}

@PutMapping("/{id}/status")
public ResponseEntity<OrderDto> changeStatus(
@PathVariable Long id, @Valid @RequestBody OrderStatus status) {
log.info("Change order status {}", status);
return ResponseEntity.ok(orderService.changeStatus(id, status));
}
}
22 changes: 22 additions & 0 deletions src/main/java/ua/kishkastrybaie/order/OrderDto.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package ua.kishkastrybaie.order;

import com.fasterxml.jackson.annotation.JsonInclude;
import lombok.*;
import org.springframework.hateoas.RepresentationModel;
import org.springframework.hateoas.server.core.Relation;
import ua.kishkastrybaie.order.payment.type.PaymentType;
import ua.kishkastrybaie.order.status.OrderStatus;

@Data
@AllArgsConstructor
@EqualsAndHashCode(callSuper = false)
@Relation(itemRelation = "order", collectionRelation = "orders")
@JsonInclude(JsonInclude.Include.NON_NULL)
public class OrderDto extends RepresentationModel<OrderDto> {
private Long id;
private String userEmail;
private Double totalPrice;
private String address;
private OrderStatus currentStatus;
private PaymentType paymentType;
}
15 changes: 15 additions & 0 deletions src/main/java/ua/kishkastrybaie/order/OrderExceptionHandler.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package ua.kishkastrybaie.order;

import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import ua.kishkastrybaie.shared.ErrorDto;

@RestControllerAdvice
public class OrderExceptionHandler {
@ExceptionHandler(OrderNotFoundException.class)
public ResponseEntity<ErrorDto> handleOrderNotFoundException(OrderNotFoundException ex) {
return ResponseEntity.status(HttpStatus.NOT_FOUND).body(new ErrorDto(ex.getMessage()));
}
}
12 changes: 12 additions & 0 deletions src/main/java/ua/kishkastrybaie/order/OrderMapper.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package ua.kishkastrybaie.order;

import org.mapstruct.*;
import ua.kishkastrybaie.order.item.OrderItemMapper;

@Mapper(
componentModel = "spring",
uses = {OrderItemMapper.class})
public interface OrderMapper {
@Mapping(target = "currentStatus", source = "status")
OrderDto toDto(Order order);
}
32 changes: 32 additions & 0 deletions src/main/java/ua/kishkastrybaie/order/OrderModelAssembler.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package ua.kishkastrybaie.order;

import static org.springframework.hateoas.server.mvc.WebMvcLinkBuilder.linkTo;
import static org.springframework.hateoas.server.mvc.WebMvcLinkBuilder.methodOn;

import lombok.RequiredArgsConstructor;
import org.springframework.hateoas.CollectionModel;
import org.springframework.hateoas.server.RepresentationModelAssembler;
import org.springframework.lang.NonNull;
import org.springframework.stereotype.Component;

@Component
@RequiredArgsConstructor
public class OrderModelAssembler implements RepresentationModelAssembler<Order, OrderDto> {
private final OrderMapper orderMapper;

@Override
@NonNull
public OrderDto toModel(@NonNull Order entity) {
return orderMapper
.toDto(entity)
.add(linkTo(methodOn(OrderController.class).one(entity.getId())).withSelfRel());
}

@Override
@NonNull
public CollectionModel<OrderDto> toCollectionModel(@NonNull Iterable<? extends Order> entities) {
return RepresentationModelAssembler.super
.toCollectionModel(entities)
.add(linkTo(methodOn(OrderController.class).all(null)).withSelfRel());
}
}
11 changes: 11 additions & 0 deletions src/main/java/ua/kishkastrybaie/order/OrderNotFoundException.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package ua.kishkastrybaie.order;

import lombok.extern.slf4j.Slf4j;

@Slf4j
public class OrderNotFoundException extends RuntimeException {
public OrderNotFoundException(Long id) {
super("Order not found with id: " + id);
log.error(getMessage());
}
}
9 changes: 9 additions & 0 deletions src/main/java/ua/kishkastrybaie/order/OrderRepository.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package ua.kishkastrybaie.order;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;

public interface OrderRepository extends JpaRepository<Order, Long> {
Page<Order> findAllByUserEmail(String email, Pageable pageable);
}
20 changes: 20 additions & 0 deletions src/main/java/ua/kishkastrybaie/order/OrderRequestDto.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package ua.kishkastrybaie.order;

import jakarta.validation.constraints.Email;
import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.NotNull;
import jakarta.validation.constraints.Size;
import java.util.Set;
import ua.kishkastrybaie.order.item.OrderItemRequestDto;
import ua.kishkastrybaie.order.payment.type.PaymentType;
import ua.kishkastrybaie.order.shipping.method.ShippingMethod;
import ua.kishkastrybaie.validation.phonenumber.PhoneNumber;

public record OrderRequestDto(
@PhoneNumber String phoneNumber,
@NotNull @Email String email,
@NotNull @Size(min = 3) String customerFullName,
@NotNull PaymentType paymentType,
@NotBlank String address,
@NotNull ShippingMethod shippingMethod,
@NotNull Set<OrderItemRequestDto> items) {}
17 changes: 17 additions & 0 deletions src/main/java/ua/kishkastrybaie/order/OrderService.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package ua.kishkastrybaie.order;

import org.springframework.data.domain.Pageable;
import org.springframework.hateoas.CollectionModel;
import ua.kishkastrybaie.order.status.OrderStatus;

public interface OrderService {
CollectionModel<OrderDto> findAll(Pageable pageable);

OrderDto findById(Long id);

OrderDto save(OrderRequestDto orderRequest);

OrderDto replace(Long id, OrderRequestDto orderRequest);

OrderDto changeStatus(Long id, OrderStatus status);
}
Loading

0 comments on commit c7ca528

Please sign in to comment.