public async Task GetBookByIdShouldreturnBookWithSelectedId() { var optionsBuilder = new DbContextOptionsBuilder <BookTubeContext>() .UseInMemoryDatabase(Guid.NewGuid().ToString()); var dbContext = new BookTubeContext(optionsBuilder.Options); var service = new BooksService(dbContext); var books = new List <Book> { new Book { Id = 1, Title = "It", AuthorId = 1, PublisherId = 1, CategoryId = 1, PublicationYear = 1986, Isbn = "1111", Description = "Some description" }, new Book { Id = 2, Title = "Crime and punishment", AuthorId = 2, PublisherId = 2, CategoryId = 2, PublicationYear = 1866, Isbn = "2222", Description = "Some description" }, new Book { Id = 3, Title = "Bay Ganyo", AuthorId = 3, PublisherId = 3, CategoryId = 3, PublicationYear = 1896, Isbn = "3333", Description = "Some description" } }; foreach (var book in books) { await dbContext.Books.AddAsync(book); } await dbContext.SaveChangesAsync(); var expectedBook = books.First(x => x.Id == 3); var actual = service.GetBookById(3); Assert.Equal(expectedBook, actual); }
public async Task AllShouldReturnAllcategories() { var categories = new List <Category> { new Category { Name = "Art" }, new Category { Name = "History" }, new Category { Name = "Philosophy" } }; var optionsBuilder = new DbContextOptionsBuilder <BookTubeContext>() .UseInMemoryDatabase(Guid.NewGuid().ToString()); var dbContext = new BookTubeContext(optionsBuilder.Options); var service = new CategoriesService(dbContext); foreach (var category in categories) { await dbContext.Categories.AddAsync(category); } await dbContext.SaveChangesAsync(); var actual = service.All(); Assert.Equal(categories, actual); }
public async Task <int> Create(int bookId, int bookRating, string content, string userId) { var book = db.Books.First(x => x.Id == bookId); book.RatingVotes = book.RatingVotes + 1; book.Rating = book.Rating + bookRating; var review = new Review { Date = DateTime.Now.ToString("dd-MMM-yyyy"), Content = content, BookTubeUserId = userId, BookId = bookId }; await db.AddAsync(review); await db.SaveChangesAsync(); return(bookId); }
public async Task <ResultModel> AddPublisher(string name, string contacts) { var publisher = new Publisher { Name = name, Contacts = contacts }; var publisherExist = db.Publishers.FirstOrDefault(x => x.Name == name); var result = new ResultModel(); if (publisherExist != null) { result.Message = "Publisher already exist!"; result.ResultId = 0; return(result); } await db.AddAsync(publisher); await db.SaveChangesAsync(); result.Message = "Publisher was successfully added!"; result.ResultId = publisher.Id; return(result); }
public async Task <ResultModel> AddAuthor(string name, string bio) { var author = new Author { Name = name, Bio = bio }; var authorExist = db.Authors.FirstOrDefault(x => x.Name == name); var result = new ResultModel(); if (authorExist != null) { result.Message = "Author already exist!"; result.ResultId = 0; return(result); } await db.AddAsync(author); await db.SaveChangesAsync(); result.Message = "Author was successfully added!"; result.ResultId = author.Id; return(result); }