Пример #1
0
        public IActionResult Edit(int?id)
        {
            if (id == null)
            {
                return(RedirectToPage("/Index"));
            }

            var pictureToEdit = this.Context.Pictures
                                .Include(a => a.Tags)
                                .Where(b => b.Id == id)
                                .FirstOrDefault();

            if (Authorized.IsAuthorizedUser(pictureToEdit.UserName, this.User.Identity.Name))
            {
                return(RedirectToPage("/Index"));
            }

            string tagsInString = "";

            foreach (var tag in pictureToEdit.Tags)
            {
                tagsInString += this.Context.Tags.Find(tag.TagId).TagName + " ";
            }

            tagsInString.Trim();

            var viewModel = new ViewEditPictureModel
            {
                Id     = (int)id,
                Title  = pictureToEdit.Title,
                Tags   = tagsInString,
                UserId = pictureToEdit.UserId,
            };

            ViewBag.ViewModel = viewModel;

            return(View());
        }
Пример #2
0
        public IActionResult Confirm()
        {
            var pictureToEdit = this.Context.Pictures.Include(a => a.Tags).Where(b => b.Id == this.Id).FirstOrDefault();

            if (Authorized.IsAuthorizedUser(pictureToEdit.UserName, this.User.Identity.Name))
            {
                return(RedirectToPage("/Index"));
            }

            pictureToEdit.Title = this.Title;

            var keywords = this.Tags
                           .Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries)
                           .ToList();

            var listOfTagsAll = this.Context.Tags;

            var listOfPictureTags = new List <PictureTag>();

            foreach (var tag in keywords)
            {
                int?tagId = 0;

                try
                {
                    tagId = this.Context.Tags.Where(a => a.TagName == tag).FirstOrDefault().Id;
                }
                catch { }

                if (pictureToEdit.Tags.Any(a => a.Tag.Id == tagId))
                {
                    continue;
                }

                var existsTag = listOfTagsAll.Where(a => a.TagName == tag).FirstOrDefault();

                if (existsTag == null)
                {
                    var newTag = new Tag
                    {
                        TagName = tag
                    };

                    var pictureTag = new PictureTag
                    {
                        Picture   = pictureToEdit,
                        PictureId = pictureToEdit.Id,
                        Tag       = newTag,
                        TagId     = newTag.Id
                    };

                    listOfPictureTags.Add(pictureTag);

                    this.Context.PictureTag.Add(pictureTag);
                    this.Context.Tags.Add(newTag);
                }
                else
                {
                    var pictureTag = new PictureTag
                    {
                        Picture   = pictureToEdit,
                        PictureId = pictureToEdit.Id,
                        Tag       = existsTag,
                        TagId     = existsTag.Id
                    };

                    listOfPictureTags.Add(pictureTag);

                    this.Context.PictureTag.Add(pictureTag);
                }
            }

            pictureToEdit.Tags = listOfPictureTags;

            this.Context.Pictures.Update(pictureToEdit);
            this.Context.SaveChanges();

            return(Redirect($"/viewImage/image/{pictureToEdit.Id}"));
        }