Server und Container Overview+Add Seite erstellt und an Datenbank angeschlossen
This commit is contained in:
56
Watcher/Controllers/ServerController.cs
Normal file
56
Watcher/Controllers/ServerController.cs
Normal file
@@ -0,0 +1,56 @@
|
||||
using Microsoft.AspNetCore.Authorization;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Watcher.Data;
|
||||
using Watcher.Models;
|
||||
using Watcher.ViewModels;
|
||||
|
||||
[Authorize]
|
||||
public class ServerController : Controller
|
||||
{
|
||||
private readonly AppDbContext _context;
|
||||
|
||||
public ServerController(AppDbContext context)
|
||||
{
|
||||
_context = context;
|
||||
}
|
||||
|
||||
public async Task<IActionResult> Overview()
|
||||
{
|
||||
var vm = new ServerOverviewViewModel
|
||||
{
|
||||
Servers = await _context.Servers.OrderBy(s => s.Id).ToListAsync()
|
||||
};
|
||||
|
||||
return View(vm);
|
||||
}
|
||||
|
||||
[HttpGet]
|
||||
public IActionResult AddServer()
|
||||
{
|
||||
return View();
|
||||
}
|
||||
|
||||
[HttpPost]
|
||||
public async Task<IActionResult> AddServer(AddServerViewModel vm)
|
||||
{
|
||||
if (!ModelState.IsValid)
|
||||
return View(vm);
|
||||
|
||||
var server = new Server
|
||||
{
|
||||
Name = vm.Name,
|
||||
IPAddress = vm.IPAddress,
|
||||
Hostname = vm.Hostname,
|
||||
Status = vm.Status,
|
||||
Type = vm.Type,
|
||||
IsOnline = vm.IsOnline,
|
||||
CreatedAt = DateTime.UtcNow
|
||||
};
|
||||
|
||||
_context.Servers.Add(server);
|
||||
await _context.SaveChangesAsync();
|
||||
|
||||
return RedirectToAction("Overview");
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user