public ActionResult Register(User u) { try { ViewData["UserName"] = Request.Form["UserName"].ToString(); ViewData["Password"] = Request.Form["Password"].ToString(); String UserName = Request.Form["UserName"].ToString(); String Password = Request.Form["Password"].ToString(); User user = null; user = userService.GetByUserName(UserName); if (user == null) { var newUser = userService.Add(new User() { Name = UserName, Password = Password}); userService.Save(); Session["LogedUserID"] = newUser.UserId.ToString(); Session["LogedUserFullName"] = newUser.Name.ToString(); return RedirectToAction("ShowNotes", "Note"); } else { ViewData["ErrorMessage"] = "Try another name"; return View(u); } } catch (NullReferenceException) { return RedirectToAction("ErrorMessage", "User"); } catch (SqlException) { return RedirectToAction("ErrorMessage", "User"); } }
public ActionResult Login(User u) { try { ViewData["UserName"] = Request.Form["UserName"].ToString(); ViewData["Password"] = Request.Form["Password"].ToString(); String UserName = Request.Form["UserName"].ToString(); String Password = Request.Form["Password"].ToString(); var user = userService.GetByUserNameAndPassword(UserName, Password); if (user != null) { Session["LogedUserID"] = user.UserId.ToString(); Session["LogedUserFullName"] = user.Name.ToString(); return RedirectToAction("ShowNotes", "Note"); } else { ViewData["ErrorMessage"] = "Invalid name or password"; return View(u); } } catch (NullReferenceException) { return RedirectToAction("ErrorMessage", "User"); } catch (SqlException) { return RedirectToAction("ErrorMessage", "User"); } }
public void Update(User user) { db.Entry(user).State = EntityState.Modified; }
public bool IsExists(User user) { var users = db.Users.Where(a => a.Name == user.Name && a.Password == user.Password).FirstOrDefault(); return users != null ? true : false; }
public User Add(User user) { return db.Users.Add(user); }
public void Add(User user, Note note) { User findUser = db.Users.Find(user.UserId); findUser.Notes.Add(note); }