mirror of
https://github.com/ryzij/ServerMonitor.git
synced 2026-09-18 19:23:22 +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();
|
||||
|
||||
|
||||
Reference in New Issue
Block a user