public override async Task <int> HandleCommand(AddLocationCommand request, CancellationToken cancellationToken) { if (request.Location == null || request.Location.Address == null || request.Location.Contact == null) { throw new BusinessException("AddWrongInformation"); } if (request.LoginSession.Roles.FirstOrDefault(r => r == "Administrator") == null) { var retailer = await retailerQueries.GetByUserId(request.LoginSession.Id); if (retailer == null) { throw new BusinessException("Retailer.NotExisted"); } request.Location.RetailerId = retailer.Id; } else { if (request.Location.RetailerId == 0) { throw new BusinessException("AddWrongInformation"); } else { var retailer = await retailerQueries.Get(request.Location.RetailerId); if (retailer == null) { throw new BusinessException("Retailer.NotExisted"); } } } if (request.Location.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.Location.ImageData?.Length > 200) { string type = CommonHelper.GetImageType(System.Text.Encoding.ASCII.GetBytes(request.Location.ImageData)); if (!CommonHelper.IsImageType(type)) { throw new BusinessException("Image.WrongType"); } string Base64StringData = request.Location.ImageData.Substring(request.Location.ImageData.IndexOf(",") + 1); string fileName = Guid.NewGuid().ToString().Replace("-", ""); request.Location.ImageURL = CommonHelper.SaveImage($"{GlobalConfiguration.RetailerImagePath}/{DateTime.Now.ToString("yyyyMM")}/", fileName, type, Base64StringData); } var distributions = await WebHelper.HttpGet <IEnumerable <DistributionViewModel> >(GlobalConfiguration.APIGateWayURI, AppUrl.GetDistributions, request.LoginSession.AccessToken); double minDistance = double.MaxValue; bool isInDisRadius = false; DistributionViewModel chooseDis = null; foreach (var dis in distributions) { var distance = 1000 * CommonHelper.DistanceBetween2Points(dis.Address.Latitude, dis.Address.Longitude, request.Location.Address.Latitude, request.Location.Address.Longitude); if (isInDisRadius) { if (distance < minDistance && distance <= dis.Radius) { minDistance = distance; chooseDis = dis; } } else { if (distance < minDistance) { minDistance = distance; chooseDis = dis; } if (distance <= dis.Radius) { isInDisRadius = true; } } } if (chooseDis != null) { request.Location.DistributionId = chooseDis.Id; } var rs = -1; using (var conn = DALHelper.GetConnection()) { conn.Open(); using (var trans = conn.BeginTransaction()) { try { retailerRepository.JoinTransaction(conn, trans); retailerQueries.JoinTransaction(conn, trans); locationRepository.JoinTransaction(conn, trans); request.Location.GLN = await retailerQueries.GenarateLocationCode(); request.Location = CreateBuild(request.Location, request.LoginSession); request.Location.Id = await retailerRepository.AddLocation(request.Location); request.Location.Address.Id = 0; request.Location.Address.ObjectType = LocationOjectType.R.ToString(); request.Location.Address.ObjectId = request.Location.RetailerId; request.Location.Address.IsUsed = true; request.Location.Address = CreateBuild(request.Location.Address, request.LoginSession); var addressId = await locationRepository.AddOrUpdateAddress(request.Location.Address); request.Location.Contact.Id = 0; request.Location.Contact.ObjectType = LocationOjectType.R.ToString(); request.Location.Contact.ObjectId = request.Location.RetailerId; request.Location.Contact.IsUsed = true; request.Location.Contact = CreateBuild(request.Location.Contact, request.LoginSession); var contactId = await locationRepository.AddOrUpdateContact(request.Location.Contact); request.Location.AddressId = addressId; request.Location.ContactId = contactId; rs = await retailerRepository.UpdateLocation(request.Location); } finally { if (rs == 0) { trans.Commit(); } else { try { trans.Commit(); } catch { } CommonHelper.DeleteImage(request.Location.ImageURL); } } } }; return(rs); }
public override async Task <int> HandleCommand(AddCommand request, CancellationToken cancellationToken) { if (request.Retailer == null || request.Retailer.Address == null || request.Retailer.Contact == null) { throw new BusinessException("AddWrongInformation"); } if (request.Retailer.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.Retailer.ImageData?.Length > 200) { string type = CommonHelper.GetImageType(System.Text.Encoding.ASCII.GetBytes(request.Retailer.ImageData)); if (!CommonHelper.IsImageType(type)) { throw new BusinessException("Image.WrongType"); } string Base64StringData = request.Retailer.ImageData.Substring(request.Retailer.ImageData.IndexOf(",") + 1); string fileName = Guid.NewGuid().ToString().Replace("-", ""); request.Retailer.ImageURL = CommonHelper.SaveImage($"{GlobalConfiguration.RetailerImagePath}/{DateTime.Now.ToString("yyyyMM")}/", fileName, type, Base64StringData); } var rs = -1; using (var conn = DALHelper.GetConnection()) { conn.Open(); using (var trans = conn.BeginTransaction()) { try { retailerRepository.JoinTransaction(conn, trans); locationRepository.JoinTransaction(conn, trans); request.Retailer = CreateBuild(request.Retailer, request.LoginSession); if (!request.Retailer.IsCompany) { request.Retailer.TaxCode = string.Empty; } request.Retailer.Id = await retailerRepository.Add(request.Retailer); request.Retailer.Address.Id = 0; request.Retailer.Address.ObjectType = LocationOjectType.R.ToString(); request.Retailer.Address.ObjectId = request.Retailer.Id; request.Retailer.Address.IsUsed = true; request.Retailer.Address = CreateBuild(request.Retailer.Address, request.LoginSession); var addressId = await locationRepository.AddOrUpdateAddress(request.Retailer.Address); request.Retailer.Contact.Id = 0; request.Retailer.Contact.ObjectType = LocationOjectType.R.ToString(); request.Retailer.Contact.ObjectId = request.Retailer.Id; request.Retailer.Contact.IsUsed = true; request.Retailer.Contact = CreateBuild(request.Retailer.Contact, request.LoginSession); var contactId = await locationRepository.AddOrUpdateContact(request.Retailer.Contact); request.Retailer.AddressId = addressId; request.Retailer.ContactId = contactId; rs = await retailerRepository.Update(request.Retailer); } catch (Exception ex) { LogHelper.GetLogger().Error(ex.Message); return(rs = -1); } finally { if (rs == 0) { trans.Commit(); } else { try { trans.Commit(); } catch { } CommonHelper.DeleteImage(request.Retailer.ImageURL); } } } }; return(rs); }
public override async Task <int> HandleCommand(UpdateCommand request, CancellationToken cancellationToken) { if (request.Fulfillment == null || request.Fulfillment.Id == 0 || request.Fulfillment.Address == null || request.Fulfillment.Contact == null) { throw new BusinessException("Fulfillment.NotExisted"); } var fulfillment = await fulfillmentQueries.Get(request.Fulfillment.Id); if (fulfillment == null) { throw new BusinessException("Fulfillment.NotExisted"); } string oldImageUrl = request.Fulfillment.ImageURL; if (request.Fulfillment.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.Fulfillment.ImageData?.Length > 100) { string type = CommonHelper.GetImageType(System.Text.Encoding.ASCII.GetBytes(request.Fulfillment.ImageData)); if (!CommonHelper.IsImageType(type)) { throw new BusinessException("Image.WrongType"); } string Base64StringData = request.Fulfillment.ImageData.Substring(request.Fulfillment.ImageData.IndexOf(",") + 1); string fileName = Guid.NewGuid().ToString().Replace("-", ""); request.Fulfillment.ImageURL = CommonHelper.SaveImage($"{GlobalConfiguration.FulfillmentImagePath}/{DateTime.Now.ToString("yyyyMM")}/", fileName, type, Base64StringData); } var rs = -1; using (var conn = DALHelper.GetConnection()) { conn.Open(); using (var trans = conn.BeginTransaction()) { try { fulfillmentRepository.JoinTransaction(conn, trans); locationRepository.JoinTransaction(conn, trans); fulfillment.Address = fulfillment.Address ?? new Address(); fulfillment.Address.Street = request.Fulfillment.Address.Street; fulfillment.Address.CountryId = request.Fulfillment.Address.CountryId; fulfillment.Address.ProvinceId = request.Fulfillment.Address.ProvinceId; fulfillment.Address.DistrictId = request.Fulfillment.Address.DistrictId; fulfillment.Address.WardId = request.Fulfillment.Address.WardId; fulfillment.Address.Longitude = request.Fulfillment.Address.Longitude; fulfillment.Address.Latitude = request.Fulfillment.Address.Latitude; fulfillment.Address = UpdateBuild(fulfillment.Address, request.LoginSession); if (await locationRepository.AddOrUpdateAddress(fulfillment.Address) == -1) { return(rs = -1); } fulfillment.Contact = fulfillment.Contact ?? new Contact(); fulfillment.Contact.Name = request.Fulfillment.Contact.Name; fulfillment.Contact.Phone = request.Fulfillment.Contact.Phone; fulfillment.Contact.Email = request.Fulfillment.Contact.Email; fulfillment.Contact.Gender = request.Fulfillment.Contact.Gender; fulfillment.Contact = UpdateBuild(fulfillment.Contact, request.LoginSession); if (await locationRepository.AddOrUpdateContact(fulfillment.Contact) == -1) { return(rs = -1); } fulfillment = UpdateBuild(fulfillment, request.LoginSession); fulfillment.ImageURL = request.Fulfillment.ImageURL; fulfillment.Name = request.Fulfillment.Name; fulfillment.Code = request.Fulfillment.Code; fulfillment.ManagerId = request.Fulfillment.ManagerId; fulfillment.IsUsed = request.Fulfillment.IsUsed; rs = await fulfillmentRepository.Update(fulfillment); } finally { if (rs == 0) { trans.Commit(); CommonHelper.DeleteImage(oldImageUrl); } else { try { trans.Commit(); } catch { } CommonHelper.DeleteImage(request.Fulfillment.ImageURL); } } } } 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); }