예제 #1
0
        public async Task <IActionResult> Edit(ImageEditVM viewModel)
        {
            if (!ModelState.IsValid)
            {
                return(View(viewModel));
            }

            await service.UpdateImage(viewModel);

            return(RedirectToAction(nameof(GalleryController.Index), Gallery));
        }
예제 #2
0
        public async Task UpdateImage(ImageEditVM viewModel)
        {
            Image image = context.Image.Include(o => o.TagToImage).Single(o => o.Id == viewModel.ImageId);

            image.Title     = viewModel.Title;
            image.GalleryId = viewModel.GalleryId;

            if (!string.IsNullOrWhiteSpace(viewModel.Tags))
            {
                var tags = await context.Tag.ToListAsync();

                foreach (var tag in viewModel.Tags.Split(","))
                {
                    var tagName         = tag.Trim();
                    var tagToBeInserted = tags.SingleOrDefault(t => t.Name == tagName);
                    var tagToImage      = new TagToImage();

                    if (tagToBeInserted == null)
                    {
                        tagToImage.Tag = new Tag {
                            Name = tagName
                        };
                        tagToImage.Image = image;

                        image.TagToImage.Add(tagToImage);
                    }
                    else
                    {
                        if (image.TagToImage.Count(o => o.ImageId == image.Id && o.TagId == tagToBeInserted.Id) == 0)
                        {
                            tagToImage.Tag   = tagToBeInserted;
                            tagToImage.Image = image;

                            image.TagToImage.Add(tagToImage);
                        }
                        tags.Remove(tagToBeInserted);
                    }
                }

                var tagsToRemove = tags.Where(o => o.TagToImage.Any(t => t.ImageId == image.Id)).SelectMany(a => a.TagToImage);
                if (tagsToRemove != null)
                {
                    context.TagToImage.RemoveRange(tagsToRemove);
                }
            }

            await context.SaveChangesAsync();

            await RemoveUnusedTags();
        }
예제 #3
0
        public async Task <IActionResult> Edit(int id)
        {
            var image = await service.GetSingleImage(id);

            var tags = await service.GetTagsForImage(id);

            ImageEditVM viewModel = new ImageEditVM
            {
                Title     = image.Title,
                ImageId   = image.Id,
                GalleryId = image.GalleryId,
                ImageUrl  = image.Url,
                Tags      = string.Join(", ", tags.Select(t => t.Name)),
                Galleries = service.GetAllGalleries(),
            };

            return(View(viewModel));
        }