Skip to content

Instantly share code, notes, and snippets.

@lamoboos223
Created May 7, 2022 00:28
Show Gist options
  • Save lamoboos223/da8cab0e43242164656119be17b25ee5 to your computer and use it in GitHub Desktop.
Save lamoboos223/da8cab0e43242164656119be17b25ee5 to your computer and use it in GitHub Desktop.
import com.example.restaurant.avro.schema.OrderAvro;
import com.example.restaurant.kafka.AvroProducer;
import com.example.restaurant.mapper.OrderMapper;
import com.example.restaurant.models.EventTypes;
import com.example.restaurant.models.OrderModel;
import com.example.restaurant.models.OrderStatus;
import com.example.restaurant.repository.OrderRepo;
import com.example.restaurant.request.OrderRequest;
import com.example.restaurant.service.OrderService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Service;
import org.springframework.web.server.ResponseStatusException;
import java.util.List;
@Service
@RequiredArgsConstructor
@Slf4j
public class OrderServiceImpl implements OrderService {
@Autowired
private final OrderRepo orderRepo;
@Autowired
private final AvroProducer avroProducer;
@Override
public void addOrder(OrderRequest orderRequest) {
OrderModel orderModel = OrderMapper.orderRequestToOrderModel(orderRequest);
OrderAvro orderAvro = OrderMapper.OrderModelToOrderAvro(orderModel);
orderAvro.setEventType(String.valueOf(EventTypes.ORDER_CREATED));
avroProducer.publish(orderAvro);
}
@Override
public void addOrder(OrderModel orderModel) {
orderRepo.save(orderModel);
}
@Override
@Cacheable(value = "Order")
public List<OrderModel> getOrder() {
return orderRepo.findAll();
}
@Override
@Cacheable(value = "Order")
public OrderModel getOrderById(int id) {
return orderRepo.findById(id)
.orElseThrow(() -> new ResponseStatusException(HttpStatus.NOT_FOUND,
String.format("id Resource %s not found", id)));
}
@Override
@CachePut(value = "Order")
public OrderModel updateOrder(OrderRequest orderRequest, int id) {
OrderModel orderModel = OrderMapper.orderRequestToOrderModel(orderRequest);
OrderModel order = getOrderById(id);
order.setTakeAway(orderModel.isTakeAway());
order.setItems(orderModel.getItems());
order.setTotal(orderModel.getTotal());
order.setVat(orderModel.getVat());
order.setStatus(String.valueOf(OrderStatus.PENDING));
order.setTimestamp(orderModel.getTimestamp());
OrderAvro orderAvro = OrderMapper.OrderModelToOrderAvro(orderModel);
orderAvro.setEventType(String.valueOf(EventTypes.ORDER_UPDATED));
avroProducer.publish(orderAvro);
return orderRepo.save(order);
}
@Override
@CacheEvict(value = "Order")
public void deleteOrder(int id) {
OrderModel orderModel = getOrderById(id);
OrderAvro orderAvro = OrderMapper.OrderModelToOrderAvro(orderModel);
orderAvro.setEventType(String.valueOf(EventTypes.ORDER_DELETED));
avroProducer.publish(orderAvro);
orderRepo.delete(orderModel);
}
@Override
@CachePut(value = "Order")
public OrderModel updateOrder(int id, OrderStatus orderStatus, EventTypes eventTypes) {
OrderModel orderModel = getOrderById(id);
OrderAvro orderAvro = OrderMapper.OrderModelToOrderAvro(orderModel);
orderAvro.setEventType(String.valueOf(eventTypes));
avroProducer.publish(orderAvro);
orderModel.setStatus(String.valueOf(orderStatus));
return orderModel;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment