what da
All checks were successful
Build and Deploy API / build-and-deploy (push) Successful in 8s

This commit is contained in:
2026-03-24 19:41:48 -05:00
parent 63c2da652c
commit eeee94d0d6
2 changed files with 89 additions and 0 deletions

View File

@@ -0,0 +1,50 @@
// this is basically a demo on roles
// level 0 can't access the users endpoint at all
// level 1 has read permissions
// level 2 has modify permissions
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Authorization;
using agologumApi.Models;
using agologumApi.Services;
[ApiController]
[Route("api/[controller]")]
public class UsersController : ControllerBase {
private readonly UserService service_;
public UsersController(UserService service) {
service_ = service;
}
[Authorize(Roles = "Admin, Superuser")]
[HttpGet]
public async Task<ActionResult<List<User>>> getUsers() {
return Ok(await service_.GetAll());
}
[Authorize(Roles = "Admin, Superuser")]
[HttpGet("{id:int}")]
public async Task<ActionResult<User>> getUser(int id) {
var user = await service_.Get(id);
if (user == null) return NotFound();
return Ok(user);
}
[Authorize(Roles = "Superuser")]
[HttpDelete("{id}")]
public async Task<ActionResult> deleteUser(int id) {
var success = await service_.Delete(id);
if (!success) return NotFound();
return NoContent();
}
}

View File

@@ -0,0 +1,39 @@
using Microsoft.EntityFrameworkCore;
using agologumApi.Models;
namespace agologumApi.Services;
public class UserService {
private readonly AppDbContext db_;
public UserService(AppDbContext db) {
db_ = db;
}
public async Task<List<User>> GetAll() {
return await db_.Users.ToListAsync();
}
public async Task<User?> Get(int id) {
return await db_.Users.FindAsync(id);
}
public async Task<User?> Get(string name) {
return await db_.Users.FirstOrDefaultAsync(u => u.UserName == name);
}
public async Task<bool> Delete(int id) {
User? User = await db_.Users.FindAsync(id);
if(User != null) {
db_.Users.Remove(User);
await db_.SaveChangesAsync();
return true;
} else {
return false;
}
}
}