update .net, setup modern logging
Some checks failed
Build and Publish / Build Yale Access Backend (pull_request) Failing after 1m28s
Build and Publish / Push Yale Access Backend Docker Image (pull_request) Has been skipped
Build and Publish / Build Yale Access Frontend (pull_request) Successful in 1m42s
Build and Publish / Push Yale Access Frontend Docker Image (pull_request) Has been skipped

This commit is contained in:
2026-02-18 08:48:15 +11:00
parent f577617b4d
commit 6d5749acd3
9 changed files with 84 additions and 122 deletions

View File

@@ -2,7 +2,6 @@ using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Cors;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Options;
using Serilog;
using YaleAccess.Models;
using YaleAccess.Models.Options;
using YaleAccess.Services;
@@ -14,33 +13,27 @@ namespace YaleAccess.Controllers
[Route("api/[controller]")]
[EnableCors]
[Authorize]
public class YaleController : ControllerBase
public class YaleController(
ILogger<YaleController> logger,
IYaleAccessor yaleAccessor,
IOptions<CodesOptions> codeOptions,
SMSService smsService
) : ControllerBase
{
private readonly IYaleAccessor _yaleAccessor;
private readonly CodesOptions _codeOptions;
private readonly SMSService _smsService;
public YaleController(IYaleAccessor yaleAccessor, IOptions<CodesOptions> codeOptions, SMSService smsService)
{
_yaleAccessor = yaleAccessor;
_codeOptions = codeOptions.Value;
_smsService = smsService;
}
[HttpGet("codes")]
public async Task<IActionResult> GetUserCodes()
{
try
{
// Get the home code first
YaleUserCode homeCode = await _yaleAccessor.GetCodeInformationAsync(_codeOptions.Home);
YaleUserCode homeCode = await yaleAccessor.GetCodeInformationAsync(codeOptions.Value.Home);
homeCode.IsHome = true;
// Get the guest codes
List<YaleUserCode> guestCodes = new();
foreach (int code in Enumerable.Range(_codeOptions.GuestCodeRangeStart, _codeOptions.GuestCodeRangeCount))
foreach (int code in Enumerable.Range(codeOptions.Value.GuestCodeRangeStart, codeOptions.Value.GuestCodeRangeCount))
{
guestCodes.Add(await _yaleAccessor.GetCodeInformationAsync(code));
guestCodes.Add(await yaleAccessor.GetCodeInformationAsync(code));
}
// Add the home code to the list
@@ -51,7 +44,7 @@ namespace YaleAccess.Controllers
}
catch(Exception ex)
{
Log.Logger.Error(ex, "An error occurred retriving the codes.");
logger.LogError(ex, "An error occurred retriving the codes.");
return BadRequest(new ApiResponse("An error occurred retriving the codes."));
}
}
@@ -69,23 +62,23 @@ namespace YaleAccess.Controllers
}
// Set the new code
bool result = await _yaleAccessor.SetUserCode(id, newCode);
bool result = await yaleAccessor.SetUserCode(id, newCode);
// Return the result
if (result)
{
Log.Logger.Information("Updated code for user {id} to {code}", id, newCode);
logger.LogInformation("Updated code for user {id} to {code}", id, newCode);
return Ok(new ApiResponse(true));
}
else
{
Log.Logger.Information("Failed to update code for user {id} to {code}", id, newCode);
logger.LogInformation("Failed to update code for user {id} to {code}", id, newCode);
return BadRequest(new ApiResponse("An error occurred setting the code."));
}
}
catch (Exception ex)
{
Log.Logger.Error(ex, "An error occurred setting the code.");
logger.LogError(ex, "An error occurred setting the code.");
return BadRequest(new ApiResponse("An error occurred setting the code."));
}
}
@@ -96,30 +89,30 @@ namespace YaleAccess.Controllers
try
{
// First validate the user code
string validCode = YaleAccessor.ValidateClearCode(id, _codeOptions.Home);
string validCode = YaleAccessor.ValidateClearCode(id, codeOptions.Value.Home);
if (validCode != string.Empty)
{
return BadRequest(new ApiResponse(validCode));
}
// Set the available status
bool result = await _yaleAccessor.SetCodeAsAvailable(id);
bool result = await yaleAccessor.SetCodeAsAvailable(id);
// Return the result
if (result)
{
Log.Logger.Information("Updated code status for user {id} to available", id);
logger.LogInformation("Updated code status for user {id} to available", id);
return Ok(new ApiResponse(true));
}
else
{
Log.Logger.Information("Failed to update code status for user {id} to available", id);
logger.LogInformation("Failed to update code status for user {id} to available", id);
return BadRequest(new ApiResponse("An error occurred setting the code status."));
}
}
catch (Exception ex)
{
Log.Logger.Error(ex, "An error occurred setting the code status.");
logger.LogError(ex, "An error occurred setting the code status.");
return BadRequest(new ApiResponse("An error occurred setting the code status."));
}
}
@@ -130,17 +123,17 @@ namespace YaleAccess.Controllers
try
{
// Get the user code
YaleUserCode userCode = await _yaleAccessor.GetCodeInformationAsync(id);
YaleUserCode userCode = await yaleAccessor.GetCodeInformationAsync(id);
// Send the code via SMS to the phone number
await _smsService.SendCodeViaSMSAsync(userCode.Code, phoneNumber);
await smsService.SendCodeViaSMSAsync(userCode.Code, phoneNumber);
// Return success
return Ok(new ApiResponse(true));
}
catch (Exception ex)
{
Log.Logger.Error(ex, "An error occurred sending the code.");
logger.LogError(ex, "An error occurred sending the code.");
return BadRequest(new ApiResponse("An error occurred sending the code."));
}
}