public async Task<Book> AddAsync(Book item) { Author existingAuthor = await _dbContext.Authors.FirstOrDefaultAsync(r => r.FirstName.ToUpper() == item.Author.FirstName.ToUpper() && r.LastName.ToUpper() == item.Author.LastName.ToUpper()); Category category = await _dbContext.Categories.FirstOrDefaultAsync(r => r.CategoryName.ToUpper() == item.Category.CategoryName.ToUpper()); if (existingAuthor != null && category != null) { item.Author = existingAuthor; item.Category = category; _dbContext.Books.Add(item); _dbContext.SaveChanges(); return item; } Author newAuthor = new Author() { FirstName = item.Author.FirstName, LastName = item.Author.LastName }; Category newCategory = new Category() { CategoryName = item.Category.CategoryName, CategoryDescription = item.Category.CategoryDescription, }; item.Author = newAuthor; item.Category = newCategory; _dbContext.Books.Add(item); await _dbContext.SaveChangesAsync(); return item; }
public async Task<Author> UpdateAsync(Author item) { var updateAuthor = await _dbContext.Authors.SingleOrDefaultAsync(r => r.AuthorId == item.AuthorId); if (updateAuthor != null) { updateAuthor.FirstName = item.FirstName; updateAuthor.LastName = item.LastName; await _dbContext.SaveChangesAsync(); return updateAuthor; } return null; }
public async Task<Author> AddAsync(Author item) { Author existingAuthor = await _dbContext.Authors.SingleOrDefaultAsync(r => r.FirstName.ToUpper() == item.FirstName.ToUpper() && r.LastName.ToUpper() == item.LastName.ToUpper()); if (existingAuthor != null) { return null; } Author newAuthor = new Author() { FirstName = item.FirstName, LastName = item.LastName }; _dbContext.Authors.Add(newAuthor); await _dbContext.SaveChangesAsync(); return item; }