Update repository, seed and service layers

This commit is contained in:
Aaron Po
2026-01-15 14:48:18 -05:00
parent b8cd855916
commit c5aaf8cd05
15 changed files with 322 additions and 412 deletions

View File

@@ -0,0 +1,26 @@
using BusinessLayer.Services;
using DataAccessLayer.Entities;
using Microsoft.AspNetCore.Mvc;
namespace WebAPI.Controllers
{
[ApiController]
[Route("api/[controller]")]
public class UserController(IUserService userService) : ControllerBase
{
[HttpGet]
public async Task<ActionResult<IEnumerable<UserAccount>>> GetAll([FromQuery] int? limit, [FromQuery] int? offset)
{
var users = await userService.GetAllAsync(limit, offset);
return Ok(users);
}
[HttpGet("{id:guid}")]
public async Task<ActionResult<UserAccount>> GetById(Guid id)
{
var user = await userService.GetByIdAsync(id);
if (user is null) return NotFound();
return Ok(user);
}
}
}

View File

@@ -1,107 +0,0 @@
using BusinessLayer.Services;
using DataAccessLayer.Entities;
using Microsoft.AspNetCore.Mvc;
namespace WebAPI.Controllers
{
[ApiController]
[Route("api/users")]
public class UsersController : ControllerBase
{
private readonly IUserService _userService;
public UsersController(IUserService userService)
{
_userService = userService;
}
// all users
[HttpGet]
public IActionResult GetAllUsers(
[FromQuery] int? limit,
[FromQuery] int? offset
)
{
if (offset.HasValue && !limit.HasValue)
{
return BadRequest("Limit is required when offset is provided.");
}
if (limit.HasValue && limit <= 0)
{
return BadRequest("Limit must be greater than zero.");
}
if (offset.HasValue && offset < 0)
{
return BadRequest("Offset cannot be negative.");
}
var users = _userService.GetAll(limit, offset);
return Ok(users);
}
[HttpGet("{id:guid}")]
public IActionResult GetUserById(Guid id)
{
var user = _userService.GetById(id);
return user is null ? NotFound() : Ok(user);
}
[HttpGet("username/{username}")]
public IActionResult GetUserByUsername(string username)
{
var user = _userService.GetByUsername(username);
return user is null ? NotFound() : Ok(user);
}
[HttpGet("email/{email}")]
public IActionResult GetUserByEmail(string email)
{
var user = _userService.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();
}
_userService.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;
_userService.Update(userAccount);
return NoContent();
}
[HttpDelete("{id:guid}")]
public IActionResult DeleteUser(Guid id)
{
_userService.Delete(id);
return NoContent();
}
}
}

View File

@@ -0,0 +1,25 @@
using DataAccessLayer.Sql;
using Microsoft.Data.SqlClient;
namespace WebAPI.Infrastructure
{
public class DefaultSqlConnectionFactory : ISqlConnectionFactory
{
private readonly string _connectionString;
public DefaultSqlConnectionFactory(IConfiguration configuration)
{
_connectionString =
Environment.GetEnvironmentVariable("DB_CONNECTION_STRING")
?? configuration.GetConnectionString("Default")
?? throw new InvalidOperationException(
"Database connection string not configured. Set DB_CONNECTION_STRING env var or ConnectionStrings:Default."
);
}
public SqlConnection CreateConnection()
{
return new SqlConnection(_connectionString);
}
}
}

View File

@@ -1,6 +1,7 @@
using BusinessLayer.Services;
using DataAccessLayer;
using DataAccessLayer.Repositories;
using DataAccessLayer.Sql;
using WebAPI.Infrastructure;
var builder = WebApplication.CreateBuilder(args);
@@ -8,9 +9,11 @@ builder.Services.AddControllers();
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();
builder.Services.AddOpenApi();
// Dependency Injection
builder.Services.AddSingleton<ISqlConnectionFactory, DefaultSqlConnectionFactory>();
builder.Services.AddScoped<IUserAccountRepository, UserAccountRepository>();
builder.Services.AddScoped<IUserService, UserService>();
var app = builder.Build();
if (app.Environment.IsDevelopment())