// DELETE api/<controller>/5 /// <summary> /// Deletes the specified id. /// </summary> /// <param name="id">The id.</param> public void Delete(int id) { using (var dataContext = new DataContext()) { var item = dataContext.Tasks.FirstOrDefault(x => x.Id == id && x.User.UserName == this.User.Identity.Name); if (item == null) return; dataContext.Tasks.Remove(item); dataContext.SaveChanges(); } }
public ActionResult Index(int id, TaskModel model) { using (var dataContext = new DataContext()) { var item = dataContext.Tasks.FirstOrDefault(x => x.Id == model.Id && x.User.UserName == User.Identity.Name); if (item == null) return new HttpStatusCodeResult(HttpStatusCode.NotFound); item.Title = model.Title; item.Completed = model.Completed; dataContext.SaveChanges(); return new HttpStatusCodeResult(HttpStatusCode.OK); } }
public ActionResult Index(int id) { using (var dataContext = new DataContext()) { var item = dataContext.Tasks.FirstOrDefault(x => x.Id == id && x.User.UserName == User.Identity.Name); if (item != null) { dataContext.Tasks.Remove(item); dataContext.SaveChanges(); } return new HttpStatusCodeResult(HttpStatusCode.OK); } }
// POST /Tasks /// <summary> /// Posts the specified model. /// </summary> /// <param name="model">The model.</param> public void Post(TaskModel model) { using (var dataContext = new DataContext()) { var item = new Task { Title = model.Title, Completed = model.Completed, User = dataContext.UserProfiles.AsNoTracking().First(x => x.UserName == this.User.Identity.Name) }; dataContext.Tasks.Add(item); dataContext.SaveChanges(); } }
public ActionResult Index(TaskModel model) { using (var dataContext = new DataContext()) { var item = new Task { Title = model.Title, Completed = model.Completed, User = dataContext.UserProfiles.AsNoTracking().First(x => x.UserName == User.Identity.Name) }; dataContext.Tasks.Add(item); dataContext.SaveChanges(); return Json(item, JsonRequestBehavior.AllowGet); } }
public ActionResult ExternalLoginConfirmation(RegisterExternalLoginModel model, string returnUrl) { string provider = null; string providerUserId = null; if (User.Identity.IsAuthenticated || !OAuthWebSecurity.TryDeserializeProviderUserId(model.ExternalLoginData, out provider, out providerUserId)) { return RedirectToAction("Manage"); } if (ModelState.IsValid) { // Insert a new user into the database using (DataContext db = new DataContext()) { UserProfile user = db.UserProfiles.FirstOrDefault(u => u.UserName.ToLower() == model.UserName.ToLower()); // Check if user already exists if (user == null) { // Insert name into the profile table db.UserProfiles.Add(new UserProfile { UserName = model.UserName }); db.SaveChanges(); OAuthWebSecurity.CreateOrUpdateAccount(provider, providerUserId, model.UserName); OAuthWebSecurity.Login(provider, providerUserId, createPersistentCookie: false); return RedirectToLocal(returnUrl); } else { ModelState.AddModelError("UserName", "User name already exists. Please enter a different user name."); } } } ViewBag.ProviderDisplayName = OAuthWebSecurity.GetOAuthClientData(provider).DisplayName; ViewBag.ReturnUrl = returnUrl; return View(model); }
// PUT api/<controller>/5 /// <summary> /// Puts the specified id. /// </summary> /// <param name="id">The id.</param> /// <param name="model">The model.</param> /// <exception cref="System.Web.Http.HttpResponseException"></exception> public void Put(int id, TaskModel model) { using (var dataContext = new DataContext()) { var item = dataContext.Tasks.FirstOrDefault(x => x.Id == model.Id && x.User.UserName == this.User.Identity.Name); if (item == null) throw new HttpResponseException(HttpStatusCode.NotFound); item.Title = model.Title; item.Completed = model.Completed; dataContext.SaveChanges(); } }