summaryrefslogtreecommitdiffstats
path: root/api/WhatApi/Endpoints/CreateUserEndpoint.cs
diff options
context:
space:
mode:
Diffstat (limited to 'api/WhatApi/Endpoints/CreateUserEndpoint.cs')
-rw-r--r--api/WhatApi/Endpoints/CreateUserEndpoint.cs30
1 files changed, 30 insertions, 0 deletions
diff --git a/api/WhatApi/Endpoints/CreateUserEndpoint.cs b/api/WhatApi/Endpoints/CreateUserEndpoint.cs
new file mode 100644
index 0000000..dc89c16
--- /dev/null
+++ b/api/WhatApi/Endpoints/CreateUserEndpoint.cs
@@ -0,0 +1,30 @@
+namespace WhatApi.Endpoints;
+
+public class CreateUserEndpoint(AppDatabase db, IConfiguration configuration) : BaseEndpoint
+{
+ public class CreateUserRequest
+ {
+ public required string Email { get; set; }
+ public required string Password { get; set; }
+ public required string Name { get; set; }
+ }
+
+ [AllowAnonymous]
+ [HttpPost("~/create-user")]
+ public async Task<ActionResult> HandleAsync(CreateUserRequest req, CancellationToken ct = default) {
+ var userList = await db.Users.Select(c => new {
+ c.Name
+ }).ToListAsync(ct);
+ if (userList.Count == 0 && !configuration.IsDevelopment) return Unauthorized();
+ if (userList.Any(c => c.Name.Equals(req.Name, StringComparison.InvariantCultureIgnoreCase))) return BadRequest("Username taken");
+ var user = new User {
+ Name = req.Name,
+ Email = req.Email,
+ PasswordHash = PasswordHasher.HashPassword(req.Password)
+ };
+ user.SetCreated(Constants.SystemUid);
+ db.Users.Add(user);
+ await db.SaveChangesAsync(ct);
+ return Ok();
+ }
+} \ No newline at end of file