/// <summary> /// convert to user entity then save changes. /// </summary> /// <param name="user"></param> /// <returns></returns> public async Task <bool> UpdateUserAsync(int id, User user) { // make sure the user can be converted var userEntity = DbEntityConverter.ToUserEntity(user); var entities = _context.UserEntities; if (id < 1 || !entities.Any() || id > entities.Max(u => u.Id)) { throw new ArgumentException($"{id} is not a valid id."); } UserEntity entity = await entities.FindAsync(id); // assign all the values { entity.Status = userEntity.Status; entity.FirstName = userEntity.FirstName; if (!userEntity.Email.IsNullOrEmpty()) { entity.Email = userEntity.Email; } entity.LastName = userEntity.LastName; } // save changes. _context.SaveChanges(); return(true); }
public ActionResult Delete(Thread _thread) { using (FakebookContext Fk = new FakebookContext()) { Thread thread = Fk.Threads.First(x => x.Id == _thread.Id); Fk.Threads.Remove(thread); Fk.SaveChanges(); return(RedirectToAction("Index")); } }
public ActionResult AddComment(DetailThread _detailThread) { using (FakebookContext Fk = new FakebookContext()) { _detailThread.Comment.IdThread = _detailThread.Thread.Id; Fk.Comments.Add(_detailThread.Comment); Fk.SaveChanges(); return(RedirectToAction("Detail", "Thread", new{ _id = _detailThread.Thread.Id })); } }
public ActionResult AddOrEdit(ThreadCRU _threadCRUD) { Thread _thread = _threadCRUD.Thread; using (FakebookContext Fk = new FakebookContext()) { if (Fk.Threads.Any(x => x.Id == _thread.Id)) { Thread threadEdit = Fk.Threads.First(x => x.Id == _thread.Id); threadEdit.Title = _thread.Title; threadEdit.Description = _thread.Description; threadEdit.IdCategory = _thread.IdCategory; Fk.SaveChanges(); } else { Fk.Threads.Add(_thread); Fk.SaveChanges(); } return(RedirectToAction("Index")); } }