From cd70f54266d708867a1eb35870bc755bc5b2df32 Mon Sep 17 00:00:00 2001 From: ivar Date: Wed, 3 Dec 2025 21:49:20 +0100 Subject: Refactor db --- api/WhatApi/Endpoints/CreateUserEndpoint.cs | 30 +++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 api/WhatApi/Endpoints/CreateUserEndpoint.cs (limited to 'api/WhatApi/Endpoints/CreateUserEndpoint.cs') 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 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 -- cgit v1.3