Exemple #1
0
        public static async Task <IActionResult> UpdateDriverLocation([HttpTrigger(AuthorizationLevel.Anonymous, "put", Route = "driverlocations")] HttpRequest req,
                                                                      ILogger log)
        {
            log.LogInformation("UpdateDriverLocation triggered....");

            try
            {
                await Utilities.ValidateToken(req);

                string             requestBody    = new StreamReader(req.Body).ReadToEnd();
                DriverLocationItem driverLocation = JsonConvert.DeserializeObject <DriverLocationItem>(requestBody);
                var persistenceService            = ServiceFactory.GetPersistenceService();
                return((ActionResult) new OkObjectResult(await persistenceService.UpsertDriverLocation(driverLocation)));
            }
            catch (Exception e)
            {
                var error = $"UpdateDriverLocation failed: {e.Message}";
                log.LogError(error);
                if (error.Contains(Constants.SECURITY_VALITION_ERROR))
                {
                    return(new StatusCodeResult(401));
                }
                else
                {
                    return(new BadRequestObjectResult(error));
                }
            }
        }
        public async Task <string> UpsertDriverLocation(DriverLocationItem location, bool isIgnoreChangeFeed = false)
        {
            var    error      = "";
            double cost       = 0;
            string resourceId = "";

            try
            {
                if (string.IsNullOrEmpty(_docDbDigitalMainCollectionName))
                {
                    throw new Exception("No Digital Main collection defined!");
                }

                DriverItem driver = await RetrieveDriver(location.DriverCode);

                if (driver == null)
                {
                    throw new Exception($"Unable to locate a driver with code {location.DriverCode}");
                }

                // Just making sure...
                location.CollectionType = ItemCollectionTypes.DriverLocation;
                location.UpsertDate     = DateTime.Now;

                if (location.Id == "")
                {
                    location.Id = $"{location.DriverCode}-{location.CollectionType}-{Guid.NewGuid().ToString()}";
                }

                var response = await(await GetDocDBClient(_settingService)).UpsertDocumentAsync(UriFactory.CreateDocumentCollectionUri(_docDbDatabaseName, _docDbDigitalMainCollectionName), location);
                cost = response.RequestCharge;

                // Also update the driver latest location
                driver.Latitude  = location.Latitude;
                driver.Longitude = location.Longitude;
                await UpsertDriver(driver, isIgnoreChangeFeed);

                if (!isIgnoreChangeFeed)
                {
                    //TODO: This is one way we can react to changes in Cosmos and perhaps enqueue a message or whatever
                }
            }
            catch (Exception ex)
            {
                error = ex.Message;
                throw new Exception(error);
            }
            finally
            {
                _loggerService.Log($"{LOG_TAG} - UpsertDriverLocation - Error: {error}");
            }

            return(resourceId);
        }
        public async Task <string> UpsertDriverLocation(DriverLocationItem location, bool isIgnoreChangeFeed = false)
        {
            var    error      = "";
            string resourceId = "";

            try
            {
                if (string.IsNullOrEmpty(_docDbDigitalMainCollectionName))
                {
                    throw new Exception("No Digital Main collection defined!");
                }

                DriverItem driver = await RetrieveDriver(location.DriverCode);

                if (driver == null)
                {
                    throw new Exception($"Unable to locate a driver with code {location.DriverCode}");
                }

                // Just making sure...
                location.CollectionType = ItemCollectionTypes.DriverLocation;
                location.UpsertDate     = DateTime.Now;

                if (location.Id == "")
                {
                    location.Id = $"{location.DriverCode}-{location.CollectionType}-{Guid.NewGuid().ToString()}";
                }

                //TODO: DriverLocationItem has no Code property, which is the PK 😬
                var response = await(await GetCosmosContainer()).UpsertItemAsync(location);

                // Also update the driver latest location
                driver.Latitude  = location.Latitude;
                driver.Longitude = location.Longitude;
                await UpsertDriver(driver, isIgnoreChangeFeed);

                if (!isIgnoreChangeFeed)
                {
                    //TODO: This is one way we can react to changes in Cosmos and perhaps enqueue a message or whatever
                }
            }
            catch (Exception ex)
            {
                error = ex.Message;
                throw new Exception(ex.Message, ex);
            }
            finally
            {
                _loggerService.Log($"{LOG_TAG} - UpsertDriverLocation - Error: {error}");
            }

            //TODO: Always returns empty string...
            return(resourceId);
        }
 public Task <string> UpsertDriverLocation(DriverLocationItem driver, bool isIgnoreChangeFeed = false)
 {
     throw new NotImplementedException();
 }