public static async Task <IActionResult> RunGeoHash(
            [HttpTrigger(AuthorizationLevel.Function, "delete", Route = "interventions/{latitude}/{longitude}/{interventionId}/comments/{commentId}")]
            [RequestBodyType(typeof(DeletionRequest), "DeletionRequest")] DeletionRequest request,
            [Table(Config.InterventionsTableName, Connection = Config.StorageConnectionName)] CloudTable interventionsTable,
            string latitude, string longitude, string interventionId, string commentId, ILogger log)
        {
            var geoHash     = GeoHasher.GetGeoHash(latitude, longitude);
            var finalFilter = InterventionFilterBuilder.GetInterventionGeoHashFilter(geoHash, interventionId);

            var queryResult = await interventionsTable.ExecuteQuerySegmentedAsync(new TableQuery <InterventionEntity>().Where(
                                                                                      finalFilter).Take(1), null);

            var requestedIntervention = queryResult.Results.FirstOrDefault();

            if (requestedIntervention == null)
            {
                return(new StatusCodeResult(StatusCodes.Status404NotFound));
            }

            try
            {
                requestedIntervention.DeleteComment(commentId);
                await interventionsTable.ExecuteAsync(TableOperation.Merge(requestedIntervention));
            }
            catch (InvalidOperationException e)
            {
                return(new StatusCodeResult(StatusCodes.Status404NotFound));
            }

            return(new StatusCodeResult(StatusCodes.Status200OK));
        }
Пример #2
0
        public static async Task <IActionResult> RunGeoHash(
            [HttpTrigger(AuthorizationLevel.Function, "post", Route = "interventions/{latitude}/{longitude}/{interventionId}/comments")]
            AddCommentDto commentDto,
            [Table(Config.InterventionsTableName, Connection = Config.StorageConnectionName)] CloudTable interventionsTable,
            string latitude, string longitude, string interventionId, ILogger log)
        {
            var geoHash = GeoHasher.GetGeoHash(latitude, longitude);

            return(await AddComment(
                       InterventionFilterBuilder.GetInterventionGeoHashFilter(geoHash, interventionId),
                       commentDto,
                       interventionsTable
                       ));
        }
Пример #3
0
        public async Task <IActionResult> RunWithGeoHash(
            [HttpTrigger(AuthorizationLevel.Function, "get", Route = "interventions/{latitude}/{longitude}/{interventionId}")] HttpRequestMessage req,
            [Table(Config.InterventionsTableName, Connection = Config.StorageConnectionName)] CloudTable interventionsTable,
            string latitude, string longitude, string interventionId, ILogger log)
        {
            var geoHash     = GeoHasher.GetGeoHash(latitude, longitude);
            var finalFilter = InterventionFilterBuilder.GetInterventionGeoHashFilter(geoHash, interventionId);

            var queryResult = await interventionsTable.ExecuteQuerySegmentedAsync(new TableQuery <InterventionEntity>().Where(
                                                                                      finalFilter).Take(1), null);

            var interventionItemResponses = queryResult.Results.FirstOrDefault();

            if (interventionItemResponses != null)
            {
                return(new JsonResult(_mapper.Map <InterventionItemResponse>(interventionItemResponses)));
            }

            return(new StatusCodeResult(StatusCodes.Status404NotFound));
        }
        public async Task <IActionResult> RunWithGeoHash(
            [HttpTrigger(AuthorizationLevel.Function, "put", Route = "interventions/{latitude}/{longitude}/{interventionId}")]
            [RequestBodyType(typeof(InterventionDto), "InterventionDto")] InterventionDto editedIntervention,
            [Table(Config.InterventionsTableName, Connection = Config.StorageConnectionName)] CloudTable interventionsTable,
            string latitude, string longitude, string interventionId, ILogger log)
        {
            var geoHash     = GeoHasher.GetGeoHash(latitude, longitude);
            var finalFilter = InterventionFilterBuilder.GetInterventionGeoHashFilter(geoHash, interventionId);

            var queryResult = await interventionsTable.ExecuteQuerySegmentedAsync(new TableQuery <InterventionEntity>().Where(
                                                                                      finalFilter).Take(1), null);

            var interventionToEdit = queryResult.Results.FirstOrDefault();

            if (interventionToEdit == null)
            {
                return(new StatusCodeResult(StatusCodes.Status404NotFound));
            }

            if (AddressChanged(interventionToEdit, editedIntervention))
            {
                Address convertedGeoAddress = new Address();
                try
                {
                    convertedGeoAddress = await _addressConverter.ConvertToGeoAddress(editedIntervention.Address);
                }
                catch (Exception e)
                {
                    log.LogError(e, "error");
                    return(new StatusCodeResult(StatusCodes.Status500InternalServerError));
                }

                InterventionEntity adddedInterventionEntity = new InterventionEntity()
                {
                    Email            = editedIntervention.Email,
                    CreationDate     = interventionToEdit.CreationDate,
                    ModificationDate = DateTime.UtcNow,
                    Description      = editedIntervention.Description,
                    FullName         = editedIntervention.FullName,
                    PhoneNumber      = editedIntervention.PhoneNumber,
                    Status           = (int)editedIntervention.Status,
                    City             = editedIntervention.City,
                    Street           = editedIntervention.Street,
                    StreetNumber     = editedIntervention.StreetNumber,
                    GeoLat           = convertedGeoAddress.Latitude,
                    GeoLng           = convertedGeoAddress.Lognitude,
                    PartitionKey     = GeoHash.Encode(convertedGeoAddress.Latitude, convertedGeoAddress.Lognitude, Config.GeoHashPrecision)
                };

                TableOperation deleteOldIntervention = TableOperation.Delete(interventionToEdit);
                TableOperation insertNewIntervention = TableOperation.InsertOrReplace(adddedInterventionEntity);

                TableBatchOperation batch = new TableBatchOperation {
                    deleteOldIntervention, insertNewIntervention
                };
                await interventionsTable.ExecuteBatchAsync(batch);

                var addedItemResponse = _mapper.Map <InterventionItemResponse>(adddedInterventionEntity);
                return(new JsonResult(addedItemResponse));
            }

            interventionToEdit.Email            = editedIntervention.Email;
            interventionToEdit.ModificationDate = DateTime.UtcNow;
            interventionToEdit.Description      = editedIntervention.Description;
            interventionToEdit.FullName         = editedIntervention.FullName;
            interventionToEdit.PhoneNumber      = editedIntervention.PhoneNumber;
            interventionToEdit.Status           = (int)editedIntervention.Status;

            await interventionsTable.ExecuteAsync(TableOperation.Merge(interventionToEdit));

            var interventionItemResponses = _mapper.Map <InterventionItemResponse>(interventionToEdit);

            return(new JsonResult(interventionItemResponses));
        }