コード例 #1
0
        public IActionResult Create([FromBody] NewOrderResource item)
        {
            if (item == null)
            {
                return(BadRequest());
            }
            Address     address  = null;
            Customer    customer = null;
            Card        card     = null;
            List <Item> items    = null;

            Task.Factory.ContinueWhenAll(
                new Task[] {
                createHalAsyncTask <Address>(item.Address.AbsoluteUri)
                .ContinueWith((task) => { address = task.Result; }),
                createHalAsyncTask <Customer>(item.Customer.AbsoluteUri)
                .ContinueWith((task) => { customer = task.Result; }),
                createHalAsyncTask <Card>(item.Card.AbsoluteUri)
                .ContinueWith((task) => { card = task.Result; }),
                createHalAsyncTask <List <Item> >(item.Items.AbsoluteUri)
                .ContinueWith((task) => { items = task.Result; })
            },
                _ => {})
            .Wait();

            PaymentResponse paymentResponse = null;

            float          amount         = CalculateTotal(items);
            PaymentRequest paymentRequest = new PaymentRequest()
            {
                Address  = address,
                Card     = card,
                Customer = customer,
                Amount   = amount
            };

            client.PostAsync <PaymentResponse>(new HalKit.Models.Response.Link {
                HRef = AppSettings.ServiceEndpoints.PaymentServiceEndpoint, IsTemplated = false
            }, paymentRequest)
            .ContinueWith((task) => {
                paymentResponse = task.Result;
            })
            .Wait();

            if (!paymentResponse.Authorised)
            {
                return(BadRequest());
            }

            string   ACustomerId = customer.Id;
            Shipment Shipment    = null;
            Shipment AShipment   = new Shipment()
            {
                Name = ACustomerId
            };

            client.PostAsync <Shipment>(new HalKit.Models.Response.Link {
                HRef = AppSettings.ServiceEndpoints.ShippingServiceEndpoint, IsTemplated = false
            }, AShipment)
            .ContinueWith((task) => {
                Shipment = task.Result;
            })
            .Wait();

            CustomerOrder order = new CustomerOrder()
            {
                CustomerId = ACustomerId,
                Address    = address,
                Card       = card,
                Customer   = customer,
                Items      = items,
                Total      = amount,
                Shipment   = Shipment
            };

            collection.InsertOne(order);
            //return CreatedAtRoute("GetOffer", new { id = order.Id }, order);

            customer.Id = null;
            address.Id  = null;
            card.Id     = null;

            return(new ObjectResult(order)
            {
                StatusCode = 201
            });
        }
コード例 #2
0
        public IActionResult Create([FromBody] NewOrderResource item)
        {
            if (item == null)
            {
                return(BadRequest());
            }
            Thread.Sleep(2000);
            Address     address  = null;
            Customer    customer = null;
            Card        card     = null;
            List <Item> items    = null;

            /* Task.Factory.ContinueWhenAll(
             *  new Task[] {
             *      createHalAsyncTask<Address>(item.Address.AbsoluteUri)
             *          .ContinueWith((task) => { address = task.Result; }),
             *      createHalAsyncTask<Customer>(item.Customer.AbsoluteUri)
             *          .ContinueWith((task) => { customer = task.Result; }),
             *      createHalAsyncTask<Card>(item.Card.AbsoluteUri)
             *          .ContinueWith((task) => { card = task.Result; }),
             *      createHalAsyncTask<List<Item>>(item.Items.AbsoluteUri)
             *          .ContinueWith((task) => { items = task.Result; })
             *      },
             *      _ => {})
             *  .Wait();
             */
            WebRequest cartRequest  = WebRequest.Create(item.Items.AbsoluteUri);
            var        cartResponse = cartRequest.GetResponseAsync().Result;

            StreamReader cartreader = new StreamReader(cartResponse.GetResponseStream());

            string responseFromServer = cartreader.ReadToEnd();

            items = JsonConvert.DeserializeObject <List <Item> >(responseFromServer);

            WebRequest customerRequest  = WebRequest.Create(item.Customer.AbsoluteUri);
            var        customerResponse = customerRequest.GetResponseAsync().Result;

            StreamReader customerreader = new StreamReader(customerResponse.GetResponseStream());

            responseFromServer = customerreader.ReadToEnd();
            customer           = JsonConvert.DeserializeObject <Customer>(responseFromServer);

            WebRequest addressRequest  = WebRequest.Create(item.Address.AbsoluteUri);
            var        addressResponse = addressRequest.GetResponseAsync().Result;

            StreamReader addressreader = new StreamReader(addressResponse.GetResponseStream());

            responseFromServer = addressreader.ReadToEnd();
            address            = JsonConvert.DeserializeObject <Address>(responseFromServer);

            WebRequest cardRequest   = WebRequest.Create(item.Card.AbsoluteUri);
            var        cardsResponse = cardRequest.GetResponseAsync().Result;

            StreamReader cardreader = new StreamReader(cardsResponse.GetResponseStream());

            responseFromServer = cardreader.ReadToEnd();
            card = JsonConvert.DeserializeObject <Card>(responseFromServer);

            Thread.Sleep(2000);

            PaymentResponse paymentResponse = null;

            float          amount         = CalculateTotal(items);
            PaymentRequest paymentRequest = new PaymentRequest()
            {
                Address  = address,
                Card     = card,
                Customer = customer,
                Amount   = amount
            };

/*
 *          client.PostAsync<PaymentResponse>(new HalKit.Models.Response.Link {HRef =  AppSettings.ServiceEndpoints.PaymentServiceEndpoint, IsTemplated = false}, paymentRequest)
 *              .ContinueWith((task) => {
 *                  paymentResponse = task.Result;
 *              })
 *             .Wait(); */

            var        data       = System.Text.Encoding.ASCII.GetBytes(JsonConvert.SerializeObject(paymentRequest).ToString());
            WebRequest payRequest = WebRequest.Create(AppSettings.ServiceEndpoints.PaymentServiceEndpoint);

            payRequest.Method      = "POST";
            payRequest.ContentType = "application/json";
            Stream reqStream = payRequest.GetRequestStreamAsync().Result;

            reqStream.Write(data, 0, data.Length);

            var payResponse = payRequest.GetResponseAsync().Result;

            StreamReader payreader = new StreamReader(payResponse.GetResponseStream());

            responseFromServer = payreader.ReadToEnd();
            paymentResponse    = JsonConvert.DeserializeObject <PaymentResponse>(responseFromServer);

            if (!paymentResponse.Authorised)
            {
                return(BadRequest());
            }

            string   ACustomerId = customer.Id;
            Shipment Shipment    = null;
            Shipment AShipment   = new Shipment()
            {
                Name = ACustomerId
            };

            /*  client.PostAsync<Shipment>(new HalKit.Models.Response.Link {HRef =  AppSettings.ServiceEndpoints.ShippingServiceEndpoint, IsTemplated = false}, AShipment)
             * .ContinueWith((task) => {
             *   Shipment = task.Result;
             * })
             * .Wait(); */

            var        shipmentData    = System.Text.Encoding.ASCII.GetBytes(JsonConvert.SerializeObject(AShipment).ToString());
            WebRequest shipmentRequest = WebRequest.Create(AppSettings.ServiceEndpoints.ShippingServiceEndpoint);

            shipmentRequest.Method      = "POST";
            shipmentRequest.ContentType = "application/json";
            Stream reqShipmentStream = shipmentRequest.GetRequestStreamAsync().Result;

            reqShipmentStream.Write(shipmentData, 0, shipmentData.Length);

            var shipmentResponse = shipmentRequest.GetResponseAsync().Result;

            StreamReader shipmentReader = new StreamReader(shipmentResponse.GetResponseStream());

            responseFromServer = shipmentReader.ReadToEnd();
            Shipment           = JsonConvert.DeserializeObject <Shipment>(responseFromServer);

            CustomerOrder order = new CustomerOrder()
            {
                CustomerId = ACustomerId,
                Address    = address,
                Card       = card,
                Customer   = customer,
                Items      = items,
                Total      = amount,
                Shipment   = Shipment
            };

            collection.InsertOne(order);
            //return CreatedAtRoute("GetOffer", new { id = order.Id }, order);

            customer.Id = null;
            address.Id  = null;
            card.Id     = null;

            return(new ObjectResult(order)
            {
                StatusCode = 201
            });
        }