public async Task <APIResult> Get(int retailerId) { return(new APIResult() { Result = 0, Data = await retailerQueries.Get(retailerId) }); }
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(UpdateCommand request, CancellationToken cancellationToken) { if (request.Retailer == null || request.Retailer.Id == 0 || request.Retailer.Address == null || request.Retailer.Contact == null) { throw new BusinessException("Retailer.NotExisted"); } var retailer = await retailerQueries.Get(request.Retailer.Id); if (retailer == null) { throw new BusinessException("Retailer.NotExisted"); } string oldImageUrl = request.Retailer.ImageURL; 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); retailer.Address = retailer.Address == null ? new Address() : retailer.Address; retailer.Address.Street = request.Retailer.Address.Street; retailer.Address.CountryId = request.Retailer.Address.CountryId; retailer.Address.ProvinceId = request.Retailer.Address.ProvinceId; retailer.Address.DistrictId = request.Retailer.Address.DistrictId; retailer.Address.WardId = request.Retailer.Address.WardId; retailer.Address.Longitude = request.Retailer.Address.Longitude; retailer.Address.Latitude = request.Retailer.Address.Latitude; retailer.Address = UpdateBuild(retailer.Address, request.LoginSession); if (await locationRepository.AddOrUpdateAddress(retailer.Address) == -1) { return(rs = -1); } retailer.Contact = retailer.Contact == null ? new Contact() : retailer.Contact; retailer.Contact.Name = request.Retailer.Contact.Name; retailer.Contact.Phone = request.Retailer.Contact.Phone; retailer.Contact.Email = request.Retailer.Contact.Email; retailer.Contact.Gender = request.Retailer.Contact.Gender; retailer.Contact = UpdateBuild(retailer.Contact, request.LoginSession); if (await locationRepository.AddOrUpdateContact(retailer.Contact) == -1) { return(rs = -1); } retailer = UpdateBuild(retailer, request.LoginSession); retailer.ImageURL = request.Retailer.ImageURL; retailer.Name = request.Retailer.Name; retailer.UserId = request.Retailer.UserId; retailer.IsUsed = request.Retailer.IsUsed; retailer.IsCompany = request.Retailer.IsCompany; if (!request.Retailer.IsCompany) { retailer.TaxCode = string.Empty; } else { retailer.TaxCode = request.Retailer.TaxCode; } rs = await retailerRepository.Update(retailer); } finally { if (rs == 0) { trans.Commit(); if (request.Retailer.ImageData?.Length > 200) { CommonHelper.DeleteImage(oldImageUrl); } } else { try { trans.Commit(); } catch { } CommonHelper.DeleteImage(request.Retailer.ImageURL); } } } } return(rs); }