mirror of
https://github.com/ryzij/ServerMonitor.git
synced 2026-09-18 11:13:21 +00:00
Добавил сервис и контроллер для управления серверами
This commit is contained in:
@@ -0,0 +1,67 @@
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using MonitorPanel.Core.Abstractions;
|
||||
using MonitorPanel.Core.Models;
|
||||
using MonitorPanel.API.Dto;
|
||||
|
||||
namespace MonitorPanel.API.Controllers;
|
||||
|
||||
[ApiController]
|
||||
[Route("[controller]")]
|
||||
public class ServerController(IServerService serverService) : ControllerBase
|
||||
{
|
||||
[HttpGet]
|
||||
public async Task<ActionResult<List<ServerDto>>> GetAllServers(CancellationToken cancellationToken = default)
|
||||
{
|
||||
var servers = await serverService.GetAllServersAsync(cancellationToken);
|
||||
return Ok(servers.Select(s => new ServerDto
|
||||
{
|
||||
Id = s.Id,
|
||||
Name = s.Name,
|
||||
Address = s.Address,
|
||||
Path = s.Path,
|
||||
IsHttps = s.IsHttps,
|
||||
Port = s.Port
|
||||
}));
|
||||
}
|
||||
|
||||
[HttpGet("{id}")]
|
||||
public async Task<ActionResult<ServerDto>> GetServerById(Guid id, CancellationToken cancellationToken = default)
|
||||
{
|
||||
var server = await serverService.GetServerByIdAsync(id, cancellationToken);
|
||||
if (server == null)
|
||||
{
|
||||
return NotFound();
|
||||
}
|
||||
return Ok(new ServerDto
|
||||
{
|
||||
Id = server.Id,
|
||||
Name = server.Name,
|
||||
Address = server.Address,
|
||||
Path = server.Path,
|
||||
IsHttps = server.IsHttps,
|
||||
Port = server.Port
|
||||
});
|
||||
}
|
||||
|
||||
[HttpPost]
|
||||
public async Task<IActionResult> AddServer(ServerDto dto, CancellationToken cancellationToken = default)
|
||||
{
|
||||
var server = new Server(Guid.NewGuid(), dto.Name, dto.Address, dto.Path, dto.IsHttps, dto.Port);
|
||||
var id = await serverService.AddServerAsync(server, cancellationToken);
|
||||
return CreatedAtAction(nameof(GetServerById), new { id }, server);
|
||||
}
|
||||
|
||||
[HttpPut("{id}")]
|
||||
public async Task<IActionResult> UpdateServer(Guid id, ServerDto dto, CancellationToken cancellationToken = default)
|
||||
{
|
||||
var updatedId = await serverService.UpdateServerAsync(id, dto.Name, dto.IsHttps, dto.Address, dto.Path, dto.Port, cancellationToken);
|
||||
return Ok(new { Id = updatedId });
|
||||
}
|
||||
|
||||
[HttpDelete("{id}")]
|
||||
public async Task<IActionResult> DeleteServer(Guid id)
|
||||
{
|
||||
var deletedId = await serverService.DeleteServerAsync(id);
|
||||
return Ok(new { Id = deletedId });
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
namespace MonitorPanel.API.Dto;
|
||||
|
||||
public class ServerDto
|
||||
{
|
||||
public Guid Id { get; set; }
|
||||
public string Name { get; set; } = string.Empty;
|
||||
public bool IsHttps { get; set; }
|
||||
public string Address { get; set; } = string.Empty;
|
||||
public string? Path { get; set; }
|
||||
public int Port { get; set; }
|
||||
}
|
||||
@@ -7,8 +7,12 @@
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Microsoft.AspNetCore.OpenApi" Version="10.0.9" />
|
||||
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="10.0.9">
|
||||
<PackageReference Include="Microsoft.AspNetCore.OpenApi" Version="10.0.10" />
|
||||
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="10.0.10">
|
||||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
|
||||
<PrivateAssets>all</PrivateAssets>
|
||||
</PackageReference>
|
||||
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="10.0.10">
|
||||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
|
||||
<PrivateAssets>all</PrivateAssets>
|
||||
</PackageReference>
|
||||
@@ -18,6 +22,7 @@
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\MonitorPanel.Core\MonitorPanel.Core.csproj" />
|
||||
<ProjectReference Include="..\MonitorPanel.DataAccess\MonitorPanel.DataAccess.csproj" />
|
||||
<ProjectReference Include="..\MonitorPanel.Application\MonitorPanel.Application.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
|
||||
@@ -2,6 +2,7 @@ using Microsoft.EntityFrameworkCore;
|
||||
using MonitorPanel.Core.Abstractions;
|
||||
using MonitorPanel.DataAccess;
|
||||
using MonitorPanel.DataAccess.Repositories;
|
||||
using MonitorPanel.Application.Services;
|
||||
|
||||
var builder = WebApplication.CreateBuilder(args);
|
||||
|
||||
@@ -16,7 +17,9 @@ builder.Services.AddSwaggerGen();
|
||||
builder.Services.AddDbContext<MonitorPanelDbContext>(options =>
|
||||
options.UseNpgsql(builder.Configuration.GetConnectionString("DefaultConnection")));
|
||||
|
||||
builder.Services.AddScoped<IServersRepository, ServersRepository>();
|
||||
builder.Services
|
||||
.AddScoped<IServersRepository, ServersRepository>()
|
||||
.AddScoped<IServerService, ServerService>();
|
||||
|
||||
var app = builder.Build();
|
||||
|
||||
|
||||
@@ -0,0 +1,13 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\MonitorPanel.Core\MonitorPanel.Core.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>net10.0</TargetFramework>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
</PropertyGroup>
|
||||
|
||||
</Project>
|
||||
@@ -0,0 +1,32 @@
|
||||
using MonitorPanel.Core.Abstractions;
|
||||
using MonitorPanel.Core.Models;
|
||||
|
||||
namespace MonitorPanel.Application.Services;
|
||||
|
||||
public class ServerService(IServersRepository serversRepository) : IServerService
|
||||
{
|
||||
public Task<Guid> AddServerAsync(Server server, CancellationToken cancellationToken = default)
|
||||
{
|
||||
return serversRepository.AddServerAsync(server, cancellationToken);
|
||||
}
|
||||
|
||||
public Task<Guid> DeleteServerAsync(Guid id, CancellationToken cancellationToken = default)
|
||||
{
|
||||
return serversRepository.DeleteServerAsync(id, cancellationToken);
|
||||
}
|
||||
|
||||
public Task<IEnumerable<Server>> GetAllServersAsync(CancellationToken cancellationToken = default)
|
||||
{
|
||||
return serversRepository.GetAllServersAsync(cancellationToken);
|
||||
}
|
||||
|
||||
public Task<Server?> GetServerByIdAsync(Guid id, CancellationToken cancellationToken = default)
|
||||
{
|
||||
return serversRepository.GetServerByIdAsync(id, cancellationToken);
|
||||
}
|
||||
|
||||
public Task<Guid> UpdateServerAsync(Guid id, string name, bool isHttps, string address, string? path, int port, CancellationToken cancellationToken = default)
|
||||
{
|
||||
return serversRepository.UpdateServerAsync(id, name, isHttps, address, path, port, cancellationToken);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
using MonitorPanel.Core.Models;
|
||||
|
||||
namespace MonitorPanel.Core.Abstractions;
|
||||
|
||||
public interface IServerService
|
||||
{
|
||||
Task<Guid> AddServerAsync(Server server, CancellationToken cancellationToken = default);
|
||||
Task<Guid> DeleteServerAsync(Guid id, CancellationToken cancellationToken = default);
|
||||
Task<IEnumerable<Server>> GetAllServersAsync(CancellationToken cancellationToken = default);
|
||||
Task<Server?> GetServerByIdAsync(Guid id, CancellationToken cancellationToken = default);
|
||||
Task<Guid> UpdateServerAsync(Guid id, string name, bool isHttps, string address, string? path, int port, CancellationToken cancellationToken = default);
|
||||
}
|
||||
@@ -3,9 +3,9 @@ namespace MonitorPanel.Core.Abstractions;
|
||||
|
||||
public interface IServersRepository
|
||||
{
|
||||
Task<IEnumerable<Server>> GetAllServersAsync();
|
||||
Task<Server?> GetServerByIdAsync(Guid id);
|
||||
Task<Guid> AddServerAsync(Server server);
|
||||
Task<Guid> UpdateServerAsync(Guid id, string name, bool isHttps, string address, string? path, int port);
|
||||
Task<Guid> DeleteServerAsync(Guid id);
|
||||
Task<IEnumerable<Server>> GetAllServersAsync(CancellationToken cancellationToken = default);
|
||||
Task<Server?> GetServerByIdAsync(Guid id, CancellationToken cancellationToken = default);
|
||||
Task<Guid> AddServerAsync(Server server, CancellationToken cancellationToken = default);
|
||||
Task<Guid> UpdateServerAsync(Guid id, string name, bool isHttps, string address, string? path, int port, CancellationToken cancellationToken = default);
|
||||
Task<Guid> DeleteServerAsync(Guid id, CancellationToken cancellationToken = default);
|
||||
}
|
||||
@@ -7,16 +7,16 @@
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="10.0.9" />
|
||||
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="10.0.9">
|
||||
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="10.0.10" />
|
||||
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="10.0.10">
|
||||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
|
||||
<PrivateAssets>all</PrivateAssets>
|
||||
</PackageReference>
|
||||
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="10.0.9">
|
||||
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="10.0.10">
|
||||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
|
||||
<PrivateAssets>all</PrivateAssets>
|
||||
</PackageReference>
|
||||
<PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL" Version="10.0.2" />
|
||||
<PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL" Version="10.0.3" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
|
||||
@@ -6,7 +6,7 @@ namespace MonitorPanel.DataAccess.Repositories;
|
||||
|
||||
public class ServersRepository(MonitorPanelDbContext db) : IServersRepository
|
||||
{
|
||||
public async Task<Guid> AddServerAsync(Server server)
|
||||
public async Task<Guid> AddServerAsync(Server server, CancellationToken cancellationToken = default)
|
||||
{
|
||||
var entity = new ServerEntity
|
||||
{
|
||||
@@ -18,34 +18,34 @@ public class ServersRepository(MonitorPanelDbContext db) : IServersRepository
|
||||
Port = server.Port
|
||||
};
|
||||
|
||||
await db.Servers.AddAsync(entity);
|
||||
await db.SaveChangesAsync();
|
||||
await db.Servers.AddAsync(entity, cancellationToken);
|
||||
await db.SaveChangesAsync(cancellationToken);
|
||||
|
||||
return entity.Id;
|
||||
}
|
||||
|
||||
public async Task<Guid> DeleteServerAsync(Guid id)
|
||||
public async Task<Guid> DeleteServerAsync(Guid id, CancellationToken cancellationToken = default)
|
||||
{
|
||||
await db.Servers.Where(s => s.Id == id).ExecuteDeleteAsync();
|
||||
await db.Servers.Where(s => s.Id == id).ExecuteDeleteAsync(cancellationToken);
|
||||
return id;
|
||||
}
|
||||
|
||||
public async Task<IEnumerable<Server>> GetAllServersAsync()
|
||||
public async Task<IEnumerable<Server>> GetAllServersAsync(CancellationToken cancellationToken = default)
|
||||
{
|
||||
return await db.Servers.AsNoTracking()
|
||||
.Select(s => new Server(s.Id, s.Name, s.Address, s.Path, s.IsHttps, s.Port))
|
||||
.ToListAsync();
|
||||
.ToListAsync(cancellationToken);
|
||||
}
|
||||
|
||||
public async Task<Server?> GetServerByIdAsync(Guid id)
|
||||
public async Task<Server?> GetServerByIdAsync(Guid id, CancellationToken cancellationToken = default)
|
||||
{
|
||||
var entity = await db.Servers.FirstOrDefaultAsync(s => s.Id == id);
|
||||
var entity = await db.Servers.FirstOrDefaultAsync(s => s.Id == id, cancellationToken);
|
||||
return entity == null ? null : new Server(entity.Id, entity.Name, entity.Address, entity.Path, entity.IsHttps, entity.Port);
|
||||
}
|
||||
|
||||
public async Task<Guid> UpdateServerAsync(Guid id, string name, bool isHttps, string address, string? path, int port)
|
||||
public async Task<Guid> UpdateServerAsync(Guid id, string name, bool isHttps, string address, string? path, int port, CancellationToken cancellationToken = default)
|
||||
{
|
||||
var entity = await db.Servers.FirstOrDefaultAsync(s => s.Id == id)
|
||||
var entity = await db.Servers.FirstOrDefaultAsync(s => s.Id == id, cancellationToken)
|
||||
?? throw new InvalidOperationException("Server not found");
|
||||
|
||||
entity.Name = name;
|
||||
@@ -54,7 +54,7 @@ public class ServersRepository(MonitorPanelDbContext db) : IServersRepository
|
||||
entity.Path = path;
|
||||
entity.Port = port;
|
||||
|
||||
await db.SaveChangesAsync();
|
||||
await db.SaveChangesAsync(cancellationToken);
|
||||
return entity.Id;
|
||||
}
|
||||
}
|
||||
@@ -1,5 +1,6 @@
|
||||
<Solution>
|
||||
<Project Path="MonitorPanel.API/MonitorPanel.API.csproj" />
|
||||
<Project Path="MonitorPanel.Application/MonitorPanel.Application.csproj" />
|
||||
<Project Path="MonitorPanel.Core/MonitorPanel.Core.csproj" />
|
||||
<Project Path="MonitorPanel.DataAccess/MonitorPanel.DataAccess.csproj" />
|
||||
</Solution>
|
||||
|
||||
Reference in New Issue
Block a user