Restructure data access layer/data layer

This commit is contained in:
Aaron Po
2026-01-11 23:36:26 -05:00
parent 8d6b903aa7
commit 372aac897a
17 changed files with 457 additions and 256 deletions

View File

@@ -1,5 +1,6 @@
using Microsoft.AspNetCore.Mvc;
using DataAccessLayer;
using DataAccessLayer.Entities;
using Microsoft.AspNetCore.Mvc;
namespace WebAPI.Controllers
{
@@ -7,7 +8,7 @@ namespace WebAPI.Controllers
[Route("api/users")]
public class UsersController : ControllerBase
{
private readonly UserAccountRepository _userAccountRepository;
private readonly IUserAccountRepository _userAccountRepository;
public UsersController()
{
@@ -15,6 +16,7 @@ namespace WebAPI.Controllers
}
// all users
[HttpGet]
[HttpGet("users")]
public IActionResult GetAllUsers()
{
@@ -22,5 +24,61 @@ namespace WebAPI.Controllers
return Ok(users);
}
[HttpGet("{id:guid}")]
public IActionResult GetUserById(Guid id)
{
var user = _userAccountRepository.GetById(id);
return user is null ? NotFound() : Ok(user);
}
[HttpGet("by-username/{username}")]
public IActionResult GetUserByUsername(string username)
{
var user = _userAccountRepository.GetByUsername(username);
return user is null ? NotFound() : Ok(user);
}
[HttpGet("by-email/{email}")]
public IActionResult GetUserByEmail(string email)
{
var user = _userAccountRepository.GetByEmail(email);
return user is null ? NotFound() : Ok(user);
}
[HttpPost]
public IActionResult CreateUser([FromBody] UserAccount userAccount)
{
if (userAccount.UserAccountID == Guid.Empty)
{
userAccount.UserAccountID = Guid.NewGuid();
}
_userAccountRepository.Add(userAccount);
return CreatedAtAction(
nameof(GetUserById),
new { id = userAccount.UserAccountID },
userAccount
);
}
[HttpPut("{id:guid}")]
public IActionResult UpdateUser(Guid id, [FromBody] UserAccount userAccount)
{
if (userAccount.UserAccountID != Guid.Empty && userAccount.UserAccountID != id)
{
return BadRequest("UserAccountID does not match route id.");
}
userAccount.UserAccountID = id;
_userAccountRepository.Update(userAccount);
return NoContent();
}
[HttpDelete("{id:guid}")]
public IActionResult DeleteUser(Guid id)
{
_userAccountRepository.Delete(id);
return NoContent();
}
}
}
}