public override async Task <int> HandleCommand(DeleteProductCommand request, CancellationToken cancellationToken) { Product product = null; if (request.Model == 0) { throw new BusinessException("Product.NotSelected"); } else { product = await productQueries.GetByIdAsync(request.Model); if (product == null) { throw new BusinessException("Product.NotSelected"); } } var rs = -1; using (var conn = DALHelper.GetConnection()) { conn.Open(); using (var trans = conn.BeginTransaction()) { try { product.IsDeleted = true; product.ModifiedDate = DateTime.Now; product.ModifiedBy = request.LoginSession.Id; if (await productRepository.UpdateAsync(product) > 0) { rs = 0; } } catch (Exception ex) { throw ex; } finally { if (rs == 0) { trans.Commit(); } else { try { trans.Rollback(); } catch { } } } } } return(rs); }
public async Task <APIResult> GetById(int id) { var rs = await productQueries.GetByIdAsync(id); return(new APIResult() { Result = 0, Data = rs }); }
public override async Task <int> HandleCommand(UpdateProductCommand request, CancellationToken cancellationToken) { Product product = null; if (request.Model == null || request.Model.Id == 0) { throw new BusinessException("Product.NotExisted"); } else { product = await productQueries.GetByIdAsync(request.Model.Id); if (product == null) { throw new BusinessException("Product.NotExisted"); } } if ((request.Model.ImageData?.Length ?? 0) > Constant.MaxImageLength) { throw new BusinessException("Image.OutOfLength"); } var rs = -1; using (var conn = DALHelper.GetConnection()) { conn.Open(); using (var trans = conn.BeginTransaction()) { try { request.Model.CreatedBy = product.CreatedBy; request.Model.CreatedDate = product.CreatedDate; request.Model.ModifiedDate = DateTime.Now; request.Model.ModifiedBy = request.LoginSession.Id; var filePath = await storageFile.getProductFilePathAsync(request.Model); if (request.Model.IsChangedImage && (request.Model.ImageData?.Length ?? 0) > 0) { string oldLogoPath = product?.ImagePath ?? ""; string type = CommonHelper.GetImageType(System.Text.Encoding.ASCII.GetBytes(request.Model.ImageData)); if (!CommonHelper.IsImageType(type)) { throw new BusinessException("Image.WrongType"); } string base64Data = request.Model.ImageData.Substring(request.Model.ImageData.IndexOf(",") + 1); string fileName = Guid.NewGuid().ToString().Replace("-", ""); request.Model.ImagePath = await storageFile.SaveProductImage(filePath, type, base64Data); if (string.IsNullOrWhiteSpace(oldLogoPath)) { var imageFolderPath = settingQueries.GetValueAsync(SettingKeys.Path_Product); var scheduleAction = (new RemoveFileAction() { FilePath = $"{imageFolderPath}/{oldLogoPath}" }) .GetScheduleAction(request.LoginSession?.Id ?? 0); await scheduleActionRepository.AddAsync(scheduleAction); } } if (request.Model.ProductLanguages != null && request.Model.ProductLanguages.Count > 0) { foreach (var plang in request.Model.ProductLanguages) { if (plang.Id <= 0) { await productLanguageRepository.AddAsync(plang); } else { await productLanguageRepository.UpdateAsync(plang); } } } if (await productRepository.UpdateAsync(request.Model) > 0) { rs = 0; } } catch (Exception ex) { throw ex; } finally { if (rs == 0) { trans.Commit(); } else { try { trans.Rollback(); } catch { } } } } } return(rs); }
public override async Task <int> HandleCommand(InsertProductCommand request, CancellationToken cancellationToken) { var id = 0; using (var conn = DALHelper.GetConnection()) { conn.Open(); using (var trans = conn.BeginTransaction()) { try { request.Model.CreatedDate = DateTime.Now; request.Model.CreatedBy = request.LoginSession?.Id ?? 0; request.Model.ModifiedDate = DateTime.Now; request.Model.ModifiedBy = request.LoginSession?.Id ?? 0; id = await productRepository.AddAsync(request.Model); Product product = await productQueries.GetByIdAsync(id); if (request.Model.ProductLanguages != null && request.Model.ProductLanguages.Count > 0) { foreach (var plang in request.Model.ProductLanguages) { if (plang.Id <= 0) { plang.ProductId = id; await productLanguageRepository.AddAsync(plang); } else { await productLanguageRepository.UpdateAsync(plang); } } } var filePath = await storageFile.getProductFilePathAsync(product); if ((request.Model.ImageData?.Length ?? 0) > Constant.MaxImageLength) { throw new BusinessException("Image.OutOfLength"); } if (request.Model.IsChangedImage && (request.Model.ImageData?.Length ?? 0) > 0) { string type = CommonHelper.GetImageType(System.Text.Encoding.ASCII.GetBytes(request.Model.ImageData)); if (!CommonHelper.IsImageType(type)) { throw new BusinessException("Image.WrongType"); } string base64Data = request.Model.ImageData.Substring(request.Model.ImageData.IndexOf(",") + 1); string fileName = Guid.NewGuid().ToString().Replace("-", ""); product.ImagePath = await storageFile.SaveProductImage(filePath, type, base64Data); await productRepository.UpdateAsync(product); } } catch (Exception ex) { throw ex; } finally { if (id > 0) { trans.Commit(); } else { try { trans.Rollback(); } catch { } } } } } return(id); }
public async Task <IActionResult> GetById(Guid id) { var product = await _productQueries.GetByIdAsync(new ProductGetRequest { Id = id }); return(Ok(product)); }