public async Task <ActionResult <ResponseModel <string> > > TakeOperation(TakerModel model) { var response = new ResponseModel <string> () { ReponseName = nameof(TakeOperation), Content = null }; double _modelProdcutWeight = model.Weight; string _modelProductName = model.ProductName, _modelUserId = model.UserId; var _takerUserResponse = await _userService.GetUser(_modelUserId); var _takerUser = _takerUserResponse.Content[0]; #region Get products var productResponse = await _productService.GetProductsByName(_modelProductName); if (productResponse.Status.Value != ResponseStatus.Success.Value) { response.Status = productResponse.Status; response.Message = $"{nameof (TakeOperation)} was interrupted due to \"{productResponse.Message}\""; return(response); } var products = productResponse.Content.FindAll(p => (p.UnitPrice == model.UnitPrice) && p.Status == 1); if (products.Count == 0) { Product product = new Product(); product.Name = productResponse.Content[0].Name; product.ImgUrl = productResponse.Content[0].ImgUrl; product.UserId = model.UserId; product.UnitPrice = model.UnitPrice; product.Weight = model.Weight; product.Status = 0; var _createProductResponse = await _productService.CreateProduct(product); //* If operation has interrupted on updating if (_createProductResponse.Status.Value != ResponseStatus.Success.Value) { response.Status = _createProductResponse.Status; response.Message = $"{nameof (TakeOperation)} was interrupted due to \"{_createProductResponse.Message}\""; } response.Message = "Take Order Operation successfully"; response.Status = ResponseStatus.Success; return(response); } //* If operation has interrupted on updating //* Sorting products products = InsertionSort.sort(products); #endregion #region Taker product check bool isThereProduct = false; Product _tmpTakerProduct = new Product(); foreach (var p in _takerUser.Products) { var takerProductResponse = await _productService.GetProduct(p); _tmpTakerProduct = takerProductResponse.Content[0]; if (_tmpTakerProduct.Name == _modelProductName) { isThereProduct = true; break; } } _tmpTakerProduct = new Product(); //* If there is product named by taker wants, Update this product if (!isThereProduct) { _tmpTakerProduct.Id = null; _tmpTakerProduct.Name = products[0].Name; _tmpTakerProduct.ImgUrl = products[0].ImgUrl; _tmpTakerProduct.UserId = _modelUserId; _tmpTakerProduct.Weight = 0; _tmpTakerProduct.Status = 1; //* else, create a new one var _createTakerProductResponse = await _productService.CreateProduct(_tmpTakerProduct); //* If operation has interrupted on updating if (_createTakerProductResponse.Status.Value != ResponseStatus.Success.Value) { response.Status = _createTakerProductResponse.Status; response.Message = $"{nameof (TakeOperation)} was interrupted due to \"{_createTakerProductResponse.Message}\""; } _tmpTakerProduct.Id = _createTakerProductResponse.Content[0].Id; _takerUser.Products.Add(_tmpTakerProduct.Id); var _updateTakerResponse = await _userService.UpdateUser(_takerUser); //* If operation has interrupted on updating if (_createTakerProductResponse.Status.Value != ResponseStatus.Success.Value) { response.Status = _createTakerProductResponse.Status; response.Message = $"{nameof (TakeOperation)} was interrupted due to \"{_createTakerProductResponse.Message}\""; } } #endregion #region Take operation var _tmpTakerProdcutWeight = _modelProdcutWeight; foreach (var _tmpSellerProdcut in products) { var _sellerUserResponse = await _userService.GetUser(_tmpSellerProdcut.UserId); var _sellerUser = _sellerUserResponse.Content[0]; Console.WriteLine("Taker:: " + _tmpTakerProdcutWeight); Console.WriteLine("Seller:: " + _tmpSellerProdcut.Weight); if (_tmpTakerProdcutWeight >= _tmpSellerProdcut.Weight) { //* this operation was make seller's product weight was zero,so this product must be delete _tmpTakerProdcutWeight -= _tmpSellerProdcut.Weight; var _deleteSellerProductResponse = await _productService.DeleteProduct(_tmpSellerProdcut.Id); //* If operation has interrupted on deleting if (_deleteSellerProductResponse.Status.Value != ResponseStatus.Success.Value) { response.Status = _deleteSellerProductResponse.Status; response.Message = $"{nameof (TakeOperation)} was interrupted due to \"{_deleteSellerProductResponse.Message}\""; break; } //* Seller's credit += As much as the weight of the product that the taker wants * Seller's unit price of the product _sellerUser.Credit += _tmpSellerProdcut.Weight * _tmpSellerProdcut.UnitPrice; //* Taker's credit -= As much as the weight of the product that the taker wants * Seller's unit price of the product _takerUser.Credit -= _tmpSellerProdcut.Weight * _tmpSellerProdcut.UnitPrice; _tmpTakerProduct.Weight += _tmpSellerProdcut.Weight; } else { //* this operation was filled as much as the weight of the product that the taker wants and Seller's product weight was decreased _tmpSellerProdcut.Weight -= _tmpTakerProdcutWeight; var _updateSellerProductResponse = await _productService.UpdateProduct(_tmpSellerProdcut); //* If operation has interrupted on updating if (_updateSellerProductResponse.Status.Value != ResponseStatus.Success.Value) { response.Status = _updateSellerProductResponse.Status; response.Message = $"{nameof (TakeOperation)} was interrupted due to \"{_updateSellerProductResponse.Message}\""; break; } //* Seller's credit += As much as the weight of the product that the taker wants * Seller's unit price of the product _sellerUser.Credit += _tmpTakerProdcutWeight * _tmpSellerProdcut.UnitPrice; //* Taker's credit -= As much as the weight of the product that the taker wants * Seller's unit price of the product _takerUser.Credit -= _tmpTakerProdcutWeight * _tmpSellerProdcut.UnitPrice; _tmpTakerProduct.Weight += _tmpTakerProdcutWeight; _tmpTakerProdcutWeight = 0; } var _updateSellerResponse = await _userService.UpdateUser(_sellerUser); //* If operation has interrupted on updating if (_updateSellerResponse.Status.Value != ResponseStatus.Success.Value) { response.Status = _updateSellerResponse.Status; response.Message = $"{nameof (TakeOperation)} was interrupted due to \"{_updateSellerResponse.Message}\""; break; } User accounter = new User(); var _getAccounterResponse = await _userService.GetUser("60cf52c9f33b98db66afd71d"); //* If operation has interrupted on getting if (_getAccounterResponse.Status.Value != ResponseStatus.Success.Value) { response.Status = _getAccounterResponse.Status; response.Message = $"{nameof (TakeOperation)} was interrupted due to \"{_getAccounterResponse.Message}\""; break; } accounter = _getAccounterResponse.Content[0]; accounter.Credit += _takerUser.Credit * (0.01); var _updateAccounterResponse = await _userService.UpdateUser(accounter); //* If operation has interrupted on updating if (_updateAccounterResponse.Status.Value != ResponseStatus.Success.Value) { response.Status = _updateAccounterResponse.Status; response.Message = $"{nameof (TakeOperation)} was interrupted due to \"{_updateAccounterResponse.Message}\""; break; } _takerUser.Credit -= _takerUser.Credit * (0.01); var _updateTakerResponse = await _userService.UpdateUser(_takerUser); //* If operation has interrupted on updating if (_updateTakerResponse.Status.Value != ResponseStatus.Success.Value) { response.Status = _updateTakerResponse.Status; response.Message = $"{nameof (TakeOperation)} was interrupted due to \"{_updateTakerResponse.Message}\""; break; } var _updateTakerProductResponse = await _productService.UpdateProduct(_tmpTakerProduct); //* If operation has interrupted on updating if (_updateTakerProductResponse.Status.Value != ResponseStatus.Success.Value) { response.Status = _updateTakerProductResponse.Status; response.Message = $"{nameof (TakeOperation)} was interrupted due to \"{_updateTakerProductResponse.Message}\""; break; } if (_tmpTakerProdcutWeight == 0) { break; } } #endregion Report report = new Report() { CreatedBy = model.UserId, CreatedDate = DateTime.UtcNow, Operation = OperationType.Take, ProductName = model.ProductName, Weight = model.Weight, UnitPrice = model.UnitPrice }; var ReportResponse = await _reportService.CreateReport(report); if (ReportResponse.Status.Value != ResponseStatus.Success.Value) { response.Status = ReportResponse.Status; response.Message = $"{nameof (TakeOperation)} was interrupted due to \"{ReportResponse.Message}\""; return(response); } response.Message = "Take Operation successfully"; response.Status = ResponseStatus.Success; return(response); }
public async Task <ActionResult <ResponseModel <string> > > SellOperation(SellerModel model) { var response = new ResponseModel <string> () { ReponseName = nameof(SellOperation) }; double _modelProdcutWeight = model.Weight, _modelProductUnitPrice = model.UnitPrice; string _modelUserId = model.UserId, _modelProductId = model.ProductId; //* Get Seller User var sellerUserReponse = await _userService.GetUser(_modelUserId); if (sellerUserReponse.Status.Value != ResponseStatus.Success.Value) { response.Status = sellerUserReponse.Status; response.Message = $"{nameof (SellOperation)} was interrupted due to \"{sellerUserReponse.Message}\""; return(response); } var sellerUser = sellerUserReponse.Content[0]; //*Get Seller's Product var sellerProductResponse = await _productService.GetProduct(_modelProductId); if (sellerProductResponse.Status.Value != ResponseStatus.Success.Value) { response.Status = sellerProductResponse.Status; response.Message = $"{nameof (SellOperation)} was interrupted due to \"{sellerProductResponse.Message}\""; return(response); } var sellerProduct = sellerProductResponse.Content[0]; //* Sell Operation //* The total weight of the seller's product minus the weight of the product the seller wants to sell sellerProduct.Weight = (sellerProduct.Weight - _modelProdcutWeight) > 0 ? (sellerProduct.Weight - _modelProdcutWeight) : 0; if (sellerProduct.Weight == 0) { //*delete seller product var deleteSellerProductResponse = await _productService.DeleteProduct(sellerProduct.Id); if (deleteSellerProductResponse.Status.Value != ResponseStatus.Success.Value) { response.Status = deleteSellerProductResponse.Status; response.Message = $"{nameof (SellOperation)} was interrupted due to \"{deleteSellerProductResponse.Message}\""; return(response); } sellerUser.Products.Remove(sellerProduct.Id); var updateSellerUserResponse = await _userService.UpdateUser(sellerUser); if (updateSellerUserResponse.Status.Value != ResponseStatus.Success.Value) { response.Status = updateSellerUserResponse.Status; response.Message = $"{nameof (SellOperation)} was interrupted due to \"{updateSellerUserResponse.Message}\""; return(response); } } else { //* update seller product var updateSellerProductResponse = await _productService.UpdateProduct(sellerProduct); if (updateSellerProductResponse.Status.Value != ResponseStatus.Success.Value) { response.Status = updateSellerProductResponse.Status; response.Message = $"{nameof (SellOperation)} was interrupted due to \"{updateSellerProductResponse.Message}\""; return(response); } } sellerProduct.UnitPrice = _modelProductUnitPrice; //* Create new product item Product newProduct = new Product(); newProduct = sellerProduct; newProduct.Weight = _modelProdcutWeight; newProduct.Id = null; newProduct.Status = 1; var createSellerProductResponse = await _productService.CreateProduct(newProduct); if (createSellerProductResponse.Status.Value != ResponseStatus.Success.Value) { response.Status = createSellerProductResponse.Status; response.Message = $"{nameof (SellOperation)} was interrupted due to \"{createSellerProductResponse.Message}\""; return(response); } var productWithStatusByNameResponse = await _productService.GetProductsWithStatusByName(sellerProduct.Name); if (productWithStatusByNameResponse.Status.Value != ResponseStatus.Success.Value) { response.Status = productWithStatusByNameResponse.Status; response.Message = $"{nameof (SellOperation)} was interrupted due to \"{productWithStatusByNameResponse.Message}\""; return(response); } /* * If the weight of the product, which is a purchase order at the sold price, * is greater or equal to the weight of the currently sold product; Perform the sell transaction and ignore the next steps, update the weight of the remaining buy order after the sell transaction. * If it is small; Execute the sell transaction and delete the buy order. Update weight of remaining sell order and apply next steps */ foreach (Product product in productWithStatusByNameResponse.Content) { if (product.Weight >= _modelProdcutWeight) { TakerModel takerModel = new TakerModel(); takerModel.UnitPrice = product.UnitPrice; takerModel.Weight = _modelProdcutWeight; takerModel.UserId = product.UserId; takerModel.ProductName = product.Name; await TakeOperation(takerModel); product.Weight -= _modelProdcutWeight; var updateProductResponse = await _productService.UpdateProduct(product); if (updateProductResponse.Status.Value != ResponseStatus.Success.Value) { response.Status = updateProductResponse.Status; response.Message = $"{nameof (SellOperation)} was interrupted due to \"{updateProductResponse.Message}\""; return(response); } } else { TakerModel takerModel = new TakerModel(); takerModel.UnitPrice = product.UnitPrice; takerModel.Weight = product.Weight; takerModel.UserId = product.UserId; takerModel.ProductName = product.Name; await TakeOperation(takerModel); var deleteProductResponse = await _productService.DeleteProduct(product.Id); if (deleteProductResponse.Status.Value != ResponseStatus.Success.Value) { response.Status = deleteProductResponse.Status; response.Message = $"{nameof (SellOperation)} was interrupted due to \"{deleteProductResponse.Message}\""; return(response); } } } Report report = new Report() { CreatedBy = model.UserId, CreatedDate = DateTime.UtcNow, Operation = OperationType.Sell, ProductName = sellerProduct.Name, Weight = model.Weight, UnitPrice = model.UnitPrice }; var ReportResponse = await _reportService.CreateReport(report); if (ReportResponse.Status.Value != ResponseStatus.Success.Value) { response.Status = ReportResponse.Status; response.Message = $"{nameof (TakeOperation)} was interrupted due to \"{ReportResponse.Message}\""; return(response); } response.Message = "Operation successfully"; response.Status = ResponseStatus.Success; return(response); }