public void Update(Subcategory subcategory) { var currentSubcategory = Get(subcategory.Id); if (currentSubcategory.CanEdit) { var exists = _repository.GetAll(subcategory.Name, subcategory.CategoryId) .Count(s => !s.Id.Equals(subcategory.Id)) > 0; subcategory.Enabled = true; subcategory.CanEdit = true; if (!exists) { _repository.Update(subcategory); } else { throw new ObjectAlreadyExistsException($"Already exists a subcategory with the name {subcategory.Name}"); } } else { throw new InvalidOperationException("This subcategory was generated by another routine and can not be edited"); } }
/// <summary> /// Update an existing subcategory /// </summary> /// <param name="subcategory"></param> public async Task Update(Subcategory subcategory) { var currentSubcategory = await GetById(subcategory.Id); if (currentSubcategory.CanEdit) { var quantity = (await _repository.GetSubcategoriesByName(subcategory.Name, subcategory.CategoryId)) .Count(s => !s.Id.Equals(currentSubcategory.Id)); subcategory.Enabled = true; subcategory.CanEdit = currentSubcategory.CanEdit; if (quantity.Equals(0)) { await _repository.Update(subcategory); } else { throw new AlreadyExistsException($"Already exists a subcategory {subcategory.Name} in this category"); } } else { throw new NotValidOperationException("This subcategory was generated by other routine and cannot be changed"); } }