public async Task <Result <CourseCategoryDb> > UpdateAsync(CourseCategoryDb categoryDb) { try { if (categoryDb == null) { throw new ArgumentNullException("category"); } context.Entry(categoryDb).State = EntityState.Modified; await context.SaveChangesAsync().ConfigureAwait(false); return(Result <CourseCategoryDb> .Ok(categoryDb)); } catch (DbUpdateConcurrencyException e) { return((Result <CourseCategoryDb>) Result <CourseCategoryDb> .Fail <CourseCategoryDb>($"Cannot save category. {e.Message}")); } catch (DbUpdateException e) { return((Result <CourseCategoryDb>) Result <CourseCategoryDb> .Fail <CourseCategoryDb>($"Cannot save category. Duplicate field. {e.Message}")); } catch (DbEntityValidationException e) { return((Result <CourseCategoryDb>) Result <CourseCategoryDb> .Fail <CourseCategoryDb>($"Invalid category. {e.Message}")); } }
private async Task <CourseCategoryDb> CreateCategoryDbAsync(CourseCategoryDb category) { context.CourseCategories.Add(category); await context.SaveChangesAsync().ConfigureAwait(false); return(context.CourseCategories.FirstOrDefault(x => x.Name == category.Name)); }
public async Task <Result <CourseCategoryDb> > DeleteAsync(CourseCategoryDb categoryDb) { try { context.CourseCategories.Remove(categoryDb); await context.SaveChangesAsync().ConfigureAwait(false); return(Result <CourseCategoryDb> .Ok(categoryDb)); } catch (DbUpdateConcurrencyException e) { return(Result <CourseCategoryDb> .Fail <CourseCategoryDb>($"Category not deleted. {e.Message}")); } catch (DbEntityValidationException e) { return(Result <CourseCategoryDb> .Fail <CourseCategoryDb>($"Invalid category. {e.Message}")); } }
public async Task <Result <CourseCategoryDb> > AddAsync(CourseCategoryDb category) { try { context.CourseCategories.Add(category); await context.SaveChangesAsync().ConfigureAwait(false); return(Result <CourseCategoryDb> .Ok(category)); } catch (DbUpdateConcurrencyException e) { return((Result <CourseCategoryDb>) Result <CourseCategoryDb> .Fail <CourseCategoryDb>($"Cannot save category. {e.Message}")); } catch (DbUpdateException e) { return((Result <CourseCategoryDb>) Result <CourseCategoryDb> .Fail <CourseCategoryDb>($"Cannot save category. Duplicate field. {e.Message}")); } catch (DbEntityValidationException e) { return((Result <CourseCategoryDb>) Result <CourseCategoryDb> .Fail <CourseCategoryDb>($"Invalid category. {e.Message + "/n" + e.EntityValidationErrors}")); } }