blob: dc89c16134748c0d661869881ad7334823c78d42 (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
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();
}
}
|