コード例 #1
0
        public async Task <IActionResult> Create([Bind("Id,Password,Email,Role")] Users users)
        {
            if (ModelState.IsValid)
            {
                _context.Add(users);
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }
            return(View(users));
        }
コード例 #2
0
 public async Task <IActionResult> WriteOff(Books item)
 {
     try
     {
         _context.Update(item);
         await _context.SaveChangesAsync();
     }
     catch (DbUpdateConcurrencyException)
     {
         throw;
     }
     return(RedirectToAction(nameof(Index)));
 }
コード例 #3
0
        public async Task <IActionResult> Register(RegisterModel model)
        {
            if (ModelState.IsValid)
            {
                Users user = await db.Users.FirstOrDefaultAsync(u => u.Email == model.Email);

                if (user == null)
                {
                                        // добавляем пользователя в бд
                        user = new Users {
                        Email = model.Email, Password = model.Password, Role = "user"
                    };

                    if (user.Role != null)
                    {
                        db.Users.Add(user);
                        await db.SaveChangesAsync();
                        await Authenticate(user); // аутентификация
                    }
                    return(RedirectToAction("Index", "BooksUser"));
                }
                else
                {
                    ModelState.AddModelError("", "Некорректные логин и(или) пароль");
                }
            }
            return(View(model));
        }
コード例 #4
0
        public async Task <IActionResult> Take(int id)
        {
            var book = await _context.Books.FirstOrDefaultAsync(m => m.Id == id);

            if (book.Issued == false)
            {
                var user = await _context.Users.FirstOrDefaultAsync(u => u.Email.Equals(User.Identity.Name));

                _context.History.Add(new History(id, user.Id));
                book.Issued = true;
                await _context.SaveChangesAsync();
            }
            return(RedirectToAction("TakenBooks", "Histories"));
        }
コード例 #5
0
        public async Task <IActionResult> Return(int id)
        {
            var history = await _context.History
                          .Include(h => h.Book)
                          .Include(h => h.User)
                          .FirstOrDefaultAsync(m => m.Id == id);

            if (history.Book.Issued == true)
            {
                history.ReturnDate  = DateTime.Now;
                history.Book.Issued = false;
                await _context.SaveChangesAsync();
            }

            return(RedirectToAction("TakenBooks", "Histories"));
        }