diff options
| author | Jake Mannens <jake@asger.xyz> | 2026-05-29 22:35:18 +1000 |
|---|---|---|
| committer | Jake Mannens <jake@asger.xyz> | 2026-05-30 03:38:05 +1000 |
| commit | c3e9d39034e5afc3f2c3a12c8c7682eabe360b7d (patch) | |
| tree | 73c5f43ec246f700a164ef700956b9d82b95f594 /Controllers/ApiUserController.cs | |
| parent | cfd9959de69f9ab52e846e8acd232dc30d20aa27 (diff) | |
Modified controller endpoints to throw exceptions instead of returning HTTP responses on error
Diffstat (limited to 'Controllers/ApiUserController.cs')
| -rw-r--r-- | Controllers/ApiUserController.cs | 20 |
1 files changed, 12 insertions, 8 deletions
diff --git a/Controllers/ApiUserController.cs b/Controllers/ApiUserController.cs index d678287..3230218 100644 --- a/Controllers/ApiUserController.cs +++ b/Controllers/ApiUserController.cs @@ -1,4 +1,5 @@ -using HyperBooru.Services; +using HyperBooru.ApiModels; +using HyperBooru.Services; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Mvc; using Microsoft.EntityFrameworkCore; @@ -30,7 +31,10 @@ public class ApiUserController : Controller { var user = await db.Users .FirstOrDefaultAsync(u => u.Guid == userId); - return user is null ? NotFound() : Ok((ApiModels.User) user); + if(user is null) + throw new ObjectNotFoundException([ userId ]); + + return Ok((ApiModels.User) user); } [HttpPost] @@ -40,7 +44,7 @@ public class ApiUserController : Controller { using var transaction = await db.Database.BeginTransactionAsync(); if(await db.Users.AnyAsync(u => u.Username == request.Username)) - return BadRequest("Username already exists"); + throw new ApiModels.ArgumentException("Username already exists"); var user = new User() { Username = request.Username, @@ -66,17 +70,17 @@ public class ApiUserController : Controller { var user = await db.Users.FirstOrDefaultAsync(u => u.Guid == userId); if(user is null) - return NotFound(); + throw new ObjectNotFoundException([ userId ]); if(request.Username is not null) { if(string.IsNullOrWhiteSpace(request.Username)) - return BadRequest("Username cannot be empty"); + throw new ApiModels.ArgumentException("Username cannot be empty"); user.Username = request.Username; } if(request.Password is not null) { if(string.IsNullOrWhiteSpace(request.Password)) - return BadRequest("Password cannot be empty"); + throw new ApiModels.ArgumentException("Password cannot be empty"); user.PasswordHash = UserService.HashPassword(request.Password); } @@ -89,7 +93,7 @@ public class ApiUserController : Controller { [HttpDelete("{userId}")] public async Task<IActionResult> DeleteUserAsync([FromRoute] Guid userId) { if(userId == HBContext.AdminUser) - return BadRequest("Cannot delete the admin user"); + throw new ApiModels.ArgumentException("Cannot delete the admin user"); using var db = dbFactory.CreateDbContext(); @@ -97,7 +101,7 @@ public class ApiUserController : Controller { var user = await db.Users.FirstOrDefaultAsync(u => u.Guid == userId); if(user is null) - return NotFound(); + throw new ObjectNotFoundException([ userId ]); db.Users.Remove(user); |
