public IActionResult <IEnumerable <AllUsernamesViewModel> > All(HttpSession session, HttpResponse response)
        {
            if (!this.singInManager.IsAuthenticated(session))
            {
                Redirect(response, "/users/login");
                return(null);
            }
            List <User> users = null;

            using (var context = new NotesApplicationContext())
            {
                users = context.Users.ToList();
            }

            List <AllUsernamesViewModel> models = new List <AllUsernamesViewModel>();

            foreach (User user in users)
            {
                models.Add(new AllUsernamesViewModel()
                {
                    UserId   = user.Id,
                    Username = user.Username
                });
            }

            return(View(models.AsEnumerable()));
        }
        public void LogOut(HttpSession session)
        {
            using (var context = new NotesApplicationContext())
            {
                Login login = context.Logins.Where(s => s.SessionId == session.Id).FirstOrDefault();

                context.Logins.Remove(login);
                context.SaveChanges();
            }
        }
 public IActionResult <UserProfileViewModel> Profile(AddNoteBindingModel model)
 {
     using (var context = new NotesApplicationContext())
     {
         User user = context.Users.Find(model.UserId);
         Note note = new Note()
         {
             Title   = model.Title,
             Content = model.Content
         };
         user.Notes.Add(note);
         context.SaveChanges();
     }
     return(Profile(model.UserId));
 }
        public IActionResult Register(RegisterUserBindingModel model)
        {
            var user = new User()
            {
                Username = model.Username,
                Password = model.Password
            };

            using (var context = new NotesApplicationContext())
            {
                context.Users.Add(user);
                context.SaveChanges();
            }

            return(View());
        }
 public IActionResult <UserProfileViewModel> Profile(int id)
 {
     using (var context = new NotesApplicationContext())
     {
         User user = context.Users.Find(id);
         UserProfileViewModel model = new UserProfileViewModel()
         {
             UserId   = user.Id,
             Username = user.Username,
             Notes    = user.Notes.Select(x => new NoteViewModel()
             {
                 Title   = x.Title,
                 Content = x.Content
             })
         };
         return(View(model));
     }
 }
        public IActionResult <AllUsersIdUsernameViewModel> All(HttpSession session)
        {
            if (!signInManager.IsAuthenticated(session))
            {
                return(Redirect(new AllUsersIdUsernameViewModel(), "../users/login"));
            }

            Dictionary <string, string> users = null;

            using (var context = new NotesApplicationContext())
            {
                users = context.Users.Select(u => new { u.Id, u.Username }).ToDictionary(u => u.Id.ToString(), u => u.Username);
            }

            var viewModel = new AllUsersIdUsernameViewModel()
            {
                Users = users
            };

            return(View(viewModel));
        }
        public IActionResult Login(LoginUserBindingModel model, HttpSession session)
        {
            string username     = model.Username;
            string password     = model.Password;
            string sessionId    = session.Id;
            bool   loginSuccess = false;

            // Validate such user with password exist in db.
            using (var context = new NotesApplicationContext())
            {
                var user = context.Users.Where(u => (u.Username == username && u.Password == password)).FirstOrDefault();

                if (user != null)
                {
                    loginSuccess = true;

                    Login login = new Login()
                    {
                        SessionId = sessionId,
                        User      = user,
                        IsActive  = true
                    };

                    // Add login to db.
                    context.Logins.Add(login);
                    context.SaveChanges();
                }
            }

            if (loginSuccess == true)
            {
                return(Redirect("../home/index"));
            }

            return(View());
        }
        public IActionResult Login(LoginUserBindingModel model, HttpSession session, HttpResponse response)
        {
            string username  = model.Username;
            string password  = model.Password;
            string sessionId = session.Id;

            using (var context = new NotesApplicationContext())
            {
                User user = context.Users.FirstOrDefault(u => u.Username == username && u.Password == password);
                if (user != null)
                {
                    Login login = new Login()
                    {
                        User = user, SessionId = sessionId
                    };
                    context.Logins.Add(login);
                    context.SaveChanges();
                    Redirect(response, "/home/index");
                    return(null);
                }
            }

            return(this.View());
        }
 public SingInManager(NotesApplicationContext context)
 {
     this.dbContext = context;
 }