PCRB follow up client side services

This commit is contained in:
Chase Tucker
2025-03-12 17:34:41 -07:00
parent 2dbde5d70c
commit c4d29dad4e
9 changed files with 498 additions and 6 deletions

View File

@ -4,7 +4,6 @@ using System.Text.Json;
using MesaFabApproval.Shared.Models;
using Microsoft.AspNetCore.Components.Forms;
using Microsoft.AspNetCore.StaticFiles;
using Microsoft.Extensions.Caching.Memory;
@ -40,6 +39,10 @@ public interface IPCRBService {
Task NotifyApprovers(PCRBNotification notification);
Task NotifyOriginator(PCRBNotification notification);
Task NotifyResponsiblePerson(PCRBActionItemNotification notification);
Task CreateFollowUp(PCRBFollowUp followUp);
Task<IEnumerable<PCRBFollowUp>> GetFollowUpsByPlanNumber(int planNumber, bool bypassCache);
Task UpdateFollowUp(PCRBFollowUp followUp);
Task DeleteFollowUp(int id);
}
public class PCRBService : IPCRBService {
@ -764,4 +767,86 @@ public class PCRBService : IPCRBService {
if (!responseMessage.IsSuccessStatusCode)
throw new Exception($"Unable to notify PCRB responsible person, because {responseMessage.ReasonPhrase}");
}
public async Task CreateFollowUp(PCRBFollowUp followUp) {
if (followUp is null) throw new ArgumentNullException("follow up cannot be null");
HttpClient httpClient = _httpClientFactory.CreateClient("API");
HttpRequestMessage requestMessage = new(HttpMethod.Post, $"pcrb/followUp") {
Content = new StringContent(JsonSerializer.Serialize(followUp),
Encoding.UTF8,
"application/json")
};
HttpResponseMessage responseMessage = await httpClient.SendAsync(requestMessage);
if (!responseMessage.IsSuccessStatusCode)
throw new Exception(responseMessage.ReasonPhrase);
await GetFollowUpsByPlanNumber(followUp.PlanNumber, true);
}
public async Task<IEnumerable<PCRBFollowUp>> GetFollowUpsByPlanNumber(int planNumber, bool bypassCache) {
if (planNumber <= 0) throw new ArgumentException($"{planNumber} is not a valid PCRB Plan#");
IEnumerable<PCRBFollowUp>? followUps = null;
if (!bypassCache)
followUps = _cache.Get<IEnumerable<PCRBFollowUp>>($"pcrbFollowUps{planNumber}");
if (followUps is null) {
HttpClient httpClient = _httpClientFactory.CreateClient("API");
HttpRequestMessage requestMessage = new(HttpMethod.Get, $"pcrb/followUps?planNumber={planNumber}&bypassCache={bypassCache}");
HttpResponseMessage responseMessage = await httpClient.SendAsync(requestMessage);
if (responseMessage.IsSuccessStatusCode) {
string responseContent = await responseMessage.Content.ReadAsStringAsync();
JsonSerializerOptions jsonSerializerOptions = new() {
PropertyNameCaseInsensitive = true
};
followUps = JsonSerializer.Deserialize<IEnumerable<PCRBFollowUp>>(responseContent, jsonSerializerOptions) ??
new List<PCRBFollowUp>();
if (followUps.Count() > 0)
_cache.Set($"pcrbFollowUps{planNumber}", followUps, DateTimeOffset.Now.AddMinutes(5));
} else {
throw new Exception(responseMessage.ReasonPhrase);
}
}
return followUps;
}
public async Task UpdateFollowUp(PCRBFollowUp followUp) {
if (followUp is null) throw new ArgumentNullException("follow up cannot be null");
HttpClient httpClient = _httpClientFactory.CreateClient("API");
HttpRequestMessage requestMessage = new(HttpMethod.Put, $"pcrb/followUp") {
Content = new StringContent(JsonSerializer.Serialize(followUp),
Encoding.UTF8,
"application/json")
};
HttpResponseMessage responseMessage = await httpClient.SendAsync(requestMessage);
if (!responseMessage.IsSuccessStatusCode)
throw new Exception(responseMessage.ReasonPhrase);
}
public async Task DeleteFollowUp(int id) {
if (id <= 0) throw new ArgumentException($"{id} is not a valid PCRB follow up ID");
HttpClient httpClient = _httpClientFactory.CreateClient("API");
HttpRequestMessage requestMessage = new(HttpMethod.Delete, $"pcrb/followUp?id={id}");
HttpResponseMessage responseMessage = await httpClient.SendAsync(requestMessage);
if (!responseMessage.IsSuccessStatusCode) throw new Exception(responseMessage.ReasonPhrase);
}
}