diff options
| author | ivarlovlie <git@ivarlovlie.no> | 2022-01-22 22:43:38 +0100 |
|---|---|---|
| committer | ivarlovlie <git@ivarlovlie.no> | 2022-01-22 22:43:38 +0100 |
| commit | 88110f536f9c3843ecf5016122e101f8a424af77 (patch) | |
| tree | e8be4e77ccfb5ad37f49f89adad59ff12b4c85ea /src/server/Api/Internal/Account/CreateTokenRoute.cs | |
| download | bookmark-thing-88110f536f9c3843ecf5016122e101f8a424af77.tar.xz bookmark-thing-88110f536f9c3843ecf5016122e101f8a424af77.zip | |
Initial commit
Diffstat (limited to 'src/server/Api/Internal/Account/CreateTokenRoute.cs')
| -rw-r--r-- | src/server/Api/Internal/Account/CreateTokenRoute.cs | 34 |
1 files changed, 34 insertions, 0 deletions
diff --git a/src/server/Api/Internal/Account/CreateTokenRoute.cs b/src/server/Api/Internal/Account/CreateTokenRoute.cs new file mode 100644 index 0000000..ea0e01f --- /dev/null +++ b/src/server/Api/Internal/Account/CreateTokenRoute.cs @@ -0,0 +1,34 @@ +namespace IOL.BookmarkThing.Server.Api.Internal.Account; + +public class CreateTokenRoute : RouteBaseInternalSync.WithRequest<CreateTokenRequest>.WithActionResult +{ + private readonly AppDbContext _context; + + public CreateTokenRoute(AppDbContext context) { + _context = context; + } + + [ApiVersionNeutral] + [ApiExplorerSettings(IgnoreApi = true)] + [HttpPost("~/v{version:apiVersion}/account/create-token")] + public override ActionResult Handle(CreateTokenRequest request) { + var user = _context.Users.SingleOrDefault(c => c.Id == LoggedInUser.Id); + if (user == default) { + return NotFound(new ErrorResult("User does not exist")); + } + + if (request.Name.IsNullOrWhiteSpace()) { + return BadRequest(new ErrorResult("Token name is required")); + } + + var token = new AccessToken { + Id = Guid.NewGuid(), + Name = request.Name, + User = user + }; + + _context.AccessTokens.Add(token); + _context.SaveChanges(); + return Ok(token); + } +} |
