public ActionResult <string> AddBook(BookDTO bDto) { var book = LibraryAPI.Core.Domain.Book.AddBook(bDto.name, bDto.userId); _bookrepo.AddBook(book); return(book.bookName); }
public IActionResult Index(HomeViewModel model) { //Only add record if data is valid if (ModelState.IsValid) { //Perform method to add book to database then redirect to Record Added action method var result = _bookRepository.AddBook(model.Book); if (result == true) { return(RedirectToAction("RecordAdded")); } else { return(RedirectToAction("DupeBook")); } } //If record is not added because of validation errors, you stay on the same screen. Must get all books for table again //Use LINQ to get Books var books = from b in _bookRepository.GetAllBooks() select b; //Contruct view model and set book values and publisher dropdown values var publishers = _bookRepository.GetAllPublishers(); HomeViewModel HomeViewmodel = new HomeViewModel { Books = books.ToList(), Publishers = _bookRepository.SelectList(publishers) }; return(View(HomeViewmodel)); }
public async Task <IActionResult> AddBook([FromBody] BookModels _model) { var id = await _bookRepository.AddBook(_model); return(Ok(id)); //return CreatedAtAction("GetBookById",new { id = id,Controller = "BookApi" } , id); }
public IActionResult Create(BookViewModel model) { if (!ModelState.IsValid) { ViewModel.Book = model.Book; return(View(ViewModel)); } //_db.Books.Add(model.Book); _repository.AddBook(model.Book); //await _db.SaveChangesAsync(); var RootDirectory = _hostingEnvironment.WebRootPath; var files = HttpContext.Request.Form.Files; if (files.Count > 0) { var extension = Path.GetExtension(files[0].FileName); var filePath = Path.Combine(DataContext.ImageDirectory, model.Book.BookId + extension); using (var fileStream = new FileStream(Path.Combine(RootDirectory, filePath), FileMode.Create)) { files[0].CopyTo(fileStream); } model.Book.ImagePath = DataContext.ImageSource + model.Book.BookId + extension; } else { //_db.Attach(model.Book); model.Book.ImagePath = DataContext.ImageSource + DataContext.DefaultImage; } //await _db.SaveChangesAsync(); _repository.UpdateBook(model.Book); return(RedirectToAction(nameof(Index))); }
public BookMutation(IBookRepository bookRepository, IAuthorRepository authorRepository) { FieldAsync <BookType>("createBook", arguments: new QueryArguments(new QueryArgument <NonNullGraphType <BookInputType> > { Name = "book" }), resolve: async context => { var book = context.GetArgument <Book>("book"); await bookRepository.AddBook(book); return(book); }); FieldAsync <AuthorType>("createAuthor", arguments: new QueryArguments(new QueryArgument <NonNullGraphType <AuthorInputType> > { Name = "author", Description = "Create single author." }), resolve: async context => { var author = context.GetArgument <Author>("author"); await authorRepository.CreateAuthor(author); return(author); }); //FieldAsync<AuthorType>("craeteAuthorWithBooks", // arguments: new QueryArguments(new QueryArgument<NonNullGraphType<AuthorWithBooksInputType>> { Name = "author", Description = "Create a single author with or without books." }), // resolve: async context => // { // var author = context.GetArgument<Author>("author"); // await authorRepository.CreateAuthor(author); // return author; // }); }
public BookMutation(IBookRepository bookRepository) { Field <BookType>( "addBook", arguments: new QueryArguments( new QueryArgument <NonNullGraphType <BookInputType> > { Name = "book" }), resolve: ctx => { var todo = ctx.GetArgument <Book>("book"); return(bookRepository.AddBook(todo)); }); Field <BookType>( "removeBook", arguments: new QueryArguments( new QueryArgument <IntGraphType> { Name = "id" }), resolve: ctx => { var id = ctx.GetArgument <int>("id"); return(bookRepository.DeleteBook(id)); }); }
public ActionResult Create(IFormCollection collection) { var newBook = new Domain.Models.Book(); if (ModelState.IsValid) { newBook.Title = collection["Title"]; newBook.AuthorFirstName = collection["AuthorFirstName"]; newBook.AuthorLastName = collection["AuthorLastName"]; newBook.Imagelink = collection["ImageLink"]; newBook.ISBN = collection["ISBN"]; newBook.Price = decimal.Parse(collection["Price"]); newBook.Genre = new Domain.Models.Genre { ID = Int32.Parse(collection["GenreList"]) }; } try { _bookrepository.AddBook(newBook); return(RedirectToAction(nameof(Index))); } catch { return(RedirectToAction(nameof(Create), newBook)); } }
public async Task <IActionResult> Create(CreateBookViewModel model) { if (ModelState.IsValid) { string uniqueFileName = null; if (model.Photo != null) { string uploadsFolder = Path.Combine(_webHostEnvironment.WebRootPath, "Images", "Uploads"); uniqueFileName = Guid.NewGuid().ToString() + "_" + model.Photo.FileName; string filePath = Path.Combine(uploadsFolder, uniqueFileName); model.Photo.CopyTo(new FileStream(filePath, FileMode.Create)); } Book book = new Book { Title = model.Title, Author = model.Author, Format = model.Format, PubYear = model.PubYear, Condition = model.Condition, ImageUrl = uniqueFileName }; var currentUser = await _userManager.FindByNameAsync(User.Identity.Name); book.appUserId = currentUser.Id; repo.AddBook(book); return(RedirectToAction(nameof(Index))); } return(View(model)); }
public async Task <AddBookCommandResponseViewModel> Handle(AddBookCommand request, CancellationToken cancellationToken) { AddBookCommandResponseViewModel result = new AddBookCommandResponseViewModel(); if (!request.ValidateAuthorGuid()) { return(result); } Author author = await _authorRepository.GetAuthorById(Guid.Parse(request.AuthorId)); if (!request.ValidateAuthorNotNul(author)) { return(result); } Book book = new Book(Guid.NewGuid(), request.Title, request.ReleaseYear, request.Edition, request.ISBN, author); if (!book.Validate()) { return(result); } await _bookRepository.AddBook(book); result.BookId = book.BookId; result.Title = book.Title; result.ReleaseYear = book.ReleaseYear; result.Edition = book.Edition; result.ISBN = book.ISBN; result.AuthorName = book.Author.Name; return(result); }
public async Task <ActionResult <Book> > AddBook(Book newBook) { try { if (newBook == null) { return(BadRequest()); } var checkBook = await bookRepository.CheckIfBookExists(newBook); if (checkBook != null) { ModelState.AddModelError("name", "Error!,Book name already exists.. if you still want to add it please Update the existing Book First "); return(BadRequest(ModelState)); } var result = await bookRepository.AddBook(newBook); return(CreatedAtAction(nameof(GetBook), new { id = newBook.id }, newBook)); } catch (Exception) { return(StatusCode(StatusCodes.Status500InternalServerError, "Error retrieving data from the database")); } }
private void AddNewTitle() { Console.Write("Enter author's name: "); var firstName = Console.ReadLine(); Console.Write("Enter author's last name: "); var lastName = Console.ReadLine(); Author newAuthor = new Author(); newAuthor.FirstName = firstName; newAuthor.LastName = lastName; _authorRepository.AddAuthor(newAuthor); Console.Write("Enter book's title: "); var title = Console.ReadLine(); Console.Write("Enter book's genre: "); var genre = Console.ReadLine(); var authorId = _authorRepository.GetAllAuthors().Count(); Book newBook = new Book(); newBook.Title = title; newBook.Genre = genre; newBook.AuthorID = authorId; _bookRepository.AddBook(newBook); Console.WriteLine("The new title has been added successfully"); }
public async Task <IActionResult> AddBook(BookForCreation bookForCreation) { if (bookForCreation == null) { throw new ArgumentNullException(nameof(bookForCreation)); } //Mapping BookForCreation object to actual Book entity object var bookEntity = _mapper.Map <Entities.Book>(bookForCreation); //Call repository service to add the book _bookRepository.AddBook(bookEntity); //Async call to Save the book to database/store await _bookRepository.SaveChangesAsync(); //Author won't be refreshed here //Fetch (refresh) the book from the data store, including the author await _bookRepository.GetBookAsync(bookEntity.Id); //Generate the "201" status code along with "Location" header value containing URI pointed for created author return(CreatedAtRoute( "GetBook", new { id = bookEntity.Id }, bookEntity)); }
public async Task <IActionResult> PostLibraryInfo([FromBody] AdminBookInfoDto newBook) { try { var newBookEntity = _mapper.Map <Book>(newBook.Book); _bookRepository.AddBook(newBookEntity); await _bookRepository.Save(); foreach (var lib in newBook.Libraries) { lib.BookId = newBookEntity.Id; } var newLibraryEntities = _mapper.Map <IEnumerable <Library> >(newBook.Libraries); foreach (var library in newLibraryEntities) { if (library.BookCount != 0) { _libraryRepository.AddLibrary(library); } } await _libraryRepository.Save(); return(StatusCode(201)); } catch (Exception ex) { Log.Error(ex, $"LibraryController.PostLibrayInfo({nameof(newBook)}={newBook}). User={User.Identity?.Name}"); return(BadRequest()); } }
private void btnAddBook_Click(object sender, EventArgs e) { int year; if (txtNameBook.Text == "" || txtAmountBook.Text == "" || txtAuthorBook.Text == "" || !(Int32.TryParse(txtYearBook.Text, out year))) { MessageBox.Show("One of * fields are empty or wrong data"); } else { Book book = new Book(); book.Name = txtNameBook.Text; book.Author = txtAuthorBook.Text; book.Genre = txtGenreBook.Text; book.Publisher = txtPublisherBook.Text; if (pcBPhotoBook.Image != null) { book.Photo = ImageManager.ImageToByteArray(pcBPhotoBook.Image); } book.YearPublished = year; book.Status = 1; _bookRepository.AddBook(book, Convert.ToInt32(txtAmountBook.Text)); this.Close(); } }
public IActionResult AddBook([FromBody] BookModel book) { _bookRepository.AddBook(book); // Add book using first instance var books = _bookRepository2.GetAllBooks(); // Get all books using secind instance return(Ok(books)); }
public async Task <IActionResult> AddBook(BookModel bookModel) { if (ModelState.IsValid) { if (bookModel.CoverPhoto != null) { string folder = "Books/cover/"; folder += Guid.NewGuid().ToString() + "_" + bookModel.CoverPhoto.FileName; bookModel.CoverImageUrl = "/" + folder; string serverFolder = Path.Combine(_webHostEnvironment.WebRootPath, folder); await bookModel.CoverPhoto.CopyToAsync(new FileStream(serverFolder, FileMode.Create)); } if (bookModel.BookPdf != null) { string folder = "Books/pdf/"; folder += Guid.NewGuid().ToString() + "_" + bookModel.BookPdf.FileName; bookModel.BookPdfUrl = "/" + folder; string serverFolder = Path.Combine(_webHostEnvironment.WebRootPath, folder); await bookModel.BookPdf.CopyToAsync(new FileStream(serverFolder, FileMode.Create)); } var id = await _bookRepository.AddBook(bookModel); if (id > 0) { return(RedirectToAction(nameof(AddBook), new { isSuccess = true, bookId = id })); } } /*ViewBag.isSuccess = isSuccess; * ViewBag.BookId = bookId;*/ /*ModelState.AddModelError("", "Error from Book Controller");*/ return(View()); }
public ActionResult <Book> AddBook([FromBody] CreateBookDto createBookDto) { Book book = _mapper.Map <Book>(createBookDto); _bookRepository.AddBook(book); return(CreatedAtRoute(nameof(GetBookById), new { book.Id }, book)); }
public void AddBook(BLBook book) { if (book != null) { Book abook = Mapper.Map <BLBook, Book>(book); db.AddBook(abook); } }
public IActionResult Create(Book book) { if (ModelState.IsValid) { repository.AddBook(book); ViewBag.IsAdded = true; } return(View()); }
public BookResponse AddBook(CreateBookViewModel book) { if (book.ListOfTags.Count == 0) { throw new CustomException("Invalid empty Book Tag list you Must enter at least one Tag"); } return(_mapper.Map <Book, BookResponse>(_bookRepository.AddBook(book))); }
public IActionResult AddBook(CreateBookModel model) { if (ModelState.IsValid) { _bookRepository.AddBook(model); } return(RedirectToAction("Index", "Home")); }
public string TrySaveBook(Book book) { if (_bookValidator.HasThirteenDigitsInISBNNumber(book.ISBNNumber)) { _bookRepository.AddBook(book); return($"You added a book: {book.BookName}"); } return("Something is wrong with ISBN number"); }
public IActionResult Create(BookDetails book) { if (!ModelState.IsValid) { return(View()); } _bookRepos.AddBook(book); return(RedirectToAction("Index", "Book")); }
public IActionResult Create([Bind("Id,BookName,Price,Publisher,Author")] Book book) { if (ModelState.IsValid) { _bookRepository.AddBook(book); return(RedirectToAction(nameof(Index))); } return(View(book)); }
public RedirectToActionResult AddBook(Book book) { if (ModelState.IsValid) { repo.AddBook(book); } return(RedirectToAction("Index")); }
public Task <bool> Handle(AddBookRequest request, CancellationToken cancellationToken) { _bookRepository.AddBook(new Book { Author = request.Author, Id = request.Id, PublishYear = request.PublishYear, Title = request.Title }); return(Task.FromResult(true)); }
public IActionResult Create(Book book) { if (!ModelState.IsValid) { return(View()); } _bookrepository.AddBook(book); return(RedirectToAction("Index", "Books")); }
public ActionResult <Book> PostBook([FromForm] Book book) { if (books.AddBook(book)) { return(book); } return(BadRequest()); }
public IActionResult AddBooks(Book book) { if (ModelState.IsValid) { _bookRepository.AddBook(book); return(RedirectToAction("Index", "Home")); } return(View(book)); }
public ActionResult <BookReadDTO> CreateBookForAuthor(int authorId, BookCreationDTO book) { var newBook = mapper.Map <BookReadDTO>(repository.AddBook(authorId, mapper.Map <Book>(book))); return(CreatedAtRoute( routeName: "GetBook", routeValues: new { authorId, bookId = newBook.BookId }, value: newBook )); }