Compare commits

...

4 Commits

15 changed files with 286 additions and 0 deletions
@@ -0,0 +1,26 @@
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,23 @@
<Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup>
<TargetFramework>net10.0</TargetFramework>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.OpenApi" Version="10.0.9" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="10.0.9">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="Swashbuckle.AspNetCore" Version="10.2.3" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\MonitorPanel.Core\MonitorPanel.Core.csproj" />
<ProjectReference Include="..\MonitorPanel.DataAccess\MonitorPanel.DataAccess.csproj" />
</ItemGroup>
</Project>
@@ -0,0 +1,6 @@
@MonitorPanel.API_HostAddress = http://localhost:5068
GET {{MonitorPanel.API_HostAddress}}/weatherforecast/
Accept: application/json
###
@@ -0,0 +1,37 @@
using Microsoft.EntityFrameworkCore;
using MonitorPanel.Core.Abstractions;
using MonitorPanel.DataAccess;
using MonitorPanel.DataAccess.Repositories;
var builder = WebApplication.CreateBuilder(args);
// Add services to the container.
builder.Services.AddControllers();
// Learn more about configuring OpenAPI at https://aka.ms/aspnet/openapi
builder.Services.AddOpenApi();
builder.Services.AddSwaggerGen();
builder.Services.AddDbContext<MonitorPanelDbContext>(options =>
options.UseNpgsql(builder.Configuration.GetConnectionString("DefaultConnection")));
builder.Services.AddScoped<IServersRepository, ServersRepository>();
var app = builder.Build();
// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
app.MapOpenApi();
}
app.UseHttpsRedirection();
app.UseAuthorization();
app.MapControllers();
app.UseSwagger().UseSwaggerUI();
app.Run();
@@ -0,0 +1,23 @@
{
"$schema": "https://json.schemastore.org/launchsettings.json",
"profiles": {
"http": {
"commandName": "Project",
"dotnetRunMessages": true,
"launchBrowser": false,
"applicationUrl": "http://localhost:5068",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
},
"https": {
"commandName": "Project",
"dotnetRunMessages": true,
"launchBrowser": false,
"applicationUrl": "https://localhost:7204;http://localhost:5068",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
}
}
}
@@ -0,0 +1,9 @@
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
},
"AllowedHosts": "*"
}
@@ -0,0 +1,11 @@
using MonitorPanel.Core.Models;
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);
}
@@ -0,0 +1,17 @@
namespace MonitorPanel.Core.Models;
public class Server(
Guid id,
string name,
bool isHttps,
string address,
string? path,
int port)
{
public readonly Guid Id = id;
public string Name { get; set; } = name;
public bool IsHttps { get; set; } = isHttps;
public string Address { get; set; } = address;
public string? Path { get; set; } = path;
public int Port { get; set; } = port;
}
@@ -0,0 +1,12 @@
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,9 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net10.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
</Project>
@@ -0,0 +1,14 @@
using System.ComponentModel.DataAnnotations;
namespace MonitorPanel.DataAccess.Entities;
public class ServerEntity
{
public Guid Id { get; set; }
public string Name { get; set; } = null!;
public bool IsHttps { get; set; }
[Required]
public string Address { get; set; } = null!;
public string? Path { get; set; }
[Required]
public int Port { get; set; }
}
@@ -0,0 +1,26 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net10.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="10.0.9" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="10.0.9">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="10.0.9">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL" Version="10.0.2" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\MonitorPanel.Core\MonitorPanel.Core.csproj" />
</ItemGroup>
</Project>
@@ -0,0 +1,8 @@
using Microsoft.EntityFrameworkCore;
using MonitorPanel.DataAccess.Entities;
namespace MonitorPanel.DataAccess;
public class MonitorPanelDbContext(DbContextOptions<MonitorPanelDbContext> options) : DbContext(options)
{
public DbSet<ServerEntity> Servers => Set<ServerEntity>();
}
@@ -0,0 +1,60 @@
using Microsoft.EntityFrameworkCore;
using MonitorPanel.Core.Abstractions;
using MonitorPanel.Core.Models;
using MonitorPanel.DataAccess.Entities;
namespace MonitorPanel.DataAccess.Repositories;
public class ServersRepository(MonitorPanelDbContext db) : IServersRepository
{
public async Task<Guid> AddServerAsync(Server server)
{
var entity = new ServerEntity
{
Id = server.Id,
Name = server.Name,
IsHttps = server.IsHttps,
Address = server.Address,
Path = server.Path,
Port = server.Port
};
await db.Servers.AddAsync(entity);
await db.SaveChangesAsync();
return entity.Id;
}
public async Task<Guid> DeleteServerAsync(Guid id)
{
await db.Servers.Where(s => s.Id == id).ExecuteDeleteAsync();
return id;
}
public async Task<IEnumerable<Server>> GetAllServersAsync()
{
return await db.Servers.AsNoTracking()
.Select(s => new Server(s.Id, s.Name, s.IsHttps, s.Address, s.Path, s.Port))
.ToListAsync();
}
public async Task<Server?> GetServerByIdAsync(Guid id)
{
var entity = await db.Servers.FirstOrDefaultAsync(s => s.Id == id);
return entity == null ? null : new Server(entity.Id, entity.Name, entity.IsHttps, entity.Address, entity.Path, entity.Port);
}
public async Task<Guid> UpdateServerAsync(Guid id, string name, bool isHttps, string address, string? path, int port)
{
var entity = await db.Servers.FirstOrDefaultAsync(s => s.Id == id)
?? throw new InvalidOperationException("Server not found");
entity.Name = name;
entity.IsHttps = isHttps;
entity.Address = address;
entity.Path = path;
entity.Port = port;
await db.SaveChangesAsync();
return entity.Id;
}
}
+5
View File
@@ -0,0 +1,5 @@
<Solution>
<Project Path="MonitorPanel.API/MonitorPanel.API.csproj" />
<Project Path="MonitorPanel.Core/MonitorPanel.Core.csproj" />
<Project Path="MonitorPanel.DataAccess/MonitorPanel.DataAccess.csproj" />
</Solution>