package com.remy.graphql.resolvers.mutation;

import com.remy.common.module.OrderDTO;
import com.remy.graphql.evn.RestAPIEnv;
import com.remy.graphql.helper.BeanHelper;
import graphql.kickstart.tools.GraphQLMutationResolver;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;

import java.util.concurrent.CompletableFuture;

import static java.util.concurrent.CompletableFuture.supplyAsync;

@Component
@Slf4j
public class OrderMutation implements GraphQLMutationResolver {

    @Autowired
    RestAPIEnv apiEnv;

    CompletableFuture<OrderDTO> createOrder(OrderDTO orderDTO) {
        log.info("start create order: " + BeanHelper.toJSON(orderDTO));
        return supplyAsync(() ->
                new RestTemplate().postForObject(apiEnv.getOrdersEndpoint(),
                        orderDTO, OrderDTO.class));
    }
}
