public bool Update(BookBindingModel model) { try { using (var libraryDb = new LibraryManagementEntities()) { // Check first get item var book = libraryDb.Books.FirstOrDefault(s => s.Id.Equals(model.Id)); if (book == null) { throw new ArgumentNullException("No exist"); } book.Name = model.Name; book.Language = model.Language; book.ReleaseYear = model.ReleaseYear; book.PageCount = model.PageCount; //book.AuthorId = model.AuthorId; //book.PublisherId = model.PulisherId; book.Note = model.Note; book.Price = model.Price; //book.CategoryId = model.CategoryId; //book.SpecialCategoryId = model.SpecialCategoryId; //book.BookShelfId = model.BookShelfId; libraryDb.SaveChanges(); return(true); } } catch (Exception) { return(false); } }
// POST: api/Book //[ResponseType(typeof(Book))] public IHttpActionResult PostBook(BookBindingModel bookbind) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } var book = new Book(); book.BookId = bookbind.BookId; book.Isbn = bookbind.Isbn; book.Title = bookbind.Title; book.ano = bookbind.ano; var authors = new List <Author>(); if (bookbind.Authors != null) { foreach (var p in bookbind.Authors) { var author = new Author(); author.AuthorId = p.AuthorId; author.DataNascimento = p.DataNascimento; author.Email = p.Email; author.FirstName = p.FirstName; author.LastName = p.LastName; } } dal.RegistrarLivro(book); return(Ok()); /*CreatedAtRoute("DefaultApi", new { id = book.BookId }, book);*/ }
public bool Create(BookBindingModel model) { try { using (var libraryDb = new LibraryManagementEntities()) { // check duplicated var currentBook = libraryDb.Books.FirstOrDefault(s => s.Id.Equals(model.Id)); if (currentBook != null) { throw new ArgumentNullException(); } // Insert db var bookInfo = _entityMapper.Map <BookBindingModel, Book>(model); libraryDb.Books.Add(bookInfo); libraryDb.SaveChanges(); return(true); } } catch (Exception) { return(false); } }
public IHttpActionResult PostBook(BookBindingModel bookModel) { var catNames = bookModel.Categories.Split(' '); ICollection <Category> categories = catNames .Select(name => this._context.Categories.FirstOrDefault(c => c.Name == name)) .ToList(); this._context.Books.Add(new Book { Title = bookModel.Title, Description = bookModel.Description, EditionType = bookModel.EditionType, AgeRestriction = bookModel.AgeRestriction, Price = bookModel.Price, Copies = bookModel.Copies, ReleaseDate = bookModel.ReleaseDate, AuthorId = bookModel.AuthorId, Author = this._context.Authors.FirstOrDefault(a => a.Id == bookModel.AuthorId), Categories = categories }); this._context.SaveChanges(); return(this.Ok("Book added successfully!")); }
public IHttpActionResult UpdateBookById(int id, BookBindingModel bookModel) { var existingBook = this.BookShopData.Books.Find(id); if (existingBook == null) { return this.BadRequest("No book with such Id."); } if (!this.ModelState.IsValid) { return this.BadRequest(this.ModelState); } existingBook.Title = bookModel.Title; existingBook.Description = bookModel.Description; existingBook.Price = bookModel.Price; existingBook.Copies = bookModel.Copies; existingBook.EditionType = bookModel.EditionType; existingBook.AuthorId = bookModel.AuthorId; this.BookShopData.Books.Update(existingBook); this.BookShopData.SaveChanges(); var bookViewModel = BookViewModel.CreateViewModel(existingBook); return this.Ok(bookViewModel); }
public ActionResult Add(BookBindingModel model) { if (ModelState.IsValid) { string folderPath = "~/media/covers/"; bool existFolder = Directory.Exists(Server.MapPath(folderPath)); if (!existFolder) { Directory.CreateDirectory(Server.MapPath(folderPath)); } var timeNowToString = DateTime.Now.ToString("ddMMyyyyhhmmsstt"); var imageName = timeNowToString + model.ImageFile.FileName; model.ImageFile.SaveAs(HttpContext.Server.MapPath(folderPath + imageName)); var newBook = new Book { Name = model.Name, CreatedOn = DateTime.Now, Genre = model.Genre, PageCount = model.PageCount, ImagePath = imageName, CreatorId = User.Identity.GetUserId() }; this.Data.Books.Add(newBook); this.Data.SaveChanges(); return(RedirectToAction("List")); } return(View(model)); }
public IHttpActionResult Post([FromBody] BookBindingModel newBookInfo) { if (!ModelState.IsValid) { return(this.BadRequest(this.ModelState)); } var newBook = new Book() { Title = newBookInfo.Title, Price = newBookInfo.Price, Description = newBookInfo.Description, Copies = newBookInfo.Copies, EditionType = newBookInfo.EditionType != null ? (EditionType?)Enum.Parse(typeof(EditionType), newBookInfo.EditionType.Value.ToString(), true) : null, AgeRestriction = newBookInfo.AgeRestriction != null ? (AgeRestriction?)Enum.Parse(typeof(AgeRestriction), newBookInfo.AgeRestriction.Value.ToString(), true) : null, ReleaseDate = newBookInfo.ReleaseDate, Categories = newBookInfo.Categories.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries) .Select(name => new Category() { Name = name }).ToList(), AuthorId = newBookInfo.AuthorId }; ctx.Books.Add(newBook); ctx.SaveChanges(); return(this.Ok(newBook)); }
public IHttpActionResult Put(int id, [FromUri] BookBindingModel newBookInfo) { if (newBookInfo == null || !this.ModelState.IsValid) { return(this.BadRequest(this.ModelState)); } var book = ctx.Books.FirstOrDefault(b => b.Id == id); if (book == null) { throw new HttpResponseException(HttpStatusCode.NotFound); } book.Title = newBookInfo.Title; book.Price = newBookInfo.Price; book.Description = newBookInfo.Description; book.Copies = newBookInfo.Copies; book.ReleaseDate = newBookInfo.ReleaseDate; book.AuthorId = newBookInfo.AuthorId; book.EditionType = newBookInfo.EditionType != null ? (EditionType?)Enum.Parse(typeof(EditionType), newBookInfo.EditionType.Value.ToString(), true) : null; book.AgeRestriction = newBookInfo.AgeRestriction != null ? (AgeRestriction?)Enum.Parse(typeof(AgeRestriction), newBookInfo.AgeRestriction.Value.ToString(), true) : null; ctx.SaveChanges(); return(this.Ok(book)); }
public IHttpActionResult PutBook(BookBindingModel bookbind) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } var book = new Book(); book.BookId = bookbind.BookId; book.Isbn = bookbind.Isbn; book.Title = bookbind.Title; book.ano = bookbind.ano; if (bookbind.Authors != null) { foreach (var p in bookbind.Authors) { var author = new Author(); author.AuthorId = p.AuthorId; author.DataNascimento = p.DataNascimento; author.Email = p.Email; author.FirstName = p.FirstName; author.LastName = p.LastName; book.Authors.Add(author); } } dal.EditBookR(book); return(StatusCode(HttpStatusCode.NoContent)); }
public IActionResult EditBook(BookBindingModel bookModel, [FromRoute] int bookId) { var book = _mapper.Map <Book>(bookModel); var updatedBook = _bookInventory.EditBook(book, bookId); var updatedBookModel = _mapper.Map <BookDetailsApiModel>(updatedBook); return(Ok(updatedBookModel)); }
public IActionResult AddBook(BookBindingModel bookModel) { var book = _mapper.Map <Book>(bookModel); var books = _bookInventory.AddBook(book, bookModel.BookGenre); var addedBookModel = _mapper.Map <IEnumerable <BookDetailsApiModel> >(books); return(Ok(addedBookModel)); }
public IHttpActionResult Create(BookBindingModel bookBindingModel) { if (!this.ModelState.IsValid) { return(this.BadRequest(this.ModelState)); } var author = this.data.Authors.Find(bookBindingModel.AuthorId); if (author == null) { return(this.BadRequest("Author does not exist - invalid id!")); } Book book = new Book() { Title = bookBindingModel.Title, Description = bookBindingModel.Description, Price = bookBindingModel.Price, Copies = bookBindingModel.Copies, EditionType = bookBindingModel.EditionType, AgeRestriction = bookBindingModel.AgeRestriction, ReleaseDate = bookBindingModel.ReleaseDate, Author = author }; string[] categories = bookBindingModel.Categories.Split(new [] { ' ', ',' }, StringSplitOptions.RemoveEmptyEntries); foreach (var categoryName in categories) { Category category = this.data .Categories .All() .FirstOrDefault(c => c.Name == categoryName); if (category == null) { category = new Category { Name = categoryName }; this.data.Categories.Add(category); } book.Categories.Add(category); } this.data.Books.Add(book); this.data.Save(); BookViewModel bookViewModel = BookViewModel.ConvertToBookViewModel(book); return(this.Ok(bookViewModel)); }
public IHttpActionResult Create(BookBindingModel bookBindingModel) { if (!this.ModelState.IsValid) { return this.BadRequest(this.ModelState); } var author = this.data.Authors.Find(bookBindingModel.AuthorId); if (author == null) { return this.BadRequest("Author does not exist - invalid id!"); } Book book = new Book() { Title = bookBindingModel.Title, Description = bookBindingModel.Description, Price = bookBindingModel.Price, Copies = bookBindingModel.Copies, EditionType = bookBindingModel.EditionType, AgeRestriction = bookBindingModel.AgeRestriction, ReleaseDate = bookBindingModel.ReleaseDate, Author = author }; string[] categories = bookBindingModel.Categories.Split(new [] {' ', ','}, StringSplitOptions.RemoveEmptyEntries); foreach (var categoryName in categories) { Category category = this.data .Categories .All() .FirstOrDefault(c => c.Name == categoryName); if (category == null) { category = new Category { Name = categoryName }; this.data.Categories.Add(category); } book.Categories.Add(category); } this.data.Books.Add(book); this.data.Save(); BookViewModel bookViewModel = BookViewModel.ConvertToBookViewModel(book); return this.Ok(bookViewModel); }
public async Task <int> AddBookAsync(BookBindingModel model) { var checkForDuplicate = this.DbContext .Books .FirstOrDefault(x => x.Title == model.Title); if (checkForDuplicate != null) { return(ErrorId); } var book = this.Mapper.Map <Book>(model); var bookCategory = this.DbContext .BookCategories .FirstOrDefault(x => x.BookCategoryName == model.BookCategoryStr); if (bookCategory == null) { bookCategory = new BookCategory() { BookCategoryName = model.BookCategoryStr }; await this.DbContext.BookCategories.AddAsync(bookCategory); await this.DbContext.SaveChangesAsync(); } var bookAuthor = this.DbContext .BookAuthors .FirstOrDefault(x => x.FullName == model.BookAuthorStr); if (bookAuthor == null) { return(ErrorId); } book.BookAuthorId = bookAuthor.Id; book.BookAuthor = bookAuthor; book.BookCategoryId = bookCategory.Id; book.BookCategory = bookCategory; await this.DbContext.Books.AddAsync(book); await this.DbContext.SaveChangesAsync(); return(book.Id); }
public IHttpActionResult AddBook(BookBindingModel model) { if (model == null) { this.ModelState.AddModelError("model", "The model cannot be null - (request is empty)."); } if (!ModelState.IsValid) { return(this.BadRequest(ModelState)); } var categories = model.Categories.Split(' '); var categoryList = new List <Category>(); if (categories.Count() != 0) { foreach (var categoryName in categories) { Category category = new Category() { Name = categoryName }; categoryList.Add(category); } } var book = new Book() { Title = model.Title, Description = model.Description ?? null, Edition = model.Edition, Price = model.Price, AgeRestriction = model.Restriction, Copies = model.Copies, ReleaseDate = DateTime.Now, Author = this.Data.Authors .First(a => a.Id == model.AuthorId) ?? null, RelatedBooks = this.Data.Books .Where(b => b.Id == model.BookId) .ToList() .Count == 0 ? null : this.Data.Books.Where(b => b.Id == model.BookId).ToList(), Categories = categoryList ?? null }; this.Data.Books.Add(book); this.Data.SaveChanges(); return(this.Ok("created book with id = " + book.Id)); }
// Метод содержит разделяемую логику методов CreateBook() и UpdateBook() (принцип DRY). private async Task <IActionResult> PerformAddOrUpdateAsync(BookBindingModel model, Func <Book, IActionResult> returnAction) { var bookData = new Book { Id = model.Id, Title = model.Title, Author = model.Author, Genre = model.Genre, Year = model.Year }; var bookEntity = _bookRepository.AddOrUpdateBook(bookData); await _bookRepository.SaveChangesAsync(); return(returnAction.Invoke(bookEntity)); }
public IHttpActionResult EditBook(int id, [FromBody] BookBindingModel bookModel) { var book = this._context.Books.FirstOrDefault(b => b.Id == id); book.Title = bookModel.Title; book.Description = bookModel.Description; book.EditionType = bookModel.EditionType; book.AgeRestriction = bookModel.AgeRestriction; book.Price = bookModel.Price; book.Copies = bookModel.Copies; book.ReleaseDate = bookModel.ReleaseDate; book.AuthorId = bookModel.AuthorId; book.Author = this._context.Authors.FirstOrDefault(a => a.Id == bookModel.AuthorId); this._context.SaveChanges(); return(this.Ok("Book edited successfully!")); }
public bool PaymentBook(string bookId) { try { // update status for book BookBindingModel book = bookManager.GetBook(bookId); bookManager.UpdateStatusForPayment(book); // delete int borrowerId = GetBorrowerId(book.Id); Delete(borrowerId); return(true); } catch (Exception) { return(false); } }
public async Task <IActionResult> AddBook(BookBindingModel model) { if (!this.ModelState.IsValid) { SetErrorMessage(CommonConstants.DangerMessage); return(this.AddBook()); } int generatedId = await this.bookService.AddBookAsync(model); if (generatedId < 1) { SetErrorMessage(CommonConstants.DuplicateMessage); return(this.View()); } SetSuccessMessage(string.Format(CommonConstants.SuccessMessage, CommonConstants.BookSuffix)); return(Redirect(string.Format(RedirectConstants.AdministratorAreaBookDetailsPage, generatedId))); }
public IHttpActionResult UpdateBook(int id, BookBindingModel bookBindingModel) { if (!this.ModelState.IsValid) { return(this.BadRequest(this.ModelState)); } var existingBook = this.data.Books.Find(id); if (existingBook == null) { return(this.BadRequest("Book does not exist - invalid id!")); } Author author = this.data.Authors.Find(bookBindingModel.AuthorId); if (author == null) { return(this.BadRequest("Author does not exist - invalid id!")); } existingBook.AgeRestriction = bookBindingModel.AgeRestriction; existingBook.Author = author; existingBook.Copies = bookBindingModel.Copies; existingBook.Description = bookBindingModel.Description; existingBook.EditionType = bookBindingModel.EditionType; existingBook.Price = bookBindingModel.Price; existingBook.ReleaseDate = bookBindingModel.ReleaseDate; existingBook.Title = bookBindingModel.Title; this.data.Books.Update(existingBook); this.data.Save(); BookViewModel bookViewModel = BookViewModel.ConvertToBookViewModel(existingBook); return(this.Ok(bookViewModel)); }
public bool UpdateStatusForPayment(BookBindingModel model) { try { using (var libraryDb = new LibraryManagementEntities()) { // Check first get item var book = libraryDb.Books.FirstOrDefault(s => s.Id.Equals(model.Id)); if (book == null) { throw new ArgumentNullException("No exist"); } book.Status = SystemConstants.NotBorrow; libraryDb.SaveChanges(); return(true); } } catch (Exception) { return(false); } }
public IHttpActionResult CreateBook(BookBindingModel bookModel) { if (!this.ModelState.IsValid) { return this.BadRequest(this.ModelState); } var newBook = new Book { Title = bookModel.Title, Description = bookModel.Description, Price = bookModel.Price, Copies = bookModel.Copies, EditionType = bookModel.EditionType, AuthorId = bookModel.AuthorId }; var newBookCategoriesNames = bookModel.Categories.Split(' '); this.AddCategoriesToBook(newBookCategoriesNames, newBook); this.BookShopData.Books.Add(newBook); this.BookShopData.SaveChanges(); var bookViewModel = BookViewModel.CreateViewModel(newBook); return this.Ok(bookViewModel); }
public Task <IActionResult> CreateBookAsync(BookBindingModel model) { return(PerformAddOrUpdateAsync(model, book => CreatedAtRoute("GetBook", new { id = book.Id }, book))); }
public Task <IActionResult> UpdateBookAsync(BookBindingModel model) { return(PerformAddOrUpdateAsync(model, book => Ok(book))); }
public IHttpActionResult UpdateBook(int id, BookBindingModel bookBindingModel) { if (!this.ModelState.IsValid) { return this.BadRequest(this.ModelState); } var existingBook = this.data.Books.Find(id); if (existingBook == null) { return this.BadRequest("Book does not exist - invalid id!"); } Author author = this.data.Authors.Find(bookBindingModel.AuthorId); if (author == null) { return this.BadRequest("Author does not exist - invalid id!"); } existingBook.AgeRestriction = bookBindingModel.AgeRestriction; existingBook.Author = author; existingBook.Copies = bookBindingModel.Copies; existingBook.Description = bookBindingModel.Description; existingBook.EditionType = bookBindingModel.EditionType; existingBook.Price = bookBindingModel.Price; existingBook.ReleaseDate = bookBindingModel.ReleaseDate; existingBook.Title = bookBindingModel.Title; this.data.Books.Update(existingBook); this.data.Save(); BookViewModel bookViewModel = BookViewModel.ConvertToBookViewModel(existingBook); return this.Ok(bookViewModel); }
private void btnSave_Click(object sender, EventArgs e) { int pageCount = 0; if (!string.IsNullOrWhiteSpace(txtPageCount.Text)) { pageCount = int.Parse(txtPageCount.Text.Trim()); } if (Add) { BookBindingModel model = new BookBindingModel() { Id = txtId.Text.Trim(), Name = txtName.Text.Trim(), Language = txtLanguage.Text.Trim(), ReleaseYear = txtReleaseYear.Text.Trim(), Note = txtNote.Text.Trim(), PageCount = pageCount, Price = txtPageCount.Text.Trim(), Status = SystemConstants.NotBorrow, AuthorId = int.Parse(cmbAuthor.SelectedValue.ToString()), CategoryId = int.Parse(cmbCategory.SelectedValue.ToString()), SpecialCategoryId = int.Parse(cmbSpecialCategory.SelectedValue.ToString()), PublisherId = int.Parse(cmbPublisher.SelectedValue.ToString()), BookShelfId = int.Parse(cmbBookShelf.SelectedValue.ToString()) }; if (model.AuthorId == -1 || model.CategoryId == -1 || model.SpecialCategoryId == -1 || model.PublisherId == -1 || model.BookShelfId == -1) { MessageBox.Show("Chưa chọn tác giả, hoặc danh mục, hoặc chủng loại ngành, hoặc giá sách hoặc nhà xuất bản.", "Thông báo"); return; } if (manager.Create(model)) { txtId.Enabled = false; Add = false; MessageBox.Show("Thêm mới thành công !", "Thông báo"); } else { MessageBox.Show("Thêm mới không thành công!. Liên hệ với quản trị viên.", "Thông báo"); } } else { BookBindingModel model = new BookBindingModel() { Id = txtId.Text.Trim(), Name = txtName.Text.Trim(), Language = txtLanguage.Text.Trim(), ReleaseYear = txtReleaseYear.Text.Trim(), Note = txtNote.Text.Trim(), PageCount = pageCount, Price = txtPageCount.Text.Trim() //AuthorId = int.Parse(cmbAuthor.SelectedValue.ToString()), //CategoryId = int.Parse(cmbCategory.SelectedValue.ToString()), //SpecialCategoryId = int.Parse(cmbSpecialCategory.SelectedValue.ToString()), //PublisherId = int.Parse(cmbPublisher.SelectedValue.ToString()), //BookShelfId = int.Parse(cmbBookShelf.SelectedValue.ToString()) }; if (manager.Update(model)) { MessageBox.Show("Sửa thành công !", "Thông báo"); } else { MessageBox.Show("Sửa không thành công!. Liên hệ với quản trị viên.", "Thông báo"); } } LoadData(Searching, CategoryIdSearch, SpecialCategoryIdSearch, BookShelfIdSearch, PublisherIdSearch, AuthorIdSearch, PageIndex, PageSize); }
public async Task <IActionResult> DeleteBook(BookBindingModel model) { bool isDeleted = await this.bookService.DeleteBookAsync(model.Id); return(RedirectToAction(RedirectConstants.IndexSuffix)); }
public IHttpActionResult PostBook(BookBindingModel bookModel) { var catNames = bookModel.Categories.Split(' '); ICollection<Category> categories = catNames .Select(name => this._context.Categories.FirstOrDefault(c => c.Name == name)) .ToList(); this._context.Books.Add(new Book { Title = bookModel.Title, Description = bookModel.Description, EditionType = bookModel.EditionType, AgeRestriction = bookModel.AgeRestriction, Price = bookModel.Price, Copies = bookModel.Copies, ReleaseDate = bookModel.ReleaseDate, AuthorId = bookModel.AuthorId, Author = this._context.Authors.FirstOrDefault(a => a.Id == bookModel.AuthorId), Categories = categories }); this._context.SaveChanges(); return this.Ok("Book added successfully!"); }