quick refactor for clarity
This commit is contained in:
parent
92280503ed
commit
8934a09cf3
3 changed files with 105 additions and 65 deletions
|
|
@ -1,6 +1,20 @@
|
|||
@AuthAPI_HostAddress = http://localhost:5127
|
||||
|
||||
GET {{AuthAPI_HostAddress}}/weatherforecast/
|
||||
GET {{AuthAPI_HostAddress}}/validate/
|
||||
Accept: application/json
|
||||
Content-Type: application/json
|
||||
{
|
||||
"Email": "someValue",
|
||||
"FirstName": "name"
|
||||
}
|
||||
|
||||
###
|
||||
|
||||
GET {{AuthAPI_HostAddress}}/validate/
|
||||
Accept: application/json
|
||||
Content-Type: application/json
|
||||
{
|
||||
"Ssn": "10 or 8 number length"
|
||||
}
|
||||
|
||||
###
|
||||
|
|
@ -1,7 +1,8 @@
|
|||
using Microsoft.AspNetCore.Mvc;
|
||||
using System.Text.Json.Serialization;
|
||||
using AuthAPI;
|
||||
|
||||
var builder = WebApplication.CreateBuilder(args);
|
||||
builder.Services.AddHttpClient();
|
||||
builder.Services.ConfigureHttpJsonOptions(options =>
|
||||
{
|
||||
options.SerializerOptions.AllowTrailingCommas = true;
|
||||
|
|
@ -11,75 +12,18 @@ var app = builder.Build();
|
|||
|
||||
var enviromentVariables = builder.Configuration.GetSection("EnviromentVariables");
|
||||
|
||||
app.MapGet("/validate", async ([FromBody] SsnRequest validationRequest) =>
|
||||
app.MapGet("/validate", async ([FromBody] Request validationRequest, HttpClient httpClient) =>
|
||||
{
|
||||
if ((string.IsNullOrWhiteSpace(validationRequest.Ssn) &&
|
||||
(string.IsNullOrWhiteSpace(validationRequest.Email) ||
|
||||
string.IsNullOrWhiteSpace(validationRequest.FirstName))) ||
|
||||
(!string.IsNullOrWhiteSpace(validationRequest.Email) &&
|
||||
string.IsNullOrWhiteSpace(validationRequest.FirstName)))
|
||||
{
|
||||
return Results.BadRequest("Invalid request. Either SSN or both Email and FirstName must be provided.");
|
||||
}
|
||||
|
||||
var httpClient = new HttpClient();
|
||||
var request = GetRequestWithApiKey(validationRequest, enviromentVariables);
|
||||
|
||||
var response = await httpClient.PostAsJsonAsync(enviromentVariables["ApiUrl"], request);
|
||||
var request = Request.GetRequestWithApiKey(validationRequest, enviromentVariables);
|
||||
if (request.IsFailure)
|
||||
return Results.BadRequest(request.Failure);
|
||||
|
||||
var response = await httpClient.PostAsJsonAsync(enviromentVariables["ApiUrl"], request.Success);
|
||||
if (!response.IsSuccessStatusCode)
|
||||
{
|
||||
return Results.StatusCode((int)response.StatusCode);
|
||||
}
|
||||
|
||||
return Results.Ok(response.Content.ReadAsStringAsync().Result.Contains("\"member_found\":true,"));
|
||||
})
|
||||
.WithName("ValidateMember");
|
||||
|
||||
app.Run();
|
||||
|
||||
static ValidateMembersRoot GetRequestWithApiKey(SsnRequest original, IConfigurationSection enviromentVariables)
|
||||
{
|
||||
var newRequest = new
|
||||
{
|
||||
ApiKey = enviromentVariables["ApiKey"],
|
||||
Action = "confirm_membership",
|
||||
AssociationNumber = enviromentVariables["AssociationNumber"]
|
||||
};
|
||||
|
||||
return new ValidateMembersRoot(
|
||||
new ValidateMembersRequest(
|
||||
newRequest.Action,
|
||||
newRequest.AssociationNumber ?? "",
|
||||
newRequest.ApiKey ?? "",
|
||||
YearId: null,
|
||||
Firstname: original.FirstName ?? null,
|
||||
Lastname: null,
|
||||
SSN: original.Ssn ?? null,
|
||||
Email: original.Email ?? null,
|
||||
Phone: null,
|
||||
MemberNick: null,
|
||||
DiscordId: null
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
record SsnRequest(string? Ssn, string? Email, string? FirstName);
|
||||
|
||||
record ValidateMembersRoot(
|
||||
[property: JsonPropertyName("request")] ValidateMembersRequest Request
|
||||
);
|
||||
|
||||
record ValidateMembersRequest(
|
||||
[property: JsonPropertyName("action")] string Action,
|
||||
[property: JsonPropertyName("association_number")] string AssociationNumber,
|
||||
[property: JsonPropertyName("api_key")] string ApiKey,
|
||||
[property: JsonPropertyName("year_id")] int? YearId,
|
||||
[property: JsonPropertyName("firstname")] string? Firstname,
|
||||
[property: JsonPropertyName("lastname")] string? Lastname,
|
||||
[property: JsonPropertyName("socialsecuritynumber")] string? SSN,
|
||||
[property: JsonPropertyName("email")] string? Email,
|
||||
[property: JsonPropertyName("phone1")] string? Phone,
|
||||
[property: JsonPropertyName("member_nick")] string? MemberNick,
|
||||
[property: JsonPropertyName("discord_user_id")] string? DiscordId
|
||||
);
|
||||
app.Run();
|
||||
82
src/Auth/AuthAPI/Request.cs
Normal file
82
src/Auth/AuthAPI/Request.cs
Normal file
|
|
@ -0,0 +1,82 @@
|
|||
using System.Text.Json.Serialization;
|
||||
|
||||
namespace AuthAPI;
|
||||
|
||||
public class Request(string? ssn, string? email, string? firstName)
|
||||
{
|
||||
public string? Ssn { get; } = ssn;
|
||||
public string? Email { get; } = email;
|
||||
public string? FirstName { get; } = firstName;
|
||||
|
||||
public static Result GetRequestWithApiKey(Request request, IConfigurationSection enviromentVariables)
|
||||
{
|
||||
if (ValidateRequest(request))
|
||||
return Result.CreateFailure(
|
||||
"Invalid request. Either SSN or both Email and FirstName must be provided.");
|
||||
|
||||
return Result.CreateSuccess(
|
||||
new ValidateMembersRoot(
|
||||
new ValidateMembersRequest(
|
||||
"confirm_membership",
|
||||
enviromentVariables["AssociationNumber"] ?? "",
|
||||
enviromentVariables["ApiKey"] ?? "",
|
||||
YearId: null,
|
||||
Firstname: request.FirstName ?? null,
|
||||
Lastname: null,
|
||||
SSN: request.Ssn ?? null,
|
||||
Email: request.Email ?? null,
|
||||
Phone: null,
|
||||
MemberNick: null,
|
||||
DiscordId: null
|
||||
)
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
private static bool ValidateRequest(Request validationRequest) => (
|
||||
(string.IsNullOrWhiteSpace(validationRequest.Ssn) &&
|
||||
(string.IsNullOrWhiteSpace(validationRequest.Email) ||
|
||||
string.IsNullOrWhiteSpace(validationRequest.FirstName))) ||
|
||||
(!string.IsNullOrWhiteSpace(validationRequest.Email) &&
|
||||
string.IsNullOrWhiteSpace(validationRequest.FirstName))) ||
|
||||
(!string.IsNullOrWhiteSpace(validationRequest.Ssn) &&
|
||||
long.TryParse(validationRequest.Ssn ?? "", out _) == false);
|
||||
};
|
||||
|
||||
public record ValidateMembersRoot(
|
||||
[property: JsonPropertyName("request")] ValidateMembersRequest Request
|
||||
);
|
||||
|
||||
public record ValidateMembersRequest(
|
||||
[property: JsonPropertyName("action")] string Action,
|
||||
[property: JsonPropertyName("association_number")] string AssociationNumber,
|
||||
[property: JsonPropertyName("api_key")] string ApiKey,
|
||||
[property: JsonPropertyName("year_id")] int? YearId,
|
||||
[property: JsonPropertyName("firstname")] string? Firstname,
|
||||
[property: JsonPropertyName("lastname")] string? Lastname,
|
||||
[property: JsonPropertyName("socialsecuritynumber")] string? SSN,
|
||||
[property: JsonPropertyName("email")] string? Email,
|
||||
[property: JsonPropertyName("phone1")] string? Phone,
|
||||
[property: JsonPropertyName("member_nick")] string? MemberNick,
|
||||
[property: JsonPropertyName("discord_user_id")] string? DiscordId
|
||||
);
|
||||
|
||||
public record Result
|
||||
{
|
||||
public ValidateMembersRoot? Success { get; }
|
||||
public string? Failure { get; }
|
||||
public bool IsSuccess => Success is not null;
|
||||
public bool IsFailure => Failure is not null;
|
||||
|
||||
private Result(ValidateMembersRoot? success, string? failure)
|
||||
{
|
||||
Success = success;
|
||||
Failure = failure;
|
||||
}
|
||||
|
||||
public static Result CreateSuccess(ValidateMembersRoot success) =>
|
||||
new(success, default);
|
||||
|
||||
public static Result CreateFailure(string failure) =>
|
||||
new(default, failure);
|
||||
}
|
||||
Loading…
Reference in a new issue