Created
January 30, 2019 16:25
-
-
Save evgomes/72595a698c5e1ddcb688e16f7aeb0539 to your computer and use it in GitHub Desktop.
CategoryService after implementing the SaveAsync method, from Supermarket API
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class CategoryService : ICategoryService | |
{ | |
private readonly ICategoryRepository _categoryRepository; | |
private readonly IUnitOfWork _unitOfWork; | |
public CategoryService(ICategoryRepository categoryRepository, IUnitOfWork unitOfWork) | |
{ | |
_categoryRepository = categoryRepository; | |
_unitOfWork = unitOfWork; | |
} | |
public async Task<IEnumerable<Category>> ListAsync() | |
{ | |
return await _categoryRepository.ListAsync(); | |
} | |
public async Task<SaveCategoryResponse> SaveAsync(Category category) | |
{ | |
try | |
{ | |
await _categoryRepository.AddAsync(category); | |
await _unitOfWork.CompleteAsync(); | |
return new SaveCategoryResponse(category); | |
} | |
catch (Exception ex) | |
{ | |
// Do some logging stuff | |
return new SaveCategoryResponse($"An error occurred when saving the category: {ex.Message}"); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment