コード例 #1
0
        public async Task <IActionResult> Run(
            [HttpTrigger(AuthorizationLevel.Anonymous, "delete", Route = "TaxiPrediction/{id}")] HttpRequest req,
            string id)
        {
            IActionResult result = null;

            try
            {
                QueryDefinition getContactQueryDefinition = new QueryDefinition(
                    $"SELECT * FROM {_taxiContainer.Id} c WHERE c.id = @id")
                                                            .WithParameter("@id", id);

                FeedIterator <TaxiTripDTO> getResultSet = _taxiContainer.GetItemQueryIterator <TaxiTripDTO>
                                                          (
                    getContactQueryDefinition,
                    requestOptions: new QueryRequestOptions()
                {
                    MaxItemCount = 1
                }
                                                          );

                if (getResultSet == null)
                {
                    _logger.LogInformation($"TaxiTrip {id} not found!");
                    result = new StatusCodeResult(StatusCodes.Status404NotFound);
                }

                while (getResultSet.HasMoreResults)
                {
                    FeedResponse <TaxiTripDTO> response = await getResultSet.ReadNextAsync();

                    TaxiTripDTO contact = response.First();
                    ItemResponse <TaxiTripDTO> itemResponse = await _taxiContainer.DeleteItemAsync <TaxiTripDTO>
                                                                  (id : id, partitionKey : new PartitionKey(contact.VendorId));

                    result = new OkResult();
                }
            }
            catch (Exception ex)
            {
                _logger.LogError($"Internal Server Error. Exception thrown: {ex.Message}");
                result = new StatusCodeResult(StatusCodes.Status500InternalServerError);
            }

            return(result);
        }
コード例 #2
0
        public async Task <IActionResult> Run(
            [HttpTrigger(AuthorizationLevel.Anonymous, "post", Route = "/TaxiPrediction")] HttpRequest req)
        {
            IActionResult result = null;

            try
            {
                string requestBody = await new StreamReader(req.Body).ReadToEndAsync();

                var input = JsonConvert.DeserializeObject <TaxiTrip>(requestBody);

                TaxiTripFarePrediction prediction = _predictionEnginePool.Predict(
                    modelName: "TaxiTripModel",
                    example: input);

                var insertedPrediction = new TaxiTripDTO()
                {
                    Id                  = Guid.NewGuid().ToString(),
                    VendorId            = input.VendorId,
                    RateCode            = input.RateCode,
                    PassengerCount      = input.PassengerCount,
                    TripTime            = input.TripTime,
                    TripDistance        = input.TripDistance,
                    PaymentType         = input.PaymentType,
                    FareAmount          = input.FareAmount,
                    PredictedFareAmount = prediction.FareAmount
                };

                ItemResponse <TaxiTripDTO> response = await _taxiContainer.CreateItemAsync(
                    insertedPrediction,
                    new PartitionKey(insertedPrediction.VendorId));

                _logger.LogInformation($"Inserting prediction cost: {response.RequestCharge} RU/s");
                result = new StatusCodeResult(StatusCodes.Status201Created);
            }
            catch (Exception ex)
            {
                _logger.LogError($"Something went wrong. Exception thrown: {ex.Message}");
                result = new StatusCodeResult(StatusCodes.Status500InternalServerError);
            }

            return(result);
        }