Skip to content

Commit

Permalink
create order confirm flow
Browse files Browse the repository at this point in the history
  • Loading branch information
Fagorym committed Nov 3, 2023
1 parent b08f1cb commit 32ddb03
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import lombok.RequiredArgsConstructor;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
Expand Down Expand Up @@ -52,4 +51,14 @@ public void cancelOrder(@RequestParam Long orderId) {
public List<ResponseOrderDto> getOrders(@RequestParam(required = false) Integer status) {
return orderService.getOrders(status);
}

/**
* Put request for confirming order.
*
* @param orderId identifier of order to confirm order.
*/
@PutMapping(value = "/confirm")
public void confirmOrder(@RequestParam Long orderId) {
orderService.confirmOrder(orderId);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package ru.nsu.fit.directors.userservice.event;

import lombok.Getter;
import lombok.Setter;
import lombok.experimental.Accessors;

@Getter
@Setter
@Accessors(chain = true)
public class OrderConfirmedEvent implements OrderEvent {
private Long orderId;
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,6 @@ public interface OrderService {
void cancelOrder(Long orderId);

List<ResponseOrderDto> getOrders(Integer status);

void confirmOrder(Long orderId);
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import ru.nsu.fit.directors.userservice.dto.request.RequestOrderDto;
import ru.nsu.fit.directors.userservice.dto.response.ResponseOrderDto;
import ru.nsu.fit.directors.userservice.event.OrderCancelledEvent;
import ru.nsu.fit.directors.userservice.event.OrderConfirmedEvent;
import ru.nsu.fit.directors.userservice.event.OrderEvent;
import ru.nsu.fit.directors.userservice.exception.OrderBookingTimeException;
import ru.nsu.fit.directors.userservice.mapper.OrderMapper;
Expand Down Expand Up @@ -58,6 +59,13 @@ public List<ResponseOrderDto> getOrders(Integer status) {

}

@Override
public void confirmOrder(Long orderId) {
validateUserOrder(orderId);
kafkaTemplate.send("orderTopic", new OrderConfirmedEvent()
.setOrderId(orderId));
}

private void validateUserOrder(Long orderId) {
var orders = getOrders(0);
if (orders.stream()
Expand Down
3 changes: 2 additions & 1 deletion src/main/resources/application.properties
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ spring.kafka.consumer.group-id=orderId
spring.kafka.producer.key-serializer=org.apache.kafka.common.serialization.StringSerializer
spring.kafka.producer.value-serializer=org.springframework.kafka.support.serializer.JsonSerializer
spring.kafka.producer.properties.spring.json.type.mapping=created:ru.nsu.fit.directors.userservice.event.OrderCreatedEvent,\
\ cancelled:ru.nsu.fit.directors.userservice.event.OrderCancelledEvent
\ cancelled:ru.nsu.fit.directors.userservice.event.OrderCancelledEvent,\
\ confirmed:ru.nsu.fit.directors.userservice.event.OrderConfirmedEvent
spring.kafka.template.default-topic=orderTopic

spring.kafka.consumer.key-deserializer=org.apache.kafka.common.serialization.StringDeserializer
Expand Down

0 comments on commit 32ddb03

Please sign in to comment.