diff --git a/Backend/MonitorPanel/MonitorPanel.API/Controllers/ServerController.cs b/Backend/MonitorPanel/MonitorPanel.API/Controllers/ServerController.cs new file mode 100644 index 0000000..027c07f --- /dev/null +++ b/Backend/MonitorPanel/MonitorPanel.API/Controllers/ServerController.cs @@ -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>> 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> 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 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 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 DeleteServer(Guid id) + { + var deletedId = await serverService.DeleteServerAsync(id); + return Ok(new { Id = deletedId }); + } +} \ No newline at end of file diff --git a/Backend/MonitorPanel/MonitorPanel.API/Dto/ServerDto.cs b/Backend/MonitorPanel/MonitorPanel.API/Dto/ServerDto.cs new file mode 100644 index 0000000..3039cb1 --- /dev/null +++ b/Backend/MonitorPanel/MonitorPanel.API/Dto/ServerDto.cs @@ -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; } +} \ No newline at end of file diff --git a/Backend/MonitorPanel/MonitorPanel.API/MonitorPanel.API.csproj b/Backend/MonitorPanel/MonitorPanel.API/MonitorPanel.API.csproj index b3d87aa..65626d4 100644 --- a/Backend/MonitorPanel/MonitorPanel.API/MonitorPanel.API.csproj +++ b/Backend/MonitorPanel/MonitorPanel.API/MonitorPanel.API.csproj @@ -7,8 +7,12 @@ - - + + + runtime; build; native; contentfiles; analyzers; buildtransitive + all + + runtime; build; native; contentfiles; analyzers; buildtransitive all @@ -18,6 +22,7 @@ + diff --git a/Backend/MonitorPanel/MonitorPanel.API/Program.cs b/Backend/MonitorPanel/MonitorPanel.API/Program.cs index 26f147d..5277d1b 100644 --- a/Backend/MonitorPanel/MonitorPanel.API/Program.cs +++ b/Backend/MonitorPanel/MonitorPanel.API/Program.cs @@ -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(options => options.UseNpgsql(builder.Configuration.GetConnectionString("DefaultConnection"))); -builder.Services.AddScoped(); +builder.Services + .AddScoped() + .AddScoped(); var app = builder.Build(); diff --git a/Backend/MonitorPanel/MonitorPanel.Application/MonitorPanel.Application.csproj b/Backend/MonitorPanel/MonitorPanel.Application/MonitorPanel.Application.csproj new file mode 100644 index 0000000..574a6ca --- /dev/null +++ b/Backend/MonitorPanel/MonitorPanel.Application/MonitorPanel.Application.csproj @@ -0,0 +1,13 @@ + + + + + + + + net10.0 + enable + enable + + + diff --git a/Backend/MonitorPanel/MonitorPanel.Application/Services/ServerService.cs b/Backend/MonitorPanel/MonitorPanel.Application/Services/ServerService.cs new file mode 100644 index 0000000..104251b --- /dev/null +++ b/Backend/MonitorPanel/MonitorPanel.Application/Services/ServerService.cs @@ -0,0 +1,32 @@ +using MonitorPanel.Core.Abstractions; +using MonitorPanel.Core.Models; + +namespace MonitorPanel.Application.Services; + +public class ServerService(IServersRepository serversRepository) : IServerService +{ + public Task AddServerAsync(Server server, CancellationToken cancellationToken = default) + { + return serversRepository.AddServerAsync(server, cancellationToken); + } + + public Task DeleteServerAsync(Guid id, CancellationToken cancellationToken = default) + { + return serversRepository.DeleteServerAsync(id, cancellationToken); + } + + public Task> GetAllServersAsync(CancellationToken cancellationToken = default) + { + return serversRepository.GetAllServersAsync(cancellationToken); + } + + public Task GetServerByIdAsync(Guid id, CancellationToken cancellationToken = default) + { + return serversRepository.GetServerByIdAsync(id, cancellationToken); + } + + public Task 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); + } +} \ No newline at end of file diff --git a/Backend/MonitorPanel/MonitorPanel.Core/Abstractions/IServerService.cs b/Backend/MonitorPanel/MonitorPanel.Core/Abstractions/IServerService.cs new file mode 100644 index 0000000..4cf5221 --- /dev/null +++ b/Backend/MonitorPanel/MonitorPanel.Core/Abstractions/IServerService.cs @@ -0,0 +1,12 @@ +using MonitorPanel.Core.Models; + +namespace MonitorPanel.Core.Abstractions; + +public interface IServerService +{ + Task AddServerAsync(Server server, CancellationToken cancellationToken = default); + Task DeleteServerAsync(Guid id, CancellationToken cancellationToken = default); + Task> GetAllServersAsync(CancellationToken cancellationToken = default); + Task GetServerByIdAsync(Guid id, CancellationToken cancellationToken = default); + Task UpdateServerAsync(Guid id, string name, bool isHttps, string address, string? path, int port, CancellationToken cancellationToken = default); +} \ No newline at end of file diff --git a/Backend/MonitorPanel/MonitorPanel.Core/Abstractions/IServersRepository.cs b/Backend/MonitorPanel/MonitorPanel.Core/Abstractions/IServersRepository.cs index 3ea09e0..5019101 100644 --- a/Backend/MonitorPanel/MonitorPanel.Core/Abstractions/IServersRepository.cs +++ b/Backend/MonitorPanel/MonitorPanel.Core/Abstractions/IServersRepository.cs @@ -3,9 +3,9 @@ namespace MonitorPanel.Core.Abstractions; public interface IServersRepository { - Task> GetAllServersAsync(); - Task GetServerByIdAsync(Guid id); - Task AddServerAsync(Server server); - Task UpdateServerAsync(Guid id, string name, bool isHttps, string address, string? path, int port); - Task DeleteServerAsync(Guid id); + Task> GetAllServersAsync(CancellationToken cancellationToken = default); + Task GetServerByIdAsync(Guid id, CancellationToken cancellationToken = default); + Task AddServerAsync(Server server, CancellationToken cancellationToken = default); + Task UpdateServerAsync(Guid id, string name, bool isHttps, string address, string? path, int port, CancellationToken cancellationToken = default); + Task DeleteServerAsync(Guid id, CancellationToken cancellationToken = default); } \ No newline at end of file diff --git a/Backend/MonitorPanel/MonitorPanel.DataAccess/MonitorPanel.DataAccess.csproj b/Backend/MonitorPanel/MonitorPanel.DataAccess/MonitorPanel.DataAccess.csproj index ca21264..3b1d58a 100644 --- a/Backend/MonitorPanel/MonitorPanel.DataAccess/MonitorPanel.DataAccess.csproj +++ b/Backend/MonitorPanel/MonitorPanel.DataAccess/MonitorPanel.DataAccess.csproj @@ -7,16 +7,16 @@ - - + + runtime; build; native; contentfiles; analyzers; buildtransitive all - + runtime; build; native; contentfiles; analyzers; buildtransitive all - + diff --git a/Backend/MonitorPanel/MonitorPanel.DataAccess/Repositories/ServersRepository.cs b/Backend/MonitorPanel/MonitorPanel.DataAccess/Repositories/ServersRepository.cs index 7cf9ec2..f75a097 100644 --- a/Backend/MonitorPanel/MonitorPanel.DataAccess/Repositories/ServersRepository.cs +++ b/Backend/MonitorPanel/MonitorPanel.DataAccess/Repositories/ServersRepository.cs @@ -6,7 +6,7 @@ namespace MonitorPanel.DataAccess.Repositories; public class ServersRepository(MonitorPanelDbContext db) : IServersRepository { - public async Task AddServerAsync(Server server) + public async Task 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 DeleteServerAsync(Guid id) + public async Task 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> GetAllServersAsync() + public async Task> 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 GetServerByIdAsync(Guid id) + public async Task 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 UpdateServerAsync(Guid id, string name, bool isHttps, string address, string? path, int port) + public async Task 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; } } \ No newline at end of file diff --git a/Backend/MonitorPanel/MonitorPanel.slnx b/Backend/MonitorPanel/MonitorPanel.slnx index eacb59b..aa05c62 100644 --- a/Backend/MonitorPanel/MonitorPanel.slnx +++ b/Backend/MonitorPanel/MonitorPanel.slnx @@ -1,5 +1,6 @@ +