summaryrefslogtreecommitdiffstats
path: root/server/src/Endpoints/V1/ApiTokens/DeleteTokenRoute.cs
blob: a90b4c044b677ca0e56bf2faa47837592641c6ee (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
31
32
33
namespace IOL.GreatOffice.Api.Endpoints.V1.ApiTokens;

public class DeleteTokenRoute : RouteBaseSync.WithRequest<Guid>.WithActionResult
{
	private readonly AppDbContext _context;
	private readonly ILogger<DeleteTokenRoute> _logger;

	public DeleteTokenRoute(AppDbContext context, ILogger<DeleteTokenRoute> logger) {
		_context = context;
		_logger = logger;
	}

	/// <summary>
	/// Delete an api token, rendering it unusable
	/// </summary>
	/// <param name="id">Id of the token to delete</param>
	/// <returns>Nothing</returns>
	[ApiVersion(ApiSpecV1.VERSION_STRING)]
	[HttpDelete("~/v{version:apiVersion}/api-tokens/delete")]
	[ProducesResponseType(200)]
	[ProducesResponseType(404)]
	public override ActionResult Handle(Guid id) {
		var token = _context.AccessTokens.SingleOrDefault(c => c.Id == id);
		if (token == default) {
			_logger.LogWarning("A deletion request of an already deleted (maybe) api token was received.");
			return NotFound();
		}

		_context.AccessTokens.Remove(token);
		_context.SaveChanges();
		return Ok();
	}
}