public async Task <CategoryResponse> SaveAsync(Category category)
        {
            _unitOfWork.StartTransaction(_session);
            try
            {
                var existingCategory = await _categoryRepository.FindByIdAsync(category.Id);

                if (existingCategory != null)
                {
                    return(new CategoryResponse("Category already existed"));
                }

                await _categoryRepository.AddAsync(category);

                var foundCategory = await FindByIdAsync(category.Id);

                await _unitOfWork.CompleteAsync(_session);

                return(foundCategory);
            }
            catch (Exception ex)
            {
                await _unitOfWork.AbortAsync(_session);

                return(new CategoryResponse($"An error occurred when saving the category: {ex.Message}"));
            }
            finally
            {
                _unitOfWork.EndSession(_session);
            }
        }
Beispiel #2
0
        public async Task <ProductResponse> SaveAsync(Product product)
        {
            _unitOfWork.StartTransaction(_session);
            try
            {
                var existingProduct = await _productRepository.FindByIdAsync(product.Id);

                if (existingProduct != null)
                {
                    return(new ProductResponse("Product already existed"));
                }

                await _productRepository.AddAsync(product);

                var foundProduct = await FindByIdAsync(product.Id);

                await _unitOfWork.CompleteAsync(_session);

                return(foundProduct);
            }
            catch (Exception ex)
            {
                await _unitOfWork.AbortAsync(_session);

                return(new ProductResponse($"An error occurred when saving the product: {ex.Message}"));
            }
            finally
            {
                _unitOfWork.EndSession(_session);
            }
        }