Cleaned up interfaces. Extracted server state machine trigger handler to seperated classes. Removed legacy code
This commit is contained in:
@@ -3,7 +3,6 @@ using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using MoonCore.Exceptions;
|
||||
using MoonCore.Extended.Abstractions;
|
||||
using Moonlight.ApiServer.Database.Entities;
|
||||
using MoonlightServers.ApiServer.Database.Entities;
|
||||
using MoonlightServers.ApiServer.Services;
|
||||
using MoonlightServers.DaemonShared.Enums;
|
||||
@@ -38,11 +37,14 @@ public class FilesController : Controller
|
||||
}
|
||||
|
||||
[HttpGet("list")]
|
||||
public async Task<ServerFilesEntryResponse[]> List([FromRoute] int serverId, [FromQuery] string path)
|
||||
public async Task<ActionResult<ServerFilesEntryResponse[]>> List([FromRoute] int serverId, [FromQuery] string path)
|
||||
{
|
||||
var server = await GetServerById(serverId, ServerPermissionLevel.Read);
|
||||
|
||||
if (server.Value == null)
|
||||
return server.Result ?? Problem("Unable to retrieve server");
|
||||
|
||||
var entries = await ServerFileSystemService.List(server, path);
|
||||
var entries = await ServerFileSystemService.List(server.Value, path);
|
||||
|
||||
return entries.Select(x => new ServerFilesEntryResponse()
|
||||
{
|
||||
@@ -55,41 +57,62 @@ public class FilesController : Controller
|
||||
}
|
||||
|
||||
[HttpPost("move")]
|
||||
public async Task Move([FromRoute] int serverId, [FromQuery] string oldPath, [FromQuery] string newPath)
|
||||
public async Task<ActionResult> Move([FromRoute] int serverId, [FromQuery] string oldPath, [FromQuery] string newPath)
|
||||
{
|
||||
var server = await GetServerById(serverId, ServerPermissionLevel.ReadWrite);
|
||||
|
||||
if (server.Value == null)
|
||||
return server.Result ?? Problem("Unable to retrieve server");
|
||||
|
||||
await ServerFileSystemService.Move(server, oldPath, newPath);
|
||||
await ServerFileSystemService.Move(server.Value, oldPath, newPath);
|
||||
return NoContent();
|
||||
}
|
||||
|
||||
[HttpDelete("delete")]
|
||||
public async Task Delete([FromRoute] int serverId, [FromQuery] string path)
|
||||
public async Task<ActionResult> Delete([FromRoute] int serverId, [FromQuery] string path)
|
||||
{
|
||||
var server = await GetServerById(serverId, ServerPermissionLevel.ReadWrite);
|
||||
|
||||
if (server.Value == null)
|
||||
return server.Result ?? Problem("Unable to retrieve server");
|
||||
|
||||
await ServerFileSystemService.Delete(server, path);
|
||||
await ServerFileSystemService.Delete(server.Value, path);
|
||||
return NoContent();
|
||||
}
|
||||
|
||||
[HttpPost("mkdir")]
|
||||
public async Task Mkdir([FromRoute] int serverId, [FromQuery] string path)
|
||||
public async Task<ActionResult> Mkdir([FromRoute] int serverId, [FromQuery] string path)
|
||||
{
|
||||
var server = await GetServerById(serverId, ServerPermissionLevel.ReadWrite);
|
||||
|
||||
if (server.Value == null)
|
||||
return server.Result ?? Problem("Unable to retrieve server");
|
||||
|
||||
await ServerFileSystemService.Mkdir(server, path);
|
||||
await ServerFileSystemService.Mkdir(server.Value, path);
|
||||
return NoContent();
|
||||
}
|
||||
|
||||
[HttpPost("touch")]
|
||||
public async Task Touch([FromRoute] int serverId, [FromQuery] string path)
|
||||
public async Task<ActionResult> Touch([FromRoute] int serverId, [FromQuery] string path)
|
||||
{
|
||||
var server = await GetServerById(serverId, ServerPermissionLevel.ReadWrite);
|
||||
|
||||
if (server.Value == null)
|
||||
return server.Result ?? Problem("Unable to retrieve server");
|
||||
|
||||
await ServerFileSystemService.Mkdir(server, path);
|
||||
await ServerFileSystemService.Mkdir(server.Value, path);
|
||||
return NoContent();
|
||||
}
|
||||
|
||||
[HttpGet("upload")]
|
||||
public async Task<ServerFilesUploadResponse> Upload([FromRoute] int serverId)
|
||||
public async Task<ActionResult<ServerFilesUploadResponse>> Upload([FromRoute] int serverId)
|
||||
{
|
||||
var server = await GetServerById(serverId, ServerPermissionLevel.ReadWrite);
|
||||
var serverResult = await GetServerById(serverId, ServerPermissionLevel.ReadWrite);
|
||||
|
||||
if (serverResult.Value == null)
|
||||
return serverResult.Result ?? Problem("Unable to retrieve server");
|
||||
|
||||
var server = serverResult.Value;
|
||||
|
||||
var accessToken = NodeService.CreateAccessToken(
|
||||
server.Node,
|
||||
@@ -114,9 +137,14 @@ public class FilesController : Controller
|
||||
}
|
||||
|
||||
[HttpGet("download")]
|
||||
public async Task<ServerFilesDownloadResponse> Download([FromRoute] int serverId, [FromQuery] string path)
|
||||
public async Task<ActionResult<ServerFilesDownloadResponse>> Download([FromRoute] int serverId, [FromQuery] string path)
|
||||
{
|
||||
var server = await GetServerById(serverId, ServerPermissionLevel.Read);
|
||||
var serverResult = await GetServerById(serverId, ServerPermissionLevel.Read);
|
||||
|
||||
if (serverResult.Value == null)
|
||||
return serverResult.Result ?? Problem("Unable to retrieve server");
|
||||
|
||||
var server = serverResult.Value;
|
||||
|
||||
var accessToken = NodeService.CreateAccessToken(
|
||||
server.Node,
|
||||
@@ -142,28 +170,36 @@ public class FilesController : Controller
|
||||
}
|
||||
|
||||
[HttpPost("compress")]
|
||||
public async Task Compress([FromRoute] int serverId, [FromBody] ServerFilesCompressRequest request)
|
||||
public async Task<ActionResult> Compress([FromRoute] int serverId, [FromBody] ServerFilesCompressRequest request)
|
||||
{
|
||||
var server = await GetServerById(serverId, ServerPermissionLevel.ReadWrite);
|
||||
|
||||
if (server.Value == null)
|
||||
return server.Result ?? Problem("Unable to retrieve server");
|
||||
|
||||
if (!Enum.TryParse(request.Type, true, out CompressType type))
|
||||
throw new HttpApiException("Invalid compress type provided", 400);
|
||||
return Problem("Invalid compress type provided", statusCode: 400);
|
||||
|
||||
await ServerFileSystemService.Compress(server, type, request.Items, request.Destination);
|
||||
await ServerFileSystemService.Compress(server.Value, type, request.Items, request.Destination);
|
||||
return Ok();
|
||||
}
|
||||
|
||||
[HttpPost("decompress")]
|
||||
public async Task Decompress([FromRoute] int serverId, [FromBody] ServerFilesDecompressRequest request)
|
||||
public async Task<ActionResult> Decompress([FromRoute] int serverId, [FromBody] ServerFilesDecompressRequest request)
|
||||
{
|
||||
var server = await GetServerById(serverId, ServerPermissionLevel.ReadWrite);
|
||||
|
||||
if (server.Value == null)
|
||||
return server.Result ?? Problem("Unable to retrieve server");
|
||||
|
||||
if (!Enum.TryParse(request.Type, true, out CompressType type))
|
||||
throw new HttpApiException("Invalid compress type provided", 400);
|
||||
return Problem("Invalid decompress type provided", statusCode: 400);
|
||||
|
||||
await ServerFileSystemService.Decompress(server, type, request.Path, request.Destination);
|
||||
await ServerFileSystemService.Decompress(server.Value, type, request.Path, request.Destination);
|
||||
return NoContent();
|
||||
}
|
||||
|
||||
private async Task<Server> GetServerById(int serverId, ServerPermissionLevel level)
|
||||
private async Task<ActionResult<Server>> GetServerById(int serverId, ServerPermissionLevel level)
|
||||
{
|
||||
var server = await ServerRepository
|
||||
.Get()
|
||||
@@ -171,7 +207,7 @@ public class FilesController : Controller
|
||||
.FirstOrDefaultAsync(x => x.Id == serverId);
|
||||
|
||||
if (server == null)
|
||||
throw new HttpApiException("No server with this id found", 404);
|
||||
return Problem("No server with this id found", statusCode: 404);
|
||||
|
||||
var authorizeResult = await AuthorizeService.Authorize(
|
||||
User, server,
|
||||
@@ -181,9 +217,9 @@ public class FilesController : Controller
|
||||
|
||||
if (!authorizeResult.Succeeded)
|
||||
{
|
||||
throw new HttpApiException(
|
||||
return Problem(
|
||||
authorizeResult.Message ?? "No permission for the requested resource",
|
||||
403
|
||||
statusCode: 403
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
@@ -14,7 +14,7 @@ namespace MoonlightServers.ApiServer.Http.Controllers.Client;
|
||||
|
||||
[ApiController]
|
||||
[Authorize]
|
||||
[Route("api/client/servers")]
|
||||
[Route("api/client/servers/{serverId:int}")]
|
||||
public class PowerController : Controller
|
||||
{
|
||||
private readonly DatabaseRepository<Server> ServerRepository;
|
||||
@@ -32,31 +32,46 @@ public class PowerController : Controller
|
||||
AuthorizeService = authorizeService;
|
||||
}
|
||||
|
||||
[HttpPost("{serverId:int}/start")]
|
||||
[HttpPost("start")]
|
||||
[Authorize]
|
||||
public async Task Start([FromRoute] int serverId)
|
||||
public async Task<ActionResult> Start([FromRoute] int serverId)
|
||||
{
|
||||
var server = await GetServerById(serverId);
|
||||
await ServerService.Start(server);
|
||||
|
||||
if (server.Value == null)
|
||||
return server.Result ?? Problem("Unable to retrieve server");
|
||||
|
||||
await ServerService.Start(server.Value);
|
||||
return NoContent();
|
||||
}
|
||||
|
||||
[HttpPost("{serverId:int}/stop")]
|
||||
[HttpPost("stop")]
|
||||
[Authorize]
|
||||
public async Task Stop([FromRoute] int serverId)
|
||||
public async Task<ActionResult> Stop([FromRoute] int serverId)
|
||||
{
|
||||
var server = await GetServerById(serverId);
|
||||
await ServerService.Stop(server);
|
||||
|
||||
if (server.Value == null)
|
||||
return server.Result ?? Problem("Unable to retrieve server");
|
||||
|
||||
await ServerService.Stop(server.Value);
|
||||
return NoContent();
|
||||
}
|
||||
|
||||
[HttpPost("{serverId:int}/kill")]
|
||||
[HttpPost("kill")]
|
||||
[Authorize]
|
||||
public async Task Kill([FromRoute] int serverId)
|
||||
public async Task<ActionResult> Kill([FromRoute] int serverId)
|
||||
{
|
||||
var server = await GetServerById(serverId);
|
||||
await ServerService.Kill(server);
|
||||
|
||||
if (server.Value == null)
|
||||
return server.Result ?? Problem("Unable to retrieve server");
|
||||
|
||||
await ServerService.Kill(server.Value);
|
||||
return NoContent();
|
||||
}
|
||||
|
||||
private async Task<Server> GetServerById(int serverId)
|
||||
private async Task<ActionResult<Server>> GetServerById(int serverId)
|
||||
{
|
||||
var server = await ServerRepository
|
||||
.Get()
|
||||
@@ -64,7 +79,7 @@ public class PowerController : Controller
|
||||
.FirstOrDefaultAsync(x => x.Id == serverId);
|
||||
|
||||
if (server == null)
|
||||
throw new HttpApiException("No server with this id found", 404);
|
||||
return Problem("No server with this id found", statusCode: 404);
|
||||
|
||||
var authorizeResult = await AuthorizeService.Authorize(
|
||||
User, server,
|
||||
@@ -74,9 +89,9 @@ public class PowerController : Controller
|
||||
|
||||
if (!authorizeResult.Succeeded)
|
||||
{
|
||||
throw new HttpApiException(
|
||||
return Problem(
|
||||
authorizeResult.Message ?? "No permission for the requested resource",
|
||||
403
|
||||
statusCode: 403
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
@@ -5,6 +5,7 @@ using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using MoonCore.Exceptions;
|
||||
using MoonCore.Extended.Abstractions;
|
||||
using MoonCore.Extended.Models;
|
||||
using MoonCore.Models;
|
||||
using Moonlight.ApiServer.Database.Entities;
|
||||
using MoonlightServers.ApiServer.Database.Entities;
|
||||
@@ -51,15 +52,12 @@ public class ServersController : Controller
|
||||
}
|
||||
|
||||
[HttpGet]
|
||||
public async Task<PagedData<ServerDetailResponse>> GetAll(
|
||||
[FromQuery] [Range(0, int.MaxValue)] int page,
|
||||
[FromQuery] [Range(0, 100)] int pageSize
|
||||
)
|
||||
public async Task<ActionResult<PagedData<ServerDetailResponse>>> GetAll([FromQuery] PagedOptions options)
|
||||
{
|
||||
var userIdClaim = User.FindFirstValue("userId");
|
||||
var userIdClaim = User.FindFirstValue("UserId");
|
||||
|
||||
if (string.IsNullOrEmpty(userIdClaim))
|
||||
throw new HttpApiException("Only users are able to use this endpoint", 400);
|
||||
return Problem("Only users are able to use this endpoint", statusCode: 400);
|
||||
|
||||
var userId = int.Parse(userIdClaim);
|
||||
|
||||
@@ -71,11 +69,12 @@ public class ServersController : Controller
|
||||
.Where(x => x.OwnerId == userId);
|
||||
|
||||
var count = await query.CountAsync();
|
||||
|
||||
|
||||
var items = await query
|
||||
.OrderBy(x => x.Id)
|
||||
.Skip(page * pageSize)
|
||||
.Take(pageSize)
|
||||
.Skip(options.Page * options.PageSize)
|
||||
.Take(options.PageSize)
|
||||
.AsNoTracking()
|
||||
.ToArrayAsync();
|
||||
|
||||
var mappedItems = items.Select(x => new ServerDetailResponse()
|
||||
@@ -98,23 +97,20 @@ public class ServersController : Controller
|
||||
return new PagedData<ServerDetailResponse>()
|
||||
{
|
||||
Items = mappedItems,
|
||||
CurrentPage = page,
|
||||
PageSize = pageSize,
|
||||
CurrentPage = options.Page,
|
||||
TotalItems = count,
|
||||
TotalPages = count == 0 ? 0 : count / pageSize
|
||||
PageSize = options.PageSize,
|
||||
TotalPages = (int)Math.Ceiling(Math.Max(0, count) / (double)options.PageSize)
|
||||
};
|
||||
}
|
||||
|
||||
[HttpGet("shared")]
|
||||
public async Task<PagedData<ServerDetailResponse>> GetAllShared(
|
||||
[FromQuery] [Range(0, int.MaxValue)] int page,
|
||||
[FromQuery] [Range(0, 100)] int pageSize
|
||||
)
|
||||
public async Task<ActionResult<PagedData<ServerDetailResponse>>> GetAllShared([FromQuery] PagedOptions options)
|
||||
{
|
||||
var userIdClaim = User.FindFirstValue("userId");
|
||||
var userIdClaim = User.FindFirstValue("UserId");
|
||||
|
||||
if (string.IsNullOrEmpty(userIdClaim))
|
||||
throw new HttpApiException("Only users are able to use this endpoint", 400);
|
||||
return Problem("Only users are able to use this endpoint", statusCode: 400);
|
||||
|
||||
var userId = int.Parse(userIdClaim);
|
||||
|
||||
@@ -129,11 +125,11 @@ public class ServersController : Controller
|
||||
.Where(x => x.UserId == userId);
|
||||
|
||||
var count = await query.CountAsync();
|
||||
|
||||
|
||||
var items = await query
|
||||
.OrderBy(x => x.Id)
|
||||
.Skip(page * pageSize)
|
||||
.Take(pageSize)
|
||||
.Skip(options.Page * options.PageSize)
|
||||
.Take(options.PageSize)
|
||||
.ToArrayAsync();
|
||||
|
||||
var ownerIds = items
|
||||
@@ -171,15 +167,15 @@ public class ServersController : Controller
|
||||
return new PagedData<ServerDetailResponse>()
|
||||
{
|
||||
Items = mappedItems,
|
||||
CurrentPage = page,
|
||||
PageSize = pageSize,
|
||||
CurrentPage = options.Page,
|
||||
TotalItems = count,
|
||||
TotalPages = count == 0 ? 0 : count / pageSize
|
||||
PageSize = options.PageSize,
|
||||
TotalPages = (int)Math.Ceiling(Math.Max(0, count) / (double)options.PageSize)
|
||||
};
|
||||
}
|
||||
|
||||
[HttpGet("{serverId:int}")]
|
||||
public async Task<ServerDetailResponse> Get([FromRoute] int serverId)
|
||||
public async Task<ActionResult<ServerDetailResponse>> Get([FromRoute] int serverId)
|
||||
{
|
||||
var server = await ServerRepository
|
||||
.Get()
|
||||
@@ -189,7 +185,7 @@ public class ServersController : Controller
|
||||
.FirstOrDefaultAsync(x => x.Id == serverId);
|
||||
|
||||
if (server == null)
|
||||
throw new HttpApiException("No server with this id found", 404);
|
||||
return Problem("No server with this id found", statusCode: 404);
|
||||
|
||||
var authorizationResult = await AuthorizeService.Authorize(
|
||||
User,
|
||||
@@ -200,9 +196,9 @@ public class ServersController : Controller
|
||||
|
||||
if (!authorizationResult.Succeeded)
|
||||
{
|
||||
throw new HttpApiException(
|
||||
return Problem(
|
||||
authorizationResult.Message ?? "No server with this id found",
|
||||
404
|
||||
statusCode: 404
|
||||
);
|
||||
}
|
||||
|
||||
@@ -242,15 +238,18 @@ public class ServersController : Controller
|
||||
}
|
||||
|
||||
[HttpGet("{serverId:int}/status")]
|
||||
public async Task<ServerStatusResponse> GetStatus([FromRoute] int serverId)
|
||||
public async Task<ActionResult<ServerStatusResponse>> GetStatus([FromRoute] int serverId)
|
||||
{
|
||||
var server = await GetServerById(
|
||||
serverId,
|
||||
ServerPermissionConstants.Console,
|
||||
ServerPermissionLevel.None
|
||||
);
|
||||
|
||||
if (server.Value == null)
|
||||
return server.Result ?? Problem("Unable to retrieve server");
|
||||
|
||||
var status = await ServerService.GetStatus(server);
|
||||
var status = await ServerService.GetStatus(server.Value);
|
||||
|
||||
return new ServerStatusResponse()
|
||||
{
|
||||
@@ -259,13 +258,18 @@ public class ServersController : Controller
|
||||
}
|
||||
|
||||
[HttpGet("{serverId:int}/ws")]
|
||||
public async Task<ServerWebSocketResponse> GetWebSocket([FromRoute] int serverId)
|
||||
public async Task<ActionResult<ServerWebSocketResponse>> GetWebSocket([FromRoute] int serverId)
|
||||
{
|
||||
var server = await GetServerById(
|
||||
var serverResult = await GetServerById(
|
||||
serverId,
|
||||
ServerPermissionConstants.Console,
|
||||
ServerPermissionLevel.Read
|
||||
);
|
||||
|
||||
if (serverResult.Value == null)
|
||||
return serverResult.Result ?? Problem("Unable to retrieve server");
|
||||
|
||||
var server = serverResult.Value;
|
||||
|
||||
// TODO: Handle transparent node proxy
|
||||
|
||||
@@ -288,15 +292,18 @@ public class ServersController : Controller
|
||||
}
|
||||
|
||||
[HttpGet("{serverId:int}/logs")]
|
||||
public async Task<ServerLogsResponse> GetLogs([FromRoute] int serverId)
|
||||
public async Task<ActionResult<ServerLogsResponse>> GetLogs([FromRoute] int serverId)
|
||||
{
|
||||
var server = await GetServerById(
|
||||
serverId,
|
||||
ServerPermissionConstants.Console,
|
||||
ServerPermissionLevel.Read
|
||||
);
|
||||
|
||||
if (server.Value == null)
|
||||
return server.Result ?? Problem("Unable to retrieve server");
|
||||
|
||||
var logs = await ServerService.GetLogs(server);
|
||||
var logs = await ServerService.GetLogs(server.Value);
|
||||
|
||||
return new ServerLogsResponse()
|
||||
{
|
||||
@@ -305,15 +312,18 @@ public class ServersController : Controller
|
||||
}
|
||||
|
||||
[HttpGet("{serverId:int}/stats")]
|
||||
public async Task<ServerStatsResponse> GetStats([FromRoute] int serverId)
|
||||
public async Task<ActionResult<ServerStatsResponse>> GetStats([FromRoute] int serverId)
|
||||
{
|
||||
var server = await GetServerById(
|
||||
serverId,
|
||||
ServerPermissionConstants.Console,
|
||||
ServerPermissionLevel.Read
|
||||
);
|
||||
|
||||
if (server.Value == null)
|
||||
return server.Result ?? Problem("Unable to retrieve server");
|
||||
|
||||
var stats = await ServerService.GetStats(server);
|
||||
var stats = await ServerService.GetStats(server.Value);
|
||||
|
||||
return new ServerStatsResponse()
|
||||
{
|
||||
@@ -327,7 +337,7 @@ public class ServersController : Controller
|
||||
}
|
||||
|
||||
[HttpPost("{serverId:int}/command")]
|
||||
public async Task Command([FromRoute] int serverId, [FromBody] ServerCommandRequest request)
|
||||
public async Task<ActionResult> Command([FromRoute] int serverId, [FromBody] ServerCommandRequest request)
|
||||
{
|
||||
var server = await GetServerById(
|
||||
serverId,
|
||||
@@ -335,10 +345,15 @@ public class ServersController : Controller
|
||||
ServerPermissionLevel.ReadWrite
|
||||
);
|
||||
|
||||
await ServerService.RunCommand(server, request.Command);
|
||||
if (server.Value == null)
|
||||
return server.Result ?? Problem("Unable to retrieve server");
|
||||
|
||||
await ServerService.RunCommand(server.Value, request.Command);
|
||||
|
||||
return NoContent();
|
||||
}
|
||||
|
||||
private async Task<Server> GetServerById(int serverId, string permissionId, ServerPermissionLevel level)
|
||||
private async Task<ActionResult<Server>> GetServerById(int serverId, string permissionId, ServerPermissionLevel level)
|
||||
{
|
||||
var server = await ServerRepository
|
||||
.Get()
|
||||
@@ -346,15 +361,15 @@ public class ServersController : Controller
|
||||
.FirstOrDefaultAsync(x => x.Id == serverId);
|
||||
|
||||
if (server == null)
|
||||
throw new HttpApiException("No server with this id found", 404);
|
||||
return Problem("No server with this id found", statusCode: 404);
|
||||
|
||||
var authorizeResult = await AuthorizeService.Authorize(User, server, permissionId, level);
|
||||
|
||||
if (!authorizeResult.Succeeded)
|
||||
{
|
||||
throw new HttpApiException(
|
||||
return Problem(
|
||||
authorizeResult.Message ?? "No permission for the requested resource",
|
||||
403
|
||||
statusCode: 403
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
@@ -32,13 +32,18 @@ public class SettingsController : Controller
|
||||
|
||||
[HttpPost("{serverId:int}/install")]
|
||||
[Authorize]
|
||||
public async Task Install([FromRoute] int serverId)
|
||||
public async Task<ActionResult> Install([FromRoute] int serverId)
|
||||
{
|
||||
var server = await GetServerById(serverId);
|
||||
await ServerService.Install(server);
|
||||
|
||||
if (server.Value == null)
|
||||
return server.Result ?? Problem("Unable to retrieve server");
|
||||
|
||||
await ServerService.Install(server.Value);
|
||||
return NoContent();
|
||||
}
|
||||
|
||||
private async Task<Server> GetServerById(int serverId)
|
||||
private async Task<ActionResult<Server>> GetServerById(int serverId)
|
||||
{
|
||||
var server = await ServerRepository
|
||||
.Get()
|
||||
@@ -46,7 +51,7 @@ public class SettingsController : Controller
|
||||
.FirstOrDefaultAsync(x => x.Id == serverId);
|
||||
|
||||
if (server == null)
|
||||
throw new HttpApiException("No server with this id found", 404);
|
||||
return Problem("No server with this id found", statusCode: 404);
|
||||
|
||||
var authorizeResult = await AuthorizeService.Authorize(
|
||||
User, server,
|
||||
@@ -56,9 +61,9 @@ public class SettingsController : Controller
|
||||
|
||||
if (!authorizeResult.Succeeded)
|
||||
{
|
||||
throw new HttpApiException(
|
||||
return Problem(
|
||||
authorizeResult.Message ?? "No permission for the requested resource",
|
||||
403
|
||||
statusCode: 403
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
@@ -4,6 +4,7 @@ using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using MoonCore.Exceptions;
|
||||
using MoonCore.Extended.Abstractions;
|
||||
using MoonCore.Extended.Models;
|
||||
using MoonCore.Models;
|
||||
using Moonlight.ApiServer.Database.Entities;
|
||||
using MoonlightServers.ApiServer.Database.Entities;
|
||||
@@ -41,24 +42,26 @@ public class SharesController : Controller
|
||||
}
|
||||
|
||||
[HttpGet]
|
||||
public async Task<PagedData<ServerShareResponse>> GetAll(
|
||||
public async Task<ActionResult<IPagedData<ServerShareResponse>>> GetAll(
|
||||
[FromRoute] int serverId,
|
||||
[FromQuery] [Range(0, int.MaxValue)] int page,
|
||||
[FromQuery] [Range(1, 100)] int pageSize
|
||||
[FromQuery] PagedOptions options
|
||||
)
|
||||
{
|
||||
var server = await GetServerById(serverId);
|
||||
|
||||
if (server.Value == null)
|
||||
return server.Result ?? Problem("Unable to retrieve server");
|
||||
|
||||
var query = ShareRepository
|
||||
.Get()
|
||||
.Where(x => x.Server.Id == server.Id);
|
||||
.Where(x => x.Server.Id == server.Value.Id);
|
||||
|
||||
var count = await query.CountAsync();
|
||||
|
||||
var items = await query
|
||||
.OrderBy(x => x.Id)
|
||||
.Skip(page * pageSize)
|
||||
.Take(pageSize)
|
||||
.Skip(options.Page * options.PageSize)
|
||||
.Take(options.PageSize)
|
||||
.ToArrayAsync();
|
||||
|
||||
var userIds = items
|
||||
@@ -81,27 +84,30 @@ public class SharesController : Controller
|
||||
return new PagedData<ServerShareResponse>()
|
||||
{
|
||||
Items = mappedItems,
|
||||
CurrentPage = page,
|
||||
PageSize = pageSize,
|
||||
CurrentPage = options.Page,
|
||||
PageSize = options.PageSize,
|
||||
TotalItems = count,
|
||||
TotalPages = count == 0 ? 0 : count / pageSize
|
||||
TotalPages = (int)Math.Ceiling(Math.Max(0, count) / (double)options.PageSize)
|
||||
};
|
||||
}
|
||||
|
||||
[HttpGet("{id:int}")]
|
||||
public async Task<ServerShareResponse> Get(
|
||||
public async Task<ActionResult<ServerShareResponse>> Get(
|
||||
[FromRoute] int serverId,
|
||||
[FromRoute] int id
|
||||
)
|
||||
{
|
||||
var server = await GetServerById(serverId);
|
||||
|
||||
if (server.Value == null)
|
||||
return server.Result ?? Problem("Unable to retrieve server");
|
||||
|
||||
var share = await ShareRepository
|
||||
.Get()
|
||||
.FirstOrDefaultAsync(x => x.Server.Id == server.Id && x.Id == id);
|
||||
.FirstOrDefaultAsync(x => x.Server.Id == server.Value.Id && x.Id == id);
|
||||
|
||||
if (share == null)
|
||||
throw new HttpApiException("A share with that id cannot be found", 404);
|
||||
return Problem("A share with that id cannot be found", statusCode: 404);
|
||||
|
||||
var user = await UserRepository
|
||||
.Get()
|
||||
@@ -118,23 +124,26 @@ public class SharesController : Controller
|
||||
}
|
||||
|
||||
[HttpPost]
|
||||
public async Task<ServerShareResponse> Create(
|
||||
public async Task<ActionResult<ServerShareResponse>> Create(
|
||||
[FromRoute] int serverId,
|
||||
[FromBody] CreateShareRequest request
|
||||
)
|
||||
{
|
||||
var server = await GetServerById(serverId);
|
||||
|
||||
if (server.Value == null)
|
||||
return server.Result ?? Problem("Unable to retrieve server");
|
||||
|
||||
var user = await UserRepository
|
||||
.Get()
|
||||
.FirstOrDefaultAsync(x => x.Username == request.Username);
|
||||
|
||||
if (user == null)
|
||||
throw new HttpApiException("A user with that username could not be found", 400);
|
||||
return Problem("A user with that username could not be found", statusCode: 400);
|
||||
|
||||
var share = new ServerShare()
|
||||
{
|
||||
Server = server,
|
||||
Server = server.Value,
|
||||
Content = ShareMapper.MapToServerShareContent(request.Permissions),
|
||||
CreatedAt = DateTime.UtcNow,
|
||||
UpdatedAt = DateTime.UtcNow,
|
||||
@@ -154,20 +163,23 @@ public class SharesController : Controller
|
||||
}
|
||||
|
||||
[HttpPatch("{id:int}")]
|
||||
public async Task<ServerShareResponse> Update(
|
||||
public async Task<ActionResult<ServerShareResponse>> Update(
|
||||
[FromRoute] int serverId,
|
||||
[FromRoute] int id,
|
||||
[FromBody] UpdateShareRequest request
|
||||
)
|
||||
{
|
||||
var server = await GetServerById(serverId);
|
||||
|
||||
if (server.Value == null)
|
||||
return server.Result ?? Problem("Unable to retrieve server");
|
||||
|
||||
var share = await ShareRepository
|
||||
.Get()
|
||||
.FirstOrDefaultAsync(x => x.Server.Id == server.Id && x.Id == id);
|
||||
.FirstOrDefaultAsync(x => x.Server.Id == server.Value.Id && x.Id == id);
|
||||
|
||||
if (share == null)
|
||||
throw new HttpApiException("A share with that id cannot be found", 404);
|
||||
return Problem("A share with that id cannot be found", statusCode: 404);
|
||||
|
||||
share.Content = ShareMapper.MapToServerShareContent(request.Permissions);
|
||||
|
||||
@@ -180,7 +192,7 @@ public class SharesController : Controller
|
||||
.FirstOrDefaultAsync(x => x.Id == share.UserId);
|
||||
|
||||
if (user == null)
|
||||
throw new HttpApiException("A user with that id could not be found", 400);
|
||||
return Problem("A user with that id could not be found", statusCode: 400);
|
||||
|
||||
var mappedItem = new ServerShareResponse()
|
||||
{
|
||||
@@ -193,31 +205,35 @@ public class SharesController : Controller
|
||||
}
|
||||
|
||||
[HttpDelete("{id:int}")]
|
||||
public async Task Delete(
|
||||
public async Task<ActionResult> Delete(
|
||||
[FromRoute] int serverId,
|
||||
[FromRoute] int id
|
||||
)
|
||||
{
|
||||
var server = await GetServerById(serverId);
|
||||
|
||||
if (server.Value == null)
|
||||
return server.Result ?? Problem("Unable to retrieve server");
|
||||
|
||||
var share = await ShareRepository
|
||||
.Get()
|
||||
.FirstOrDefaultAsync(x => x.Server.Id == server.Id && x.Id == id);
|
||||
.FirstOrDefaultAsync(x => x.Server.Id == server.Value.Id && x.Id == id);
|
||||
|
||||
if (share == null)
|
||||
throw new HttpApiException("A share with that id cannot be found", 404);
|
||||
return Problem("A share with that id cannot be found", statusCode: 404);
|
||||
|
||||
await ShareRepository.Remove(share);
|
||||
return NoContent();
|
||||
}
|
||||
|
||||
private async Task<Server> GetServerById(int serverId)
|
||||
private async Task<ActionResult<Server>> GetServerById(int serverId)
|
||||
{
|
||||
var server = await ServerRepository
|
||||
.Get()
|
||||
.FirstOrDefaultAsync(x => x.Id == serverId);
|
||||
|
||||
if (server == null)
|
||||
throw new HttpApiException("No server with this id found", 404);
|
||||
return Problem("No server with this id found", statusCode: 404);
|
||||
|
||||
var authorizeResult = await AuthorizeService.Authorize(
|
||||
User, server,
|
||||
@@ -227,9 +243,9 @@ public class SharesController : Controller
|
||||
|
||||
if (!authorizeResult.Succeeded)
|
||||
{
|
||||
throw new HttpApiException(
|
||||
return Problem(
|
||||
authorizeResult.Message ?? "No permission for the requested resource",
|
||||
403
|
||||
statusCode: 403
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
@@ -4,6 +4,7 @@ using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using MoonCore.Exceptions;
|
||||
using MoonCore.Extended.Abstractions;
|
||||
using MoonCore.Extended.Models;
|
||||
using MoonCore.Models;
|
||||
using Moonlight.ApiServer.Database.Entities;
|
||||
using MoonlightServers.ApiServer.Database.Entities;
|
||||
@@ -40,24 +41,26 @@ public class VariablesController : Controller
|
||||
}
|
||||
|
||||
[HttpGet]
|
||||
public async Task<PagedData<ServerVariableDetailResponse>> Get(
|
||||
public async Task<ActionResult<PagedData<ServerVariableDetailResponse>>> Get(
|
||||
[FromRoute] int serverId,
|
||||
[FromQuery] [Range(0, int.MaxValue)] int page,
|
||||
[FromQuery] [Range(1, 100)] int pageSize
|
||||
[FromQuery] PagedOptions options
|
||||
)
|
||||
{
|
||||
var server = await GetServerById(serverId, ServerPermissionLevel.Read);
|
||||
|
||||
if (server.Value == null)
|
||||
return server.Result ?? Problem("Unable to retrieve server");
|
||||
|
||||
var query = StarVariableRepository
|
||||
.Get()
|
||||
.Where(x => x.Star.Id == server.Star.Id);
|
||||
.Where(x => x.Star.Id == server.Value.Star.Id);
|
||||
|
||||
var count = await query.CountAsync();
|
||||
|
||||
var starVariables = await query
|
||||
.OrderBy(x => x.Id)
|
||||
.Skip(page * pageSize)
|
||||
.Take(pageSize)
|
||||
.Skip(options.Page * options.PageSize)
|
||||
.Take(options.PageSize)
|
||||
.ToArrayAsync();
|
||||
|
||||
var starVariableKeys = starVariables
|
||||
@@ -66,7 +69,7 @@ public class VariablesController : Controller
|
||||
|
||||
var serverVariables = await ServerVariableRepository
|
||||
.Get()
|
||||
.Where(x => x.Server.Id == server.Id && starVariableKeys.Contains(x.Key))
|
||||
.Where(x => x.Server.Id == server.Value.Id && starVariableKeys.Contains(x.Key))
|
||||
.ToArrayAsync();
|
||||
|
||||
var responses = starVariables.Select(starVariable =>
|
||||
@@ -87,22 +90,27 @@ public class VariablesController : Controller
|
||||
return new PagedData<ServerVariableDetailResponse>()
|
||||
{
|
||||
Items = responses,
|
||||
CurrentPage = page,
|
||||
PageSize = pageSize,
|
||||
CurrentPage = options.Page,
|
||||
PageSize = options.PageSize,
|
||||
TotalItems = count,
|
||||
TotalPages = count == 0 ? 0 : count / pageSize
|
||||
TotalPages = (int)Math.Ceiling(Math.Max(0, count) / (double)options.PageSize)
|
||||
};
|
||||
}
|
||||
|
||||
[HttpPut]
|
||||
public async Task<ServerVariableDetailResponse> UpdateSingle(
|
||||
public async Task<ActionResult<ServerVariableDetailResponse>> UpdateSingle(
|
||||
[FromRoute] int serverId,
|
||||
[FromBody] UpdateServerVariableRequest request
|
||||
)
|
||||
{
|
||||
// TODO: Handle filter
|
||||
|
||||
var server = await GetServerById(serverId, ServerPermissionLevel.ReadWrite);
|
||||
var serverResult = await GetServerById(serverId, ServerPermissionLevel.ReadWrite);
|
||||
|
||||
if (serverResult.Value == null)
|
||||
return serverResult.Result ?? Problem("Unable to retrieve server");
|
||||
|
||||
var server = serverResult.Value;
|
||||
|
||||
var serverVariable = server.Variables.FirstOrDefault(x => x.Key == request.Key);
|
||||
var starVariable = server.Star.Variables.FirstOrDefault(x => x.Key == request.Key);
|
||||
@@ -125,12 +133,17 @@ public class VariablesController : Controller
|
||||
}
|
||||
|
||||
[HttpPatch]
|
||||
public async Task<ServerVariableDetailResponse[]> Update(
|
||||
public async Task<ActionResult<ServerVariableDetailResponse[]>> Update(
|
||||
[FromRoute] int serverId,
|
||||
[FromBody] UpdateServerVariableRangeRequest request
|
||||
)
|
||||
{
|
||||
var server = await GetServerById(serverId, ServerPermissionLevel.ReadWrite);
|
||||
var serverResult = await GetServerById(serverId, ServerPermissionLevel.ReadWrite);
|
||||
|
||||
if (serverResult.Value == null)
|
||||
return serverResult.Result ?? Problem("Unable to retrieve server");
|
||||
|
||||
var server = serverResult.Value;
|
||||
|
||||
foreach (var variable in request.Variables)
|
||||
{
|
||||
@@ -164,15 +177,17 @@ public class VariablesController : Controller
|
||||
}).ToArray();
|
||||
}
|
||||
|
||||
private async Task<Server> GetServerById(int serverId, ServerPermissionLevel level)
|
||||
private async Task<ActionResult<Server>> GetServerById(int serverId, ServerPermissionLevel level)
|
||||
{
|
||||
var server = await ServerRepository
|
||||
.Get()
|
||||
.Include(x => x.Star)
|
||||
.ThenInclude(x => x.Variables)
|
||||
.Include(x => x.Variables)
|
||||
.FirstOrDefaultAsync(x => x.Id == serverId);
|
||||
|
||||
if (server == null)
|
||||
throw new HttpApiException("No server with this id found", 404);
|
||||
return Problem("No server with this id found", statusCode: 404);
|
||||
|
||||
var authorizeResult = await AuthorizeService.Authorize(
|
||||
User, server,
|
||||
@@ -182,9 +197,9 @@ public class VariablesController : Controller
|
||||
|
||||
if (!authorizeResult.Succeeded)
|
||||
{
|
||||
throw new HttpApiException(
|
||||
return Problem(
|
||||
authorizeResult.Message ?? "No permission for the requested resource",
|
||||
403
|
||||
statusCode: 403
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user