public async Task <int> SaveDataAsync(ProductVM viewModel) { var entity = _mapper.Map <ProductVM, Product>(viewModel); if (viewModel.PublicationDate == DateTime.MinValue) { entity.PublicationDate = DateTime.Now; } //Update product rating var rank = new ProductRank { ProductId = entity.ProductId, Name = entity.Name, CategoryId = entity.CategoryId, Rate = 0, Sold = 0 }; if (viewModel.ProductId == 0) { entity.CreateBy = "admin"; entity.UpdateBy = "admin"; entity.CreateDate = DateTime.Now; entity.UpdatedDate = DateTime.Now; entity.UniqueStringKey = Guid.NewGuid(); entity.Authors = new List <Author>() { await _authorDetailRepository.GetAuthorById(1) }; var zeroDiscount = await _discountRepository.GetSingleByIDAsync(1); entity.Discount = zeroDiscount; await _productRepository.AddAsync(entity); await _productRankRepository.AddAsync(rank); } else { entity.UpdatedDate = DateTime.Now; await _productRepository.UpdateAsync(entity); await _productRankRepository.UpdateAsync(rank); } return(await _unitOfWork.SaveAsync()); }