public IActionResult Login(LoginViewModel model) { return(RedirectToAction("Error", "Account")); // ID, PW authentication if (ModelState.IsValid) { using (var db = new WebNoteDbContext()) { // LinQ - method chaining //var user = db.Users.FirstOrDefault(c => //c.Id == model.Id && c.Password == model.Password); <- model.Id wrapping! memory rick var user = db.Users.FirstOrDefault(c => c.Id.Equals(model.UserId) && c.Password.Equals(model.UserPassword)); if (user != null) { // Login Success HttpContext.Session.SetInt32("USER_LOGIN_KEY", user.No); return(RedirectToAction("LoginSuccess", "Home")); } } // Login Fail ModelState.AddModelError(string.Empty, "Invalid ID or Password"); } return(View(model)); }
// GET: /<controller>/ /// <summary> /// Note list /// </summary> /// <returns></returns> public IActionResult Index() { if (HttpContext.Session.GetInt32("USER_LOGIN_KEY") == null) { // not in login return(RedirectToAction("Login", "Account")); } using (var db = new WebNoteDbContext()) { var list = db.Notes.ToList(); return(View(list)); } }
/// <summary> /// note contents open /// </summary> /// <param name="noteNo"></param> /// <returns></returns> public IActionResult Detail(int noteNo) { if (HttpContext.Session.GetInt32("USER_LOGIN_KEY") == null) { // not in login return(RedirectToAction("Login", "Account")); } using (var db = new WebNoteDbContext()) { var note = db.Notes.FirstOrDefault(n => n.No.Equals(noteNo)); return(View(note)); } }
public IActionResult Register(User model) { return(RedirectToAction("Error", "Account")); if (ModelState.IsValid) { using (var db = new WebNoteDbContext()) // using keywork : Disposiable work [ ex) try ~ finally { Dispose() } ] { db.Users.Add(model); // ride on memory db.SaveChanges(); // real database commit } return(RedirectToAction("Index", "Home")); // refresh other page } return(View(model)); }
public IActionResult Add(Note model) { if (HttpContext.Session.GetInt32("USER_LOGIN_KEY") == null) { // not in login return(RedirectToAction("Login", "Account")); } model.UserNo = int.Parse(HttpContext.Session.GetInt32("USER_LOGIN_KEY").ToString()); if (ModelState.IsValid) { using (var db = new WebNoteDbContext()) { db.Notes.Add(model); if (db.SaveChanges() > 0) { return(Redirect("Index")); } } ModelState.AddModelError(string.Empty, "Can't save note."); } return(View(model)); }