Esempio n. 1
0
        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"));
        }
Esempio n. 2
0
        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();
        }
Esempio n. 3
0
        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"));
            }
        }