Пример #1
0
        public ActionResult Create(BookAuthorViewModel model)
        {
            if (ModelState.IsValid)
            {
                try
                {
                    if (model.AuthorId == -1)
                    {
                        ViewBag.Message = "Please select an author from the list";

                        return(View(GetAllAuthors()));
                    }
                    var  author = authorRepository.Find(model.AuthorId);
                    Book book   = new Book
                    {
                        Id          = model.BookId,
                        Title       = model.Title,
                        Description = model.Description,
                        Author      = author,
                    };
                    bookRepository.Add(book);
                    return(RedirectToAction(nameof(Index)));
                }

                catch
                {
                    return(View());
                }
            }

            ModelState.AddModelError("", "You have to fill all the required fields");
            return(View(GetAllAuthors()));
        }
Пример #2
0
        public ActionResult Create(BookAuthorViewModel model)
        {
            if (ModelState.IsValid)
            {
                try
                {
                    string fileName = UploadFile(model.File) ?? string.Empty;
                    if (model.AuthorId == -1)
                    {
                        ViewBag.Message = "Please select an author from the list !!";
                        return(View(GetAllAuthors()));
                    }
                    Book book = new Book
                    {
                        Id          = model.BookId,
                        Author      = authorRepository.Find(model.AuthorId),
                        Description = model.Description,
                        Title       = model.Title,
                        ImageUrl    = fileName
                    };
                    // TODO: Add insert logic here
                    bookRepository.Add(book);
                    return(RedirectToAction(nameof(Index)));
                }
                catch
                {
                    return(View());
                }
            }

            ModelState.AddModelError("", "You have to fill the required fields!!");
            return(View(GetAllAuthors()));
        }
Пример #3
0
        public ActionResult Create(BookAuthorViewModel bookModel)
        {
            if (ModelState.IsValid)
            {
                try
                {
                    string fileName = UploadFile(bookModel.File) ?? string.Empty;


                    if (bookModel.AuthorId == -1)
                    {
                        ViewBag.Message = "Please select an author from the list!";

                        return(View(LoadAuthorsListModel()));
                    }
                    var  author = authorRepo.Find(bookModel.AuthorId);
                    Book book   = new Book
                    {
                        Id          = bookModel.BookId,
                        Title       = bookModel.Title,
                        Description = bookModel.Description,
                        Author      = author,
                        ImageUrl    = fileName
                    };
                    bookRepo.Add(book);
                    return(RedirectToAction(nameof(Index)));
                }
                catch
                {
                    return(View());
                }
            }
            ModelState.AddModelError("", "Please fill all required fields");
            return(View(LoadAuthorsListModel()));
        }
Пример #4
0
 public ActionResult Create(Author author)
 {
     try
     {
         authorRepository.Add(author);
         return(RedirectToAction(nameof(Index)));
     }
     catch
     {
         return(View());
     }
 }
Пример #5
0
        public async Task <IActionResult> AddBook(BookForCreationDto bookForCreationDto)
        {
            var book = _mapper.Map <Book>(bookForCreationDto);

            _repo.Add(book);

            if (await _repo.SaveAll())
            {
                return(CreatedAtRoute("GetBooks", null));
            }

            return(BadRequest("Could not add the book"));
        }
Пример #6
0
        public ActionResult Create(Author author)
        {
            if (ModelState.IsValid)
            {
                try
                {
                    authorRepository.Add(author);
                    // TODO: Add insert logic here

                    return(RedirectToAction(nameof(Index)));
                }
                catch
                {
                    return(View());
                }
            }
            ;
            ModelState.AddModelError("", "You have to fill the required fields!");
            return(View());
        }
Пример #7
0
        public ActionResult Create(BookAuthorViewModel model)
        {
            if (ModelState.IsValid)
            {
                try
                {
                    string fileName = UploadFile(model.File) ?? string.Empty;

                    if (model.AuthorId == -1)
                    {
                        ViewBag.Message = "Please select an author";

                        return(View(GetAllAuthors()));
                    }

                    var author = authorRepository.Find(model.AuthorId);

                    // TODO: Add insert logic here
                    Book book = new Book()
                    {
                        Id          = model.Id,
                        Title       = model.Title,
                        Description = model.Description,
                        Author      = author,
                        ImageURL    = fileName
                    };
                    bookRepository.Add(book);

                    return(RedirectToAction(nameof(Index)));
                }
                catch
                {
                    return(View());
                }
            }
            else
            {
                ModelState.AddModelError("", "You have to fill all required fields");
            }
            return(View(GetAllAuthors()));;
        }
Пример #8
0
        public ActionResult Create(BookAuthorViewModel viewModel)
        {
            if (ModelState.IsValid)
            {
                try
                {
                    string fileName = UploadFile(viewModel.File) ?? string.Empty;

                    if (viewModel.AuthorId == -1)
                    {
                        ViewBag.Message = "PLease Select an author from the List ";

                        return(View(GetAllAuthors()));
                    }

                    var  author = authorRepository.Find(viewModel.AuthorId);
                    Book Book   = new Book
                    {
                        Id          = viewModel.bookId,
                        Title       = viewModel.Title,
                        Description = viewModel.Description,
                        Author      = author,
                        ImageUrl    = fileName
                    };
                    // TODO: Add insert logic here
                    bookRepository.Add(Book);
                    return(RedirectToAction(nameof(Index)));
                }
                catch
                {
                    return(View());
                }
            }

            ModelState.AddModelError("", "You have to Fill all the required fields");
            return(View(GetAllAuthors()));
        }