Exemple #1
0
        public async Task <bool> Update(Post post, CancellationToken cancellationToken)
        {
            try
            {
                _context.Entry(post).State = EntityState.Modified;
                await _context.SaveChangesAsync();

                return(true);
            }
            catch
            {
                return(false);
            }
        }
        public async Task <IActionResult> PutCategory([FromRoute] Guid id, [FromBody] Category category)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            if (id != category.Id)
            {
                return(BadRequest());
            }

            _context.Entry(category).State = EntityState.Modified;

            try
            {
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!CategoryExists(id))
                {
                    return(NotFound());
                }
                else
                {
                    throw;
                }
            }

            return(NoContent());
        }
        public ActionResult ArticleAdd(ArticleAddViewModel vm, List <HttpPostedFileBase> files, List <TagCheck> TagsListChecked)
        {
            if (vm.Article.ArticleId > 0)
            {
                //edycja newsa
                vm.Article.DateOfPublish = DateTime.Parse(Request.Form["DateOfPublish"]);
                vm.Article.AuthorId      = User.Identity.GetUserId();
                vm.Article.Text          = Server.HtmlEncode(vm.Article.Text);

                string tagLine = "";
                if (vm.TagsListChecked != null)
                {
                    foreach (var tagChecked in vm.TagsListChecked)
                    {
                        if (tagChecked.Value == true)
                        {
                            if (tagLine == "")
                            {
                                tagLine = tagChecked.Tag.Name;
                            }
                            else
                            {
                                tagLine = tagLine + "," + tagChecked.Tag.Name;
                            }
                        }
                    }
                }
                if (vm.Tags != null)
                {
                    vm.Tags = vm.Tags + "," + tagLine;
                }
                else
                {
                    vm.Tags = tagLine;
                }

                if (vm.Tags != null)
                {
                    var tags = vm.Tags.Split(',');
                    foreach (var tag in tags)
                    {
                        if (db.ArticlesTags.Where(n => n.Name == tag).Count() == 0)
                        {
                            db.ArticlesTags.Add(new ArticlesTag {
                                Name = tag
                            });
                            db.SaveChanges();
                        }
                        ArticlesTag artTag = db.ArticlesTags.Where(n => n.Name == tag).First();
                        if (artTag.Name != "" || artTag.Name != " ")
                        {
                            db.ArticlesTagsConnections.Add(new ArticlesTagsConnection {
                                ArticleId = vm.Article.ArticleId, ArticlesTagId = artTag.ArticlesTagId
                            });
                            db.SaveChanges();
                        }
                    }
                }
                db.Entry(vm.Article).State = System.Data.Entity.EntityState.Modified;
                db.SaveChanges();
                return(RedirectToAction("ArticleAdd", new { confirmation = true }));
            }
            else
            {
                //dodawanie nowego newsa
                if (files != null && files.Count > 0) // != &&
                {
                    if (vm.Article.Title == null)
                    {
                        ModelState.AddModelError("", "Wpisz TYTUŁ.");
                    }
                    if (vm.Article.Text == null)
                    {
                        ModelState.AddModelError("", "Wpisz jakiś TEKST.");
                    }
                    if (vm.Article.Title == null || vm.Article.Text == null)
                    {
                        var articlesCategories = db.ArticlesCategories.ToList();
                        vm.ArticlesCategories = articlesCategories;
                        return(View(vm));
                    }
                    else
                    {
                        if (ModelState.IsValid)
                        {
                            //zapisywanie artykułu
                            vm.Article.DateOfPublish   = DateTime.Parse(Request.Form["DateOfPublish"]);
                            vm.Article.Visible         = vm.Article.Visible;
                            vm.Article.AuthorId        = User.Identity.GetUserId();
                            vm.Article.Text            = Server.HtmlEncode(vm.Article.Text);
                            vm.Article.IntroText       = Server.HtmlEncode(vm.Article.IntroText);
                            db.Entry(vm.Article).State = System.Data.Entity.EntityState.Added;
                            db.SaveChanges();
                            //////////////////////////////////

                            //zapisywanie tagów
                            string tagLine = "";
                            if (vm.TagsListChecked != null)
                            {
                                foreach (var tagChecked in vm.TagsListChecked)
                                {
                                    if (tagChecked.Value == true)
                                    {
                                        if (tagLine == "")
                                        {
                                            tagLine = tagChecked.Tag.Name;
                                        }
                                        else
                                        {
                                            tagLine = tagLine + "," + tagChecked.Tag.Name;
                                        }
                                    }
                                }
                            }
                            if (vm.Tags != null)
                            {
                                vm.Tags = vm.Tags + "," + tagLine;
                            }
                            else
                            {
                                vm.Tags = tagLine;
                            }

                            if (vm.Tags != null)
                            {
                                var tags = vm.Tags.Split(',');
                                foreach (var tag in tags)
                                {
                                    if (tag.Length > 0)
                                    {
                                        if (db.ArticlesTags.Where(n => n.Name == tag).Count() == 0)
                                        {
                                            db.ArticlesTags.Add(new ArticlesTag {
                                                Name = tag
                                            });
                                            db.SaveChanges();
                                        }
                                        ArticlesTag artTag = db.ArticlesTags.Where(n => n.Name == tag).First();
                                        db.ArticlesTagsConnections.Add(new ArticlesTagsConnection {
                                            ArticleId = vm.Article.ArticleId, ArticlesTagId = artTag.ArticlesTagId
                                        });
                                        db.SaveChanges();
                                    }
                                }
                            }
                            /////////////////////////////

                            //zapisywanie zdjec
                            var photo = new ArticlesPhoto();
                            int i     = 0;
                            foreach (var file in files)
                            {
                                var fileExt  = Path.GetExtension(file.FileName);
                                var filename = Guid.NewGuid() + fileExt;
                                var path     = Path.Combine(Server.MapPath(AppConfig.ArticlesImagesFolderRelated), filename);
                                file.SaveAs(path);

                                photo.ArticleId = vm.Article.ArticleId;
                                photo.FilePath  = filename;
                                photo.OrderNo   = i;
                                photo.Width     = 1300;
                                photo.Height    = 722;
                                db.ArticlesPhotos.Add(photo);
                                db.SaveChanges();
                                i++;
                            }
                            /////////////////////////////////////////////////////////////////////////////////////////

                            return(RedirectToAction("ArticleAdd", new { confirmation = true }));
                        }
                        else
                        {
                            var articleCategories = db.ArticlesCategories.ToList();
                            vm.ArticlesCategories = articleCategories;
                            return(View(vm));
                        }
                    }
                }
                else
                {
                    ModelState.AddModelError("", "Nie wskazano pliku.");
                    var articleCategories = db.ArticlesCategories.ToList();
                    vm.ArticlesCategories = articleCategories;
                    return(View("ArticleAdd", vm));
                }
            }
        }