/// <summary> /// this method adds product to category the category /// </summary> /// <param name="productId">product id</param> /// <param name="categoryId">category id</param> /// <param name="currentUserId">category id</param> public async Task <string> AssignProductToCategoryAsync(int productId, int categoryId, string currentUserId) { var product = await _productRepository.GetByIdAsync(productId); var category = _categoryRepository.FindAll().FirstOrDefault(x => x.Id == categoryId); var ProductCategory = _productCategoryRepository.FindAll().FirstOrDefault(x => x.CategoryId == categoryId && x.ProductId == productId); if (ProductCategory != null) { throw new AuctionException("Product alredy in category", System.Net.HttpStatusCode.BadRequest); } else if (product == null) { throw new AuctionException("Product not found", System.Net.HttpStatusCode.NotFound); } else if (category == null) { throw new AuctionException("Category not found", System.Net.HttpStatusCode.NotFound); } else if (product.UserId != currentUserId) { throw new AuctionException("Product doesn't belong to user", System.Net.HttpStatusCode.Forbidden); } await _productCategoryRepository.AddAsync(new ProductCategory { CategoryId = categoryId, ProductId = productId }); await _productCategoryRepository.SaveAsync(); return($"Product {product.Name} has been assign to category {category.Name}"); }
public async Task HandleAsync(AddProductCategory command, ICorrelationContext context) { _logger.LogInformation($"Adding a product category: name = {command.Name}' Id = '{command.Id}'"); var productCategory = new ProductCategory(command.Id, command.Name); await _productCategoryRepository.AddAsync(productCategory); _logger.LogInformation($"Added a product category: name = {command.Name}' Id = '{command.Id}'"); await _busPublisher.PublishAsync(new ProductCategoryAdded(command.Id, command.Name), context); }
public async Task addasync_invoke_once_time() { //Arrange _mockMongoRepository.Setup(s => s.AddAsync(_productCategory)).Returns(Task.CompletedTask); //Act await _repository.AddAsync(_productCategory); //Assert _mockMongoRepository.Verify(r => r.AddAsync(_productCategory), Times.Once); }
public async Task <AddProductCategoryResponse> SaveAsync(ProductCategory category) { try { await _categoryRepository.AddAsync(category); await _unitOfWork.CompleteAsync(); return(new AddProductCategoryResponse(category)); } catch (Exception ex) { // Do some logging stuff return(new AddProductCategoryResponse($"An error occurred when saving the category: {ex.Message}")); } }
public async Task AddAsync(string name, string nameEng, Guid?productCategoryId) { var productCategory = new ProductCategory(name, nameEng); if (productCategoryId == null) { await productCategoryRepository.AddAsync(productCategory); } else { var rootProductCategory = await productCategoryRepository.GetAndEnsureExistAsync(productCategoryId.Value); rootProductCategory.AddCategoryProduct(productCategory); } await productCategoryRepository.SaveChangesAsync(); }
public async Task <ApiRequestResult> AddAsync(ProductCategoryDto dto) { var command = dto.EntityMap <ProductCategoryDto, ProductCategory>(); command.Id = Guid.NewGuid(); await _productCategoryRepository.AddAsync(command); foreach (var item in dto.ProductAttributeIdList) { var productAttrCateR = new ProductCategoryAttributeRelation { ProductCategoryId = command.Id, ProductAttributeId = item, }; await _productCategoryRepository.AddProductCateAttrAsync(productAttrCateR); } return(ApiRequestResult.Success("添加成功")); }