2024-10-01 17:56:06 -05:00
|
|
|
|
using AutoMapper;
|
|
|
|
|
|
using LANCommander.Server.Data.Models;
|
|
|
|
|
|
using LANCommander.Server.Extensions;
|
2025-07-27 17:51:50 -05:00
|
|
|
|
using LANCommander.Server.ImportExport;
|
2024-10-01 17:56:06 -05:00
|
|
|
|
using LANCommander.Server.Models;
|
|
|
|
|
|
using LANCommander.Server.Services;
|
|
|
|
|
|
using Microsoft.AspNetCore.Authorization;
|
|
|
|
|
|
using Microsoft.AspNetCore.Mvc;
|
|
|
|
|
|
using Microsoft.EntityFrameworkCore;
|
|
|
|
|
|
|
|
|
|
|
|
namespace LANCommander.Server.Controllers.Api
|
|
|
|
|
|
{
|
2024-10-16 02:06:02 -05:00
|
|
|
|
[Authorize(AuthenticationSchemes = "Bearer", Roles = RoleService.AdministratorRoleName)]
|
2024-10-01 17:56:06 -05:00
|
|
|
|
[Route("api/[controller]")]
|
|
|
|
|
|
[ApiController]
|
|
|
|
|
|
public class ServersController : BaseApiController
|
|
|
|
|
|
{
|
|
|
|
|
|
private readonly IMapper Mapper;
|
|
|
|
|
|
private readonly ServerService ServerService;
|
2025-07-19 17:39:55 -05:00
|
|
|
|
private readonly ArchiveService ArchiveService;
|
|
|
|
|
|
private readonly ImportContext ImportContext;
|
2024-10-01 17:56:06 -05:00
|
|
|
|
|
|
|
|
|
|
public ServersController(
|
|
|
|
|
|
ILogger<ServersController> logger,
|
|
|
|
|
|
IMapper mapper,
|
|
|
|
|
|
ServerService serverService,
|
2025-07-19 17:39:55 -05:00
|
|
|
|
ArchiveService archiveService,
|
|
|
|
|
|
ImportContext importContext) : base(logger)
|
2024-10-01 17:56:06 -05:00
|
|
|
|
{
|
|
|
|
|
|
Mapper = mapper;
|
|
|
|
|
|
ServerService = serverService;
|
2025-07-19 17:39:55 -05:00
|
|
|
|
ArchiveService = archiveService;
|
|
|
|
|
|
ImportContext = importContext;
|
2024-10-01 17:56:06 -05:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
[HttpGet]
|
2024-11-12 23:22:01 -06:00
|
|
|
|
public async Task<ActionResult<IEnumerable<SDK.Models.Server>>> GetAsync()
|
2024-10-01 17:56:06 -05:00
|
|
|
|
{
|
2024-11-12 18:32:46 -06:00
|
|
|
|
return Ok(Mapper.Map<IEnumerable<SDK.Models.Server>>(await ServerService.GetAsync()));
|
2024-10-01 17:56:06 -05:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
[HttpGet("{id}")]
|
|
|
|
|
|
public async Task<ActionResult<SDK.Models.Server>> Get(Guid id)
|
|
|
|
|
|
{
|
2024-11-12 18:32:46 -06:00
|
|
|
|
var server = await ServerService.GetAsync(id);
|
2024-10-01 17:56:06 -05:00
|
|
|
|
|
|
|
|
|
|
if (server == null)
|
|
|
|
|
|
return NotFound();
|
|
|
|
|
|
|
|
|
|
|
|
return Ok(Mapper.Map<SDK.Models.Server>(server));
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
[HttpPost("Import/{objectKey}")]
|
2024-11-12 23:22:01 -06:00
|
|
|
|
public async Task<IActionResult> ImportAsync(Guid objectKey)
|
2024-10-01 17:56:06 -05:00
|
|
|
|
{
|
|
|
|
|
|
try
|
|
|
|
|
|
{
|
2025-07-19 17:39:55 -05:00
|
|
|
|
var uploadedPath = await ArchiveService.GetArchiveFileLocationAsync(objectKey.ToString());
|
|
|
|
|
|
|
|
|
|
|
|
var result = await ImportContext.InitializeImportAsync(uploadedPath);
|
|
|
|
|
|
|
|
|
|
|
|
return Ok(result);
|
2024-10-01 17:56:06 -05:00
|
|
|
|
|
|
|
|
|
|
return Ok();
|
|
|
|
|
|
}
|
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
|
{
|
|
|
|
|
|
Logger?.LogError(ex, "Could not import server from upload");
|
|
|
|
|
|
return BadRequest(ex.Message);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|