public async Task <IActionResult> UpdateImage(ImagePostUpdateModel input)
        {
            if (!this.ModelState.IsValid)
            {
                input.LocationsDropDown = this.locationsService.GetAllLocationsAsKeyValuePair();
                input.CamerasDropDown   = this.cameraService.GetAllCamerasAsKeyValuePair();
                input.TagsDropDown      = this.tagsService.GetAllTagsAsKeyValuePair();

                return(this.View(input));
            }

            await this.postsService.UpdateImage(input);

            return(this.Redirect($"/PhotoPosts/PhotosNewest"));
        }
Example #2
0
        public async Task UpdateImage(ImagePostUpdateModel input)
        {
            var image = this.postRepository.All().FirstOrDefault(x => x.Id == input.PhotoId);

            if (image.Caption != input.Caption)
            {
                image.Caption = input.Caption;
            }

            image.Camera = await this.cameraService.GetCameraByNameAsync(input.Camera);

            var country = await this.countriesService.GetCountry(input.Country);

            image.Location = await this.locationsService.GetLocation(input.Location, country);

            var tags = await this.tagsService.GetTagsForPost(input.Tags);

            var postTags = this.postTagsRepository.All().Where(x => x.PostId == input.PhotoId);

            foreach (var tag in postTags)
            {
                this.postTagsRepository.Delete(tag);
            }

            await this.postTagsRepository.SaveChangesAsync();

            foreach (var tag in tags)
            {
                var postTag = new PostTag
                {
                    PostId = image.Id,
                    TagId  = tag.Id,
                };

                image.Tags.Add(postTag);
            }

            await this.postRepository.SaveChangesAsync();
        }