You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

103 lines
3.0 KiB

11 months ago
  1. using Business.LoqateApi;
  2. using Common.Models.Loqate;
  3. using Common.Models.RequestResponse;
  4. using log4net;
  5. using System;
  6. using System.Collections.Generic;
  7. using System.Configuration;
  8. using System.Linq;
  9. using System.Text;
  10. using System.Threading.Tasks;
  11. namespace Business.BusinessLogic.Address
  12. {
  13. public class ServiceApi
  14. {
  15. public string _key { get; set; }
  16. PostcodeAnywhere_SoapClient _SoapClient { get; set; }
  17. public ServiceApi()
  18. {
  19. _key = ConfigurationManager.AppSettings["loqatekey"].ToString();
  20. _SoapClient = new PostcodeAnywhere_SoapClient();
  21. }
  22. public Capture_Interactive_Find_v1_10_ArrayOfResults Find_v1_10(string search)
  23. {
  24. return _SoapClient.Capture_Interactive_Find_v1_10(_key, search, true, null, null, "GB", 20, "en-gb", false, "", "");
  25. }
  26. public Capture_Interactive_Find_v1_10_ArrayOfResults Find_v1_10(string search, string container)
  27. {
  28. return _SoapClient.Capture_Interactive_Find_v1_10(_key, search, true, container, "", "GB", 20, "en-gb", false, "", "");
  29. }
  30. }
  31. public class LocateBusiness:ILocateBusiness
  32. {
  33. private static readonly ILog Log = LogManager.GetLogger(typeof(LocateBusiness));
  34. ServiceApi serviceApi { get; set; }
  35. public LocateBusiness()
  36. {
  37. serviceApi = new ServiceApi();
  38. }
  39. public TPResponse QueryAddress(string postCode)
  40. {
  41. TPResponse rxResponse = new TPResponse() { ResponseCode = "1", Msg = "No Data found" };
  42. var results = serviceApi.Find_v1_10(postCode);
  43. //Log.Debug(Newtonsoft.Json.JsonConvert.SerializeObject(results));
  44. if (results != null && results.Any())
  45. {
  46. var single = results.FirstOrDefault();
  47. if (single != null && single.Type.Equals("Postcode"))
  48. {
  49. rxResponse.ResponseCode = "0";
  50. rxResponse.Msg = "Post code found";
  51. var result2 = serviceApi.Find_v1_10(postCode, single.Id);
  52. QueryResponse queryResponsse = new QueryResponse();
  53. queryResponsse.Addresses = new List<Common.Models.Loqate.Address>();
  54. foreach (Capture_Interactive_Find_v1_10_Results item in result2)
  55. {
  56. queryResponsse.Addresses.Add(new Common.Models.Loqate.Address()
  57. {
  58. Id = item.Id,
  59. Type = item.Type,
  60. Address1 = item.Text,
  61. City = item.Description.Substring(0, item.Description.LastIndexOf(",")),
  62. Address2= item.Description
  63. });
  64. }
  65. rxResponse.Data = queryResponsse;
  66. }
  67. }
  68. else
  69. {
  70. rxResponse.Msg = "No result!";
  71. }
  72. return rxResponse;
  73. }
  74. }
  75. }