public async Task <APIResult> Get(int productId) { return(new APIResult() { Result = 0, Data = await productQueries.Get(productId) }); }
public override async Task <int> HandleCommand(UpdateCommand request, CancellationToken cancellationToken) { if (request.Product == null || request.Product.Id == 0) { throw new BusinessException("Product.NotExisted"); } var product = (await productQueries.Get(request.Product.Id)).FirstOrDefault(); if (product == null) { throw new BusinessException("Product.NotExisted"); } if (request.Product.ImageData?.Length > Constant.MaxImageLength) { throw new BusinessException("Image.OutOfLength"); } string oldImageUrl = request.Product.ImageURL; var rs = -1; using (var conn = DALHelper.GetConnection()) { conn.Open(); using (var trans = conn.BeginTransaction()) { try { //With ImageData < 100byte. This is a link image. With Image > 100byte, It can a real imageData. if (request.Product.ImageData?.Length > 200) { string type = CommonHelper.GetImageType(System.Text.Encoding.ASCII.GetBytes(request.Product.ImageData)); if (!CommonHelper.IsImageType(type)) { throw new BusinessException("Image.WrongType"); } string Base64StringData = request.Product.ImageData.Substring(request.Product.ImageData.IndexOf(",") + 1); string fileName = Guid.NewGuid().ToString().Replace("-", ""); request.Product.ImageURL = CommonHelper.SaveImage($"{GlobalConfiguration.ProductImagePath}/{DateTime.Now.ToString("yyyyMM")}/", fileName, type, Base64StringData); } request.Product.CreatedDate = product.CreatedDate; request.Product.CreatedBy = product.CreatedBy; request.Product = UpdateBuild(request.Product, request.LoginSession); request.Product.Code = product.Code; rs = await productRepository.Update(request.Product); if (rs != 0) { return(-1); } //for language // languages foreach (var item in request.Product.Languages) { item.ProductId = request.Product.Id; await productRepository.AddOrUpdateLanguage(item); } // prices foreach (var item in request.Product.Prices) { item.ProductId = request.Product.Id; if (item.Id != 0) { await productRepository.UpdatePrice(item); } else { await productRepository.AddPrice(item); } } rs = 0; } catch (Exception ex) { throw ex; } finally { if (rs == 0) { trans.Commit(); if (request.Product.ImageData?.Length > 200) { LogHelper.GetLogger().Debug($"ImageData Length: {request.Product.ImageData.Length}. Deleted Image: {oldImageUrl}"); CommonHelper.DeleteImage(oldImageUrl); } } else { try { trans.Rollback(); } catch { } LogHelper.GetLogger().Debug($"Delete Image for process failed. Deleted Image: {request.Product.ImageURL}"); CommonHelper.DeleteImage(request.Product.ImageURL); } } } } return(rs); }
public ProductViewModel Get(Guid id) { return(ProductMapper.DtoToViewModel(_queries.Get(id).Result)); }
public override async Task <int> HandleCommand(UpdateCommand request, CancellationToken cancellationToken) { if (request.Order == null || request.Order.Id == 0 || request.Order.Items == null || request.Order.Items.Count == 0) { return(-1); } var rs = 0; using (var conn = DALHelper.GetConnection()) { conn.Open(); using (var trans = conn.BeginTransaction(System.Data.IsolationLevel.ReadCommitted)) { try { farmerOrderRepository.JoinTransaction(conn, trans); farmerOrderQueries.JoinTransaction(conn, trans); productQueries.JoinTransaction(conn, trans); var order = await farmerOrderQueries.Get(request.Order.Id); if (order.FarmerId != request.Order.FarmerId) { return(rs = -1); //fake api } #warning Hoang Uncompleted //check business update farmer order here bool isOk = false; if (isOk) { return(rs = -1); //Time out } decimal totalAmount = 0; await farmerOrderRepository.DeleteItems(request.Order.Id); foreach (var item in request.Order.Items) { var prod = (await productQueries.Get(item.ProductId)).FirstOrDefault(p => p.CurrentUoM == item.UoMId); if (prod == null) { throw new Exception("Product doesn't existed"); } item.Id = request.Order.Id; item.StatusId = (int)FarmerOrderStatuses.BeginOrder; item.DeliveriedQuantity = 0; item.Price = prod.BuyingCurrentPrice; await farmerOrderRepository.AddItem(item); totalAmount += prod.BuyingCurrentPrice * item.OrderedQuantity; } request.Order = UpdateBuild(request.Order, request.LoginSession); request.Order.StatusId = (int)FarmerOrderStatuses.BeginOrder; request.Order.FarmerBuyingCalendarId = order.FarmerBuyingCalendarId; request.Order.TotalAmount = totalAmount; if (await farmerOrderRepository.Update(request.Order) != 0) { throw new Exception("Update Order failed"); } } catch (Exception ex) { rs = -1; throw ex; } finally { if (rs == 0) { trans.Commit(); } else { try { trans.Rollback(); } catch { } } } } } return(rs); }