public async Task <IActionResult> DeletePicture([FromQuery] string troubleId, [FromQuery] string pictureUrl, CancellationToken cancellationToken)
        {
            if (!pictureUrl.Contains(troubleId))
            {
                var response = new Response
                {
                    StatusCode      = System.Net.HttpStatusCode.BadRequest,
                    ResponseDetails = new ResponseDetails
                    {
                        Code    = ResponseCodes.NotFound,
                        Message = "There is no such image URL for given trouble ID",
                        Target  = nameof(pictureUrl)
                    }
                };

                return(NotFound(response));
            }

            var deleteResult = DeleteFile(pictureUrl);

            if (deleteResult.Code != DeleteCode.Success)
            {
                return(BadRequest(deleteResult.ToResponse()));
            }

            var paths = GetImageUrlsForTrouble(_hostingEnvironment, troubleId);

            var troublePatchInfo = new TroublePatchInfo(TroubleConverterUtils.ConvertId(troubleId), null, null, paths,
                                                        null, null, null, null, null);
            await _troubleRepository.PatchAsync(troublePatchInfo, cancellationToken).ConfigureAwait(false);

            return(Ok(deleteResult));
        }
        public async Task <IActionResult> AddPicture(string troubleId, IFormFile picture, CancellationToken cancellationToken)
        {
            var extension = Path.GetExtension(picture.FileName);

            var dir = GetDirName(_hostingEnvironment, troubleId);

            CreateDirectoryIfNotExists(dir);
            var path = $"{dir}/{GetAvailableIdForPicture(dir)}{extension}";

            var response = await UploadPictureAsync(_hostingEnvironment, path, picture, cancellationToken);

            if (response.StatusCode == System.Net.HttpStatusCode.OK)
            {
                var paths            = GetImageUrlsForTrouble(_hostingEnvironment, troubleId);
                var troublePatchInfo = new TroublePatchInfo(TroubleConverterUtils.ConvertId(troubleId), null, null, paths,
                                                            null, null, null, null, null);

                await _troubleRepository.PatchAsync(troublePatchInfo, cancellationToken).ConfigureAwait(false);

                return(Ok(new Picture(path)));
            }
            else
            {
                return(BadRequest(response));
            }
        }
        public async Task <IActionResult> DeleteAllPictures([FromQuery] string troubleId, CancellationToken cancellationToken)
        {
            var deleteResults = DeleteAllPicturesForTrouble(_hostingEnvironment, troubleId);

            var paths = new string[] { };

            var troublePatchInfo = new TroublePatchInfo(TroubleConverterUtils.ConvertId(troubleId), null, null, paths,
                                                        null, null, null, null, null);
            await _troubleRepository.PatchAsync(troublePatchInfo, cancellationToken).ConfigureAwait(false);

            foreach (var deleteResult in deleteResults)
            {
                if (deleteResult.Code != DeleteCode.Success)
                {
                    return(BadRequest(deleteResult.ToResponse()));
                }
            }

            return(Ok(deleteResults));
        }
示例#4
0
        public Task <Trouble> PatchAsync(TroublePatchInfo patchInfo, CancellationToken cancellationToken)
        {
            if (patchInfo == null)
            {
                throw new ArgumentNullException(nameof(patchInfo));
            }

            cancellationToken.ThrowIfCancellationRequested();
            var trouble = troubles.Find(item => item.Id == patchInfo.Id).FirstOrDefault();

            if (trouble == null)
            {
                throw new TroubleNotFoundException(patchInfo.Id.ToString());
            }

            var updated = false;

            if (patchInfo.Name != null)
            {
                trouble.Name = patchInfo.Name;
                updated      = true;
            }

            if (patchInfo.Description != null)
            {
                trouble.Description = patchInfo.Description;
                updated             = true;
            }

            if (patchInfo.Latitude != null && patchInfo.Longitude != null)
            {
                trouble.Coordinates = new GeoCoordinate(patchInfo.Latitude.Value, patchInfo.Longitude.Value);
                updated             = true;
            }

            if (patchInfo.Address != null)
            {
                trouble.Address = patchInfo.Address;
                updated         = true;
            }

            if (patchInfo.Tags != null && patchInfo.Tags.Any())
            {
                trouble.Tags = patchInfo.Tags;
                updated      = true;
            }

            if (patchInfo.Status != null)
            {
                trouble.Status = patchInfo.Status.Value;
                updated        = true;
            }

            if (patchInfo.Images != null)
            {
                trouble.Images = patchInfo.Images;
                updated        = true;
            }

            if (updated)
            {
                trouble.LastUpdateAt = DateTime.UtcNow;
                troubles.ReplaceOne(item => item.Id == patchInfo.Id, trouble);
            }

            return(Task.FromResult(trouble));
        }