Compare commits

..

5 Commits

17 changed files with 324 additions and 67 deletions
@@ -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("all")]
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);
}
[HttpPatch("{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 });
}
}
@@ -1,26 +0,0 @@
using Microsoft.AspNetCore.Mvc;
using MonitorPanel.Core.Models;
namespace MonitorPanel.API.Controllers;
[ApiController]
[Route("[controller]")]
public class WeatherForecastController : ControllerBase
{
private static readonly string[] Summaries =
[
"Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot", "Sweltering", "Scorching"
];
[HttpGet(Name = "GetWeatherForecast")]
public IEnumerable<WeatherForecast> Get()
{
return Enumerable.Range(1, 5).Select(index => new WeatherForecast
{
Date = DateOnly.FromDateTime(DateTime.Now.AddDays(index)),
TemperatureC = Random.Shared.Next(-20, 55),
Summary = Summaries[Random.Shared.Next(Summaries.Length)]
})
.ToArray();
}
}
@@ -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> </PropertyGroup>
<ItemGroup> <ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.OpenApi" Version="10.0.9" /> <PackageReference Include="Microsoft.AspNetCore.OpenApi" Version="10.0.10" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="10.0.9"> <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> <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets> <PrivateAssets>all</PrivateAssets>
</PackageReference> </PackageReference>
@@ -18,6 +22,7 @@
<ItemGroup> <ItemGroup>
<ProjectReference Include="..\MonitorPanel.Core\MonitorPanel.Core.csproj" /> <ProjectReference Include="..\MonitorPanel.Core\MonitorPanel.Core.csproj" />
<ProjectReference Include="..\MonitorPanel.DataAccess\MonitorPanel.DataAccess.csproj" /> <ProjectReference Include="..\MonitorPanel.DataAccess\MonitorPanel.DataAccess.csproj" />
<ProjectReference Include="..\MonitorPanel.Application\MonitorPanel.Application.csproj" />
</ItemGroup> </ItemGroup>
</Project> </Project>
@@ -2,6 +2,7 @@ using Microsoft.EntityFrameworkCore;
using MonitorPanel.Core.Abstractions; using MonitorPanel.Core.Abstractions;
using MonitorPanel.DataAccess; using MonitorPanel.DataAccess;
using MonitorPanel.DataAccess.Repositories; using MonitorPanel.DataAccess.Repositories;
using MonitorPanel.Application.Services;
var builder = WebApplication.CreateBuilder(args); var builder = WebApplication.CreateBuilder(args);
@@ -16,7 +17,9 @@ builder.Services.AddSwaggerGen();
builder.Services.AddDbContext<MonitorPanelDbContext>(options => builder.Services.AddDbContext<MonitorPanelDbContext>(options =>
options.UseNpgsql(builder.Configuration.GetConnectionString("DefaultConnection"))); options.UseNpgsql(builder.Configuration.GetConnectionString("DefaultConnection")));
builder.Services.AddScoped<IServersRepository, ServersRepository>(); builder.Services
.AddScoped<IServersRepository, ServersRepository>()
.AddScoped<IServerService, ServerService>();
var app = builder.Build(); 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 public interface IServersRepository
{ {
Task<IEnumerable<Server>> GetAllServersAsync(); Task<IEnumerable<Server>> GetAllServersAsync(CancellationToken cancellationToken = default);
Task<Server?> GetServerByIdAsync(Guid id); Task<Server?> GetServerByIdAsync(Guid id, CancellationToken cancellationToken = default);
Task<Guid> AddServerAsync(Server server); Task<Guid> AddServerAsync(Server server, CancellationToken cancellationToken = default);
Task<Guid> UpdateServerAsync(Guid id, string name, bool isHttps, string address, string? path, int port); Task<Guid> UpdateServerAsync(Guid id, string name, bool isHttps, string address, string? path, int port, CancellationToken cancellationToken = default);
Task<Guid> DeleteServerAsync(Guid id); Task<Guid> DeleteServerAsync(Guid id, CancellationToken cancellationToken = default);
} }
@@ -3,10 +3,10 @@ namespace MonitorPanel.Core.Models;
public class Server( public class Server(
Guid id, Guid id,
string name, string name,
bool isHttps,
string address, string address,
string? path, string? path = null,
int port) bool isHttps = true,
int port = 443)
{ {
public readonly Guid Id = id; public readonly Guid Id = id;
public string Name { get; set; } = name; public string Name { get; set; } = name;
@@ -1,12 +0,0 @@
namespace MonitorPanel.Core.Models;
public class WeatherForecast
{
public DateOnly Date { get; set; }
public int TemperatureC { get; set; }
public int TemperatureF => 32 + (int)(TemperatureC / 0.5556);
public string? Summary { get; set; }
}
@@ -0,0 +1,58 @@
// <auto-generated />
using System;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.EntityFrameworkCore.Migrations;
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
using MonitorPanel.DataAccess;
using Npgsql.EntityFrameworkCore.PostgreSQL.Metadata;
#nullable disable
namespace MonitorPanel.DataAccess.Migrations
{
[DbContext(typeof(MonitorPanelDbContext))]
[Migration("20260809010527_InitialCreate")]
partial class InitialCreate
{
/// <inheritdoc />
protected override void BuildTargetModel(ModelBuilder modelBuilder)
{
#pragma warning disable 612, 618
modelBuilder
.HasAnnotation("ProductVersion", "10.0.10")
.HasAnnotation("Relational:MaxIdentifierLength", 63);
NpgsqlModelBuilderExtensions.UseIdentityByDefaultColumns(modelBuilder);
modelBuilder.Entity("MonitorPanel.DataAccess.Entities.ServerEntity", b =>
{
b.Property<Guid>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("uuid");
b.Property<string>("Address")
.IsRequired()
.HasColumnType("text");
b.Property<bool>("IsHttps")
.HasColumnType("boolean");
b.Property<string>("Name")
.IsRequired()
.HasColumnType("text");
b.Property<string>("Path")
.HasColumnType("text");
b.Property<int>("Port")
.HasColumnType("integer");
b.HasKey("Id");
b.ToTable("Servers");
});
#pragma warning restore 612, 618
}
}
}
@@ -0,0 +1,38 @@
using System;
using Microsoft.EntityFrameworkCore.Migrations;
#nullable disable
namespace MonitorPanel.DataAccess.Migrations
{
/// <inheritdoc />
public partial class InitialCreate : Migration
{
/// <inheritdoc />
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.CreateTable(
name: "Servers",
columns: table => new
{
Id = table.Column<Guid>(type: "uuid", nullable: false),
Name = table.Column<string>(type: "text", nullable: false),
IsHttps = table.Column<bool>(type: "boolean", nullable: false),
Address = table.Column<string>(type: "text", nullable: false),
Path = table.Column<string>(type: "text", nullable: true),
Port = table.Column<int>(type: "integer", nullable: false)
},
constraints: table =>
{
table.PrimaryKey("PK_Servers", x => x.Id);
});
}
/// <inheritdoc />
protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropTable(
name: "Servers");
}
}
}
@@ -0,0 +1,55 @@
// <auto-generated />
using System;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
using MonitorPanel.DataAccess;
using Npgsql.EntityFrameworkCore.PostgreSQL.Metadata;
#nullable disable
namespace MonitorPanel.DataAccess.Migrations
{
[DbContext(typeof(MonitorPanelDbContext))]
partial class MonitorPanelDbContextModelSnapshot : ModelSnapshot
{
protected override void BuildModel(ModelBuilder modelBuilder)
{
#pragma warning disable 612, 618
modelBuilder
.HasAnnotation("ProductVersion", "10.0.10")
.HasAnnotation("Relational:MaxIdentifierLength", 63);
NpgsqlModelBuilderExtensions.UseIdentityByDefaultColumns(modelBuilder);
modelBuilder.Entity("MonitorPanel.DataAccess.Entities.ServerEntity", b =>
{
b.Property<Guid>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("uuid");
b.Property<string>("Address")
.IsRequired()
.HasColumnType("text");
b.Property<bool>("IsHttps")
.HasColumnType("boolean");
b.Property<string>("Name")
.IsRequired()
.HasColumnType("text");
b.Property<string>("Path")
.HasColumnType("text");
b.Property<int>("Port")
.HasColumnType("integer");
b.HasKey("Id");
b.ToTable("Servers");
});
#pragma warning restore 612, 618
}
}
}
@@ -7,16 +7,16 @@
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="10.0.9" /> <PackageReference Include="Microsoft.EntityFrameworkCore" Version="10.0.10" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="10.0.9"> <PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="10.0.10">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets> <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets> <PrivateAssets>all</PrivateAssets>
</PackageReference> </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> <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets> <PrivateAssets>all</PrivateAssets>
</PackageReference> </PackageReference>
<PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL" Version="10.0.2" /> <PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL" Version="10.0.3" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
@@ -6,7 +6,7 @@ namespace MonitorPanel.DataAccess.Repositories;
public class ServersRepository(MonitorPanelDbContext db) : IServersRepository 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 var entity = new ServerEntity
{ {
@@ -18,34 +18,34 @@ public class ServersRepository(MonitorPanelDbContext db) : IServersRepository
Port = server.Port Port = server.Port
}; };
await db.Servers.AddAsync(entity); await db.Servers.AddAsync(entity, cancellationToken);
await db.SaveChangesAsync(); await db.SaveChangesAsync(cancellationToken);
return entity.Id; 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; return id;
} }
public async Task<IEnumerable<Server>> GetAllServersAsync() public async Task<IEnumerable<Server>> GetAllServersAsync(CancellationToken cancellationToken = default)
{ {
return await db.Servers.AsNoTracking() return await db.Servers.AsNoTracking()
.Select(s => new Server(s.Id, s.Name, s.IsHttps, s.Address, s.Path, s.Port)) .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.IsHttps, entity.Address, entity.Path, entity.Port); 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"); ?? throw new InvalidOperationException("Server not found");
entity.Name = name; entity.Name = name;
@@ -54,7 +54,7 @@ public class ServersRepository(MonitorPanelDbContext db) : IServersRepository
entity.Path = path; entity.Path = path;
entity.Port = port; entity.Port = port;
await db.SaveChangesAsync(); await db.SaveChangesAsync(cancellationToken);
return entity.Id; return entity.Id;
} }
} }
+1
View File
@@ -1,5 +1,6 @@
<Solution> <Solution>
<Project Path="MonitorPanel.API/MonitorPanel.API.csproj" /> <Project Path="MonitorPanel.API/MonitorPanel.API.csproj" />
<Project Path="MonitorPanel.Application/MonitorPanel.Application.csproj" />
<Project Path="MonitorPanel.Core/MonitorPanel.Core.csproj" /> <Project Path="MonitorPanel.Core/MonitorPanel.Core.csproj" />
<Project Path="MonitorPanel.DataAccess/MonitorPanel.DataAccess.csproj" /> <Project Path="MonitorPanel.DataAccess/MonitorPanel.DataAccess.csproj" />
</Solution> </Solution>