示例#1
0
        public async Task <IActionResult> DeletePhoto([FromRoute] int id)
        {
            Models.Photo photo = await photoRepository.GetAsync(id);

            if (photo != null)
            {
                var userName = User.FindFirstValue(ClaimTypes.Name);
                if (photo.Author.UserName != userName)
                {
                    return(Forbid());
                }

                var originalFilePath = photo.FilePath;
                this.commentRepository.DeleteAll(photo.Comments.ToList());
                this.photoRepository.Delete(photo);
                await this.unitOfWork.CompleteAsync();

                if (!string.IsNullOrEmpty(originalFilePath))
                {
                    this.photoUploadService.DeletePhoto(originalFilePath, this.uploadsFolderPath, this.outputFolderPath);
                }

                return(Ok());
            }
            else
            {
                return(NotFound());
            }
        }
示例#2
0
 public virtual void Add(Models.Photo photo)
 {
     this.context.Add(photo);
 }
示例#3
0
 public virtual void Delete(Models.Photo photo)
 {
     this.context.Remove(photo);
 }
示例#4
0
        public async Task <IActionResult> UpdatePhoto([FromRoute] int id, [FromForm] PhotoResource photoResource)
        {
            Models.Photo photo = await photoRepository.GetAsync(id);

            if (photoResource != null && photo != null)
            {
                var userName = User.FindFirstValue(ClaimTypes.Name);
                if (photo.Author.UserName != userName)
                {
                    return(Forbid());
                }
                if (string.IsNullOrEmpty(photoResource.Name))
                {
                    return(BadRequest());
                }

                if (photoResource.FileToUpload != null)
                {
                    var fileToUpload     = photoResource.FileToUpload;
                    var originalFilePath = photo.FilePath;
                    photo.FilePath = await this.photoUploadService.UploadPhoto(fileToUpload, this.uploadsFolderPath);

                    var dimensions = await this.photoRepository.GetImageDimensions(fileToUpload);

                    photo.Height = dimensions.Height;
                    photo.Width  = dimensions.Width;
                    this.photoUploadService.CopyPhoto(photo.FilePath, this.uploadsFolderPath, this.outputFolderPath);
                    if (!string.IsNullOrEmpty(originalFilePath))
                    {
                        this.photoUploadService.DeletePhoto(originalFilePath, this.uploadsFolderPath, this.outputFolderPath);
                    }
                }

                photo.Name      = photoResource.Name;
                photo.LocLng    = photoResource.LocLng;
                photo.LocLat    = photoResource.LocLat;
                photo.CenterLng = photoResource.CenterLng;
                photo.CenterLat = photoResource.CenterLat;
                photo.MapZoom   = photoResource.MapZoom;

                var currentCategories         = photo.PhotoCategories.ToList();
                var currentCategoriesIds      = currentCategories.Select(cat => cat.CategoryId);
                var strSelectedCategories     = Request.Form.ToDictionary(x => x.Key, x => x.Value.ToString()).ContainsKey("PhotoCategories") ? Request.Form.ToDictionary(x => x.Key, x => x.Value.ToString())["PhotoCategories"] : "[]";
                var selectedCategoryResources = Newtonsoft.Json.JsonConvert.DeserializeObject <IEnumerable <CategoryResource> >(strSelectedCategories) ?? new List <CategoryResource>();
                var selectedCategoryIds       = selectedCategoryResources.Select(cat => cat.Id);
                var deletedCategoryIds        = photo.PhotoCategories.Where(cat => !selectedCategoryIds.Contains(cat.CategoryId)).Select(cat => cat.CategoryId);
                var newCategoryResources      = selectedCategoryResources.Where(selectedCat => !currentCategoriesIds.Contains(selectedCat.Id));
                var newCategories             = mapper.Map <IEnumerable <CategoryResource>, IEnumerable <Models.PhotoCategory> >(newCategoryResources).ToList();
                currentCategories.RemoveAll(c => deletedCategoryIds.Contains(c.CategoryId));
                currentCategories.AddRange(newCategories);
                photo.PhotoCategories = currentCategories;

                if (photoResource.Album != null && photoResource.Album.Id > 0)
                {
                    var newAlbum = await this.albumRepository.GetAsync(photoResource.Album.Id, false);

                    if (newAlbum != null)
                    {
                        photo.Album = newAlbum;
                    }
                    else
                    {
                        return(BadRequest());
                    }
                }
                else
                {
                    photo.Album = null;
                }
                photo.ModifiedDate = DateTime.UtcNow;
                await this.unitOfWork.CompleteAsync();

                var    outputPhotoResource = mapper.Map <Models.Photo, PhotoResource>(photo);
                string orgFilePath         = outputPhotoResource.FilePath;
                outputPhotoResource.FilePath            = string.Format("{0}/{1}", this.uploadsFolderUrl, orgFilePath);
                outputPhotoResource.BoundingBoxFilePath = string.Format("{0}/{1}", this.outputFolderUrl, orgFilePath);

                return(Ok(outputPhotoResource));
            }
            else
            {
                return(NotFound());
            }
        }