public void AddOne(Category item) { CustomValidator.ValidateObject(item); var exists = Exists(item.Id); if (!exists) { var hasSameName = _context.Categories.Any(cat => cat.Name.ToLower().Equals(item.Name.ToLower())); if (hasSameName) { return; } var enState = _context.Categories.Add(_categoryMapper.DomainToDb(item)); enState.State = EntityState.Added; _context.SaveChanges(); } }
public async Task UpdateOne(Category item) { CustomValidator.ValidateObject(item); await using var context = new MyDbContext(_options); _categoryMapper = new CategoryMapper(context); if (Exists(item.Id)) { var exists = await HasSameNameAsync(item); if (!exists) { var enState = context.Categories.Update(_categoryMapper.DomainToDb(item)); enState.State = EntityState.Modified; await context.SaveChangesAsync(); } } }