Пример #1
0
 public IActionResult Add(Book newBook)
 {
     if (HttpContext.Session.GetString("_USERNAME") != null && HttpContext.Session.GetString("_USERNAME") != "")
     {
         TempData["Login_Error"] = "You must sign in first.";
         return(RedirectToAction("Login", "User"));
     }
     if (ModelState.IsValid)
     {
         try{
             using (context)
             {
                 var currBook = context.Books.Include("Authors").Single(book => book.Title == newBook.Title && book.Authors == newBook.Authors);
                 if (currBook != null)
                 {
                     //we found a result, so the book exists
                     ViewBag.ErrorMsg = "A book with that title already exists.";
                     return(View());
                 }
                 //there was no book found so we can add this new book and save it
                 context.Books.Add(newBook);
                 context.SaveChanges();
             }
         }catch (Exception e)
         {
             ViewBag.ErrorMsg = "Unable to add a new book at this time.";
             return(View());
         }
     }
     return(View());
 }
Пример #2
0
 public IActionResult Register(User newUser)
 {
     if (ModelState.IsValid)
     {
         string plainPassword = newUser.Password;
         string hashPassword  = BCrypt.Net.BCrypt.HashPassword(plainPassword);
         newUser.Password = hashPassword;
         using (context)
         {
             context.Users.Add(newUser);
             context.SaveChanges();
         }
         ModelState.Clear();
         ViewBag.Message = String.Format("{0} {1} successfully registered!", newUser.FirstName, newUser.LastName);
     }
     return(View());
 }