Cleaning up with Rider
This commit is contained in:
parent
69758735de
commit
91da89a4e8
22 changed files with 574 additions and 386 deletions
137
Application/Components/ApiViewComponentBase.cs
Normal file
137
Application/Components/ApiViewComponentBase.cs
Normal file
|
|
@ -0,0 +1,137 @@
|
|||
using Microsoft.ApplicationInsights;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Newtonsoft.Json;
|
||||
using Newtonsoft.Json.Linq;
|
||||
|
||||
namespace PlanTempus.Application.Components
|
||||
{
|
||||
public abstract class ApiViewComponentBase : ViewComponent
|
||||
{
|
||||
private readonly HttpClient _httpClient;
|
||||
private readonly TelemetryClient _telemetry;
|
||||
|
||||
protected ApiViewComponentBase(IHttpClientFactory httpClientFactory, TelemetryClient telemetry)
|
||||
{
|
||||
_httpClient = httpClientFactory.CreateClient("ApiClient");
|
||||
_telemetry = telemetry;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Fetches data from API as JObject
|
||||
/// </summary>
|
||||
/// <param name="apiEndpoint">API endpoint path (e.g. "/api/product/get/1")</param>
|
||||
/// <returns>JObject with API result</returns>
|
||||
protected async Task<JObject> GetJObjectFromApiAsync(string apiEndpoint)
|
||||
{
|
||||
try
|
||||
{
|
||||
_telemetry.TrackEvent("ApiCall", new Dictionary<string, string>
|
||||
{
|
||||
["Endpoint"] = apiEndpoint,
|
||||
["Source"] = "GetJObjectFromApiAsync"
|
||||
});
|
||||
|
||||
// Use HttpClient to get JSON string
|
||||
var response = await _httpClient.GetAsync(apiEndpoint);
|
||||
response.EnsureSuccessStatusCode();
|
||||
|
||||
var jsonString = await response.Content.ReadAsStringAsync();
|
||||
return JObject.Parse(jsonString);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_telemetry.TrackException(ex, new Dictionary<string, string>
|
||||
{
|
||||
["Endpoint"] = apiEndpoint,
|
||||
["Method"] = "GetJObjectFromApiAsync"
|
||||
});
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Fetches data from API as JArray
|
||||
/// </summary>
|
||||
/// <param name="apiEndpoint">API endpoint path</param>
|
||||
/// <returns>JArray with API result</returns>
|
||||
protected async Task<JArray> GetJArrayFromApiAsync(string apiEndpoint)
|
||||
{
|
||||
try
|
||||
{
|
||||
_telemetry.TrackEvent("ApiCall", new Dictionary<string, string>
|
||||
{
|
||||
["Endpoint"] = apiEndpoint,
|
||||
["Source"] = "GetJArrayFromApiAsync"
|
||||
});
|
||||
|
||||
// Use HttpClient to get JSON string
|
||||
var response = await _httpClient.GetAsync(apiEndpoint);
|
||||
response.EnsureSuccessStatusCode();
|
||||
|
||||
var jsonString = await response.Content.ReadAsStringAsync();
|
||||
return JArray.Parse(jsonString);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_telemetry.TrackException(ex, new Dictionary<string, string>
|
||||
{
|
||||
["Endpoint"] = apiEndpoint,
|
||||
["Method"] = "GetJArrayFromApiAsync"
|
||||
});
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Sends POST request to API and receives JObject response
|
||||
/// </summary>
|
||||
/// <param name="apiEndpoint">API endpoint path</param>
|
||||
/// <param name="data">Data to be sent (can be JObject or other type)</param>
|
||||
/// <returns>JObject with response data</returns>
|
||||
protected async Task<JObject> PostToApiAsync(string apiEndpoint, object data)
|
||||
{
|
||||
try
|
||||
{
|
||||
_telemetry.TrackEvent("ApiPost", new Dictionary<string, string>
|
||||
{
|
||||
["Endpoint"] = apiEndpoint
|
||||
});
|
||||
|
||||
// Convert data to JSON string
|
||||
var content = new StringContent(
|
||||
JsonConvert.SerializeObject(data),
|
||||
System.Text.Encoding.UTF8,
|
||||
"application/json");
|
||||
|
||||
var response = await _httpClient.PostAsync(apiEndpoint, content);
|
||||
response.EnsureSuccessStatusCode();
|
||||
|
||||
var jsonString = await response.Content.ReadAsStringAsync();
|
||||
return JObject.Parse(jsonString);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_telemetry.TrackException(ex, new Dictionary<string, string>
|
||||
{
|
||||
["Endpoint"] = apiEndpoint,
|
||||
["Method"] = "PostToApiAsync"
|
||||
});
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Handles errors in a consistent way
|
||||
/// </summary>
|
||||
protected IViewComponentResult HandleError(string message = "An error occurred.")
|
||||
{
|
||||
_telemetry.TrackEvent("ComponentError", new Dictionary<string, string>
|
||||
{
|
||||
["Message"] = message,
|
||||
["Component"] = this.GetType().Name
|
||||
});
|
||||
|
||||
return Content(message);
|
||||
}
|
||||
}
|
||||
}
|
||||
43
Application/Components/OrganizationViewComponent.cs
Normal file
43
Application/Components/OrganizationViewComponent.cs
Normal file
|
|
@ -0,0 +1,43 @@
|
|||
using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.ApplicationInsights;
|
||||
|
||||
namespace PlanTempus.Application.Components
|
||||
{
|
||||
public class OrganizationViewComponent : ApiViewComponentBase
|
||||
{
|
||||
private readonly TelemetryClient _telemetry;
|
||||
|
||||
public OrganizationViewComponent(
|
||||
IHttpClientFactory httpClientFactory,
|
||||
TelemetryClient telemetry)
|
||||
: base(httpClientFactory, telemetry)
|
||||
{
|
||||
_telemetry = telemetry;
|
||||
}
|
||||
|
||||
public async Task<IViewComponentResult> InvokeAsync(int organizationId, bool showDetailedView = true)
|
||||
{
|
||||
_telemetry.TrackEvent($"{GetType().Name}Invoked", new Dictionary<string, string>
|
||||
{
|
||||
["OrganizationId"] = organizationId.ToString(),
|
||||
["ShowDetailedView"] = showDetailedView.ToString()
|
||||
});
|
||||
|
||||
var organization = await GetJObjectFromApiAsync($"/api/organization/get/{organizationId}");
|
||||
|
||||
if (organization == null)
|
||||
return HandleError("Organization not found");
|
||||
|
||||
ViewBag.ShowDetailedView = showDetailedView;
|
||||
|
||||
|
||||
_telemetry.TrackEvent("Viewed", new Dictionary<string, string>
|
||||
{
|
||||
["OrganizationId"] = organizationId.ToString(),
|
||||
["Name"] = organization["name"]?.ToString()
|
||||
});
|
||||
|
||||
return View(organization);
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue