コード例 #1
0
        public IActionResult Reserve([FromQuery, Required] int bookId)
        {
            // Check if the User is logged in
            if (HttpContext.Session.GetInt32("UserId") == null)
            {
                // Pass the error-message through the TempData, because the model cannot be passed
                TempData["ErrorMessage"] = $"It seems you are not logged in";

                return(RedirectToAction("Index", "Home"));
            }

            // Get the User by the Id from the Session and the Book by the bookId
            var user = _userBusinessLogic.GetUserById((int)HttpContext.Session.GetInt32("UserId"));
            var book = _bookBusinessLogic.GetBook(bookId);

            // Check if the Book exists
            if (book != null)
            {
                // Check if the User exists
                if (_userBusinessLogic.UserExists(user?.Name))
                {
                    // Try to reserve the Book
                    var reserveSuccessful = _bookBusinessLogic.ReserveBook(bookId, user.Id);

                    // Check if the Book has been reserved successfully
                    if (reserveSuccessful)
                    {
                        // Pass the success-message through the TempData, because the model cannot be passed
                        TempData["Successmessage"] = $"The book has been reserved";

                        // Prepare the model
                        var model = prepareDetailedViewModel(book);

                        return(View("Index", model));
                    }

                    // Pass the error-message through the TempData, because the model cannot be passed
                    TempData["ErrorMessage"] = $"The book \"{book.Title}\" could not be reserved";

                    return(RedirectToAction("Index", "Home"));
                }
                else
                {
                    // Pass the error-message through the TempData, because the model cannot be passed
                    TempData["ErrorMessage"] = $"There has been an unexpected Error with your session";

                    return(RedirectToAction("Index", "Home"));
                }
            }
            else
            {
                // Pass the error-message through the TempData, because the model cannot be passed
                TempData["ErrorMessage"] = $"The book with id {bookId} does not exist";

                return(RedirectToAction("Index", "Home"));
            }
        }