Skip to content

Instantly share code, notes, and snippets.

@evgomes
Created January 30, 2019 19:04
Show Gist options
  • Save evgomes/aab855d881ba6ac90fabf499a7fec8b6 to your computer and use it in GitHub Desktop.
Save evgomes/aab855d881ba6ac90fabf499a7fec8b6 to your computer and use it in GitHub Desktop.
UpdateAsync logic from CategoryService, of Supermarket API
public async Task<SaveCategoryResponse> UpdateAsync(int id, Category category)
{
var existingCategory = await _categoryRepository.FindByIdAsync(id);
if (existingCategory == null)
return new SaveCategoryResponse("Category not found.");
existingCategory.Name = category.Name;
try
{
_categoryRepository.Update(existingCategory);
await _unitOfWork.CompleteAsync();
return new SaveCategoryResponse(existingCategory);
}
catch (Exception ex)
{
// Do some logging stuff
return new SaveCategoryResponse($"An error occurred when updating the category: {ex.Message}");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment