public async Task <int> AddAsync(Message msg, CancellationToken cancellationToken = default(CancellationToken)) { msg.Code = await _storageQueries.GenarateCodeAsync(StorageKeys.MessageCode); var cmd = QueriesCreatingHelper.CreateQueryInsert(msg); cmd += ";SELECT LAST_INSERT_ID();"; return((await DALHelper.ExecuteQuery <int>(cmd, dbTransaction: DbTransaction, connection: DbConnection)).First()); }
public override async Task <int> HandleCommand(AddCommand request, CancellationToken cancellationToken) { if ((request.Fertilizer?.CategoryId ?? 0) > 0) { var category = await fertilizerCategoryQueries.GetById(request.Fertilizer.CategoryId ?? 0); if (category == null) { throw new BusinessException("Category.NotExisted"); } } var rs = -1; using (var conn = DALHelper.GetConnection()) { conn.Open(); using (var trans = conn.BeginTransaction()) { try { fertilizerRepository.JoinTransaction(conn, trans); fertilizerQueries.JoinTransaction(conn, trans); if (request.Fertilizer == null) { throw new BusinessException("AddWrongInformation"); } request.Fertilizer.Code = await storageQueries.GenarateCodeAsync(StorageKeys.FertilizerCode); request.Fertilizer = CreateBuild(request.Fertilizer, request.LoginSession); var fertilizerId = await fertilizerRepository.Add(request.Fertilizer); rs = 0; } finally { if (rs == 0) { trans.Commit(); } else { try { trans.Rollback(); } catch { } } } } } return(rs); }
public override async Task <int> HandleCommand(AddCommand request, CancellationToken cancellationToken) { var rs = -1; using (var conn = DALHelper.GetConnection()) { conn.Open(); using (var trans = conn.BeginTransaction()) { try { categoryRepository.JoinTransaction(conn, trans); categoryQueries.JoinTransaction(conn, trans); if (request.Category == null) { throw new BusinessException("AddWrongInformation"); } request.Category.Code = await storageQueries.GenarateCodeAsync(StorageKeys.CategoryCode); request.Category = CreateBuild(request.Category, request.LoginSession); var categoryId = await categoryRepository.Add(request.Category); // languages foreach (var item in request.Category.Languages) { item.CategoryId = categoryId; await categoryRepository.AddOrUpdateLanguage(item); } rs = 0; } finally { if (rs == 0) { trans.Commit(); } else { try { trans.Rollback(); } catch { } } } } } return(rs); }
public async Task <int> InsertPOAsync(DateTime dateRequest, UserSession userSession) { var po = new PurchaseOrder(); po.Code = await _storageQueries.GenarateCodeAsync(StorageKeys.PurchaseOrderCode); po.DateCode = dateRequest.ToString("yyyyMMdd"); po.IsDeleted = false; po.CreatedBy = userSession.Id; po.CreatedDate = DateTime.Now; po.ModifiedBy = userSession.Id; po.ModifiedDate = DateTime.Now; return(await _purchaseOrderRepository.AddAsync(po)); }
public override async Task <int> HandleCommand(AddCommand request, CancellationToken cancellationToken) { if (request.Method == null) { throw new BusinessException("AddWrongInformation"); } var rs = -1; using (var conn = DALHelper.GetConnection()) { conn.Open(); using (var trans = conn.BeginTransaction()) { try { methodRepository.JoinTransaction(conn, trans); methodQueries.JoinTransaction(conn, trans); request.Method.Code = await storageQueries.GenarateCodeAsync(StorageKeys.MethodCode); request.Method = CreateBuild(request.Method, request.LoginSession); var methodId = await methodRepository.Add(request.Method); rs = 0; } finally { if (rs == 0) { trans.Commit(); } else { try { trans.Rollback(); } catch { } } } } } return(rs); }
public override async Task <int> HandleCommand(UpdateCommand request, CancellationToken cancellationToken) { if (request.Method == null || request.Method.Id == 0) { throw new BusinessException("Method.NotExisted"); } var method = await methodQueries.GetById(request.Method.Id); if (method == null) { throw new BusinessException("Method.NotExisted"); } var rs = -1; using (var conn = DALHelper.GetConnection()) { conn.Open(); using (var trans = conn.BeginTransaction()) { try { request.Method.CreatedDate = method.CreatedDate; request.Method.CreatedBy = method.CreatedBy; request.Method = UpdateBuild(request.Method, request.LoginSession); request.Method.Code = string.IsNullOrWhiteSpace(method.Code) ? (await storageQueries.GenarateCodeAsync(StorageKeys.MethodCode)) : method.Code; rs = await methodRepository.Update(request.Method); if (rs == 0) { return(-1); } 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(UpdateCommand request, CancellationToken cancellationToken) { if (request.Farmer == null || request.Farmer.Id == 0 || request.Farmer.Address == null || request.Farmer.Contact == null) { throw new BusinessException("Farmer.NotExisted"); } var farmer = await farmerQueries.Get(request.Farmer.Id); if (farmer == null) { throw new BusinessException("Farmer.NotExisted"); } string oldImageUrl = request.Farmer.ImageURL; //With ImageData < 100byte. This is a link image. With Image > 100byte, It can a real imageData. if (request.Farmer.ImageData?.Length > 100) { string type = CommonHelper.GetImageType(System.Text.Encoding.ASCII.GetBytes(request.Farmer.ImageData)); if (!CommonHelper.IsImageType(type)) { throw new BusinessException("Image.WrongType"); } string Base64StringData = request.Farmer.ImageData.Substring(request.Farmer.ImageData.IndexOf(",") + 1); string fileName = Guid.NewGuid().ToString().Replace("-", ""); request.Farmer.ImageURL = CommonHelper.SaveImage($"{GlobalConfiguration.FarmerImagePath}/{DateTime.Now.ToString("yyyyMM")}/", fileName, type, Base64StringData); } var rs = -1; using (var conn = DALHelper.GetConnection()) { conn.Open(); using (var trans = conn.BeginTransaction()) { try { farmerRepository.JoinTransaction(conn, trans); locationRepository.JoinTransaction(conn, trans); farmer.Address = farmer.Address ?? new Address(); farmer.Address.Street = request.Farmer.Address.Street; farmer.Address.CountryId = request.Farmer.Address.CountryId; farmer.Address.ProvinceId = request.Farmer.Address.ProvinceId; farmer.Address.DistrictId = request.Farmer.Address.DistrictId; farmer.Address.WardId = request.Farmer.Address.WardId; farmer.Address.Longitude = request.Farmer.Address.Longitude; farmer.Address.Latitude = request.Farmer.Address.Latitude; farmer.Address = UpdateBuild(farmer.Address, request.LoginSession); if (await locationRepository.AddOrUpdateAddress(farmer.Address) == -1) { return(rs = -1); } farmer.Contact = farmer.Contact ?? new Contact(); farmer.Contact.Name = request.Farmer.Contact.Name; farmer.Contact.Phone = request.Farmer.Contact.Phone; farmer.Contact.Email = request.Farmer.Contact.Email; farmer.Contact.Gender = request.Farmer.Contact.Gender; farmer.Contact = UpdateBuild(farmer.Contact, request.LoginSession); if (await locationRepository.AddOrUpdateContact(farmer.Contact) == -1) { return(rs = -1); } farmer = UpdateBuild(farmer, request.LoginSession); farmer.ImageURL = request.Farmer.ImageURL; farmer.Name = request.Farmer.Name; farmer.UserId = request.Farmer.UserId; farmer.IsUsed = request.Farmer.IsUsed; farmer.IsCompany = request.Farmer.IsCompany; if (string.IsNullOrWhiteSpace(farmer.Code)) { farmer.Code = (await storageQueries.GenarateCodeAsync(StorageKeys.FarmerCode)); } if (!request.Farmer.IsCompany) { farmer.TaxCode = string.Empty; } else { farmer.TaxCode = request.Farmer.TaxCode; } rs = await farmerRepository.Update(farmer); } finally { if (rs == 0) { trans.Commit(); CommonHelper.DeleteImage(oldImageUrl); } else { try { trans.Commit(); } catch { } CommonHelper.DeleteImage(request.Farmer.ImageURL); } } } } return(rs); }
public override async Task <int> HandleCommand(UpdateCommand request, CancellationToken cancellationToken) { if (request.Plot == null || request.Plot.Id == 0) { throw new BusinessException("Plot.NotExisted"); } var plot = await plotQueries.GetById(request.Plot.Id); if (plot == null) { throw new BusinessException("Plot.NotExisted"); } if (request.Plot.FarmerId > 0) { var farmer = await WebHelper.HttpGet <Farmer>(GlobalConfiguration.APIGateWayURI, $"{Farmers.UI.FarmerUrl.ApiGet}?farmerId={request.Plot.FarmerId}", request.LoginSession.AccessToken, request.LoginSession.LanguageCode); if (farmer == null) { throw new BusinessException("Farmer.NotExisted"); } } else { throw new BusinessException("Farmer.NotExisted"); } var rs = -1; using (var conn = DALHelper.GetConnection()) { conn.Open(); using (var trans = conn.BeginTransaction()) { try { request.Plot.CreatedDate = plot.CreatedDate; request.Plot.CreatedBy = plot.CreatedBy; request.Plot = UpdateBuild(request.Plot, request.LoginSession); request.Plot.Code = string.IsNullOrWhiteSpace(plot.Code) ? (await storageQueries.GenarateCodeAsync(StorageKeys.PlotCode)) : plot.Code; rs = await plotRepository.Update(request.Plot); if (rs == 0) { return(-1); } 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(AddCommand request, CancellationToken cancellationToken) { if (request.Cultivation == null) { throw new BusinessException("AddWrongInformation"); } if (request.Cultivation.PlotId > 0) { var plot = await WebHelper.HttpGet <Plot>(GlobalConfiguration.APIGateWayURI, $"{PlotUrl.ApiGet}?id={request.Cultivation.PlotId}", request.LoginSession.AccessToken, request.LoginSession.LanguageCode); if (plot == null) { throw new BusinessException("Plot.NotExisted"); } } else { throw new BusinessException("Plot.NotExisted"); } if (request.Cultivation.SeedId > 0) { var seed = await WebHelper.HttpGet <Seed>(GlobalConfiguration.APIGateWayURI, $"{SeedUrl.ApiGet}?id={request.Cultivation.SeedId}", request.LoginSession.AccessToken, request.LoginSession.LanguageCode); if (seed == null) { throw new BusinessException("Seed.NotExisted"); } } else { throw new BusinessException("Seed.NotExisted"); } if ((request.Cultivation.MethodId ?? 0) > 0) { var method = await WebHelper.HttpGet <Method>(GlobalConfiguration.APIGateWayURI, $"{SeedUrl.ApiGet}?id={request.Cultivation.SeedId}", request.LoginSession.AccessToken, request.LoginSession.LanguageCode); if (method == null) { throw new BusinessException("Method.NotExisted"); } } else if (request.Cultivation.MethodId != null) { throw new BusinessException("Method.NotExisted"); } var rs = -1; using (var conn = DALHelper.GetConnection()) { conn.Open(); using (var trans = conn.BeginTransaction()) { try { cultivationRepository.JoinTransaction(conn, trans); cultivationQueries.JoinTransaction(conn, trans); request.Cultivation.Code = await storageQueries.GenarateCodeAsync(StorageKeys.CultivationCode); request.Cultivation = CreateBuild(request.Cultivation, request.LoginSession); var cultivationId = await cultivationRepository.Add(request.Cultivation); rs = 0; } finally { if (rs == 0) { trans.Commit(); } else { try { trans.Rollback(); } catch { } } } } } return(rs); }
public override async Task <int> HandleCommand(UpdateCommand request, CancellationToken cancellationToken) { if (request.Seed == null || request.Seed.Id == 0) { throw new BusinessException("Seed.NotExisted"); } var seed = await seedQueries.GetById(request.Seed.Id); if (seed == null) { throw new BusinessException("Seed.NotExisted"); } if (request.Seed.ProductId > 0) { var product = await WebHelper.HttpGet <ProductViewModel>(GlobalConfiguration.APIGateWayURI, $"{AppUrl.GetProductById}?productId={request.Seed.ProductId}", request.LoginSession.AccessToken, request.LoginSession.LanguageCode); if (product == null) { throw new BusinessException("Product.NotExisted"); } } else { throw new BusinessException("Product.NotExisted"); } var rs = -1; using (var conn = DALHelper.GetConnection()) { conn.Open(); using (var trans = conn.BeginTransaction()) { try { request.Seed.CreatedDate = seed.CreatedDate; request.Seed.CreatedBy = seed.CreatedBy; request.Seed = UpdateBuild(request.Seed, request.LoginSession); request.Seed.Code = string.IsNullOrWhiteSpace(seed.Code) ? (await storageQueries.GenarateCodeAsync(StorageKeys.SeedCode)) : seed.Code; rs = await seedRepository.Update(request.Seed); if (rs == 0) { return(-1); } 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(UpdateProfileCommand request, CancellationToken cancellationToken) { if (request.Farmer == null || request.Farmer.FarmerId == 0) { throw new BusinessException("Farmer.NotExisted"); } var farmer = await farmerQueries.Get(request.Farmer.FarmerId); if (farmer == null) { throw new BusinessException("Farmer.NotExisted"); } var farmerSession = await farmerQueries.GetByUser(request.LoginSession.Id); if (farmerSession == null || farmerSession.Id == 0 || farmerSession.Id != farmer.Id) { throw new NotPermissionException(); } var rs = -1; using (var conn = DALHelper.GetConnection()) { conn.Open(); using (var trans = conn.BeginTransaction()) { try { farmerRepository.JoinTransaction(conn, trans); locationRepository.JoinTransaction(conn, trans); farmer.Address = farmer.Address ?? new Address(); farmer.Address.Street = request.Farmer.Street; farmer.Address.CountryId = request.Farmer.CountryId; farmer.Address.ProvinceId = request.Farmer.ProvinceId; farmer.Address.DistrictId = request.Farmer.DistrictId; farmer.Address.WardId = request.Farmer.WardId; //farmer.Address.Longitude = request.Farmer.Longitude; //farmer.Address.Latitude = request.Farmer.Latitude; farmer.Address = UpdateBuild(farmer.Address, request.LoginSession); if (await locationRepository.AddOrUpdateAddress(farmer.Address) == -1) { return(rs = -1); } farmer.Contact = farmer.Contact ?? new Contact(); farmer.Contact.Name = request.Farmer.Name; farmer.Contact.Phone = request.Farmer.Phone; farmer.Contact.Email = request.Farmer.Email; farmer.Contact.Gender = request.Farmer.Gender; farmer.Contact = UpdateBuild(farmer.Contact, request.LoginSession); if (await locationRepository.AddOrUpdateContact(farmer.Contact) == -1) { return(rs = -1); } farmer = UpdateBuild(farmer, request.LoginSession); farmer.Name = request.Farmer.Name; if (string.IsNullOrWhiteSpace(farmer.Code)) { farmer.Code = (await storageQueries.GenarateCodeAsync(StorageKeys.FarmerCode)); } rs = await farmerRepository.Update(farmer); } finally { if (rs == 0) { trans.Commit(); } else { try { trans.Commit(); } catch { } } } } } return(rs); }
public override async Task <int> HandleCommand(AddCommand request, CancellationToken cancellationToken) { if (request.Farmer == null || request.Farmer.Address == null || request.Farmer.Contact == null) { throw new BusinessException("AddWrongInformation"); } if (request.Farmer.ImageData?.Length > Constant.MaxImageLength) { throw new BusinessException("Image.OutOfLength"); } //With ImageData < 100byte. This is a link image. With Image > 100byte, It can a real imageData. if (request.Farmer.ImageData?.Length > 100) { string type = CommonHelper.GetImageType(System.Text.Encoding.ASCII.GetBytes(request.Farmer.ImageData)); if (!CommonHelper.IsImageType(type)) { throw new BusinessException("Image.WrongType"); } string Base64StringData = request.Farmer.ImageData.Substring(request.Farmer.ImageData.IndexOf(",") + 1); string fileName = Guid.NewGuid().ToString().Replace("-", ""); request.Farmer.ImageURL = CommonHelper.SaveImage($"{GlobalConfiguration.FarmerImagePath}/{DateTime.Now.ToString("yyyyMM")}/", fileName, type, Base64StringData); } var rs = -1; using (var conn = DALHelper.GetConnection()) { conn.Open(); using (var trans = conn.BeginTransaction()) { try { farmerRepository.JoinTransaction(conn, trans); locationRepository.JoinTransaction(conn, trans); request.Farmer = CreateBuild(request.Farmer, request.LoginSession); if (!request.Farmer.IsCompany) { request.Farmer.TaxCode = string.Empty; } request.Farmer.Code = (await storageQueries.GenarateCodeAsync(StorageKeys.FarmerCode)); request.Farmer.Id = await farmerRepository.Add(request.Farmer); request.Farmer.Address.Id = 0; request.Farmer.Address.ObjectType = LocationOjectType.F.ToString(); request.Farmer.Address.ObjectId = request.Farmer.Id; request.Farmer.Address.IsUsed = true; request.Farmer.Address = CreateBuild(request.Farmer.Address, request.LoginSession); var addressId = await locationRepository.AddOrUpdateAddress(request.Farmer.Address); request.Farmer.Contact.Id = 0; request.Farmer.Contact.ObjectType = LocationOjectType.F.ToString(); request.Farmer.Contact.ObjectId = request.Farmer.Id; request.Farmer.Contact.IsUsed = true; request.Farmer.Contact = CreateBuild(request.Farmer.Contact, request.LoginSession); var contactId = await locationRepository.AddOrUpdateContact(request.Farmer.Contact); request.Farmer.AddressId = addressId; request.Farmer.ContactId = contactId; rs = await farmerRepository.Update(request.Farmer); } finally { if (rs == 0) { trans.Commit(); } else { try { trans.Commit(); } catch { } CommonHelper.DeleteImage(request.Farmer.ImageURL); } } } }; return(rs == 0 ? -1 : 0); }
public override async Task <int> HandleCommand(AddCommand request, CancellationToken cancellationToken) { if (request.Plot == null) { throw new BusinessException("AddWrongInformation"); } if (request.Plot.FarmerId > 0) { var farmer = await WebHelper.HttpGet <Farmer>(GlobalConfiguration.APIGateWayURI, $"{Farmers.UI.FarmerUrl.ApiGet}?farmerId={request.Plot.FarmerId}", request.LoginSession.AccessToken, request.LoginSession.LanguageCode); if (farmer == null) { throw new BusinessException("Farmer.NotExisted"); } } else { throw new BusinessException("Farmer.NotExisted"); } var rs = -1; using (var conn = DALHelper.GetConnection()) { conn.Open(); using (var trans = conn.BeginTransaction()) { try { plotRepository.JoinTransaction(conn, trans); plotQueries.JoinTransaction(conn, trans); request.Plot.Code = await storageQueries.GenarateCodeAsync(StorageKeys.PlotCode); request.Plot = CreateBuild(request.Plot, request.LoginSession); var plotId = await plotRepository.Add(request.Plot); rs = 0; } finally { if (rs == 0) { trans.Commit(); } else { try { trans.Rollback(); } catch { } } } } } return(rs); }
public override async Task <int> HandleCommand(UpdateCommand request, CancellationToken cancellationToken) { if (request.Fertilizer == null || request.Fertilizer.Id == 0) { throw new BusinessException("Fertilizer.NotExisted"); } var fertilizer = await fertilizerQueries.GetById(request.Fertilizer.Id); if (fertilizer == null) { throw new BusinessException("Fertilizer.NotExisted"); } if ((request.Fertilizer?.CategoryId ?? 0) > 0) { var category = await fertilizerCategoryQueries.GetById(request.Fertilizer.CategoryId ?? 0); if (category == null) { throw new BusinessException("Category.NotExisted"); } } var rs = -1; using (var conn = DALHelper.GetConnection()) { conn.Open(); using (var trans = conn.BeginTransaction()) { try { request.Fertilizer.CreatedDate = fertilizer.CreatedDate; request.Fertilizer.CreatedBy = fertilizer.CreatedBy; request.Fertilizer = UpdateBuild(request.Fertilizer, request.LoginSession); request.Fertilizer.Code = string.IsNullOrWhiteSpace(fertilizer.Code) ? (await storageQueries.GenarateCodeAsync(StorageKeys.FertilizerCode)) : fertilizer.Code; rs = await fertilizerRepository.Update(request.Fertilizer); if (rs == 0) { return(-1); } 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(AddCommand request, CancellationToken cancellationToken) { if (request.Seed == null) { throw new BusinessException("AddWrongInformation"); } if (request.Seed.ProductId > 0) { var product = await WebHelper.HttpGet <ProductViewModel>(GlobalConfiguration.APIGateWayURI, $"{AppUrl.GetProductById}?productId={request.Seed.ProductId}", request.LoginSession.AccessToken, request.LoginSession.LanguageCode); if (product == null) { throw new BusinessException("Product.NotExisted"); } } else { throw new BusinessException("Product.NotExisted"); } var rs = -1; using (var conn = DALHelper.GetConnection()) { conn.Open(); using (var trans = conn.BeginTransaction()) { try { seedRepository.JoinTransaction(conn, trans); seedQueries.JoinTransaction(conn, trans); request.Seed.Code = await storageQueries.GenarateCodeAsync(StorageKeys.SeedCode); request.Seed = CreateBuild(request.Seed, request.LoginSession); var seedId = await seedRepository.Add(request.Seed); rs = 0; } finally { if (rs == 0) { trans.Commit(); } else { try { trans.Rollback(); } catch { } } } } } return(rs); }
public override async Task <int> HandleCommand(UpdateCommand request, CancellationToken cancellationToken) { if (request.Cultivation == null || request.Cultivation.Id == 0) { throw new BusinessException("Cultivation.NotExisted"); } var cultivation = await cultivationQueries.GetById(request.Cultivation.Id); if (cultivation == null) { throw new BusinessException("Cultivation.NotExisted"); } if (request.Cultivation.PlotId > 0) { var plot = await WebHelper.HttpGet <Plot>(GlobalConfiguration.APIGateWayURI, $"{PlotUrl.ApiGet}?id={request.Cultivation.PlotId}", request.LoginSession.AccessToken, request.LoginSession.LanguageCode); if (plot == null) { throw new BusinessException("Plot.NotExisted"); } } else { throw new BusinessException("Plot.NotExisted"); } if (request.Cultivation.SeedId > 0) { var seed = await WebHelper.HttpGet <Seed>(GlobalConfiguration.APIGateWayURI, $"{SeedUrl.ApiGet}?id={request.Cultivation.SeedId}", request.LoginSession.AccessToken, request.LoginSession.LanguageCode); if (seed == null) { throw new BusinessException("Seed.NotExisted"); } } else { throw new BusinessException("Seed.NotExisted"); } if ((request.Cultivation.MethodId ?? 0) > 0) { var method = await WebHelper.HttpGet <Method>(GlobalConfiguration.APIGateWayURI, $"{SeedUrl.ApiGet}?id={request.Cultivation.SeedId}", request.LoginSession.AccessToken, request.LoginSession.LanguageCode); if (method == null) { throw new BusinessException("Method.NotExisted"); } } else if (request.Cultivation.MethodId != null) { throw new BusinessException("Method.NotExisted"); } var rs = -1; using (var conn = DALHelper.GetConnection()) { conn.Open(); using (var trans = conn.BeginTransaction()) { try { request.Cultivation.CreatedDate = cultivation.CreatedDate; request.Cultivation.CreatedBy = cultivation.CreatedBy; request.Cultivation = UpdateBuild(request.Cultivation, request.LoginSession); request.Cultivation.Code = string.IsNullOrWhiteSpace(cultivation.Code) ? (await storageQueries.GenarateCodeAsync(StorageKeys.CultivationCode)) : cultivation.Code; rs = await cultivationRepository.Update(request.Cultivation); if (rs == 0) { return(-1); } rs = 0; } finally { if (rs == 0) { trans.Commit(); } else { try { trans.Rollback(); } catch { } } } } } return(rs); }