public async Task <bool> Update(Post value, CancellationToken cancellationToken = default) { var to = RawPost.From(value); var item = await Data.Posts.FindAsync(new object[] { to.Id }, cancellationToken).ConfigureAwait(false); if (item is null) { return(false); } item.Keywords = to.Keywords; item.Category = to.Category; item.AuthorId = to.AuthorId; item.Content = to.Content; item.CreationTime = to.CreationTime; item.ModificationTime = to.ModificationTime; item.Title = to.Title; item.Type = to.Type; Data.Posts.Update(item); await Data.SaveChangesAsync(cancellationToken).ConfigureAwait(false); return(true); }
public async Task <string> Create(Post value, CancellationToken cancellationToken = default) { if (string.IsNullOrWhiteSpace(value.Id)) { value.Id = Guid.NewGuid().ToString(); } Data.Posts.Add(RawPost.From(value)); await Data.SaveChangesAsync(cancellationToken).ConfigureAwait(false); return(value.Id); }
//// GET: Posts/Create //public ActionResult Create() //{ // ViewBag.AuthorRefId = new SelectList(db.Users, "Id", "FirstName"); // ViewBag.Message = User.Identity.GetUserId(); // return View(); //} // GET: Posts/Create public ActionResult Create() { ViewBag.Message = User.Identity.GetUserId(); RawPost rawPost = new RawPost(); rawPost.RawCategories = db.Categories .Select(c => new CategoryIntermediate { CategoryName = c.Name, IsSelected = false }) .ToArray(); return(View(rawPost)); }
// GET: Posts/Edit/5 public ActionResult Edit(int?id) { if (id == null) { return(new HttpStatusCodeResult(HttpStatusCode.BadRequest)); } Post post = db.Posts.Find(id); if (post == null) { return(HttpNotFound()); } RawPost rawPost = new RawPost(post, db.Categories.ToList()); return(View(rawPost)); }
public ActionResult Create([Bind(Include = "Post,RawImageURLs,RawCategories")] RawPost rawPost) { rawPost.Post.Author = db.Users.Find(rawPost.Post.AuthorRefId); if (ModelState.IsValid) { db.Posts.Add(rawPost.Post); db.Users.Find(rawPost.Post.AuthorRefId).PostsAuthored.Add(rawPost.Post); db.SaveChanges(); var latestId = db.Posts.Where(x => x.Content == rawPost.Post.Content && x.Title == rawPost.Post.Title).Max(x => x.PostId); rawPost.updatePost(latestId, db.Categories.ToList()); db.ImageLinks.AddRange(rawPost.Post.ImageURLs); db.VideoLinks.AddRange(rawPost.Post.VideoURLs); db.SaveChanges(); return(RedirectToAction("Index")); } return(View(rawPost)); }
public ActionResult Edit([Bind(Include = "Post,RawImageURLs,RawCategories,RawVideoURLs")] RawPost rawPost) { if (ModelState.IsValid) { Post post = db.Posts.Find(rawPost.Post.PostId); List <VideoLink> updatedVideoLinks = new List <VideoLink>(); if (rawPost.RawVideoURLs != null) { updatedVideoLinks = rawPost.RawVideoURLs .Split(new[] { Environment.NewLine }, StringSplitOptions.None) .ToList() .Select(str => new VideoLink { URL = str, PostRefId = rawPost.Post.PostId }) .ToList(); } List <VideoLink> oldVideos = db.VideoLinks.Where(x => x.PostRefId == rawPost.Post.PostId).ToList(); List <VideoLink> addedVideos = updatedVideoLinks.Except(oldVideos).ToList(); List <VideoLink> deletedVideos = oldVideos.Except(updatedVideoLinks).ToList(); addedVideos.ForEach(x => db.Entry(x).State = EntityState.Added); deletedVideos.ForEach(x => db.Entry(x).State = EntityState.Deleted); List <ImageLink> updatedImageLinks = new List <ImageLink>(); if (rawPost.RawImageURLs != null) { updatedImageLinks = rawPost.RawImageURLs .Split(new[] { Environment.NewLine }, StringSplitOptions.None) .ToList() .Select(str => new ImageLink { URL = str, PostRefId = rawPost.Post.PostId }) .ToList(); } List <ImageLink> oldImages = db.ImageLinks.Where(x => x.PostRefId == rawPost.Post.PostId).ToList(); List <ImageLink> addedImages = updatedImageLinks.Except(oldImages).ToList(); List <ImageLink> deletedImages = oldImages.Except(updatedImageLinks).ToList(); deletedImages.ForEach(x => db.Entry(x).State = EntityState.Deleted); addedImages.ForEach(x => db.Entry(x).State = EntityState.Added); List <String> selectedCategoriesNames = rawPost.RawCategories.Where(c => c.IsSelected).Select(c => c.CategoryName).ToList(); List <Category> selectedCategories = db.Categories.Where(cDB => selectedCategoriesNames.Contains(cDB.Name)).ToList(); var deletedCategories = post.Categories.Except(selectedCategories).ToList <Category>(); var addedCategories = selectedCategories.Except(post.Categories).ToList <Category>(); deletedCategories.ForEach(c => post.Categories.Remove(c)); foreach (Category c in addedCategories) { if (db.Entry(c).State == EntityState.Detached) { db.Categories.Attach(c); } post.Categories.Add(c); } post.Title = rawPost.Post.Title; post.Subtitle = rawPost.Post.Subtitle; post.Content = rawPost.Post.Content; post.DateOfModification = DateTime.Now; db.Entry(post).State = EntityState.Modified; db.SaveChanges(); return(RedirectToAction("Index")); } return(View(rawPost)); }
public async Task <Post> Get(string id, CancellationToken cancellationToken = default) { var item = await Data.Posts.FindAsync(new object[] { id }, cancellationToken).ConfigureAwait(false); return(RawPost.To(item)); }