Пример #1
0
        public async Task <CategoryResponse> DeleteAsync(int id)
        {
            var existingCategory = await _categoryRepository.FindByIdAsync(id);

            if (existingCategory == null)
            {
                return(new CategoryResponse("Category not found."));
            }

            try
            {
                _categoryRepository.Remove(existingCategory);
                await _unitOfWork.CompleteAsync();

                return(new CategoryResponse(existingCategory));
            }
            catch (Exception ex)
            {
                // Do some logging stuff
                return(new CategoryResponse($"An error occurred when deleting the category: {ex.Message}"));
            }
        }
Пример #2
0
        public async Task <CategoryResponse> UpdateAsync(int id, Category category)
        {
            var existingCatagory = await _categoryRespository.FindByIdAsync(id);

            if (existingCatagory == null)
            {
                return(new CategoryResponse("Category not found"));
            }

            existingCatagory.Name = category.Name;

            try
            {
                _categoryRespository.Update(existingCatagory);
                await _unitofWork.CompleteAsyc();

                return(new CategoryResponse(existingCatagory));
            }
            catch (Exception e)
            {
                return(new CategoryResponse($"An error occured when updating the category: {e.Message}"));
            }
        }