Add Restaurant-Service domain layer.
This commit is contained in:
@@ -0,0 +1,5 @@
|
||||
package com.food.order.sysyem.valueobject;
|
||||
|
||||
public enum RestaurantOrderStatus {
|
||||
PAID
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
package com.food.order.sysyem.ports.output.message.publisher.restaurantapproval;
|
||||
|
||||
import com.food.order.sysyem.event.publisher.DomainEventPublisher;
|
||||
import com.food.order.system.domain.event.OrderPaidEvent;
|
||||
|
||||
public interface OrderPaidRestaurantRequestMessagePublisher extends DomainEventPublisher<OrderPaidEvent> {
|
||||
|
||||
}
|
||||
7
pom.xml
7
pom.xml
@@ -16,6 +16,7 @@
|
||||
<module>infrastructure/kafka</module>
|
||||
<module>customer-service</module>
|
||||
<module>payment-service</module>
|
||||
<module>restaurant-service</module>
|
||||
</modules>
|
||||
|
||||
|
||||
@@ -97,6 +98,12 @@
|
||||
<version>${project.version}</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>com.food.order</groupId>
|
||||
<artifactId>restaurant-domain-core</artifactId>
|
||||
<version>${project.version}</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>com.food.order</groupId>
|
||||
<artifactId>kafka-producer</artifactId>
|
||||
|
||||
24
restaurant-service/pom.xml
Normal file
24
restaurant-service/pom.xml
Normal file
@@ -0,0 +1,24 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<parent>
|
||||
<artifactId>food-ordering-system</artifactId>
|
||||
<groupId>com.food.order</groupId>
|
||||
<version>1.0-SNAPSHOT</version>
|
||||
</parent>
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<artifactId>restaurant-service</artifactId>
|
||||
<packaging>pom</packaging>
|
||||
<modules>
|
||||
<module>restaurant-domain</module>
|
||||
<module>restaurant-messaging</module>
|
||||
<module>restaurant-container</module>
|
||||
<module>restaurant-dataaccess</module>
|
||||
<module>restaurant-domain/restaurant-domain-core</module>
|
||||
<module>restaurant-domain/restaurant-application-service</module>
|
||||
</modules>
|
||||
|
||||
|
||||
</project>
|
||||
16
restaurant-service/restaurant-container/pom.xml
Normal file
16
restaurant-service/restaurant-container/pom.xml
Normal file
@@ -0,0 +1,16 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<parent>
|
||||
<artifactId>restaurant-service</artifactId>
|
||||
<groupId>com.food.order</groupId>
|
||||
<version>1.0-SNAPSHOT</version>
|
||||
</parent>
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<artifactId>restaurant-container</artifactId>
|
||||
|
||||
|
||||
|
||||
</project>
|
||||
15
restaurant-service/restaurant-dataaccess/pom.xml
Normal file
15
restaurant-service/restaurant-dataaccess/pom.xml
Normal file
@@ -0,0 +1,15 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<parent>
|
||||
<artifactId>restaurant-service</artifactId>
|
||||
<groupId>com.food.order</groupId>
|
||||
<version>1.0-SNAPSHOT</version>
|
||||
</parent>
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<artifactId>restaurant-dataaccess</artifactId>
|
||||
|
||||
|
||||
</project>
|
||||
22
restaurant-service/restaurant-domain/pom.xml
Normal file
22
restaurant-service/restaurant-domain/pom.xml
Normal file
@@ -0,0 +1,22 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<parent>
|
||||
<artifactId>restaurant-service</artifactId>
|
||||
<groupId>com.food.order</groupId>
|
||||
<version>1.0-SNAPSHOT</version>
|
||||
</parent>
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<modules>
|
||||
<module>restaurant-domain-core</module>
|
||||
<module>restaurant-application-service</module>
|
||||
</modules>
|
||||
<packaging>pom</packaging>
|
||||
|
||||
<artifactId>restaurant-domain</artifactId>
|
||||
|
||||
|
||||
|
||||
</project>
|
||||
@@ -0,0 +1,39 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<parent>
|
||||
<artifactId>restaurant-domain</artifactId>
|
||||
<groupId>com.food.order</groupId>
|
||||
<version>1.0-SNAPSHOT</version>
|
||||
<relativePath>../pom.xml</relativePath>
|
||||
</parent>
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<artifactId>restaurant-application-service</artifactId>
|
||||
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>com.food.order</groupId>
|
||||
<artifactId>restaurant-domain-core</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-validation</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>com.food.order</groupId>
|
||||
<artifactId>common-domain</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.springframework</groupId>
|
||||
<artifactId>spring-tx</artifactId>
|
||||
</dependency>
|
||||
|
||||
</dependencies>
|
||||
|
||||
</project>
|
||||
@@ -0,0 +1,63 @@
|
||||
package com.food.ordery.system.restaurant.domain.service;
|
||||
|
||||
import com.food.order.system.restaurant.domain.core.RestaurantDomainService;
|
||||
import com.food.order.system.restaurant.domain.core.entity.Restaurant;
|
||||
import com.food.order.system.restaurant.domain.core.event.OrderApprovalEvent;
|
||||
import com.food.order.system.restaurant.domain.core.exception.RestaurantNotFoundException;
|
||||
import com.food.order.sysyem.valueobject.OrderId;
|
||||
import com.food.ordery.system.restaurant.domain.service.dto.RestaurantApprovalRequest;
|
||||
import com.food.ordery.system.restaurant.domain.service.mapper.RestaurantDataMapper;
|
||||
import com.food.ordery.system.restaurant.domain.service.ports.output.message.publisher.OrderApprovedMessagePublisher;
|
||||
import com.food.ordery.system.restaurant.domain.service.ports.output.message.publisher.OrderRejectedMessagePublisher;
|
||||
import com.food.ordery.system.restaurant.domain.service.ports.output.repository.OrderApprovalRepository;
|
||||
import com.food.ordery.system.restaurant.domain.service.ports.output.repository.RestaurantRepository;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.stereotype.Component;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.UUID;
|
||||
|
||||
@Slf4j
|
||||
@Component
|
||||
@RequiredArgsConstructor
|
||||
public class RestaurantApprovalRequestHelper {
|
||||
|
||||
private final RestaurantDomainService restaurantDomainService;
|
||||
private final RestaurantDataMapper restaurantDataMapper;
|
||||
private final RestaurantRepository restaurantRepository;
|
||||
private final OrderApprovalRepository orderApprovalRepository;
|
||||
private final OrderApprovedMessagePublisher orderApprovedMessagePublisher;
|
||||
private final OrderRejectedMessagePublisher orderRejectedMessagePublisher;
|
||||
|
||||
@Transactional
|
||||
public OrderApprovalEvent persistOrderApproval(RestaurantApprovalRequest request) {
|
||||
log.info("Persisting order approval request: {}", request);
|
||||
List<String> failureMessages = new ArrayList<>();
|
||||
var restaurant = findRestaurant(request);
|
||||
var event = restaurantDomainService.validateOrder
|
||||
(restaurant, failureMessages, orderApprovedMessagePublisher, orderRejectedMessagePublisher);
|
||||
|
||||
orderApprovalRepository.save(restaurant.getOrderApproval());
|
||||
return event;
|
||||
|
||||
}
|
||||
|
||||
private Restaurant findRestaurant(RestaurantApprovalRequest request) {
|
||||
var restaurant = restaurantDataMapper.restaurantApprovalRequestToRestaurant(request);
|
||||
var resultRestaurant = restaurantRepository.findRestaurantInformation(restaurant).orElseThrow(
|
||||
() -> new RestaurantNotFoundException("Restaurant not found")
|
||||
);
|
||||
restaurant.setActive(resultRestaurant.isActive());
|
||||
restaurant.getOrderDetail().getProducts().forEach(product -> {
|
||||
resultRestaurant.getOrderDetail().getProducts().forEach(p -> {
|
||||
if (p.getId().equals(product.getId())) {
|
||||
p.updateWithConfirmedNamePriceAndAvailablity(p.getName(),p.getPrice(), p.isAvailable());
|
||||
}});
|
||||
});
|
||||
restaurant.getOrderDetail().setId(new OrderId(UUID.fromString(request.getOrderId())));
|
||||
return restaurant;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
package com.food.ordery.system.restaurant.domain.service;
|
||||
|
||||
import com.food.ordery.system.restaurant.domain.service.dto.RestaurantApprovalRequest;
|
||||
import com.food.ordery.system.restaurant.domain.service.ports.input.message.listener.RestaurantApprovalRequestMessageListener;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
@Slf4j
|
||||
@Service
|
||||
@RequiredArgsConstructor
|
||||
public class RestaurantApprovalRequestMessageListenerImpl implements RestaurantApprovalRequestMessageListener {
|
||||
|
||||
private final RestaurantApprovalRequestHelper restaurantApprovalRequestHelper;
|
||||
|
||||
@Override
|
||||
public void approveOrder(RestaurantApprovalRequest request) {
|
||||
var event = restaurantApprovalRequestHelper.persistOrderApproval(request);
|
||||
event.fire();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
package com.food.ordery.system.restaurant.domain.service.config;
|
||||
|
||||
import lombok.Data;
|
||||
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
@Data
|
||||
@Configuration
|
||||
@ConfigurationProperties(prefix = "restaurant-service")
|
||||
public class RestaurantServiceConfig {
|
||||
|
||||
private String restaurantApprovalRequestTopicName;
|
||||
private String restaurantApprovalResponseTopicName;
|
||||
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
package com.food.ordery.system.restaurant.domain.service.dto;
|
||||
|
||||
import com.food.order.system.restaurant.domain.core.entity.Product;
|
||||
import com.food.order.sysyem.valueobject.RestaurantOrderStatus;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Builder;
|
||||
import lombok.Getter;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
import java.time.Instant;
|
||||
import java.util.List;
|
||||
|
||||
@Getter
|
||||
@Builder
|
||||
@AllArgsConstructor
|
||||
public class RestaurantApprovalRequest {
|
||||
private String id;
|
||||
private String sagaId;
|
||||
private String restaurantId;
|
||||
private String orderId;
|
||||
private RestaurantOrderStatus status;
|
||||
private List<Product> products;
|
||||
private BigDecimal price;
|
||||
private Instant createdAt;
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
package com.food.ordery.system.restaurant.domain.service.exception;
|
||||
|
||||
import com.food.order.sysyem.exception.DomainException;
|
||||
|
||||
public class RestaurantApplicationServiceException extends DomainException {
|
||||
|
||||
public RestaurantApplicationServiceException(String message) {
|
||||
super(message);
|
||||
}
|
||||
|
||||
public RestaurantApplicationServiceException(String message, Throwable cause) {
|
||||
super(message, cause);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,35 @@
|
||||
package com.food.ordery.system.restaurant.domain.service.mapper;
|
||||
|
||||
import com.food.order.system.restaurant.domain.core.entity.OrderDetail;
|
||||
import com.food.order.system.restaurant.domain.core.entity.Product;
|
||||
import com.food.order.system.restaurant.domain.core.entity.Restaurant;
|
||||
import com.food.order.sysyem.valueobject.Money;
|
||||
import com.food.order.sysyem.valueobject.OrderId;
|
||||
import com.food.order.sysyem.valueobject.OrderStatus;
|
||||
import com.food.order.sysyem.valueobject.RestaurantId;
|
||||
import com.food.ordery.system.restaurant.domain.service.dto.RestaurantApprovalRequest;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.util.UUID;
|
||||
|
||||
@Component
|
||||
public class RestaurantDataMapper {
|
||||
|
||||
public Restaurant restaurantApprovalRequestToRestaurant(RestaurantApprovalRequest request) {
|
||||
return Restaurant.builder()
|
||||
.restaurantId(new RestaurantId(UUID.fromString(request.getRestaurantId())))
|
||||
.orderDetail(OrderDetail.builder()
|
||||
.orderId(new OrderId(UUID.fromString(request.getOrderId())))
|
||||
.products(request.getProducts().stream().map(
|
||||
product -> Product.builder()
|
||||
.productId(product.getId())
|
||||
.quantity(product.getQuantity())
|
||||
|
||||
.build()
|
||||
).toList())
|
||||
.totalAmount(new Money(request.getPrice()))
|
||||
.status(OrderStatus.valueOf(request.getStatus().name()))
|
||||
.build())
|
||||
.build();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
package com.food.ordery.system.restaurant.domain.service.ports.input.message.listener;
|
||||
|
||||
import com.food.ordery.system.restaurant.domain.service.dto.RestaurantApprovalRequest;
|
||||
|
||||
public interface RestaurantApprovalRequestMessageListener {
|
||||
void approveOrder(RestaurantApprovalRequest request);
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
package com.food.ordery.system.restaurant.domain.service.ports.output.message.publisher;
|
||||
|
||||
import com.food.order.system.restaurant.domain.core.event.OrderApprovedEvent;
|
||||
import com.food.order.sysyem.event.publisher.DomainEventPublisher;
|
||||
|
||||
public interface OrderApprovedMessagePublisher extends DomainEventPublisher<OrderApprovedEvent> {
|
||||
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
package com.food.ordery.system.restaurant.domain.service.ports.output.message.publisher;
|
||||
|
||||
import com.food.order.system.restaurant.domain.core.event.OrderRejectedEvent;
|
||||
import com.food.order.sysyem.event.publisher.DomainEventPublisher;
|
||||
|
||||
public interface OrderRejectedMessagePublisher extends DomainEventPublisher<OrderRejectedEvent> {
|
||||
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
package com.food.ordery.system.restaurant.domain.service.ports.output.repository;
|
||||
|
||||
import com.food.order.system.restaurant.domain.core.entity.OrderApproval;
|
||||
|
||||
public interface OrderApprovalRepository {
|
||||
OrderApproval save(OrderApproval orderApproval);
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
package com.food.ordery.system.restaurant.domain.service.ports.output.repository;
|
||||
|
||||
import com.food.order.system.restaurant.domain.core.entity.Restaurant;
|
||||
|
||||
import java.util.Optional;
|
||||
|
||||
public interface RestaurantRepository {
|
||||
Optional<Restaurant> findRestaurantInformation(Restaurant restaurant);
|
||||
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<parent>
|
||||
<artifactId>restaurant-domain</artifactId>
|
||||
<groupId>com.food.order</groupId>
|
||||
<version>1.0-SNAPSHOT</version>
|
||||
<relativePath>../pom.xml</relativePath>
|
||||
</parent>
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<artifactId>restaurant-domain-core</artifactId>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>com.food.order</groupId>
|
||||
<artifactId>common-domain</artifactId>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
|
||||
</project>
|
||||
@@ -0,0 +1,17 @@
|
||||
package com.food.order.system.restaurant.domain.core;
|
||||
|
||||
import com.food.order.system.restaurant.domain.core.entity.Restaurant;
|
||||
import com.food.order.system.restaurant.domain.core.event.OrderApprovalEvent;
|
||||
import com.food.order.system.restaurant.domain.core.event.OrderApprovedEvent;
|
||||
import com.food.order.system.restaurant.domain.core.event.OrderRejectedEvent;
|
||||
import com.food.order.sysyem.event.publisher.DomainEventPublisher;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public interface RestaurantDomainService {
|
||||
OrderApprovalEvent validateOrder(Restaurant restaurant,
|
||||
List<String> failureMessages,
|
||||
DomainEventPublisher<OrderApprovedEvent> publisher,
|
||||
DomainEventPublisher<OrderRejectedEvent> rejectedPublisher);
|
||||
|
||||
}
|
||||
@@ -0,0 +1,40 @@
|
||||
package com.food.order.system.restaurant.domain.core;
|
||||
|
||||
import com.food.order.system.restaurant.domain.core.entity.Restaurant;
|
||||
import com.food.order.system.restaurant.domain.core.event.OrderApprovalEvent;
|
||||
import com.food.order.system.restaurant.domain.core.event.OrderApprovedEvent;
|
||||
import com.food.order.system.restaurant.domain.core.event.OrderRejectedEvent;
|
||||
import com.food.order.sysyem.event.publisher.DomainEventPublisher;
|
||||
import com.food.order.sysyem.valueobject.OrderApprovalStatus;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
||||
import java.time.ZoneId;
|
||||
import java.time.ZonedDateTime;
|
||||
import java.util.List;
|
||||
|
||||
import static com.food.order.sysyem.DomainConstants.UTC;
|
||||
|
||||
@Slf4j
|
||||
public class RestaurantDomainServiceImpl implements RestaurantDomainService {
|
||||
|
||||
@Override
|
||||
public OrderApprovalEvent validateOrder(Restaurant restaurant,
|
||||
List<String> failureMessages,
|
||||
DomainEventPublisher<OrderApprovedEvent> publisher,
|
||||
DomainEventPublisher<OrderRejectedEvent> rejectedPublisher) {
|
||||
restaurant.validateOrder(failureMessages);
|
||||
log.info("Order validation with id {}", restaurant.getOrderDetail().getId());
|
||||
if (failureMessages.isEmpty()) {
|
||||
log.info("Order validation with id {} is successful", restaurant.getOrderDetail().getId());
|
||||
restaurant.constructOrderApproval(OrderApprovalStatus.APPROVED);
|
||||
return new OrderApprovedEvent(restaurant.getOrderApproval(), restaurant.getId(),
|
||||
failureMessages, ZonedDateTime.now(ZoneId.of(UTC)), publisher);
|
||||
} else {
|
||||
log.info("Order validation with id {} is failed", restaurant.getOrderDetail().getId());
|
||||
restaurant.constructOrderApproval(OrderApprovalStatus.REJECTED);
|
||||
return new OrderRejectedEvent(restaurant.getOrderApproval(), restaurant.getId(),
|
||||
failureMessages, ZonedDateTime.now(), rejectedPublisher);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,72 @@
|
||||
package com.food.order.system.restaurant.domain.core.entity;
|
||||
|
||||
import com.food.order.system.restaurant.domain.core.valueobject.OrderApprovalId;
|
||||
import com.food.order.sysyem.entity.BaseEntity;
|
||||
import com.food.order.sysyem.valueobject.OrderApprovalStatus;
|
||||
import com.food.order.sysyem.valueobject.OrderId;
|
||||
import com.food.order.sysyem.valueobject.RestaurantId;
|
||||
|
||||
public class OrderApproval extends BaseEntity<OrderApprovalId> {
|
||||
|
||||
private final RestaurantId restaurantId;
|
||||
private final OrderId orderId;
|
||||
private final OrderApprovalStatus status;
|
||||
|
||||
private OrderApproval(Builder builder) {
|
||||
setId(builder.orderApprovalId);
|
||||
restaurantId = builder.restaurantId;
|
||||
orderId = builder.orderId;
|
||||
status = builder.status;
|
||||
}
|
||||
|
||||
public static Builder builder() {
|
||||
return new Builder();
|
||||
}
|
||||
|
||||
public RestaurantId getRestaurantId() {
|
||||
return restaurantId;
|
||||
}
|
||||
|
||||
public OrderId getOrderId() {
|
||||
return orderId;
|
||||
}
|
||||
|
||||
public OrderApprovalStatus getStatus() {
|
||||
return status;
|
||||
}
|
||||
|
||||
|
||||
public static final class Builder {
|
||||
private OrderApprovalId orderApprovalId;
|
||||
private RestaurantId restaurantId;
|
||||
private OrderId orderId;
|
||||
private OrderApprovalStatus status;
|
||||
|
||||
private Builder() {
|
||||
}
|
||||
|
||||
public Builder orderApprovalId(OrderApprovalId val) {
|
||||
orderApprovalId = val;
|
||||
return this;
|
||||
}
|
||||
|
||||
public Builder restaurantId(RestaurantId val) {
|
||||
restaurantId = val;
|
||||
return this;
|
||||
}
|
||||
|
||||
public Builder orderId(OrderId val) {
|
||||
orderId = val;
|
||||
return this;
|
||||
}
|
||||
|
||||
public Builder status(OrderApprovalStatus val) {
|
||||
status = val;
|
||||
return this;
|
||||
}
|
||||
|
||||
public OrderApproval build() {
|
||||
return new OrderApproval(this);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,72 @@
|
||||
package com.food.order.system.restaurant.domain.core.entity;
|
||||
|
||||
import com.food.order.sysyem.entity.BaseEntity;
|
||||
import com.food.order.sysyem.valueobject.Money;
|
||||
import com.food.order.sysyem.valueobject.OrderId;
|
||||
import com.food.order.sysyem.valueobject.OrderStatus;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public class OrderDetail extends BaseEntity<OrderId> {
|
||||
private OrderStatus status;
|
||||
private Money totalAmount;
|
||||
private final List<Product> products;
|
||||
|
||||
private OrderDetail(Builder builder) {
|
||||
setId(builder.orderId);
|
||||
status = builder.status;
|
||||
totalAmount = builder.totalAmount;
|
||||
products = builder.products;
|
||||
}
|
||||
|
||||
public static Builder builder() {
|
||||
return new Builder();
|
||||
}
|
||||
|
||||
public OrderStatus getStatus() {
|
||||
return status;
|
||||
}
|
||||
|
||||
public Money getTotalAmount() {
|
||||
return totalAmount;
|
||||
}
|
||||
|
||||
public List<Product> getProducts() {
|
||||
return products;
|
||||
}
|
||||
|
||||
|
||||
public static final class Builder {
|
||||
private OrderId orderId;
|
||||
private OrderStatus status;
|
||||
private Money totalAmount;
|
||||
private List<Product> products;
|
||||
|
||||
private Builder() {
|
||||
}
|
||||
|
||||
public Builder orderId(OrderId val) {
|
||||
orderId = val;
|
||||
return this;
|
||||
}
|
||||
|
||||
public Builder status(OrderStatus val) {
|
||||
status = val;
|
||||
return this;
|
||||
}
|
||||
|
||||
public Builder totalAmount(Money val) {
|
||||
totalAmount = val;
|
||||
return this;
|
||||
}
|
||||
|
||||
public Builder products(List<Product> val) {
|
||||
products = val;
|
||||
return this;
|
||||
}
|
||||
|
||||
public OrderDetail build() {
|
||||
return new OrderDetail(this);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,88 @@
|
||||
package com.food.order.system.restaurant.domain.core.entity;
|
||||
|
||||
import com.food.order.sysyem.entity.BaseEntity;
|
||||
import com.food.order.sysyem.valueobject.Money;
|
||||
import com.food.order.sysyem.valueobject.ProductId;
|
||||
|
||||
public class Product extends BaseEntity<ProductId> {
|
||||
|
||||
private String name;
|
||||
private Money price;
|
||||
private final int quantity;
|
||||
private boolean available;
|
||||
|
||||
public void updateWithConfirmedNamePriceAndAvailablity(String name, Money price, boolean available) {
|
||||
this.name = name;
|
||||
this.price = price;
|
||||
this.available = available;
|
||||
}
|
||||
|
||||
private Product(Builder builder) {
|
||||
setId(builder.productId);
|
||||
name = builder.name;
|
||||
price = builder.price;
|
||||
quantity = builder.quantity;
|
||||
available = builder.available;
|
||||
}
|
||||
|
||||
public static Builder builder() {
|
||||
return new Builder();
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public Money getPrice() {
|
||||
return price;
|
||||
}
|
||||
|
||||
public int getQuantity() {
|
||||
return quantity;
|
||||
}
|
||||
|
||||
public boolean isAvailable() {
|
||||
return available;
|
||||
}
|
||||
|
||||
|
||||
public static final class Builder {
|
||||
private ProductId productId;
|
||||
private String name;
|
||||
private Money price;
|
||||
private int quantity;
|
||||
private boolean available;
|
||||
|
||||
private Builder() {
|
||||
}
|
||||
|
||||
public Builder productId(ProductId val) {
|
||||
productId = val;
|
||||
return this;
|
||||
}
|
||||
|
||||
public Builder name(String val) {
|
||||
name = val;
|
||||
return this;
|
||||
}
|
||||
|
||||
public Builder price(Money val) {
|
||||
price = val;
|
||||
return this;
|
||||
}
|
||||
|
||||
public Builder quantity(int val) {
|
||||
quantity = val;
|
||||
return this;
|
||||
}
|
||||
|
||||
public Builder available(boolean val) {
|
||||
available = val;
|
||||
return this;
|
||||
}
|
||||
|
||||
public Product build() {
|
||||
return new Product(this);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,110 @@
|
||||
package com.food.order.system.restaurant.domain.core.entity;
|
||||
|
||||
import com.food.order.system.restaurant.domain.core.valueobject.OrderApprovalId;
|
||||
import com.food.order.sysyem.entity.AggregateRoot;
|
||||
import com.food.order.sysyem.valueobject.Money;
|
||||
import com.food.order.sysyem.valueobject.OrderApprovalStatus;
|
||||
import com.food.order.sysyem.valueobject.OrderStatus;
|
||||
import com.food.order.sysyem.valueobject.RestaurantId;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.UUID;
|
||||
|
||||
public class Restaurant extends AggregateRoot<RestaurantId> {
|
||||
private OrderApproval orderApproval;
|
||||
private boolean active;
|
||||
private final OrderDetail orderDetail;
|
||||
|
||||
public void validateOrder(List<String> failureMessages){
|
||||
if (!orderDetail.getStatus().equals(OrderStatus.PAID)){
|
||||
failureMessages.add("Order is not paid");
|
||||
}
|
||||
var totalAmount = orderDetail.getProducts()
|
||||
.stream()
|
||||
.map(product -> {
|
||||
if (Boolean.FALSE.equals(product.isAvailable()))
|
||||
{
|
||||
failureMessages.add("Product is not available");
|
||||
}
|
||||
return product.getPrice().multiply(product.getQuantity());
|
||||
})
|
||||
.reduce(Money.ZERO, Money::add);
|
||||
|
||||
if(!totalAmount.equals(orderDetail.getTotalAmount()))
|
||||
{
|
||||
failureMessages.add("Total amount is not correct");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public void constructOrderApproval(OrderApprovalStatus status){
|
||||
this.orderApproval = OrderApproval.builder()
|
||||
.orderApprovalId(new OrderApprovalId(UUID.randomUUID()))
|
||||
.restaurantId(this.getId())
|
||||
.orderId(this.getOrderDetail().getId())
|
||||
.status(status)
|
||||
.build();
|
||||
}
|
||||
|
||||
public void setActive(boolean active) {
|
||||
this.active = active;
|
||||
}
|
||||
|
||||
private Restaurant(Builder builder) {
|
||||
setId(builder.restaurantId);
|
||||
orderApproval = builder.orderApproval;
|
||||
active = builder.active;
|
||||
orderDetail = builder.orderDetail;
|
||||
}
|
||||
|
||||
public static Builder builder() {
|
||||
return new Builder();
|
||||
}
|
||||
|
||||
public OrderApproval getOrderApproval() {
|
||||
return orderApproval;
|
||||
}
|
||||
|
||||
public boolean isActive() {
|
||||
return active;
|
||||
}
|
||||
|
||||
public OrderDetail getOrderDetail() {
|
||||
return orderDetail;
|
||||
}
|
||||
|
||||
|
||||
public static final class Builder {
|
||||
private RestaurantId restaurantId;
|
||||
private OrderApproval orderApproval;
|
||||
private boolean active;
|
||||
private OrderDetail orderDetail;
|
||||
|
||||
private Builder() {
|
||||
}
|
||||
|
||||
public Builder restaurantId(RestaurantId val) {
|
||||
restaurantId = val;
|
||||
return this;
|
||||
}
|
||||
|
||||
public Builder orderApproval(OrderApproval val) {
|
||||
orderApproval = val;
|
||||
return this;
|
||||
}
|
||||
|
||||
public Builder active(boolean val) {
|
||||
active = val;
|
||||
return this;
|
||||
}
|
||||
|
||||
public Builder orderDetail(OrderDetail val) {
|
||||
orderDetail = val;
|
||||
return this;
|
||||
}
|
||||
|
||||
public Restaurant build() {
|
||||
return new Restaurant(this);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,42 @@
|
||||
package com.food.order.system.restaurant.domain.core.event;
|
||||
|
||||
import com.food.order.system.restaurant.domain.core.entity.OrderApproval;
|
||||
import com.food.order.sysyem.event.DomainEvent;
|
||||
import com.food.order.sysyem.valueobject.RestaurantId;
|
||||
|
||||
import java.time.ZonedDateTime;
|
||||
import java.util.List;
|
||||
|
||||
public abstract class OrderApprovalEvent implements DomainEvent<OrderApproval> {
|
||||
|
||||
private final OrderApproval orderApproval;
|
||||
private final RestaurantId restaurantId;
|
||||
private final List<String > failureMessages;
|
||||
private final ZonedDateTime createdAt;
|
||||
|
||||
public OrderApprovalEvent(OrderApproval orderApproval,
|
||||
RestaurantId restaurantId,
|
||||
List<String> failureMessages,
|
||||
ZonedDateTime createdAt) {
|
||||
this.orderApproval = orderApproval;
|
||||
this.restaurantId = restaurantId;
|
||||
this.failureMessages = failureMessages;
|
||||
this.createdAt = createdAt;
|
||||
}
|
||||
|
||||
public OrderApproval getOrderApproval() {
|
||||
return orderApproval;
|
||||
}
|
||||
|
||||
public RestaurantId getRestaurantId() {
|
||||
return restaurantId;
|
||||
}
|
||||
|
||||
public List<String> getFailureMessages() {
|
||||
return failureMessages;
|
||||
}
|
||||
|
||||
public ZonedDateTime getCreatedAt() {
|
||||
return createdAt;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
package com.food.order.system.restaurant.domain.core.event;
|
||||
|
||||
import com.food.order.system.restaurant.domain.core.entity.OrderApproval;
|
||||
import com.food.order.sysyem.event.publisher.DomainEventPublisher;
|
||||
import com.food.order.sysyem.valueobject.RestaurantId;
|
||||
|
||||
import java.time.ZonedDateTime;
|
||||
import java.util.List;
|
||||
|
||||
public class OrderApprovedEvent extends OrderApprovalEvent {
|
||||
|
||||
|
||||
private final DomainEventPublisher<OrderApprovedEvent> publisher;
|
||||
|
||||
public OrderApprovedEvent(OrderApproval orderApproval,
|
||||
RestaurantId restaurantId,
|
||||
List<String> failureMessages,
|
||||
ZonedDateTime createdAt,
|
||||
DomainEventPublisher<OrderApprovedEvent> publisher) {
|
||||
super(orderApproval, restaurantId, failureMessages, createdAt);
|
||||
this.publisher = publisher;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void fire() {
|
||||
publisher.publish(this);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
package com.food.order.system.restaurant.domain.core.event;
|
||||
|
||||
import com.food.order.system.restaurant.domain.core.entity.OrderApproval;
|
||||
import com.food.order.sysyem.event.publisher.DomainEventPublisher;
|
||||
import com.food.order.sysyem.valueobject.RestaurantId;
|
||||
|
||||
import java.time.ZonedDateTime;
|
||||
import java.util.List;
|
||||
|
||||
public class OrderRejectedEvent extends OrderApprovalEvent {
|
||||
|
||||
private final DomainEventPublisher<OrderRejectedEvent> publisher;
|
||||
|
||||
public OrderRejectedEvent(OrderApproval orderApproval,
|
||||
RestaurantId restaurantId,
|
||||
List<String> failureMessages,
|
||||
ZonedDateTime createdAt,
|
||||
DomainEventPublisher<OrderRejectedEvent> publisher) {
|
||||
super(orderApproval, restaurantId, failureMessages, createdAt);
|
||||
this.publisher = publisher;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void fire() {
|
||||
publisher.publish(this);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
package com.food.order.system.restaurant.domain.core.exception;
|
||||
|
||||
import com.food.order.sysyem.exception.DomainException;
|
||||
|
||||
public class RestaurantDomainException extends DomainException {
|
||||
public RestaurantDomainException(String message) {
|
||||
super(message);
|
||||
}
|
||||
|
||||
public RestaurantDomainException(String message, Throwable cause) {
|
||||
super(message, cause);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
package com.food.order.system.restaurant.domain.core.exception;
|
||||
|
||||
import com.food.order.sysyem.exception.DomainException;
|
||||
|
||||
public class RestaurantNotFoundException extends DomainException {
|
||||
public RestaurantNotFoundException(String message) {
|
||||
super(message);
|
||||
}
|
||||
|
||||
public RestaurantNotFoundException(String message, Throwable cause) {
|
||||
super(message, cause);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
package com.food.order.system.restaurant.domain.core.valueobject;
|
||||
|
||||
import com.food.order.sysyem.valueobject.BaseId;
|
||||
|
||||
import java.util.UUID;
|
||||
|
||||
public class OrderApprovalId extends BaseId<UUID> {
|
||||
public OrderApprovalId(UUID value) {
|
||||
super(value);
|
||||
}
|
||||
}
|
||||
15
restaurant-service/restaurant-messaging/pom.xml
Normal file
15
restaurant-service/restaurant-messaging/pom.xml
Normal file
@@ -0,0 +1,15 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<parent>
|
||||
<artifactId>restaurant-service</artifactId>
|
||||
<groupId>com.food.order</groupId>
|
||||
<version>1.0-SNAPSHOT</version>
|
||||
</parent>
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<artifactId>restaurant-messaging</artifactId>
|
||||
|
||||
|
||||
</project>
|
||||
Reference in New Issue
Block a user