summaryrefslogtreecommitdiff
path: root/Controllers/ApiUserController.cs
diff options
context:
space:
mode:
Diffstat (limited to 'Controllers/ApiUserController.cs')
-rw-r--r--Controllers/ApiUserController.cs20
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);