Esempio n. 1
0
        public IActionResult EditPost(int id)
        {
            PostDetail_ViewModel model = new PostDetail_ViewModel();

            model.post               = unitOfWork.Posts.FirsOrDefault(k => k.postId == id);
            model.Categories         = unitOfWork.Categories.GetAll().ToList();
            model.SelectedCategories = unitOfWork.PostCategories.Find(k => k.postId == id).Select(k => k.categoryId).ToList();
            model.Tags               = unitOfWork.Tags.GetAll().ToList();
            model.SelectedTags       = unitOfWork.PostTags.Find(k => k.postId == id).Select(k => k.tagId).ToList();
            return(View(model));
        }
Esempio n. 2
0
        public IActionResult PostDetail(int id)
        {
            //return View(unitOfWork.Posts.Get(id));

            PostDetail_ViewModel model = new PostDetail_ViewModel();

            model.post       = unitOfWork.Posts.Find(k => k.postId == id).Include(k => k.Comments).ThenInclude(k => k.User).FirstOrDefault();
            model.Categories = unitOfWork.PostCategories.Find(k => k.postId == model.post.postId)
                               .Select(k => k.Category).ToList();
            model.Tags = unitOfWork.PostTags.Find(k => k.postId == model.post.postId).Select(k => k.Tag).ToList();

            //GÖRÜNTÜLENME SAYISI
            model.post.Views++;
            unitOfWork.SaveChanges();
            return(View(model));
        }
Esempio n. 3
0
        public IActionResult EditPost(PostDetail_ViewModel model, IFormFile file, int[] categories, string[] tags)
        {
            if (ModelState.IsValid)
            {
                var entity = model.post;

                if (file != null)
                {
                    var path = Path.Combine(Directory.GetCurrentDirectory(), "adminmaster\\images", file.FileName);
                    entity.postImage = file.FileName;
                }
                entity.dateAdded = DateTime.Now;
                unitOfWork.Posts.Edit(entity);
                unitOfWork.SaveChanges();
                //Bu Post ID'si İle Categorileme Yapalım:
                foreach (int cat in categories)
                {
                    //bu kategori database de var mı
                    Category dbCat = unitOfWork.Categories.Get(k => k.categoryId == cat);

                    if (dbCat != null)
                    {
                        //database de bu kategori ile ilişkili bir post var mı
                        var dbPostCat = unitOfWork.PostCategories.Get(k => k.postId == entity.postId);

                        // 1 - hiç yok >> ekle
                        if (dbPostCat == null)
                        {
                            unitOfWork.PostCategories.Add(new PostCategory {
                                categoryId = cat, postId = entity.postId
                            });
                            unitOfWork.SaveChanges();
                        }
                        // 2 - varsa >> cat id-ler farklıysa >> eskisini sil, yeni ekle
                        else if (dbPostCat != null && dbPostCat.categoryId != cat)
                        {
                            unitOfWork.PostCategories.Delete(dbPostCat);
                            unitOfWork.PostCategories.Add(new PostCategory {
                                categoryId = cat, postId = entity.postId
                            });
                            unitOfWork.SaveChanges();
                        }
                        // 3 - varsa >> cat eşitse >> hiçbir yapma
                    }
                    else
                    {
                        //seeddata dışından bir kategori eklenmeye çalışılırsa
                        return(RedirectToAction("Error"));
                    }
                }
                //Bu Post ID'si İle Etiketleme Yapalım:
                foreach (string tag in tags)
                {
                    Tag dbTag = unitOfWork.Tags.Get(k => k.tagName == tag.ToLower().Trim());
                    if (dbTag == null)
                    {
                        //etiket database de yoksa yeni etiket  oluştur
                        dbTag         = new Tag();
                        dbTag.tagName = tag.ToLower().Trim();
                        unitOfWork.Tags.Add(dbTag);
                        unitOfWork.SaveChanges();
                    }

                    PostTag pTag = unitOfWork.PostTags.Get(k => k.postId == entity.postId && k.tagId == dbTag.tagId);
                    if (pTag == null)
                    {
                        //etiket varsa ekle ve tagid ile postid eşitle:
                        unitOfWork.PostTags.Add(new PostTag
                        {
                            tagId  = dbTag.tagId,
                            postId = entity.postId
                        });
                        unitOfWork.SaveChanges();
                    }
                }
                return(RedirectToAction("Index"));
            }
            return(RedirectToAction("Index"));
        }