Beispiel #1
0
        public ActionResult Index(AddBookIndexViewModel viewModel)
        {
            var userRole = GetUserRole();

            if (userRole != "Admin" && userRole != "Creator")
            {
                return(new HttpStatusCodeResult(HttpStatusCode.Forbidden));
            }

            if (!ModelState.IsValid)
            {
                return(View(viewModel));
            }

            var Book = new Book()
            {
                Title = viewModel.Title
            };

            var bookExists = _bookRepository.CheckBookExists(Book, viewModel.Authors);

            if (bookExists)
            {
                ModelState.AddModelError("Exists", "Book already exists.");
                return(View(viewModel));
            }
            var           authorsString = _bookRepository.SplitAuthorString(viewModel.Authors);
            List <Author> authors       = new List <Author>();

            foreach (var authorString in authorsString)
            {
                var author       = _bookRepository.SplitAuthorName(authorString);
                var authorExists = _authorsRepository.CheckAuthorExists(author);
                if (!authorExists)
                {
                    Book.AddAuthor(author);
                    _authorsRepository.Add(author);
                }
                else
                {
                    var newAuthor = _authorsRepository.Get(author.FirstName, author.LastName);
                    Book.AddAuthor(newAuthor);
                }
            }
            _bookRepository.Add(Book);
            return(RedirectToAction("Index", "Home"));
        }
        private bool BookExists(int id)
        {
            bool check = dal.CheckBookExists(id);

            return(check);
        }