57 lines
1.2 KiB
C#
57 lines
1.2 KiB
C#
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");
|
|
}
|
|
}
|