2025-02-05 00:59:18 +03:00
|
|
|
using System.Runtime.InteropServices.JavaScript;
|
|
|
|
|
using System.Text.Json;
|
2025-02-10 15:17:02 +03:00
|
|
|
using AutoMapper;
|
|
|
|
|
using AutoMapper.QueryableExtensions;
|
2025-02-05 00:59:18 +03:00
|
|
|
using Microsoft.AspNetCore.Http.Extensions;
|
|
|
|
|
using Microsoft.AspNetCore.Mvc;
|
|
|
|
|
using Microsoft.EntityFrameworkCore;
|
|
|
|
|
using Microsoft.EntityFrameworkCore.Infrastructure;
|
|
|
|
|
using OnecMonitor.Server;
|
|
|
|
|
using OnecMonitor.Server.Models;
|
|
|
|
|
using OnecMonitor.Server.ViewModels;
|
|
|
|
|
using OnecMonitor.Server.ViewModels.Configurations;
|
|
|
|
|
|
|
|
|
|
namespace OnecMonitor.Server.Controllers;
|
|
|
|
|
|
2025-02-10 15:17:02 +03:00
|
|
|
public class ConfigurationsController(AppDbContext appDbContext, IMapper mapper, IWebHostEnvironment webHostEnvironment) : Controller
|
2025-02-05 00:59:18 +03:00
|
|
|
{
|
|
|
|
|
public async Task<IActionResult> Index()
|
|
|
|
|
{
|
|
|
|
|
return View(new ConfigurationsIndexViewModel
|
|
|
|
|
{
|
2025-02-10 15:17:02 +03:00
|
|
|
Items = await appDbContext.Configurations
|
|
|
|
|
.ProjectTo<ConfigurationListItemViewModel>(mapper.ConfigurationProvider)
|
|
|
|
|
.ToListAsync()
|
2025-02-05 00:59:18 +03:00
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public async Task<IActionResult> Edit(Guid id)
|
|
|
|
|
{
|
|
|
|
|
if (id == Guid.Empty)
|
2025-02-10 15:17:02 +03:00
|
|
|
return View(new ConfigurationEditViewModel());
|
2025-02-05 00:59:18 +03:00
|
|
|
|
2025-02-10 15:17:02 +03:00
|
|
|
var item = await appDbContext.Configurations.FindAsync(id);
|
2025-02-05 00:59:18 +03:00
|
|
|
|
|
|
|
|
if (item == null)
|
|
|
|
|
return NotFound();
|
|
|
|
|
|
2025-02-10 15:17:02 +03:00
|
|
|
return View(new ConfigurationEditViewModel
|
2025-02-05 00:59:18 +03:00
|
|
|
{
|
|
|
|
|
Id = item.Id,
|
|
|
|
|
Name = item.Name,
|
|
|
|
|
Version = item.Version,
|
|
|
|
|
IsExtension = item.IsExtension
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[HttpPost]
|
|
|
|
|
[RequestFormLimits(
|
|
|
|
|
MultipartBodyLengthLimit = int.MaxValue,
|
|
|
|
|
ValueLengthLimit = int.MaxValue)
|
|
|
|
|
]
|
2025-02-10 15:17:02 +03:00
|
|
|
public async Task<IActionResult> Save(Guid id, ConfigurationEditViewModel vm, CancellationToken cancellationToken)
|
2025-02-05 00:59:18 +03:00
|
|
|
{
|
|
|
|
|
var isNew = id == Guid.Empty;
|
|
|
|
|
|
|
|
|
|
if (!isNew)
|
2025-02-10 15:17:02 +03:00
|
|
|
ModelState.Remove(nameof(ConfigurationEditViewModel.File));
|
2025-02-05 00:59:18 +03:00
|
|
|
|
|
|
|
|
if (!ModelState.IsValid)
|
|
|
|
|
return View("Edit", vm);
|
|
|
|
|
|
|
|
|
|
var model = isNew ? new V8Configuration
|
|
|
|
|
{
|
|
|
|
|
Id = Guid.NewGuid()
|
2025-02-10 15:17:02 +03:00
|
|
|
} : await appDbContext.Configurations.FindAsync([id], cancellationToken);
|
2025-02-05 00:59:18 +03:00
|
|
|
|
|
|
|
|
if (model == null)
|
|
|
|
|
return NotFound();
|
|
|
|
|
|
|
|
|
|
if (isNew)
|
|
|
|
|
{
|
|
|
|
|
// save file
|
|
|
|
|
var fileName = $"{vm.Name}_{vm.Version}{Path.GetExtension(vm.File.FileName)}";
|
2025-02-10 15:17:02 +03:00
|
|
|
var path = Path.Combine(webHostEnvironment.ContentRootPath, "Data", fileName);
|
2025-02-05 00:59:18 +03:00
|
|
|
|
|
|
|
|
if (System.IO.File.Exists(path))
|
|
|
|
|
return View("Error", new ErrorViewModel { Message = $"File {path} already exists." });
|
|
|
|
|
|
|
|
|
|
await using var stream = new FileStream(path, FileMode.Create);
|
|
|
|
|
await vm.File.CopyToAsync(stream, cancellationToken);
|
|
|
|
|
|
|
|
|
|
model.DataPath = path;
|
|
|
|
|
|
2025-02-10 15:17:02 +03:00
|
|
|
await appDbContext.Configurations.AddAsync(model, cancellationToken);
|
2025-02-05 00:59:18 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
model.Name = vm.Name;
|
|
|
|
|
model.Version = vm.Version;
|
|
|
|
|
model.IsExtension = vm.IsExtension;
|
|
|
|
|
|
2025-02-10 15:17:02 +03:00
|
|
|
await appDbContext.SaveChangesAsync(cancellationToken);
|
2025-02-05 00:59:18 +03:00
|
|
|
|
|
|
|
|
return RedirectToAction("Index");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public async Task<IActionResult> Delete(Guid id, CancellationToken cancellationToken)
|
|
|
|
|
{
|
2025-02-10 15:17:02 +03:00
|
|
|
var item = await appDbContext.Configurations.FindAsync(
|
2025-02-05 00:59:18 +03:00
|
|
|
[id],
|
|
|
|
|
cancellationToken: cancellationToken);
|
|
|
|
|
|
|
|
|
|
if (System.IO.File.Exists(item!.DataPath))
|
|
|
|
|
System.IO.File.Delete(item.DataPath);
|
|
|
|
|
|
2025-02-10 15:17:02 +03:00
|
|
|
appDbContext.Configurations.Remove(item!);
|
|
|
|
|
await appDbContext.SaveChangesAsync(cancellationToken);
|
2025-02-05 00:59:18 +03:00
|
|
|
|
|
|
|
|
return RedirectToAction("Index");
|
|
|
|
|
}
|
|
|
|
|
}
|