scaffold auth api infrastructure (doesn't work)
Some checks failed
Build and Deploy API / build-and-deploy (push) Failing after 14s
Some checks failed
Build and Deploy API / build-and-deploy (push) Failing after 14s
This commit is contained in:
@@ -1,28 +1,30 @@
|
||||
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.AspNetCore.Authorization;
|
||||
|
||||
using agologumApi.Models;
|
||||
using agologumApi.Services;
|
||||
|
||||
[ApiController]
|
||||
[Route("api/[controller]")]
|
||||
public class UsersController : ControllerBase
|
||||
{
|
||||
public class UsersController : ControllerBase {
|
||||
|
||||
private readonly UserService service_;
|
||||
|
||||
public UsersController(UserService service)
|
||||
{
|
||||
public UsersController(UserService service) {
|
||||
service_ = service;
|
||||
}
|
||||
|
||||
[AllowAnonymous] // accessible if not authorized
|
||||
[HttpGet]
|
||||
public async Task<ActionResult<List<User>>> getUsers()
|
||||
{
|
||||
public async Task<ActionResult<List<User>>> getUsers() {
|
||||
return Ok(await service_.GetAll());
|
||||
}
|
||||
|
||||
[AllowAnonymous]
|
||||
[HttpGet("{id:int}")]
|
||||
public async Task<ActionResult<User>> getUser(int id)
|
||||
{
|
||||
public async Task<ActionResult<User>> getUser(int id) {
|
||||
|
||||
var user = await service_.Get(id);
|
||||
|
||||
if (user == null) return NotFound();
|
||||
@@ -30,9 +32,10 @@ public class UsersController : ControllerBase
|
||||
return Ok(user);
|
||||
}
|
||||
|
||||
[Authorize] // testing the authorization
|
||||
[HttpPost]
|
||||
public async Task<ActionResult<User>> createUser(User user)
|
||||
{
|
||||
public async Task<ActionResult<User>> createUser(User user) {
|
||||
|
||||
var created = await service_.Create(user);
|
||||
|
||||
return CreatedAtAction(
|
||||
@@ -42,9 +45,10 @@ public class UsersController : ControllerBase
|
||||
);
|
||||
}
|
||||
|
||||
[Authorize]
|
||||
[HttpPut("{id}")]
|
||||
public async Task<ActionResult<User>> updateUser(int id, User user)
|
||||
{
|
||||
public async Task<ActionResult<User>> updateUser(int id, User user) {
|
||||
|
||||
var updated = await service_.Update(user);
|
||||
|
||||
if (updated == null) return NotFound();
|
||||
@@ -52,9 +56,10 @@ public class UsersController : ControllerBase
|
||||
return Ok(updated);
|
||||
}
|
||||
|
||||
[Authorize]
|
||||
[HttpDelete("{id}")]
|
||||
public async Task<ActionResult> deleteUser(int id)
|
||||
{
|
||||
public async Task<ActionResult> deleteUser(int id) {
|
||||
|
||||
var success = await service_.Delete(id);
|
||||
|
||||
if (!success) return NotFound();
|
||||
|
||||
@@ -6,5 +6,6 @@ public class User {
|
||||
public int Id { get; set; }
|
||||
public string Name { get; set; } = "";
|
||||
public string Email { get; set; } = "";
|
||||
public string PasswordHash { get; set; } = "";
|
||||
|
||||
};
|
||||
|
||||
@@ -21,6 +21,10 @@ public class UserService {
|
||||
return await db_.Users.FindAsync(id);
|
||||
}
|
||||
|
||||
public async Task<User?> Get(string username) {
|
||||
return await db_.Users.FirstOrDefaultAsync(u => u.Name == username);
|
||||
}
|
||||
|
||||
public async Task<User> Create(User user) {
|
||||
db_.Users.Add(user);
|
||||
await db_.SaveChangesAsync();
|
||||
|
||||
Reference in New Issue
Block a user