public async Task <ActionResult> Create(SecretModel model) { if (!ModelState.IsValid) { return(this.View(model)); } using (var entities = new PenEntities()) { var user = await entities.Users.FirstAsync(x => x.Name == User.Identity.Name); user.Secrets.Add(new Secret() { Description = model.Description, Text = model.Text }); await entities.SaveChangesAsync(); } return(RedirectToAction("List")); }
private static async Task NewUserHandler(IOwinContext context, Func <Task> next) { var user = context.Authentication.User; if (user != null) { var username = user.Identity.Name; using (var entities = new PenEntities()) { using (var transaction = entities.Database.BeginTransaction(IsolationLevel.Serializable)) { try { var userEntity = await entities.Users.FirstOrDefaultAsync(x => x.Name == username); if (userEntity == null) { entities.Users.Add(new User() { Name = username, Status = "" }); await entities.SaveChangesAsync(); } transaction.Commit(); } catch { transaction.Rollback(); throw; } } } } await next.Invoke(); }
public async Task <ActionResult> ChangeStatus(string status = null) { using (var entities = new PenEntities()) { var user = await entities .Users .Where(x => x.Name == User.Identity.Name) .FirstAsync(); if (status == null) { return(this.View("ChangeStatus", new StatusModel { Status = user.Status })); } ; user.Status = status; await entities.SaveChangesAsync(); return(RedirectToAction("Index")); } }