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
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:
@@ -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."));
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user