mirror of
https://github.com/aaronpo97/the-biergarten-app.git
synced 2026-02-16 10:42:08 +00:00
Restructure data access layer/data layer
This commit is contained in:
@@ -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();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user