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<Category> AddAsync(Category item) { var existingCategory = _dbContext.Categories.SingleOrDefaultAsync(r => r.CategoryName.ToUpper() == item.CategoryName.ToUpper()); if (existingCategory != null) { return null; } Category category = new Category() { CategoryName = item.CategoryName, CategoryDescription = item.CategoryDescription }; _dbContext.Categories.Add(category); await _dbContext.SaveChangesAsync(); return category; }
public Task<object> RemoveAsync(Category item) { throw new NotImplementedException(); }
public Task<Category> UpdateAsync(Category item) { throw new NotImplementedException(); }