public async Task <IActionResult> Delete(int?id) { Book book = await db.Book.FindAsync(id); try { if (book == null) { return(RedirectToAction(nameof(Show))); } db.Book.Remove(book); await db.SaveChangesAsync(); return(RedirectToAction(nameof(Show))); } catch (DbUpdateException) { ModelState.AddModelError("", "Unable to save changes. " + "Please try again."); } return(RedirectToAction(nameof(Show))); }
public async Task <IActionResult> Create([Bind("OrderID,BookID,ReaderID,OrderDate,OrderReturnDate")] Order order) { if (ModelState.IsValid) { _context.Add(order); await _context.SaveChangesAsync(); var book = await _context.Books.FindAsync(order.BookID); book.Aviability = false; _context.Update(book); await _context.SaveChangesAsync(); return(RedirectToAction(nameof(Index))); } ViewData["BookID"] = new SelectList(_context.Books, "BookID", "BookID", order.BookID); ViewData["ReaderID"] = new SelectList(_context.Readers, "ID", "ID", order.ReaderID); return(View(order)); }
public async Task <Genre> CreateGenreAsync(string genreName) { var existingGenre = await _context.Genres.FirstOrDefaultAsync(g => g.Name.ToLower() == genreName.ToLower()).ConfigureAwait(false); if (existingGenre is null) { var newGenre = new Genre { Name = genreName }; await _context.Genres.AddAsync(newGenre).ConfigureAwait(false); await _context.SaveChangesAsync().ConfigureAwait(false); return(newGenre); } else { return(existingGenre); } }
public async Task <Unit> Handle(Execute request, CancellationToken cancellationToken) { var book = new Book { Title = request.Title, ReleaseDate = request.ReleaseDate, Author = request.Author }; _context.Book.Add(book); var rows = await _context.SaveChangesAsync(); _eventBus.Publish(new EmailEventQueue("*****@*****.**", request.Title, "Mail from RabbitMQ")); if (rows > 0) { return(Unit.Value); } throw new Exception("There was an error inserting Book"); }
public async Task ApplyPatch <TEntity, TKey>(TEntity entityName, List <PatchDto> patchDtos) where TEntity : class { var nameValuePairProperties = patchDtos.ToDictionary(a => a.PropertyName, a => a.PropertyValue); var dbEntityEntry = _libraryContext.Entry(entityName); dbEntityEntry.CurrentValues.SetValues(nameValuePairProperties); dbEntityEntry.State = EntityState.Modified; await _libraryContext.SaveChangesAsync(); /* example of what it accept over http, note that property names should match the entity * [ * { * "PropertyName": "ModifiedDate", * "PropertyValue": null * }, * { * "PropertyName": "CreatedDate", * "PropertyValue": "2020-01-30T12:53:51.6240518" * } * ] */ }
public async Task UpdateAuthor() { var options = getDatabase("UpdateAuthorDB"); await using (var context = new LibraryContext(options)) { var _manager = new LibraryManager(context); await _manager.AddAuthor(SingleAuthor()); await context.SaveChangesAsync(); } await using (var context = new LibraryContext(options)) { var _manager = new LibraryManager(context); var checkUpdate = await _manager.UpdateAuthor( new Author { Name = "AuthorName" }, 1); await context.SaveChangesAsync(); Assert.Equal(1, checkUpdate); Assert.Equal("AuthorName", context.Authors.Single().Name); } }
public async Task <Book> Handle(CreateBookCommand command) { var book = new Book { CategoryId = command.CategoryId, AuthorId = command.AuthorId, Title = command.Title, CountryId = command.CountryId, LanguageId = command.LanguageId, Description = command.Description, PublisherId = command.PublisherId }; _libraryContext.Set <Book>().Add(book); await _libraryContext.SaveChangesAsync(); return(_libraryContext.Set <Book>() .Include(e => e.Author) .Include(e => e.Category) .Include(e => e.Country) .Include(e => e.Language) .Include(e => e.Publisher) .SingleOrDefault(e => e.Id == book.Id)); }
public async Task <IActionResult> Create([Bind("Id,Name,PadgesCount,PublishingYear,PublisherName,Image,Authors")] Book book, IFormFile formFile) { if (formFile != null) { byte[] imageData = null; // считываем переданный файл в массив байтов using (var binaryReader = new BinaryReader(formFile.OpenReadStream())) { imageData = binaryReader.ReadBytes((int)formFile.Length); } // установка массива байтов book.Image = imageData; } if (book.Authors.Count == 0) { ModelState.AddModelError("Authors", "Должен быть хотя бы один автор"); } foreach (var a in book.Authors) { if (string.IsNullOrEmpty(a.Name)) { ModelState.AddModelError("Authors", "Введите имя автора"); } if (string.IsNullOrEmpty(a.Surname)) { ModelState.AddModelError("Authors", "Введите фамилию автора"); } } if (ModelState.IsValid) { _context.Add(book); await _context.SaveChangesAsync(); return(RedirectToAction(nameof(Index))); } return(View(book)); }
public async Task<bool> SaveChangesAsync() { // return true if 1 or more entities were changed return (await _context.SaveChangesAsync() > 0); }
public async Task AddAsync(LibraryBranch newBranch) { _context.Add(newBranch); await _context.SaveChangesAsync(); }
private async Task AddBookToInventory() { int?bookNum = await validateBookNumber(); if (!bookNum.HasValue) { txtBookNumber.SelectAll(); txtBookNumber.Focus(); return; } if (!validateAgeRestrictions()) { return; } if (!validateGradeRestrictions()) { return; } CatalogEntry entry; if (tbiLocal.IsSelected) { if (lvwLocal.SelectedValue == null || !(lvwLocal.SelectedValue is CatalogEntry catalogEntry)) { return; } entry = catalogEntry; } else if (tbiGoogle.IsSelected) { if (googleSearch.SelectedVolume == null) { return; } entry = CreateEntryFromGoogle(googleSearch.SelectedVolume); } else if (tbiManual.IsSelected) { if (!manualEditor.Validate()) { manualEditor.Focus(); return; } entry = manualEditor.SelCatalogEntry; } else { return; } if (!tbiLocal.IsSelected) { _db.Add(entry); } entry.MaxAge = ageRestrict.MaxAge; entry.MinAge = ageRestrict.MinAge; entry.MinGrade = gradeRestrict.MinGrade; entry.MaxGrade = gradeRestrict.MaxGrade; entry.ShelfLocation = txtShelf.Text.Trim(); entry.AddOwnedBook(bookNum.Value); await _db.SaveChangesAsync(); await updateMaxBookNumber(); txtBookNumber.Clear(); ClearSearch(); }
public Task Save() { return(_context.SaveChangesAsync()); }
public async Task InsertGender(Gender genero) { await _context.Gender.AddAsync(genero); await _context.SaveChangesAsync(); }
public async Task AddAsync(User newUser) { var entity = Mapper.Map<Entities.User>(newUser); await _context.AddAsync(entity); await _context.SaveChangesAsync(); }
public async void Add(Checkout c) { await _context.Checkouts.AddAsync(c); await _context.SaveChangesAsync(); }
public async Task EditBookAsync(string bookId, string title, string isbn, int year, int rack, string authorId, string publisherId, List <int> genresIds) { var tempBook = await GetBookByIdAsync(bookId); var allBooks = await GetAllBooksAsync(); var booksToEditIsbn = allBooks .Where(b => b.ISBN == tempBook.ISBN).ToList(); foreach (var book in booksToEditIsbn) { if (book.Title != title) { book.Title = title; } if (book.ISBN != isbn) { book.ISBN = isbn; } if (book.Year != year) { book.Year = year; } if (book.Rack != rack) { book.Rack = rack; } var newAuthor = await _context.Authors.FirstOrDefaultAsync(a => a.Id.ToString() == authorId).ConfigureAwait(false); book.AuthorId = newAuthor.Id; var newPublisher = await _context.Publishers.FirstOrDefaultAsync(p => p.Id.ToString() == publisherId).ConfigureAwait(false); book.PublisherId = newPublisher.Id; var genres = await _context.Genres.ToListAsync().ConfigureAwait(false); // Clean all genres for the book foreach (var bookGenre in _context.BookGenre) { if (bookGenre.BookId == book.Id) { _context.BookGenre.Remove(bookGenre); } } await _context.SaveChangesAsync().ConfigureAwait(false); // Add the genres to the clean table foreach (var genre in genres) { foreach (var genreParam in genresIds) { if (genre.Id == genreParam) { _context.BookGenre.Add(new BookGenre { BookId = book.Id, GenreId = genreParam }); } } } } await _context.SaveChangesAsync().ConfigureAwait(false); }
public async Task InsertBook(Book book) { _context.Books.Add(book); await _context.SaveChangesAsync(); }
public async Task InsertAuthor(Author autor) { await _dbContext.AddAsync(autor); await _dbContext.SaveChangesAsync(); }
public async Task DeleteAllUsers() { _context.User.RemoveRange(_context.User); await _context.SaveChangesAsync(); }
public async Task AddUserAsync(User user) { await _context.AddAsync(user); await _context.SaveChangesAsync(); }
public async Task InsertEditorial(Editorial editorial) { _context.Editorials.Add(editorial); await _context.SaveChangesAsync(); }
public async Task DeleteLibrary(Library library) { _context.Libraries.Remove(library); await _context.SaveChangesAsync(); }
public async Task AddAsync(Checkout newCheckout) { _context.Add(newCheckout); await _context.SaveChangesAsync(); }
public async Task AddExemplary(Exemplary entidade) { await _context.Exemplary.AddAsync(entidade); await _context.SaveChangesAsync(); }
public async Task Save() { await _context.SaveChangesAsync(); }
public async Task Add(Author author) { await _context.Authors.AddAsync(author); await _context.SaveChangesAsync(); }
public async void AddLibraryAsset(LibraryData.Models.LibraryAsset libraryAsset) { await _Context.LibraryAssets.AddAsync(libraryAsset); await _Context.SaveChangesAsync(); }
public async Task Add(Borrow borrow) { _context.Borrows.Update(borrow); await _context.SaveChangesAsync(); }
public async Task Create(T t) { _dbSet.Add(t); await _data.SaveChangesAsync(); }
public async Task DeleteBook(Book book) { _context.Books.Remove(book); await _context.SaveChangesAsync(); }