Removed use of crud helper. Refactored user and api key response. Removed unused request/response models
This commit is contained in:
@@ -1,8 +1,9 @@
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using System.ComponentModel.DataAnnotations;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using MoonCore.Exceptions;
|
||||
using MoonCore.Extended.Abstractions;
|
||||
using MoonCore.Extended.Helpers;
|
||||
using MoonCore.Extended.PermFilter;
|
||||
using MoonCore.Helpers;
|
||||
using MoonCore.Models;
|
||||
using Moonlight.ApiServer.Database.Entities;
|
||||
using Moonlight.ApiServer.Services;
|
||||
@@ -15,26 +16,70 @@ namespace Moonlight.ApiServer.Http.Controllers.Admin.ApiKeys;
|
||||
[Route("api/admin/apikeys")]
|
||||
public class ApiKeysController : Controller
|
||||
{
|
||||
private readonly CrudHelper<ApiKey, ApiKeyDetailResponse> CrudHelper;
|
||||
private readonly DatabaseRepository<ApiKey> ApiKeyRepository;
|
||||
private readonly ApiKeyService ApiKeyService;
|
||||
|
||||
public ApiKeysController(CrudHelper<ApiKey, ApiKeyDetailResponse> crudHelper, DatabaseRepository<ApiKey> apiKeyRepository, ApiKeyService apiKeyService)
|
||||
public ApiKeysController(DatabaseRepository<ApiKey> apiKeyRepository, ApiKeyService apiKeyService)
|
||||
{
|
||||
CrudHelper = crudHelper;
|
||||
ApiKeyRepository = apiKeyRepository;
|
||||
ApiKeyService = apiKeyService;
|
||||
}
|
||||
|
||||
|
||||
[HttpGet]
|
||||
[RequirePermission("admin.apikeys.read")]
|
||||
public async Task<IPagedData<ApiKeyDetailResponse>> Get([FromQuery] int page, [FromQuery] int pageSize = 50)
|
||||
=> await CrudHelper.Get(page, pageSize);
|
||||
public async Task<IPagedData<ApiKeyResponse>> Get(
|
||||
[FromQuery] int page,
|
||||
[FromQuery] [Range(1, 100)] int pageSize = 50
|
||||
)
|
||||
{
|
||||
var count = await ApiKeyRepository.Get().CountAsync();
|
||||
|
||||
var apiKeys = await ApiKeyRepository
|
||||
.Get()
|
||||
.OrderBy(x => x.Id)
|
||||
.Skip(page * pageSize)
|
||||
.Take(pageSize)
|
||||
.ToArrayAsync();
|
||||
|
||||
var mappedApiKey = apiKeys
|
||||
.Select(x => new ApiKeyResponse()
|
||||
{
|
||||
Id = x.Id,
|
||||
PermissionsJson = x.PermissionsJson,
|
||||
Description = x.Description,
|
||||
ExpiresAt = x.ExpiresAt
|
||||
})
|
||||
.ToArray();
|
||||
|
||||
return new PagedData<ApiKeyResponse>()
|
||||
{
|
||||
CurrentPage = page,
|
||||
Items = mappedApiKey,
|
||||
PageSize = pageSize,
|
||||
TotalItems = count,
|
||||
TotalPages = count == 0 ? 0 : (count - 1) / pageSize
|
||||
};
|
||||
}
|
||||
|
||||
[HttpGet("{id}")]
|
||||
[RequirePermission("admin.apikeys.read")]
|
||||
public async Task<ApiKeyDetailResponse> GetSingle(int id)
|
||||
=> await CrudHelper.GetSingle(id);
|
||||
public async Task<ApiKeyResponse> GetSingle(int id)
|
||||
{
|
||||
var apiKey = await ApiKeyRepository
|
||||
.Get()
|
||||
.FirstOrDefaultAsync(x => x.Id == id);
|
||||
|
||||
if (apiKey == null)
|
||||
throw new HttpApiException("No api key with that id found", 404);
|
||||
|
||||
return new ApiKeyResponse()
|
||||
{
|
||||
Id = apiKey.Id,
|
||||
PermissionsJson = apiKey.PermissionsJson,
|
||||
Description = apiKey.Description,
|
||||
ExpiresAt = apiKey.ExpiresAt
|
||||
};
|
||||
}
|
||||
|
||||
[HttpPost]
|
||||
[RequirePermission("admin.apikeys.create")]
|
||||
@@ -49,20 +94,55 @@ public class ApiKeysController : Controller
|
||||
|
||||
var finalApiKey = await ApiKeyRepository.Add(apiKey);
|
||||
|
||||
var response = Mapper.Map<CreateApiKeyResponse>(finalApiKey);
|
||||
var response = new CreateApiKeyResponse
|
||||
{
|
||||
Id = finalApiKey.Id,
|
||||
PermissionsJson = finalApiKey.PermissionsJson,
|
||||
Description = finalApiKey.Description,
|
||||
ExpiresAt = finalApiKey.ExpiresAt,
|
||||
Secret = ApiKeyService.GenerateJwt(finalApiKey)
|
||||
};
|
||||
|
||||
response.Secret = ApiKeyService.GenerateJwt(finalApiKey);
|
||||
|
||||
return response;
|
||||
}
|
||||
|
||||
[HttpPatch("{id}")]
|
||||
[RequirePermission("admin.apikeys.update")]
|
||||
public async Task<ApiKeyDetailResponse> Update([FromRoute] int id, [FromBody] UpdateApiKeyRequest request)
|
||||
=> await CrudHelper.Update(id, request);
|
||||
public async Task<ApiKeyResponse> Update([FromRoute] int id, [FromBody] UpdateApiKeyRequest request)
|
||||
{
|
||||
var apiKey = await ApiKeyRepository
|
||||
.Get()
|
||||
.FirstOrDefaultAsync(x => x.Id == id);
|
||||
|
||||
if (apiKey == null)
|
||||
throw new HttpApiException("No api key with that id found", 404);
|
||||
|
||||
apiKey.Description = request.Description;
|
||||
apiKey.PermissionsJson = request.PermissionsJson;
|
||||
apiKey.ExpiresAt = request.ExpiresAt;
|
||||
|
||||
await ApiKeyRepository.Update(apiKey);
|
||||
|
||||
return new ApiKeyResponse()
|
||||
{
|
||||
Id = apiKey.Id,
|
||||
Description = apiKey.Description,
|
||||
PermissionsJson = apiKey.PermissionsJson,
|
||||
ExpiresAt = apiKey.ExpiresAt
|
||||
};
|
||||
}
|
||||
|
||||
[HttpDelete("{id}")]
|
||||
[RequirePermission("admin.apikeys.delete")]
|
||||
public async Task Delete([FromRoute] int id)
|
||||
=> await CrudHelper.Delete(id);
|
||||
{
|
||||
var apiKey = await ApiKeyRepository
|
||||
.Get()
|
||||
.FirstOrDefaultAsync(x => x.Id == id);
|
||||
|
||||
if (apiKey == null)
|
||||
throw new HttpApiException("No api key with that id found", 404);
|
||||
|
||||
await ApiKeyRepository.Remove(apiKey);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user