33using Umbraco . AuthorizedServices . Services ;
44using Umbraco . AuthorizedServices . TestSite . Models . Dtos ;
55using Umbraco . AuthorizedServices . TestSite . Models . ServiceResponses ;
6- using Umbraco . Cms . Web . Common . Controllers ;
6+ using Umbraco . Cms . Core ;
77
88namespace Umbraco . AuthorizedServices . TestSite . Controllers ;
99
1010[ Route ( "umbraco/authorizedservice/hubspot/v1/contacts" ) ]
11- public class HubspotContactsController : UmbracoApiController
11+ public class HubspotContactsController : AuthorizedServicesApiControllerBase
1212{
1313 private const string ServiceAlias = "hubspot" ;
1414 private const string BasePath = "/crm/v3/objects/contacts/" ;
1515
16- private readonly IAuthorizedServiceCaller _authorizedServiceCaller ;
17-
18- public HubspotContactsController ( IAuthorizedServiceCaller authorizedServiceCaller ) => _authorizedServiceCaller = authorizedServiceCaller ;
16+ public HubspotContactsController ( IAuthorizedServiceCaller authorizedServiceCaller )
17+ : base ( authorizedServiceCaller )
18+ {
19+ }
1920
2021 [ HttpGet ]
2122 public async Task < IActionResult > Get ( )
2223 {
23- HubspotContactResponse ? response = await _authorizedServiceCaller . GetRequestAsync < HubspotContactResponse > (
24+ Attempt < HubspotContactResponse ? > responseAttempt = await AuthorizedServiceCaller . GetRequestAsync < HubspotContactResponse > (
2425 ServiceAlias ,
2526 BasePath ) ;
26- if ( response == null )
27+ if ( ! responseAttempt . Success || responseAttempt . Result is null )
2728 {
28- return Problem ( "Could not retrieve contacts." ) ;
29+ return HandleFailedRequest ( responseAttempt . Exception , "Could not retrieve contacts." ) ;
2930 }
3031
32+ HubspotContactResponse response = responseAttempt . Result ;
3133 return Ok (
3234 response . Results
3335 . Select ( MapToDto )
@@ -38,60 +40,64 @@ public async Task<IActionResult> Get()
3840 [ Route ( "{id}" ) ]
3941 public async Task < IActionResult > Get ( string id )
4042 {
41- HubspotContactResponse . Result ? response = await _authorizedServiceCaller . GetRequestAsync < HubspotContactResponse . Result > (
43+ Attempt < HubspotContactResponse . Result ? > responseAttempt = await AuthorizedServiceCaller . GetRequestAsync < HubspotContactResponse . Result > (
4244 ServiceAlias ,
4345 $ "{ BasePath } { id } ") ;
44- if ( response == null )
46+
47+ if ( ! responseAttempt . Success || responseAttempt . Result is null )
4548 {
46- return NotFound ( ) ;
49+ return HandleFailedRequest ( responseAttempt . Exception , "Could not retrieve contact." ) ;
4750 }
4851
52+ HubspotContactResponse . Result response = responseAttempt . Result ;
4953 return Ok ( MapToDto ( response ) ) ;
5054 }
5155
5256 [ HttpPost ]
5357 public async Task < IActionResult > Create ( [ FromBody ] ContactDto contact )
5458 {
55- HubspotContactResponse . Result ? response = await _authorizedServiceCaller . PostRequestAsync < HubspotContactResponse . Result , HubspotContactResponse . Result > (
59+ Attempt < HubspotContactResponse . Result ? > responseAttempt = await AuthorizedServiceCaller . PostRequestAsync < HubspotContactResponse . Result , HubspotContactResponse . Result > (
5660 ServiceAlias ,
5761 BasePath ,
5862 MapToRequest ( contact ) ) ;
59- if ( response == null )
63+ if ( ! responseAttempt . Success || responseAttempt . Result is null )
6064 {
61- return Problem ( "Could not create contact." ) ;
65+ return HandleFailedRequest ( responseAttempt . Exception , "Could not create contact." ) ;
6266 }
6367
68+ HubspotContactResponse . Result response = responseAttempt . Result ;
6469 return CreatedAtAction ( nameof ( Get ) , new { id = response . Id } , MapToDto ( response ) ) ;
6570 }
6671
6772 [ HttpPut ]
6873 public async Task < IActionResult > Update ( [ FromBody ] ContactDto contact )
6974 {
70- HubspotContactResponse . Result ? response = await _authorizedServiceCaller . PatchRequestAsync < HubspotContactResponse . Result , HubspotContactResponse . Result > (
75+ Attempt < HubspotContactResponse . Result ? > responseAttempt = await AuthorizedServiceCaller . PatchRequestAsync < HubspotContactResponse . Result , HubspotContactResponse . Result > (
7176 ServiceAlias ,
7277 $ "{ BasePath } { contact . Id } ",
7378 MapToRequest ( contact ) ) ;
74- if ( response == null )
79+ if ( ! responseAttempt . Success || responseAttempt . Result is null )
7580 {
76- return Problem ( "Could not update contact." ) ;
81+ return HandleFailedRequest ( responseAttempt . Exception , "Could not update contact." ) ;
7782 }
7883
84+ HubspotContactResponse . Result response = responseAttempt . Result ;
7985 return Ok ( MapToDto ( response ) ) ;
8086 }
8187
8288 [ HttpDelete ]
8389 [ Route ( "{id}" ) ]
8490 public async Task < IActionResult > Delete ( string id )
8591 {
86- await _authorizedServiceCaller . DeleteRequestAsync (
92+ await AuthorizedServiceCaller . DeleteRequestAsync (
8793 ServiceAlias ,
8894 $ "{ BasePath } { id } ") ;
8995
9096 return NoContent ( ) ;
9197 }
9298
9399 private ContactDto MapToDto ( HubspotContactResponse . Result result ) =>
94- new ContactDto
100+ new ( )
95101 {
96102 Id = result . Id ,
97103 FirstName = result . Properties . FirstName ,
@@ -100,7 +106,7 @@ private ContactDto MapToDto(HubspotContactResponse.Result result) =>
100106 } ;
101107
102108 private HubspotContactResponse . Result MapToRequest ( ContactDto dto ) =>
103- new HubspotContactResponse . Result
109+ new ( )
104110 {
105111 Id = dto . Id ,
106112 Properties = new HubspotContactResponse . ResultProperties
0 commit comments