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

@@ -4,7 +4,6 @@ using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Cors;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Options;
using Serilog;
using System.Security.Claims;
using YaleAccess.Models;
@@ -14,15 +13,8 @@ namespace YaleAccess.Controllers
[Route("api/[controller]")]
[EnableCors]
[Authorize]
public class AuthenticationController : ControllerBase
public class AuthenticationController(IOptions<Models.Options.AuthenticationOptions> authenticationOptions, ILogger<AuthenticationController> logger) : ControllerBase
{
private readonly Models.Options.AuthenticationOptions _authenticationOptions;
public AuthenticationController(IOptions<Models.Options.AuthenticationOptions> authenticationOptions)
{
_authenticationOptions = authenticationOptions.Value;
}
[HttpPost("login")]
[AllowAnonymous]
public async Task<IActionResult> Login([FromBody] string password)
@@ -30,7 +22,7 @@ namespace YaleAccess.Controllers
try
{
// Check if the password is correct
if (password != _authenticationOptions.Password)
if (password != authenticationOptions.Value.Password)
{
return Unauthorized(new ApiResponse("Incorrect password."));
}
@@ -50,7 +42,7 @@ namespace YaleAccess.Controllers
}
catch(Exception ex)
{
Log.Logger.Error(ex, "An error occurred logging in.");
logger.LogError(ex, "An error occurred logging in.");
return BadRequest(new ApiResponse("An error occurred logging in."));
}
}
@@ -68,7 +60,7 @@ namespace YaleAccess.Controllers
}
catch (Exception ex)
{
Log.Logger.Error(ex, "An error occured logging out.");
logger.LogError(ex, "An error occured logging out.");
return BadRequest(new ApiResponse("An error occured logging out."));
}
}

View File

@@ -1,7 +1,6 @@
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Cors;
using Microsoft.AspNetCore.Mvc;
using Serilog;
namespace YaleAccess.Controllers
{
@@ -9,13 +8,13 @@ namespace YaleAccess.Controllers
[Route("api/[controller]")]
[EnableCors]
[Authorize]
public class HealthController : ControllerBase
public class HealthController(ILogger<HealthController> logger) : ControllerBase
{
[HttpGet]
[AllowAnonymous]
public IActionResult Health()
{
Log.Logger.Information("Hit the health endpoint.");
logger.LogInformation("Hit the health endpoint.");
return Ok("Service is healthy");
}
}

View File

@@ -2,7 +2,6 @@
using Microsoft.AspNetCore.Cors;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using Serilog;
using YaleAccess.Data;
using YaleAccess.Models;
@@ -12,27 +11,20 @@ namespace YaleAccess.Controllers
[Route("api/[controller]")]
[EnableCors]
[Authorize]
public class PeopleController : ControllerBase
public class PeopleController(ILogger<PeopleController> logger, YaleContext context) : ControllerBase
{
private readonly YaleContext _context;
public PeopleController(YaleContext context)
{
_context = context;
}
[HttpGet]
public async Task<IActionResult> GetPeople()
{
try
{
// Return all people
List<Person> people = await _context.People.ToListAsync();
List<Person> people = await context.People.ToListAsync();
return Ok(new ApiResponse(people));
}
catch (Exception ex)
{
Log.Logger.Error(ex, "An error occured retriving the people.");
logger.LogError(ex, "An error occured retriving the people.");
return BadRequest(new ApiResponse("An error occured retriving the people."));
}
}
@@ -46,15 +38,15 @@ namespace YaleAccess.Controllers
Person newPerson = new() { Name = person.Name, PhoneNumber = person.PhoneNumber };
// Add the person
await _context.AddAsync(newPerson);
await _context.SaveChangesAsync();
await context.AddAsync(newPerson);
await context.SaveChangesAsync();
// Return the newly created person
return Ok(new ApiResponse(newPerson));
}
catch (Exception ex)
{
Log.Logger.Error(ex, "An error occured creating the person.");
logger.LogError(ex, "An error occured creating the person.");
return BadRequest(new ApiResponse("An error occured creating the person."));
}
}
@@ -65,18 +57,18 @@ namespace YaleAccess.Controllers
try
{
// Ensure the person exists
Person person = await _context.People.FindAsync(id) ?? throw new Exception("Person not found.");
Person person = await context.People.FindAsync(id) ?? throw new Exception("Person not found.");
// Remove the person
_context.Remove(person);
await _context.SaveChangesAsync();
context.Remove(person);
await context.SaveChangesAsync();
// Return the newly removed person
return Ok(new ApiResponse(person));
}
catch (Exception ex)
{
Log.Logger.Error(ex, "An error occured deletiong the person.");
logger.LogError(ex, "An error occured deletiong the person.");
return BadRequest(new ApiResponse("An error occured deletiong the person."));
}
}

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."));
}
}