using MoonCore.Attributes; using MoonCore.Common; using MoonCore.Helpers; using MoonlightServers.Shared.Http.Requests.Client.Servers; using MoonlightServers.Shared.Http.Requests.Client.Servers.Variables; using MoonlightServers.Shared.Http.Responses.Client.Servers; using MoonlightServers.Shared.Http.Responses.Client.Servers.Variables; namespace MoonlightServers.Frontend.Services; [Scoped] public class ServerService { private readonly HttpApiClient HttpApiClient; public ServerService(HttpApiClient httpApiClient) { HttpApiClient = httpApiClient; } public async Task> GetServersAsync(int startIndex, int count) { return await HttpApiClient.GetJson>( $"api/client/servers?startIndex={startIndex}&count={count}" ); } public async Task> GetSharedServersAsync(int startIndex, int count) { return await HttpApiClient.GetJson>( $"api/client/servers/shared?startIndex={startIndex}&count={count}" ); } public async Task GetServerAsync(int serverId) { return await HttpApiClient.GetJson( $"api/client/servers/{serverId}" ); } public async Task GetStatusAsync(int serverId) { return await HttpApiClient.GetJson( $"api/client/servers/{serverId}/status" ); } public async Task GetLogsAsync(int serverId) { return await HttpApiClient.GetJson( $"api/client/servers/{serverId}/logs" ); } public async Task GetStatsAsync(int serverId) { return await HttpApiClient.GetJson( $"api/client/servers/{serverId}/stats" ); } public async Task RunCommandAsync(int serverId, string command) { await HttpApiClient.Post( $"api/client/servers/{serverId}/command", new ServerCommandRequest() { Command = command } ); } public async Task GetWebSocketAsync(int serverId) { return await HttpApiClient.GetJson( $"api/client/servers/{serverId}/ws" ); } public async Task InstallAsync(int serverId) { await HttpApiClient.Post( $"api/client/servers/{serverId}/install" ); } #region Power actions public async Task StartAsync(int serverId) { await HttpApiClient.Post( $"api/client/servers/{serverId}/start" ); } public async Task StopAsync(int serverId) { await HttpApiClient.Post( $"api/client/servers/{serverId}/stop" ); } public async Task KillAsync(int serverId) { await HttpApiClient.Post( $"api/client/servers/{serverId}/kill" ); } #endregion #region Variables public async Task> GetVariablesAsync(int serverId, int startIndex, int count) { return await HttpApiClient.GetJson>( $"api/client/servers/{serverId}/variables?startIndex={startIndex}&count={count}" ); } public async Task UpdateVariablesAsync(int serverId, UpdateServerVariableRangeRequest request) { await HttpApiClient.Patch( $"api/client/servers/{serverId}/variables", request ); } public async Task UpdateVariableAsync(int serverId, UpdateServerVariableRequest request) { await HttpApiClient.Put( $"api/client/servers/{serverId}/variables", request ); } #endregion }