public void AddAuthors(BookDetailDTO book, long bookCreatedId) { book.BookAuthors.ForEach(a => { long authorId = 0; if (a.Id > 0) { authorId = a.Id; } else { var newAuthor = new Author { Name = a.Name }; _context.Author.Add(newAuthor); _context.SaveChanges(); authorId = newAuthor.Id; } var bookAuthor = new BookAuthor { BookId = bookCreatedId, AuthorId = authorId }; _context.BookAuthors.Add(bookAuthor); _context.SaveChanges(); }); }
public void AddCategories(BookDetailDTO book, long bookCreatedId) { book.BookCategories.ForEach(c => { long catId = 0; if (c.Id > 0) { catId = c.Id; } else { var newCategory = new Category { CategoryDescription = c.CategoryDescription }; _context.Category.Add(newCategory); _context.SaveChanges(); catId = newCategory.Id; } var bookCategory = new BookCategory { BookId = bookCreatedId, CategoryId = catId }; _context.BookCategory.Add(bookCategory); _context.SaveChanges(); }); }
public async Task <IHttpActionResult> GetBook(int id) { Book book = await db.Books.FindAsync(id); if (book == null) { return(NotFound()); } Author author = await db.Authors.FindAsync(book.AuthorId); if (author == null) { return(NotFound()); } BookDetailDTO result = new BookDetailDTO() { BookId = book.BookId, Title = book.Title, Price = book.Price, AuthorId = book.AuthorId, AuthorName = author.Name, Genre = book.Genre }; return(Ok(result)); }
public async Task <IHttpActionResult> PostBook(BookDetailDTO bookDetailDTO) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } //var book = new Book //{ // Id = bookDetailDTO.Id, // Title = bookDetailDTO.Title, // Year = bookDetailDTO.Year, // Price = bookDetailDTO.Price, // Genre = bookDetailDTO.Genre, // AuthorId = bookDetailDTO.AuthorId //}; Book book = Mapper.Map <Book>(bookDetailDTO); db.Books.Add(book); await db.SaveChangesAsync(); //Id naar DTO wegschrijven bookDetailDTO.Id = book.Id; return(Ok(bookDetailDTO)); //return CreatedAtRoute("DefaultApi", new { id = book.Id }, book); }
public async Task <IActionResult> PutBook(long id, BookDetailDTO book) { if (id != book.Id) { return(BadRequest()); } var updatedBook = await _bookService.UpdateBook(book); return(Ok(updatedBook)); }
public async Task <Book> AddBook(BookDetailDTO book) { var bookForCreation = _mapper.Map <Book>(book); _context.Book.Add(bookForCreation); await _context.SaveChangesAsync(); AddAuthors(book, bookForCreation.Id); AddCategories(book, bookForCreation.Id); return(bookForCreation); }
public async Task <IHttpActionResult> PutBook(int id, BookDetailDTO bookDetailDTO) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } Book book = Mapper.Map <Book>(bookDetailDTO); db.Set <Book>().Attach(book); // voor update db.Entry(book).State = EntityState.Modified; await db.SaveChangesAsync(); return(Ok(bookDetailDTO)); }
public async Task <IHttpActionResult> PostBook(BookDetailDTO bookDetailDTO) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } Book book = Mapper.Map <Book>(bookDetailDTO); db.Books.Add(book); await db.SaveChangesAsync(); // Id naar DTO wegschrijven bookDetailDTO.Id = book.Id; return(Ok(bookDetailDTO)); }
public async Task <Book> UpdateBook(BookDetailDTO book) { var bookEntity = _mapper.Map <Book>(book); _context.Entry(bookEntity).State = EntityState.Modified; await _context.SaveChangesAsync(); _context.BookAuthors.RemoveRange(_context.BookAuthors.Where(x => x.BookId == bookEntity.Id)); _context.BookCategory.RemoveRange(_context.BookCategory.Where(x => x.BookId == bookEntity.Id)); await _context.SaveChangesAsync(); AddAuthors(book, bookEntity.Id); AddCategories(book, bookEntity.Id); return(bookEntity); }
public async Task <IHttpActionResult> PutBook([FromUri] int id, [FromBody] Book book) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } if (id != book.Id) { return(BadRequest()); } db.Entry(book).State = EntityState.Modified; try { await db.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!BookExists(id)) { return(NotFound()); } else { throw; } } db.Entry(book).Reference(x => x.Author).Load(); var dto = new BookDetailDTO { Id = book.Id, Title = book.Title, Year = book.Year, Price = book.Price, Genre = book.Genre, AuthorName = book.Author.Name }; return(CreatedAtRoute("DefaultApi", new { id = book.Id }, dto)); //return StatusCode(HttpStatusCode.NoContent); }
public async Task <IHttpActionResult> GetBook(int id) { init(); if (_books.Any(b => b.Id == id)) { var book = _books.FirstOrDefault(b => b.Id == id); var detail = new BookDetailDTO { Id = book.Id, Title = book.Title, Year = book.Year, Price = book.Price, AuthorName = book.Author.Name, Genre = book.Genre }; return(Ok(book)); } return(NotFound()); }
public IHttpActionResult GetBook(int id) { Book book = db.Books.Find(id); if (book == null) { return(NotFound()); } List <Author> authors = new List <Author>(); foreach (BookAuthorCoupling bac in db.BookAuthorCouplings.Where(x => x.BookId == book.Id).ToList()) { authors.Add(db.Authors.FirstOrDefault(a => a.Id == bac.AuthorId)); } List <Genre> genres = new List <Genre>(); foreach (BookGenreCoupling bgc in db.BookGenreCouplings.Where(x => x.BookId == book.Id).ToList()) { genres.Add(db.Genres.FirstOrDefault(g => g.Id == bgc.GenreId)); } BookDetailDTO bookDetail = new BookDetailDTO() { Id = book.Id, Title = book.Title, Authors = authors.ToArray(), Genres = genres.ToArray(), Year = book.Year, Price = book.Price, ISBN = book.ISBN, ImagePath = book.ImagePath, Summary = book.Summary, InStock = book.InStock, Rating = db.Ratings.FirstOrDefault(x => x.Id == book.RatingId) }; return(Ok(bookDetail)); }
public async Task <IHttpActionResult> GetBook(int id) { BookDetailDTO book = await db.Books.Include(b => b.Author).Select(b => new BookDetailDTO() { Id = b.Id, Title = b.Title, Year = b.Year, Price = b.Price, AuthorName = b.Author.Name, Genre = b.Genre }).SingleOrDefaultAsync(b => b.Id == id); if (book == null) { return(NotFound()); } return(Ok(book)); }
public IHttpActionResult PostBook(BookDetailDTO book) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } var rating = new Rating() { TotalRating = 0, Votes = 0 }; db.Ratings.Add(rating); db.SaveChanges(); Book toAdd = new Book() { Title = book.Title, RatingId = rating.Id, Summary = book.Summary, Price = book.Price, InStock = book.InStock, ISBN = book.ISBN, Year = book.Year, ImagePath = book.ImagePath }; db.Books.Add(toAdd); db.SaveChanges(); foreach (Genre genre in book.Genres) { db.BookGenreCouplings.Add(new BookGenreCoupling() { BookId = toAdd.Id, GenreId = genre.Id }); } foreach (Author author in book.Authors) { db.BookAuthorCouplings.Add(new BookAuthorCoupling() { BookId = toAdd.Id, AuthorId = author.Id }); } db.SaveChanges(); return(CreatedAtRoute("DefaultApi", new { id = book.Id }, book)); }
public async Task <IHttpActionResult> GetBook(int id) { BookDetailDTO book = await db.Books .Include(b => b.Author) .Select(c => new BookDetailDTO { AuthorName = c.Author.Name , Genre = c.Genre , Id = c.Id , Price = c.Price , Title = c.Title , Year = c.Year } ) .FirstOrDefaultAsync(d => d.Id == id); if (book == null) { return(NotFound()); } return(Ok(book)); }
public BookDetailDTO getBookById(int id) { var book = _bookRepo.getBookById(id); if (book == null) { throw new BookNotFoundException(); } var loansByBook = _bookRepo.getLoanHistoryByBook(book); var loanHistory = new List <BookUserLoanDTO>(); foreach (BookLoan loan in loansByBook) { var user = _userRepo.getUserById(loan.UserId); var l = new BookUserLoanDTO { Username = user.FirstName + " " + user.LastName, DateOfLoan = loan.DateOfLoan, DateOfReturn = loan.DateOfReturn }; loanHistory.Add(l); } var result = new BookDetailDTO { Title = book.Title, BookId = book.BookId, AuthorFirst = book.AuthorFirst, AuthorLast = book.AuthorLast, DateOfIssue = book.DateOfIssue, ISBNNumber = book.ISBNNumber, Rating = (double)book.RatingSum / (double)book.TotalRatings, LoanHistory = loanHistory }; return(result); }
public async Task <IHttpActionResult> PutBook(int id, BookDetailDTO bookDetailDTO) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } Book book = Mapper.Map <Book>(bookDetailDTO); db.Set <Book>().Attach(book); // voo rupdate db.Entry(book).State = EntityState.Modified; await db.SaveChangesAsync(); return(Ok(bookDetailDTO)); //if (id != book.Id) //{ // return BadRequest(); //} //db.Entry(book).State = EntityState.Modified; //try //{ // await db.SaveChangesAsync(); //} //catch (DbUpdateConcurrencyException) //{ // if (!BookExists(id)) // { // return NotFound(); // } // else // { // throw; // } //} // return StatusCode(HttpStatusCode.NoContent); }
public async Task <IActionResult> PostBook(BookDetailDTO book) { var newBook = await _bookService.AddBook(book); return(Ok(newBook)); }
public IHttpActionResult PutBook(int id, BookDetailDTO book) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } if (id != book.Id) { return(BadRequest()); } var modified = db.Books.FirstOrDefault(x => x.Id == book.Id); modified.ImagePath = book.ImagePath; modified.InStock = book.InStock; modified.ISBN = book.ISBN; modified.Price = book.Price; modified.Summary = book.Summary; modified.Title = book.Title; modified.Year = book.Year; db.Entry(modified).State = EntityState.Modified; // Being dumb, I can't get this to work any other way. So, right now // I clear the DB of all genre/author couplings related to the book // and then add all from the frontend. // -Gustav var genreCouplings = db.BookGenreCouplings.Where(x => x.BookId == book.Id).ToList(); var authorCouplings = db.BookAuthorCouplings.Where(x => x.BookId == book.Id).ToList(); foreach (BookGenreCoupling bgc in genreCouplings) { db.BookGenreCouplings.Remove(bgc); } foreach (BookAuthorCoupling bac in authorCouplings) { db.BookAuthorCouplings.Remove(bac); } foreach (Genre genre in book.Genres) { db.BookGenreCouplings.Add(new BookGenreCoupling() { GenreId = genre.Id, BookId = book.Id }); } foreach (Author author in book.Authors) { db.BookAuthorCouplings.Add(new BookAuthorCoupling() { AuthorId = author.Id, BookId = book.Id }); } try { db.SaveChanges(); } catch (DbUpdateConcurrencyException) { if (!BookExists(id)) { return(NotFound()); } else { throw; } } return(StatusCode(HttpStatusCode.NoContent)); }