public IActionResult <UserProfileViewModel> Profile(AddNoteBindingModel model) { using (var context = new NotesAppContext()) { var user = context.Users.Find(model.UserId); var 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, Passsword = model.Password }; using (var context = new NotesAppContext()) { context.Users.Add(user); context.SaveChanges(); } return(View()); }
public IActionResult Login(LoginUserBindingModel model, HttpSession session, HttpResponse response) { using (var context = new NotesAppContext()) { var user = context.Users.FirstOrDefault(u => u.Username == model.Username && u.Passsword == model.Password); if (user != null) { context.Logins.Add(new Login() { SessionId = session.Id, User = user, IsActive = true }); context.SaveChanges(); Redirect(response, "/home/index"); return(null); } } return(View()); }
internal static HttpResponse CreateSession(HttpResponse response, string username, string password) { var sessionCookie = new Cookie("sid", new Random().Next().ToString()); using (var context = new NotesAppContext()) { var user = context.Users .SingleOrDefault(u => u.Username == username && u.Password == password); if (user != null) { context.Sessions.Add(new Session() { Id = sessionCookie.Value, UserId = user.Id }); } context.SaveChanges(); } response.Header.Cookies.Add(sessionCookie); return(response); }
public IActionResult Register(RegisterUserBindingModel model, HttpResponse response) { if (model.Username == null && model.Password == null) { Redirect(response, "/home/index"); return(null); } User user = new User() { Username = model.Username, Password = model.Password }; using (NotesAppContext context = new NotesAppContext()) { context.Users.Add(user); context.SaveChanges(); } return(View()); }
public IActionResult <UserProfileViewModel> Profile(AddNoteBindingModel model, HttpSession session, HttpResponse response) { if (model.Title == null || model.Content == null) { Redirect(response, "/users/login"); return(null); } using (NotesAppContext context = new NotesAppContext()) { var user = context.Users.Find(model.UserId); var note = new Note() { Title = model.Title, Content = model.Content }; user.Notes.Add(note); context.SaveChanges(); return(Profile(model.UserId, session, response)); } }