Updated to latest moonlight and mooncore version. Done refactoring to async scheme and other changes. Recreated database migrations and cleaned models
This commit is contained in:
@@ -1,7 +1,6 @@
|
||||
using Microsoft.AspNetCore.Authorization;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using MoonCore.Exceptions;
|
||||
using MoonCore.Extended.Abstractions;
|
||||
using MoonlightServers.ApiServer.Database.Entities;
|
||||
using MoonlightServers.ApiServer.Services;
|
||||
@@ -37,14 +36,14 @@ public class FilesController : Controller
|
||||
}
|
||||
|
||||
[HttpGet("list")]
|
||||
public async Task<ActionResult<ServerFilesEntryResponse[]>> List([FromRoute] int serverId, [FromQuery] string path)
|
||||
public async Task<ActionResult<ServerFilesEntryResponse[]>> ListAsync([FromRoute] int serverId, [FromQuery] string path)
|
||||
{
|
||||
var server = await GetServerById(serverId, ServerPermissionLevel.Read);
|
||||
var server = await GetServerByIdAsync(serverId, ServerPermissionLevel.Read);
|
||||
|
||||
if (server.Value == null)
|
||||
return server.Result ?? Problem("Unable to retrieve server");
|
||||
|
||||
var entries = await ServerFileSystemService.List(server.Value, path);
|
||||
var entries = await ServerFileSystemService.ListAsync(server.Value, path);
|
||||
|
||||
return entries.Select(x => new ServerFilesEntryResponse()
|
||||
{
|
||||
@@ -57,57 +56,57 @@ public class FilesController : Controller
|
||||
}
|
||||
|
||||
[HttpPost("move")]
|
||||
public async Task<ActionResult> Move([FromRoute] int serverId, [FromQuery] string oldPath, [FromQuery] string newPath)
|
||||
public async Task<ActionResult> MoveAsync([FromRoute] int serverId, [FromQuery] string oldPath, [FromQuery] string newPath)
|
||||
{
|
||||
var server = await GetServerById(serverId, ServerPermissionLevel.ReadWrite);
|
||||
var server = await GetServerByIdAsync(serverId, ServerPermissionLevel.ReadWrite);
|
||||
|
||||
if (server.Value == null)
|
||||
return server.Result ?? Problem("Unable to retrieve server");
|
||||
|
||||
await ServerFileSystemService.Move(server.Value, oldPath, newPath);
|
||||
await ServerFileSystemService.MoveAsync(server.Value, oldPath, newPath);
|
||||
return NoContent();
|
||||
}
|
||||
|
||||
[HttpDelete("delete")]
|
||||
public async Task<ActionResult> Delete([FromRoute] int serverId, [FromQuery] string path)
|
||||
public async Task<ActionResult> DeleteAsync([FromRoute] int serverId, [FromQuery] string path)
|
||||
{
|
||||
var server = await GetServerById(serverId, ServerPermissionLevel.ReadWrite);
|
||||
var server = await GetServerByIdAsync(serverId, ServerPermissionLevel.ReadWrite);
|
||||
|
||||
if (server.Value == null)
|
||||
return server.Result ?? Problem("Unable to retrieve server");
|
||||
|
||||
await ServerFileSystemService.Delete(server.Value, path);
|
||||
await ServerFileSystemService.DeleteAsync(server.Value, path);
|
||||
return NoContent();
|
||||
}
|
||||
|
||||
[HttpPost("mkdir")]
|
||||
public async Task<ActionResult> Mkdir([FromRoute] int serverId, [FromQuery] string path)
|
||||
public async Task<ActionResult> MkdirAsync([FromRoute] int serverId, [FromQuery] string path)
|
||||
{
|
||||
var server = await GetServerById(serverId, ServerPermissionLevel.ReadWrite);
|
||||
var server = await GetServerByIdAsync(serverId, ServerPermissionLevel.ReadWrite);
|
||||
|
||||
if (server.Value == null)
|
||||
return server.Result ?? Problem("Unable to retrieve server");
|
||||
|
||||
await ServerFileSystemService.Mkdir(server.Value, path);
|
||||
await ServerFileSystemService.MkdirAsync(server.Value, path);
|
||||
return NoContent();
|
||||
}
|
||||
|
||||
[HttpPost("touch")]
|
||||
public async Task<ActionResult> Touch([FromRoute] int serverId, [FromQuery] string path)
|
||||
public async Task<ActionResult> TouchAsync([FromRoute] int serverId, [FromQuery] string path)
|
||||
{
|
||||
var server = await GetServerById(serverId, ServerPermissionLevel.ReadWrite);
|
||||
var server = await GetServerByIdAsync(serverId, ServerPermissionLevel.ReadWrite);
|
||||
|
||||
if (server.Value == null)
|
||||
return server.Result ?? Problem("Unable to retrieve server");
|
||||
|
||||
await ServerFileSystemService.Mkdir(server.Value, path);
|
||||
await ServerFileSystemService.MkdirAsync(server.Value, path);
|
||||
return NoContent();
|
||||
}
|
||||
|
||||
[HttpGet("upload")]
|
||||
public async Task<ActionResult<ServerFilesUploadResponse>> Upload([FromRoute] int serverId)
|
||||
public async Task<ActionResult<ServerFilesUploadResponse>> UploadAsync([FromRoute] int serverId)
|
||||
{
|
||||
var serverResult = await GetServerById(serverId, ServerPermissionLevel.ReadWrite);
|
||||
var serverResult = await GetServerByIdAsync(serverId, ServerPermissionLevel.ReadWrite);
|
||||
|
||||
if (serverResult.Value == null)
|
||||
return serverResult.Result ?? Problem("Unable to retrieve server");
|
||||
@@ -137,9 +136,9 @@ public class FilesController : Controller
|
||||
}
|
||||
|
||||
[HttpGet("download")]
|
||||
public async Task<ActionResult<ServerFilesDownloadResponse>> Download([FromRoute] int serverId, [FromQuery] string path)
|
||||
public async Task<ActionResult<ServerFilesDownloadResponse>> DownloadAsync([FromRoute] int serverId, [FromQuery] string path)
|
||||
{
|
||||
var serverResult = await GetServerById(serverId, ServerPermissionLevel.Read);
|
||||
var serverResult = await GetServerByIdAsync(serverId, ServerPermissionLevel.Read);
|
||||
|
||||
if (serverResult.Value == null)
|
||||
return serverResult.Result ?? Problem("Unable to retrieve server");
|
||||
@@ -170,9 +169,9 @@ public class FilesController : Controller
|
||||
}
|
||||
|
||||
[HttpPost("compress")]
|
||||
public async Task<ActionResult> Compress([FromRoute] int serverId, [FromBody] ServerFilesCompressRequest request)
|
||||
public async Task<ActionResult> CompressAsync([FromRoute] int serverId, [FromBody] ServerFilesCompressRequest request)
|
||||
{
|
||||
var server = await GetServerById(serverId, ServerPermissionLevel.ReadWrite);
|
||||
var server = await GetServerByIdAsync(serverId, ServerPermissionLevel.ReadWrite);
|
||||
|
||||
if (server.Value == null)
|
||||
return server.Result ?? Problem("Unable to retrieve server");
|
||||
@@ -180,14 +179,14 @@ public class FilesController : Controller
|
||||
if (!Enum.TryParse(request.Type, true, out CompressType type))
|
||||
return Problem("Invalid compress type provided", statusCode: 400);
|
||||
|
||||
await ServerFileSystemService.Compress(server.Value, type, request.Items, request.Destination);
|
||||
await ServerFileSystemService.CompressAsync(server.Value, type, request.Items, request.Destination);
|
||||
return Ok();
|
||||
}
|
||||
|
||||
[HttpPost("decompress")]
|
||||
public async Task<ActionResult> Decompress([FromRoute] int serverId, [FromBody] ServerFilesDecompressRequest request)
|
||||
public async Task<ActionResult> DecompressAsync([FromRoute] int serverId, [FromBody] ServerFilesDecompressRequest request)
|
||||
{
|
||||
var server = await GetServerById(serverId, ServerPermissionLevel.ReadWrite);
|
||||
var server = await GetServerByIdAsync(serverId, ServerPermissionLevel.ReadWrite);
|
||||
|
||||
if (server.Value == null)
|
||||
return server.Result ?? Problem("Unable to retrieve server");
|
||||
@@ -195,11 +194,11 @@ public class FilesController : Controller
|
||||
if (!Enum.TryParse(request.Type, true, out CompressType type))
|
||||
return Problem("Invalid decompress type provided", statusCode: 400);
|
||||
|
||||
await ServerFileSystemService.Decompress(server.Value, type, request.Path, request.Destination);
|
||||
await ServerFileSystemService.DecompressAsync(server.Value, type, request.Path, request.Destination);
|
||||
return NoContent();
|
||||
}
|
||||
|
||||
private async Task<ActionResult<Server>> GetServerById(int serverId, ServerPermissionLevel level)
|
||||
private async Task<ActionResult<Server>> GetServerByIdAsync(int serverId, ServerPermissionLevel level)
|
||||
{
|
||||
var server = await ServerRepository
|
||||
.Get()
|
||||
@@ -209,7 +208,7 @@ public class FilesController : Controller
|
||||
if (server == null)
|
||||
return Problem("No server with this id found", statusCode: 404);
|
||||
|
||||
var authorizeResult = await AuthorizeService.Authorize(
|
||||
var authorizeResult = await AuthorizeService.AuthorizeAsync(
|
||||
User, server,
|
||||
ServerPermissionConstants.Files,
|
||||
level
|
||||
|
||||
@@ -1,10 +1,7 @@
|
||||
using Microsoft.AspNetCore.Authorization;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using MoonCore.Exceptions;
|
||||
using MoonCore.Extended.Abstractions;
|
||||
using MoonCore.Helpers;
|
||||
using Moonlight.ApiServer.Database.Entities;
|
||||
using MoonlightServers.ApiServer.Database.Entities;
|
||||
using MoonlightServers.ApiServer.Services;
|
||||
using MoonlightServers.Shared.Constants;
|
||||
@@ -34,44 +31,44 @@ public class PowerController : Controller
|
||||
|
||||
[HttpPost("start")]
|
||||
[Authorize]
|
||||
public async Task<ActionResult> Start([FromRoute] int serverId)
|
||||
public async Task<ActionResult> StartAsync([FromRoute] int serverId)
|
||||
{
|
||||
var server = await GetServerById(serverId);
|
||||
var server = await GetServerByIdAsync(serverId);
|
||||
|
||||
if (server.Value == null)
|
||||
return server.Result ?? Problem("Unable to retrieve server");
|
||||
|
||||
await ServerService.Start(server.Value);
|
||||
await ServerService.StartAsync(server.Value);
|
||||
return NoContent();
|
||||
}
|
||||
|
||||
[HttpPost("stop")]
|
||||
[Authorize]
|
||||
public async Task<ActionResult> Stop([FromRoute] int serverId)
|
||||
public async Task<ActionResult> StopAsync([FromRoute] int serverId)
|
||||
{
|
||||
var server = await GetServerById(serverId);
|
||||
var server = await GetServerByIdAsync(serverId);
|
||||
|
||||
if (server.Value == null)
|
||||
return server.Result ?? Problem("Unable to retrieve server");
|
||||
|
||||
await ServerService.Stop(server.Value);
|
||||
await ServerService.StopAsync(server.Value);
|
||||
return NoContent();
|
||||
}
|
||||
|
||||
[HttpPost("kill")]
|
||||
[Authorize]
|
||||
public async Task<ActionResult> Kill([FromRoute] int serverId)
|
||||
public async Task<ActionResult> KillAsync([FromRoute] int serverId)
|
||||
{
|
||||
var server = await GetServerById(serverId);
|
||||
var server = await GetServerByIdAsync(serverId);
|
||||
|
||||
if (server.Value == null)
|
||||
return server.Result ?? Problem("Unable to retrieve server");
|
||||
|
||||
await ServerService.Kill(server.Value);
|
||||
await ServerService.KillAsync(server.Value);
|
||||
return NoContent();
|
||||
}
|
||||
|
||||
private async Task<ActionResult<Server>> GetServerById(int serverId)
|
||||
private async Task<ActionResult<Server>> GetServerByIdAsync(int serverId)
|
||||
{
|
||||
var server = await ServerRepository
|
||||
.Get()
|
||||
@@ -81,7 +78,7 @@ public class PowerController : Controller
|
||||
if (server == null)
|
||||
return Problem("No server with this id found", statusCode: 404);
|
||||
|
||||
var authorizeResult = await AuthorizeService.Authorize(
|
||||
var authorizeResult = await AuthorizeService.AuthorizeAsync(
|
||||
User, server,
|
||||
ServerPermissionConstants.Power,
|
||||
ServerPermissionLevel.ReadWrite
|
||||
|
||||
@@ -1,24 +1,19 @@
|
||||
using System.ComponentModel.DataAnnotations;
|
||||
using System.Security.Claims;
|
||||
using Microsoft.AspNetCore.Authorization;
|
||||
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;
|
||||
using MoonlightServers.ApiServer.Extensions;
|
||||
using MoonlightServers.ApiServer.Mappers;
|
||||
using MoonlightServers.ApiServer.Models;
|
||||
using MoonlightServers.ApiServer.Services;
|
||||
using MoonlightServers.Shared.Constants;
|
||||
using MoonlightServers.Shared.Enums;
|
||||
using MoonlightServers.Shared.Http.Requests.Client.Servers;
|
||||
using MoonlightServers.Shared.Http.Responses.Client.Servers;
|
||||
using MoonlightServers.Shared.Http.Responses.Client.Servers.Allocations;
|
||||
using MoonlightServers.Shared.Models;
|
||||
|
||||
namespace MoonlightServers.ApiServer.Http.Controllers.Client;
|
||||
|
||||
@@ -52,8 +47,14 @@ public class ServersController : Controller
|
||||
}
|
||||
|
||||
[HttpGet]
|
||||
public async Task<ActionResult<PagedData<ServerDetailResponse>>> GetAll([FromQuery] PagedOptions options)
|
||||
public async Task<ActionResult<CountedData<ServerDetailResponse>>> GetAllAsync(
|
||||
[FromQuery] int startIndex,
|
||||
[FromQuery] int count
|
||||
)
|
||||
{
|
||||
if (count > 100)
|
||||
return Problem("Only 100 items can be fetched at a time", statusCode: 400);
|
||||
|
||||
var userIdClaim = User.FindFirstValue("UserId");
|
||||
|
||||
if (string.IsNullOrEmpty(userIdClaim))
|
||||
@@ -68,12 +69,12 @@ public class ServersController : Controller
|
||||
.Include(x => x.Node)
|
||||
.Where(x => x.OwnerId == userId);
|
||||
|
||||
var count = await query.CountAsync();
|
||||
var totalCount = await query.CountAsync();
|
||||
|
||||
var items = await query
|
||||
.OrderBy(x => x.Id)
|
||||
.Skip(options.Page * options.PageSize)
|
||||
.Take(options.PageSize)
|
||||
.Skip(startIndex)
|
||||
.Take(count)
|
||||
.AsNoTracking()
|
||||
.ToArrayAsync();
|
||||
|
||||
@@ -94,19 +95,22 @@ public class ServersController : Controller
|
||||
}).ToArray()
|
||||
}).ToArray();
|
||||
|
||||
return new PagedData<ServerDetailResponse>()
|
||||
return new CountedData<ServerDetailResponse>()
|
||||
{
|
||||
Items = mappedItems,
|
||||
CurrentPage = options.Page,
|
||||
TotalItems = count,
|
||||
PageSize = options.PageSize,
|
||||
TotalPages = (int)Math.Ceiling(Math.Max(0, count) / (double)options.PageSize)
|
||||
TotalCount = totalCount
|
||||
};
|
||||
}
|
||||
|
||||
[HttpGet("shared")]
|
||||
public async Task<ActionResult<PagedData<ServerDetailResponse>>> GetAllShared([FromQuery] PagedOptions options)
|
||||
public async Task<ActionResult<CountedData<ServerDetailResponse>>> GetAllSharedAsync(
|
||||
[FromQuery] int startIndex,
|
||||
[FromQuery] int count
|
||||
)
|
||||
{
|
||||
if (count > 100)
|
||||
return Problem("Only 100 items can be fetched at a time", statusCode: 400);
|
||||
|
||||
var userIdClaim = User.FindFirstValue("UserId");
|
||||
|
||||
if (string.IsNullOrEmpty(userIdClaim))
|
||||
@@ -124,12 +128,12 @@ public class ServersController : Controller
|
||||
.ThenInclude(x => x.Allocations)
|
||||
.Where(x => x.UserId == userId);
|
||||
|
||||
var count = await query.CountAsync();
|
||||
var totalCount = await query.CountAsync();
|
||||
|
||||
var items = await query
|
||||
.OrderBy(x => x.Id)
|
||||
.Skip(options.Page * options.PageSize)
|
||||
.Take(options.PageSize)
|
||||
.Skip(startIndex)
|
||||
.Take(count)
|
||||
.ToArrayAsync();
|
||||
|
||||
var ownerIds = items
|
||||
@@ -164,18 +168,15 @@ public class ServersController : Controller
|
||||
}
|
||||
}).ToArray();
|
||||
|
||||
return new PagedData<ServerDetailResponse>()
|
||||
return new CountedData<ServerDetailResponse>()
|
||||
{
|
||||
Items = mappedItems,
|
||||
CurrentPage = options.Page,
|
||||
TotalItems = count,
|
||||
PageSize = options.PageSize,
|
||||
TotalPages = (int)Math.Ceiling(Math.Max(0, count) / (double)options.PageSize)
|
||||
TotalCount = totalCount
|
||||
};
|
||||
}
|
||||
|
||||
[HttpGet("{serverId:int}")]
|
||||
public async Task<ActionResult<ServerDetailResponse>> Get([FromRoute] int serverId)
|
||||
public async Task<ActionResult<ServerDetailResponse>> GetAsync([FromRoute] int serverId)
|
||||
{
|
||||
var server = await ServerRepository
|
||||
.Get()
|
||||
@@ -187,7 +188,7 @@ public class ServersController : Controller
|
||||
if (server == null)
|
||||
return Problem("No server with this id found", statusCode: 404);
|
||||
|
||||
var authorizationResult = await AuthorizeService.Authorize(
|
||||
var authorizationResult = await AuthorizeService.AuthorizeAsync(
|
||||
User,
|
||||
server,
|
||||
String.Empty,
|
||||
@@ -238,18 +239,18 @@ public class ServersController : Controller
|
||||
}
|
||||
|
||||
[HttpGet("{serverId:int}/status")]
|
||||
public async Task<ActionResult<ServerStatusResponse>> GetStatus([FromRoute] int serverId)
|
||||
public async Task<ActionResult<ServerStatusResponse>> GetStatusAsync([FromRoute] int serverId)
|
||||
{
|
||||
var server = await GetServerById(
|
||||
var server = await GetServerByIdAsync(
|
||||
serverId,
|
||||
ServerPermissionConstants.Console,
|
||||
ServerPermissionLevel.None
|
||||
);
|
||||
|
||||
|
||||
if (server.Value == null)
|
||||
return server.Result ?? Problem("Unable to retrieve server");
|
||||
|
||||
var status = await ServerService.GetStatus(server.Value);
|
||||
var status = await ServerService.GetStatusAsync(server.Value);
|
||||
|
||||
return new ServerStatusResponse()
|
||||
{
|
||||
@@ -258,14 +259,14 @@ public class ServersController : Controller
|
||||
}
|
||||
|
||||
[HttpGet("{serverId:int}/ws")]
|
||||
public async Task<ActionResult<ServerWebSocketResponse>> GetWebSocket([FromRoute] int serverId)
|
||||
public async Task<ActionResult<ServerWebSocketResponse>> GetWebSocketAsync([FromRoute] int serverId)
|
||||
{
|
||||
var serverResult = await GetServerById(
|
||||
var serverResult = await GetServerByIdAsync(
|
||||
serverId,
|
||||
ServerPermissionConstants.Console,
|
||||
ServerPermissionLevel.Read
|
||||
);
|
||||
|
||||
|
||||
if (serverResult.Value == null)
|
||||
return serverResult.Result ?? Problem("Unable to retrieve server");
|
||||
|
||||
@@ -292,18 +293,18 @@ public class ServersController : Controller
|
||||
}
|
||||
|
||||
[HttpGet("{serverId:int}/logs")]
|
||||
public async Task<ActionResult<ServerLogsResponse>> GetLogs([FromRoute] int serverId)
|
||||
public async Task<ActionResult<ServerLogsResponse>> GetLogsAsync([FromRoute] int serverId)
|
||||
{
|
||||
var server = await GetServerById(
|
||||
var server = await GetServerByIdAsync(
|
||||
serverId,
|
||||
ServerPermissionConstants.Console,
|
||||
ServerPermissionLevel.Read
|
||||
);
|
||||
|
||||
|
||||
if (server.Value == null)
|
||||
return server.Result ?? Problem("Unable to retrieve server");
|
||||
|
||||
var logs = await ServerService.GetLogs(server.Value);
|
||||
var logs = await ServerService.GetLogsAsync(server.Value);
|
||||
|
||||
return new ServerLogsResponse()
|
||||
{
|
||||
@@ -312,18 +313,18 @@ public class ServersController : Controller
|
||||
}
|
||||
|
||||
[HttpGet("{serverId:int}/stats")]
|
||||
public async Task<ActionResult<ServerStatsResponse>> GetStats([FromRoute] int serverId)
|
||||
public async Task<ActionResult<ServerStatsResponse>> GetStatsAsync([FromRoute] int serverId)
|
||||
{
|
||||
var server = await GetServerById(
|
||||
var server = await GetServerByIdAsync(
|
||||
serverId,
|
||||
ServerPermissionConstants.Console,
|
||||
ServerPermissionLevel.Read
|
||||
);
|
||||
|
||||
|
||||
if (server.Value == null)
|
||||
return server.Result ?? Problem("Unable to retrieve server");
|
||||
|
||||
var stats = await ServerService.GetStats(server.Value);
|
||||
var stats = await ServerService.GetStatsAsync(server.Value);
|
||||
|
||||
return new ServerStatsResponse()
|
||||
{
|
||||
@@ -337,9 +338,9 @@ public class ServersController : Controller
|
||||
}
|
||||
|
||||
[HttpPost("{serverId:int}/command")]
|
||||
public async Task<ActionResult> Command([FromRoute] int serverId, [FromBody] ServerCommandRequest request)
|
||||
public async Task<ActionResult> CommandAsync([FromRoute] int serverId, [FromBody] ServerCommandRequest request)
|
||||
{
|
||||
var server = await GetServerById(
|
||||
var server = await GetServerByIdAsync(
|
||||
serverId,
|
||||
ServerPermissionConstants.Console,
|
||||
ServerPermissionLevel.ReadWrite
|
||||
@@ -348,12 +349,13 @@ public class ServersController : Controller
|
||||
if (server.Value == null)
|
||||
return server.Result ?? Problem("Unable to retrieve server");
|
||||
|
||||
await ServerService.RunCommand(server.Value, request.Command);
|
||||
await ServerService.RunCommandAsync(server.Value, request.Command);
|
||||
|
||||
return NoContent();
|
||||
}
|
||||
|
||||
private async Task<ActionResult<Server>> GetServerById(int serverId, string permissionId, ServerPermissionLevel level)
|
||||
private async Task<ActionResult<Server>> GetServerByIdAsync(int serverId, string permissionId,
|
||||
ServerPermissionLevel level)
|
||||
{
|
||||
var server = await ServerRepository
|
||||
.Get()
|
||||
@@ -363,7 +365,7 @@ public class ServersController : Controller
|
||||
if (server == null)
|
||||
return Problem("No server with this id found", statusCode: 404);
|
||||
|
||||
var authorizeResult = await AuthorizeService.Authorize(User, server, permissionId, level);
|
||||
var authorizeResult = await AuthorizeService.AuthorizeAsync(User, server, permissionId, level);
|
||||
|
||||
if (!authorizeResult.Succeeded)
|
||||
{
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
using Microsoft.AspNetCore.Authorization;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using MoonCore.Exceptions;
|
||||
using MoonCore.Extended.Abstractions;
|
||||
using MoonlightServers.ApiServer.Database.Entities;
|
||||
using MoonlightServers.ApiServer.Services;
|
||||
@@ -32,18 +31,18 @@ public class SettingsController : Controller
|
||||
|
||||
[HttpPost("{serverId:int}/install")]
|
||||
[Authorize]
|
||||
public async Task<ActionResult> Install([FromRoute] int serverId)
|
||||
public async Task<ActionResult> InstallAsync([FromRoute] int serverId)
|
||||
{
|
||||
var server = await GetServerById(serverId);
|
||||
var server = await GetServerByIdAsync(serverId);
|
||||
|
||||
if (server.Value == null)
|
||||
return server.Result ?? Problem("Unable to retrieve server");
|
||||
|
||||
await ServerService.Install(server.Value);
|
||||
await ServerService.InstallAsync(server.Value);
|
||||
return NoContent();
|
||||
}
|
||||
|
||||
private async Task<ActionResult<Server>> GetServerById(int serverId)
|
||||
private async Task<ActionResult<Server>> GetServerByIdAsync(int serverId)
|
||||
{
|
||||
var server = await ServerRepository
|
||||
.Get()
|
||||
@@ -53,7 +52,7 @@ public class SettingsController : Controller
|
||||
if (server == null)
|
||||
return Problem("No server with this id found", statusCode: 404);
|
||||
|
||||
var authorizeResult = await AuthorizeService.Authorize(
|
||||
var authorizeResult = await AuthorizeService.AuthorizeAsync(
|
||||
User, server,
|
||||
ServerPermissionConstants.Settings,
|
||||
ServerPermissionLevel.ReadWrite
|
||||
|
||||
@@ -1,15 +1,11 @@
|
||||
using System.ComponentModel.DataAnnotations;
|
||||
using Microsoft.AspNetCore.Authorization;
|
||||
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;
|
||||
using MoonlightServers.ApiServer.Mappers;
|
||||
using MoonlightServers.ApiServer.Models;
|
||||
using MoonlightServers.ApiServer.Services;
|
||||
using MoonlightServers.Shared.Constants;
|
||||
using MoonlightServers.Shared.Enums;
|
||||
@@ -42,12 +38,16 @@ public class SharesController : Controller
|
||||
}
|
||||
|
||||
[HttpGet]
|
||||
public async Task<ActionResult<IPagedData<ServerShareResponse>>> GetAll(
|
||||
public async Task<ActionResult<CountedData<ServerShareResponse>>> GetAllAsync(
|
||||
[FromRoute] int serverId,
|
||||
[FromQuery] PagedOptions options
|
||||
[FromQuery] int startIndex,
|
||||
[FromQuery] int count
|
||||
)
|
||||
{
|
||||
var server = await GetServerById(serverId);
|
||||
if (count > 100)
|
||||
return Problem("Only 100 items can be fetched at a time", statusCode: 400);
|
||||
|
||||
var server = await GetServerByIdAsync(serverId);
|
||||
|
||||
if (server.Value == null)
|
||||
return server.Result ?? Problem("Unable to retrieve server");
|
||||
@@ -56,12 +56,12 @@ public class SharesController : Controller
|
||||
.Get()
|
||||
.Where(x => x.Server.Id == server.Value.Id);
|
||||
|
||||
var count = await query.CountAsync();
|
||||
var totalCount = await query.CountAsync();
|
||||
|
||||
var items = await query
|
||||
.OrderBy(x => x.Id)
|
||||
.Skip(options.Page * options.PageSize)
|
||||
.Take(options.PageSize)
|
||||
.Skip(startIndex)
|
||||
.Take(count)
|
||||
.ToArrayAsync();
|
||||
|
||||
var userIds = items
|
||||
@@ -81,23 +81,20 @@ public class SharesController : Controller
|
||||
Permissions = ShareMapper.MapToPermissionLevels(x.Content)
|
||||
}).ToArray();
|
||||
|
||||
return new PagedData<ServerShareResponse>()
|
||||
return new CountedData<ServerShareResponse>()
|
||||
{
|
||||
Items = mappedItems,
|
||||
CurrentPage = options.Page,
|
||||
PageSize = options.PageSize,
|
||||
TotalItems = count,
|
||||
TotalPages = (int)Math.Ceiling(Math.Max(0, count) / (double)options.PageSize)
|
||||
TotalCount = totalCount
|
||||
};
|
||||
}
|
||||
|
||||
[HttpGet("{id:int}")]
|
||||
public async Task<ActionResult<ServerShareResponse>> Get(
|
||||
public async Task<ActionResult<ServerShareResponse>> GetAsync(
|
||||
[FromRoute] int serverId,
|
||||
[FromRoute] int id
|
||||
)
|
||||
{
|
||||
var server = await GetServerById(serverId);
|
||||
var server = await GetServerByIdAsync(serverId);
|
||||
|
||||
if (server.Value == null)
|
||||
return server.Result ?? Problem("Unable to retrieve server");
|
||||
@@ -124,12 +121,12 @@ public class SharesController : Controller
|
||||
}
|
||||
|
||||
[HttpPost]
|
||||
public async Task<ActionResult<ServerShareResponse>> Create(
|
||||
public async Task<ActionResult<ServerShareResponse>> CreateAsync(
|
||||
[FromRoute] int serverId,
|
||||
[FromBody] CreateShareRequest request
|
||||
)
|
||||
{
|
||||
var server = await GetServerById(serverId);
|
||||
var server = await GetServerByIdAsync(serverId);
|
||||
|
||||
if (server.Value == null)
|
||||
return server.Result ?? Problem("Unable to retrieve server");
|
||||
@@ -150,7 +147,7 @@ public class SharesController : Controller
|
||||
UserId = user.Id
|
||||
};
|
||||
|
||||
var finalShare = await ShareRepository.Add(share);
|
||||
var finalShare = await ShareRepository.AddAsync(share);
|
||||
|
||||
var mappedItem = new ServerShareResponse()
|
||||
{
|
||||
@@ -163,13 +160,13 @@ public class SharesController : Controller
|
||||
}
|
||||
|
||||
[HttpPatch("{id:int}")]
|
||||
public async Task<ActionResult<ServerShareResponse>> Update(
|
||||
public async Task<ActionResult<ServerShareResponse>> UpdateAsync(
|
||||
[FromRoute] int serverId,
|
||||
[FromRoute] int id,
|
||||
[FromBody] UpdateShareRequest request
|
||||
)
|
||||
{
|
||||
var server = await GetServerById(serverId);
|
||||
var server = await GetServerByIdAsync(serverId);
|
||||
|
||||
if (server.Value == null)
|
||||
return server.Result ?? Problem("Unable to retrieve server");
|
||||
@@ -185,7 +182,7 @@ public class SharesController : Controller
|
||||
|
||||
share.UpdatedAt = DateTime.UtcNow;
|
||||
|
||||
await ShareRepository.Update(share);
|
||||
await ShareRepository.UpdateAsync(share);
|
||||
|
||||
var user = await UserRepository
|
||||
.Get()
|
||||
@@ -205,12 +202,12 @@ public class SharesController : Controller
|
||||
}
|
||||
|
||||
[HttpDelete("{id:int}")]
|
||||
public async Task<ActionResult> Delete(
|
||||
public async Task<ActionResult> DeleteAsync(
|
||||
[FromRoute] int serverId,
|
||||
[FromRoute] int id
|
||||
)
|
||||
{
|
||||
var server = await GetServerById(serverId);
|
||||
var server = await GetServerByIdAsync(serverId);
|
||||
|
||||
if (server.Value == null)
|
||||
return server.Result ?? Problem("Unable to retrieve server");
|
||||
@@ -222,11 +219,11 @@ public class SharesController : Controller
|
||||
if (share == null)
|
||||
return Problem("A share with that id cannot be found", statusCode: 404);
|
||||
|
||||
await ShareRepository.Remove(share);
|
||||
await ShareRepository.RemoveAsync(share);
|
||||
return NoContent();
|
||||
}
|
||||
|
||||
private async Task<ActionResult<Server>> GetServerById(int serverId)
|
||||
private async Task<ActionResult<Server>> GetServerByIdAsync(int serverId)
|
||||
{
|
||||
var server = await ServerRepository
|
||||
.Get()
|
||||
@@ -235,7 +232,7 @@ public class SharesController : Controller
|
||||
if (server == null)
|
||||
return Problem("No server with this id found", statusCode: 404);
|
||||
|
||||
var authorizeResult = await AuthorizeService.Authorize(
|
||||
var authorizeResult = await AuthorizeService.AuthorizeAsync(
|
||||
User, server,
|
||||
ServerPermissionConstants.Shares,
|
||||
ServerPermissionLevel.ReadWrite
|
||||
|
||||
@@ -1,18 +1,14 @@
|
||||
using System.ComponentModel.DataAnnotations;
|
||||
using Microsoft.AspNetCore.Authorization;
|
||||
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;
|
||||
using MoonlightServers.ApiServer.Services;
|
||||
using MoonlightServers.Shared.Constants;
|
||||
using MoonlightServers.Shared.Enums;
|
||||
using MoonlightServers.Shared.Http.Requests.Client.Servers.Variables;
|
||||
using MoonlightServers.Shared.Http.Responses.Client.Servers.Shares;
|
||||
using MoonlightServers.Shared.Http.Responses.Client.Servers.Variables;
|
||||
|
||||
namespace MoonlightServers.ApiServer.Http.Controllers.Client;
|
||||
@@ -41,12 +37,16 @@ public class VariablesController : Controller
|
||||
}
|
||||
|
||||
[HttpGet]
|
||||
public async Task<ActionResult<PagedData<ServerVariableDetailResponse>>> Get(
|
||||
public async Task<ActionResult<CountedData<ServerVariableDetailResponse>>> GetAsync(
|
||||
[FromRoute] int serverId,
|
||||
[FromQuery] PagedOptions options
|
||||
[FromQuery] int startIndex,
|
||||
[FromQuery] int count
|
||||
)
|
||||
{
|
||||
var server = await GetServerById(serverId, ServerPermissionLevel.Read);
|
||||
if (count > 100)
|
||||
return Problem("Only 100 items can be fetched at a time", statusCode: 400);
|
||||
|
||||
var server = await GetServerByIdAsync(serverId, ServerPermissionLevel.Read);
|
||||
|
||||
if (server.Value == null)
|
||||
return server.Result ?? Problem("Unable to retrieve server");
|
||||
@@ -55,12 +55,12 @@ public class VariablesController : Controller
|
||||
.Get()
|
||||
.Where(x => x.Star.Id == server.Value.Star.Id);
|
||||
|
||||
var count = await query.CountAsync();
|
||||
var totalCount = await query.CountAsync();
|
||||
|
||||
var starVariables = await query
|
||||
.OrderBy(x => x.Id)
|
||||
.Skip(options.Page * options.PageSize)
|
||||
.Take(options.PageSize)
|
||||
.Skip(startIndex)
|
||||
.Take(count)
|
||||
.ToArrayAsync();
|
||||
|
||||
var starVariableKeys = starVariables
|
||||
@@ -87,25 +87,22 @@ public class VariablesController : Controller
|
||||
};
|
||||
}).ToArray();
|
||||
|
||||
return new PagedData<ServerVariableDetailResponse>()
|
||||
return new CountedData<ServerVariableDetailResponse>()
|
||||
{
|
||||
Items = responses,
|
||||
CurrentPage = options.Page,
|
||||
PageSize = options.PageSize,
|
||||
TotalItems = count,
|
||||
TotalPages = (int)Math.Ceiling(Math.Max(0, count) / (double)options.PageSize)
|
||||
TotalCount = totalCount
|
||||
};
|
||||
}
|
||||
|
||||
[HttpPut]
|
||||
public async Task<ActionResult<ServerVariableDetailResponse>> UpdateSingle(
|
||||
public async Task<ActionResult<ServerVariableDetailResponse>> UpdateSingleAsync(
|
||||
[FromRoute] int serverId,
|
||||
[FromBody] UpdateServerVariableRequest request
|
||||
)
|
||||
{
|
||||
// TODO: Handle filter
|
||||
|
||||
var serverResult = await GetServerById(serverId, ServerPermissionLevel.ReadWrite);
|
||||
var serverResult = await GetServerByIdAsync(serverId, ServerPermissionLevel.ReadWrite);
|
||||
|
||||
if (serverResult.Value == null)
|
||||
return serverResult.Result ?? Problem("Unable to retrieve server");
|
||||
@@ -119,7 +116,7 @@ public class VariablesController : Controller
|
||||
throw new HttpApiException($"No variable with the key found: {request.Key}", 400);
|
||||
|
||||
serverVariable.Value = request.Value;
|
||||
await ServerRepository.Update(server);
|
||||
await ServerRepository.UpdateAsync(server);
|
||||
|
||||
return new ServerVariableDetailResponse()
|
||||
{
|
||||
@@ -133,12 +130,12 @@ public class VariablesController : Controller
|
||||
}
|
||||
|
||||
[HttpPatch]
|
||||
public async Task<ActionResult<ServerVariableDetailResponse[]>> Update(
|
||||
public async Task<ActionResult<ServerVariableDetailResponse[]>> UpdateAsync(
|
||||
[FromRoute] int serverId,
|
||||
[FromBody] UpdateServerVariableRangeRequest request
|
||||
)
|
||||
{
|
||||
var serverResult = await GetServerById(serverId, ServerPermissionLevel.ReadWrite);
|
||||
var serverResult = await GetServerByIdAsync(serverId, ServerPermissionLevel.ReadWrite);
|
||||
|
||||
if (serverResult.Value == null)
|
||||
return serverResult.Result ?? Problem("Unable to retrieve server");
|
||||
@@ -158,7 +155,7 @@ public class VariablesController : Controller
|
||||
serverVariable.Value = variable.Value;
|
||||
}
|
||||
|
||||
await ServerRepository.Update(server);
|
||||
await ServerRepository.UpdateAsync(server);
|
||||
|
||||
return request.Variables.Select(requestVariable =>
|
||||
{
|
||||
@@ -177,7 +174,7 @@ public class VariablesController : Controller
|
||||
}).ToArray();
|
||||
}
|
||||
|
||||
private async Task<ActionResult<Server>> GetServerById(int serverId, ServerPermissionLevel level)
|
||||
private async Task<ActionResult<Server>> GetServerByIdAsync(int serverId, ServerPermissionLevel level)
|
||||
{
|
||||
var server = await ServerRepository
|
||||
.Get()
|
||||
@@ -189,7 +186,7 @@ public class VariablesController : Controller
|
||||
if (server == null)
|
||||
return Problem("No server with this id found", statusCode: 404);
|
||||
|
||||
var authorizeResult = await AuthorizeService.Authorize(
|
||||
var authorizeResult = await AuthorizeService.AuthorizeAsync(
|
||||
User, server,
|
||||
ServerPermissionConstants.Variables,
|
||||
level
|
||||
|
||||
Reference in New Issue
Block a user