public async Task <bool> UpdateAsync(Author item, CancellationToken cancellationToken = default) { using (var context = new BookLibraryContext(this._optionsBuilder.Options)) { var author = await context.Authors.SingleOrDefaultAsync(o => o.Id == item.Id, cancellationToken); if (author == null) { return(false); } item.Adapt(author); await context.SaveChangesAsync(cancellationToken); return(true); } }
public async Task <bool> DeleteAsync(string id, CancellationToken cancellationToken = default) { using (var context = new BookLibraryContext(this._optionsBuilder.Options)) { var item = await context.Books.SingleOrDefaultAsync(o => o.Id == id, cancellationToken); if (item == null) { return(false); } context.Books.Remove(item); await context.SaveChangesAsync(cancellationToken); return(true); } }
public async Task <bool> BorrowBookAsync(string bookId, int customerId, DateTime startDate, CancellationToken cancellationToken = default) { using (var context = new BookLibraryContext(this._optionsBuilder.Options)) { var item = new BorrowHistory { BookId = bookId, CustomerId = customerId, StartDate = startDate }; context.BorrowHistories.Add(item); if (await context.SaveChangesAsync(cancellationToken) == 1) { return(true); } } return(false); }
public async Task <bool> RemoveAuthorFromBookAsync(string bookId, int authorId, CancellationToken cancellationToken = default) { using (var context = new BookLibraryContext(this._optionsBuilder.Options)) { var relation = await context.Book2Authors.SingleOrDefaultAsync(o => o.BookId == bookId && o.AuthorId == authorId, cancellationToken); if (relation == null) { return(false); } context.Remove(relation); if (await context.SaveChangesAsync(cancellationToken) == 1) { return(true); } } return(false); }
public async Task <bool> InitializeDatabaseAsync() { try { using (var context = new BookLibraryContext(this._optionsBuilder.Options)) { this._logger.LogInformation("Check database created"); await context.Database.EnsureCreatedAsync(); this._logger.LogInformation("Database is created"); #region Customer if (!context.Customers.Any(o => o.Id == 1)) { context.Customers.Add(new Customer { Gender = (int)Gender.Male, Firstname = "Max", Surname = "Mustermann", Street = "Musterweg 1a", PostalCode = "12345", City = "Musterstadt", CountryCode = "DE", Email = "*****@*****.**", }); } if (!context.Customers.Any(o => o.Id == 2)) { context.Customers.Add(new Customer { Gender = (int)Gender.Female, Firstname = "Erika", Surname = "Mustermann", Street = "Musterweg 8c", PostalCode = "1234", City = "Musterhausen", CountryCode = "AT", Email = "*****@*****.**", }); } #endregion #region Book var author = new Author { Name = "Jojo Moyes" }; var publisher = new Publisher { Name = "Rowohlt Polaris" }; var book = new Book { Id = "9783499267369", Title = "Weit weg und ganz nah", Abstract = "Einmal angenommen... dein Mann hat sich aus dem Staub gemacht.", BookCount = 1, }; if (!context.Authors.Any(o => o.Id == 1)) { context.Authors.Add(author); } if (!context.Publishers.Any(o => o.Id == 1)) { context.Publishers.Add(publisher); } //Create ids for publisher and author await context.SaveChangesAsync(); if (!context.Books.Any(o => o.Id == book.Id)) { book.PublisherId = publisher.Id; context.Books.Add(book); context.Book2Authors.Add(new Book2Author { AuthorId = author.Id, BookId = book.Id }); } #endregion //Save book await context.SaveChangesAsync(); return(true); } } catch (Exception exception) { this._logger.LogError(exception, nameof(InitializeDatabaseAsync)); } return(false); }