1
1
using Asp . Versioning ;
2
2
using Microsoft . AspNetCore . Authorization ;
3
3
using Microsoft . AspNetCore . Mvc ;
4
+ using NetTopologySuite . Geometries ;
5
+ using StrDss . Data . Entities ;
6
+ using StrDss . Data . Repositories ;
7
+ using StrDss . Service . HttpClients ;
4
8
5
9
namespace StrDss . Api . Controllers
6
10
{
@@ -9,11 +13,170 @@ namespace StrDss.Api.Controllers
9
13
[ ApiController ]
10
14
public class NetworkCheckerController : ControllerBase
11
15
{
16
+ private readonly IGeocoderApi _geocoder ;
17
+ private readonly IOrganizationRepository _orgRepo ;
18
+
19
+ public NetworkCheckerController ( IGeocoderApi geocoder , IOrganizationRepository orgRepo )
20
+ {
21
+ _geocoder = geocoder ;
22
+ _orgRepo = orgRepo ;
23
+ }
24
+
12
25
[ AllowAnonymous ]
13
26
[ HttpGet ]
14
27
public IActionResult GetNetworkStatus ( )
15
28
{
16
29
return Ok ( new { status = true } ) ;
17
30
}
31
+
32
+ [ AllowAnonymous ]
33
+ [ HttpGet ( "geocoder" ) ]
34
+ public async Task < IActionResult > TestGeocoder ( [ FromQuery ] string address )
35
+ {
36
+ if ( string . IsNullOrWhiteSpace ( address ) )
37
+ return BadRequest ( new { error = "Address query parameter is required." } ) ;
38
+
39
+ try
40
+ {
41
+ var physicalAddress = new DssPhysicalAddress
42
+ {
43
+ OriginalAddressTxt = address
44
+ } ;
45
+
46
+ var error = await _geocoder . GetAddressAsync ( physicalAddress ) ;
47
+
48
+ return Ok ( new
49
+ {
50
+ success = string . IsNullOrEmpty ( error ) ,
51
+ error = error ,
52
+ originalAddress = physicalAddress . OriginalAddressTxt ,
53
+ matchedAddress = physicalAddress . MatchAddressTxt ,
54
+ matchScore = physicalAddress . MatchScoreAmt ,
55
+ siteNo = physicalAddress . SiteNo ,
56
+ civicNo = physicalAddress . CivicNo ,
57
+ streetName = physicalAddress . StreetNm ,
58
+ locality = physicalAddress . LocalityNm ,
59
+ province = physicalAddress . ProvinceCd ,
60
+ containingOrganizationId = physicalAddress . ContainingOrganizationId ,
61
+ hasLocationGeometry = physicalAddress . LocationGeometry != null
62
+ } ) ;
63
+ }
64
+ catch ( Exception ex )
65
+ {
66
+ return Ok ( new
67
+ {
68
+ success = false ,
69
+ error = $ "Exception occurred: { ex . Message } "
70
+ } ) ;
71
+ }
72
+ }
73
+
74
+ [ AllowAnonymous ]
75
+ [ HttpGet ( "jurisdiction" ) ]
76
+ public async Task < IActionResult > GetJurisdictionInfo ( [ FromQuery ] string address )
77
+ {
78
+ if ( string . IsNullOrWhiteSpace ( address ) )
79
+ return BadRequest ( new { error = "Address query parameter is required." } ) ;
80
+
81
+ try
82
+ {
83
+ // First, geocode the address
84
+ var physicalAddress = new DssPhysicalAddress
85
+ {
86
+ OriginalAddressTxt = address
87
+ } ;
88
+
89
+ var geocoderError = await _geocoder . GetAddressAsync ( physicalAddress ) ;
90
+
91
+ if ( ! string . IsNullOrEmpty ( geocoderError ) )
92
+ {
93
+ return Ok ( new
94
+ {
95
+ success = false ,
96
+ error = $ "Geocoder error: { geocoderError } ",
97
+ originalAddress = address
98
+ } ) ;
99
+ }
100
+
101
+ if ( physicalAddress . LocationGeometry == null || ! ( physicalAddress . LocationGeometry is Point point ) )
102
+ {
103
+ return Ok ( new
104
+ {
105
+ success = false ,
106
+ error = "No valid location geometry found for the address" ,
107
+ originalAddress = address ,
108
+ matchedAddress = physicalAddress . MatchAddressTxt
109
+ } ) ;
110
+ }
111
+
112
+ // Look up the containing organization
113
+ var containingOrganizationId = await _orgRepo . GetContainingOrganizationId ( point ) ;
114
+
115
+ if ( containingOrganizationId == null )
116
+ {
117
+ return Ok ( new
118
+ {
119
+ success = true ,
120
+ originalAddress = address ,
121
+ matchedAddress = physicalAddress . MatchAddressTxt ,
122
+ locationFound = true ,
123
+ jurisdictionFound = false ,
124
+ message = "No containing jurisdiction found for this location"
125
+ } ) ;
126
+ }
127
+
128
+ // Get the organization details
129
+ var organization = await _orgRepo . GetOrganizationByIdAsync ( containingOrganizationId . Value ) ;
130
+
131
+ if ( organization == null )
132
+ {
133
+ return Ok ( new
134
+ {
135
+ success = true ,
136
+ originalAddress = address ,
137
+ matchedAddress = physicalAddress . MatchAddressTxt ,
138
+ locationFound = true ,
139
+ jurisdictionFound = false ,
140
+ containingOrganizationId = containingOrganizationId . Value ,
141
+ message = "Organization not found for the containing organization ID"
142
+ } ) ;
143
+ }
144
+
145
+ return Ok ( new
146
+ {
147
+ success = true ,
148
+ originalAddress = address ,
149
+ matchedAddress = physicalAddress . MatchAddressTxt ,
150
+ locationFound = true ,
151
+ jurisdictionFound = true ,
152
+ jurisdiction = new
153
+ {
154
+ organizationId = organization . OrganizationId ,
155
+ organizationName = organization . OrganizationNm ,
156
+ organizationType = organization . OrganizationType ,
157
+ organizationCode = organization . OrganizationCd ,
158
+ isStraaExempt = organization . IsStraaExempt
159
+ } ,
160
+ geocoderInfo = new
161
+ {
162
+ matchScore = physicalAddress . MatchScoreAmt ,
163
+ siteNo = physicalAddress . SiteNo ,
164
+ civicNo = physicalAddress . CivicNo ,
165
+ streetName = physicalAddress . StreetNm ,
166
+ locality = physicalAddress . LocalityNm ,
167
+ province = physicalAddress . ProvinceCd
168
+ }
169
+ } ) ;
170
+ }
171
+ catch ( Exception ex )
172
+ {
173
+ return Ok ( new
174
+ {
175
+ success = false ,
176
+ error = $ "Exception occurred: { ex . Message } ",
177
+ originalAddress = address
178
+ } ) ;
179
+ }
180
+ }
18
181
}
19
182
}
0 commit comments