public int GetBooksCount() { using (var db = new BooksAPIContext()) { return(db.Books.Count()); } }
public BookDetailDto AddNewBookWithAllFields(DateTime publishDate, string author = "Ralls, Kim", string title = "test title", decimal price = 0.0m, string genre = "Genre", string description = "Description" ) { int bookId = NextBookExternalId() + 1; BookDetailDto newBook = new BookDetailDto { Id = bookId, Author = author, Title = title, Price = 100.1m, Genre = genre, Description = description, PublishDate = publishDate }; using (var db = new BooksAPIContext()) { db.Books.Add(newBook.ToModel()); db.SaveChanges(); } return(newBook); }
public int NextBookExternalId() { using (var db = new BooksAPIContext()) { return(db.Books.Max(b => b.ExternalId) + 1); } }
public List <BookDto> GetAllBooksInDb() { using (var db = new BooksAPIContext()) { return(db.Books.Select(AsBookDto).ToList()); } }
public void DeleteBooksFromId(int id) { using (var db = new BooksAPIContext()) { var booksToDelete = db.Books.Where(b => b.ExternalId >= id); db.Books.RemoveRange(booksToDelete); db.SaveChanges(); } }
private void Dispose(bool disposing) { if (disposing) { if (db != null) { db.Dispose(); db = null; } } }
public static Book ToModel(this BookDetailDto book) { using (var db = new BooksAPIContext()) { var authorId = db.Authors.Where(a => a.Name == book.Author).Single().AuthorId; return(new Book { ExternalId = book.Id, Title = book.Title, AuthorId = authorId, Description = book.Description, Genre = book.Genre, Price = book.Price, PublishDate = book.PublishDate }); } }
public List <BookDto> GetBooksByDate(DateTime date) { using (var db = new BooksAPIContext()) { return((from b in db.Books join a in db.Authors on b.AuthorId equals a.AuthorId where b.PublishDate == date select new BookDto { Id = b.ExternalId, Title = b.Title, Genre = b.Genre, Author = b.Author.Name }).ToList <BookDto>()); } }
public BookDetailDto GetDetailBookByExternalId(int bookId) { using (var db = new BooksAPIContext()) { return((from b in db.Books join a in db.Authors on b.AuthorId equals a.AuthorId where b.ExternalId == bookId select new BookDetailDto { Id = b.ExternalId, Title = b.Title, Genre = b.Genre, PublishDate = b.PublishDate, Price = b.Price, Description = b.Description, Author = b.Author.Name }).FirstOrDefault()); } }
public InMemoryBookRepository(BooksAPIContext context) { _context = context; }
public BooksController(BooksAPIContext context) { _context = context; }
public TestController(BooksAPIContext context) { _context = context; }
protected override void Seed(BooksAPIContext context) { // This method will be called after migrating to the latest version. // You can use the DbSet<T>.AddOrUpdate() helper extension method // to avoid creating duplicate seed data. E.g. // // context.People.AddOrUpdate( // p => p.FullName, // new Person { FullName = "Andrew Peters" }, // new Person { FullName = "Brice Lambson" }, // new Person { FullName = "Rowan Miller" } // ); // context.Authors.AddOrUpdate(new Author[] { new Author() { AuthorId = 1, Name = "Ahmad" }, new Author() { AuthorId = 2, Name = "Rehman" }, new Author() { AuthorId = 3, Name = "Mansoor" }, new Author() { AuthorId = 4, Name = "Akram" }, new Author() { AuthorId = 5, Name = "Shafkat" } }); context.Books.AddOrUpdate(new Book[] { new Book() { BookId = 1, Title = "Midnight Rain", Genre = "Fantasy", PublishDate = new DateTime(2000, 12, 16), AuthorId = 1, Description = "A former architect battles an evil sorceress.", Price = 14.95M }, new Book() { BookId = 2, Title = "Maeve Ascendant", Genre = "Fantasy", PublishDate = new DateTime(2000, 11, 17), AuthorId = 2, Description = "After the collapse of a nanotechnology society, the young" + "survivors lay the foundation for a new society.", Price = 12.95M }, new Book() { BookId = 3, Title = "The Sundered Grail", Genre = "Fantasy", PublishDate = new DateTime(2001, 09, 10), AuthorId = 2, Description = "The two daughters of Maeve battle for control of England.", Price = 12.95M }, new Book() { BookId = 4, Title = "Lover Birds", Genre = "Romance", PublishDate = new DateTime(2000, 09, 02), AuthorId = 3, Description = "When Carla meets Paul at an ornithology conference, tempers fly.", Price = 7.99M }, new Book() { BookId = 5, Title = "Splish Splash", Genre = "Romance", PublishDate = new DateTime(2000, 11, 02), AuthorId = 4, Description = "A deep sea diver finds true love 20,000 leagues beneath the sea.", Price = 6.99M }, }); }
private void SaveContextChanges(BooksAPIContext _booksAPIContext) { _booksAPIContext.SaveChanges(); }
public InMemoryBookRepository(BooksAPIContext booksAPIContext) { _booksAPIContext = booksAPIContext; }