2025-04-10 19:45:41 -05:00
|
|
|
using LANCommander.SDK.Enums;
|
2025-03-30 21:05:48 -05:00
|
|
|
using LANCommander.SDK.Models.Manifest;
|
2025-07-27 17:51:50 -05:00
|
|
|
using LANCommander.Server.ImportExport.Models;
|
|
|
|
|
using LANCommander.Server.Services;
|
2025-12-03 02:22:37 -06:00
|
|
|
using Microsoft.Extensions.Logging;
|
2025-03-30 21:05:48 -05:00
|
|
|
|
2025-07-27 17:51:50 -05:00
|
|
|
namespace LANCommander.Server.ImportExport.Importers;
|
2025-03-30 21:05:48 -05:00
|
|
|
|
2025-04-10 19:45:41 -05:00
|
|
|
public class ServerHttpPathImporter(
|
2025-12-03 02:22:37 -06:00
|
|
|
ILogger<ServerHttpPathImporter> logger,
|
2025-04-09 19:44:07 -05:00
|
|
|
ServerHttpPathService serverHttpPathService,
|
2025-12-03 02:22:37 -06:00
|
|
|
ServerService serverService,
|
|
|
|
|
ServerImporter serverImporter) : BaseImporter<ServerHttpPath>
|
2025-03-30 21:05:48 -05:00
|
|
|
{
|
2025-12-03 02:22:37 -06:00
|
|
|
public override string GetKey(ServerHttpPath record)
|
|
|
|
|
=> $"{nameof(ServerHttpPath)}/{record.LocalPath}";
|
|
|
|
|
|
|
|
|
|
public override async Task<ImportItemInfo<ServerHttpPath>> GetImportInfoAsync(ServerHttpPath record)
|
|
|
|
|
=> new()
|
2025-04-12 02:37:14 -05:00
|
|
|
{
|
2025-08-01 02:27:06 -05:00
|
|
|
Type = ImportExportRecordType.ServerHttpPath,
|
2025-04-12 02:37:14 -05:00
|
|
|
Name = record.Path,
|
2025-12-03 02:22:37 -06:00
|
|
|
Record = record,
|
2025-04-12 02:37:14 -05:00
|
|
|
};
|
|
|
|
|
|
2025-12-03 02:22:37 -06:00
|
|
|
public override async Task<bool> CanImportAsync(ServerHttpPath record) => ImportContext.Manifest is SDK.Models.Manifest.Server;
|
2025-03-30 21:05:48 -05:00
|
|
|
|
2025-12-03 02:22:37 -06:00
|
|
|
public override async Task<bool> AddAsync(ServerHttpPath record)
|
2025-04-09 19:44:07 -05:00
|
|
|
{
|
2025-03-30 21:05:48 -05:00
|
|
|
try
|
|
|
|
|
{
|
2025-12-03 02:22:37 -06:00
|
|
|
var server = ImportContext.Manifest as SDK.Models.Manifest.Server;
|
|
|
|
|
|
|
|
|
|
if (server == null)
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
if (ImportContext.InQueue(server, serverImporter))
|
|
|
|
|
return false;
|
|
|
|
|
|
2025-03-30 21:05:48 -05:00
|
|
|
var serverHttpPath = new Data.Models.ServerHttpPath
|
|
|
|
|
{
|
|
|
|
|
LocalPath = record.LocalPath,
|
|
|
|
|
Path = record.Path,
|
2025-12-03 02:22:37 -06:00
|
|
|
CreatedOn = record.CreatedOn,
|
|
|
|
|
UpdatedOn = record.UpdatedOn,
|
|
|
|
|
Server = await serverService.GetAsync(server.Id),
|
2025-03-30 21:05:48 -05:00
|
|
|
};
|
|
|
|
|
|
2025-12-03 02:22:37 -06:00
|
|
|
await serverHttpPathService.AddAsync(serverHttpPath);
|
2025-03-30 21:05:48 -05:00
|
|
|
|
2025-12-03 02:22:37 -06:00
|
|
|
return true;
|
2025-03-30 21:05:48 -05:00
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
2025-12-03 02:22:37 -06:00
|
|
|
logger.LogError(ex, "Could not add server HTTP path | {Key}", GetKey(record));
|
|
|
|
|
return false;
|
2025-03-30 21:05:48 -05:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-12-03 02:22:37 -06:00
|
|
|
public override async Task<bool> UpdateAsync(ServerHttpPath record)
|
2025-03-30 21:05:48 -05:00
|
|
|
{
|
2025-04-09 19:44:07 -05:00
|
|
|
var existing = await serverHttpPathService.FirstOrDefaultAsync(p => p.Path == record.Path);
|
2025-03-30 21:05:48 -05:00
|
|
|
|
|
|
|
|
try
|
|
|
|
|
{
|
2025-12-03 02:22:37 -06:00
|
|
|
var server = ImportContext.Manifest as SDK.Models.Manifest.Server;
|
|
|
|
|
|
|
|
|
|
if (server == null)
|
|
|
|
|
return false;
|
|
|
|
|
|
2025-03-30 21:05:48 -05:00
|
|
|
existing.LocalPath = record.LocalPath;
|
|
|
|
|
existing.Path = record.Path;
|
2025-12-03 02:22:37 -06:00
|
|
|
existing.Server = await serverService.GetAsync(server.Id);
|
2025-03-30 21:05:48 -05:00
|
|
|
|
2025-12-03 02:22:37 -06:00
|
|
|
await serverHttpPathService.UpdateAsync(existing);
|
2025-03-30 21:05:48 -05:00
|
|
|
|
2025-12-03 02:22:37 -06:00
|
|
|
return true;
|
2025-03-30 21:05:48 -05:00
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
2025-12-03 02:22:37 -06:00
|
|
|
logger.LogError(ex, "Could not update server HTTP path | {Key}", GetKey(record));
|
|
|
|
|
return false;
|
2025-03-30 21:05:48 -05:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-12-04 23:44:42 -06:00
|
|
|
public override async Task<bool> IngestAsync(IImportAsset asset)
|
|
|
|
|
{
|
|
|
|
|
throw new NotImplementedException();
|
|
|
|
|
}
|
|
|
|
|
|
2025-07-19 17:39:55 -05:00
|
|
|
public override async Task<bool> ExistsAsync(ServerHttpPath record)
|
2025-03-30 21:05:48 -05:00
|
|
|
{
|
2025-12-03 02:22:37 -06:00
|
|
|
if (ImportContext.Manifest is SDK.Models.Manifest.Server server)
|
|
|
|
|
return await serverHttpPathService
|
|
|
|
|
.Include(p => p.Server)
|
|
|
|
|
.ExistsAsync(p => p.Path == record.Path && p.ServerId == server.Id);
|
|
|
|
|
|
|
|
|
|
return false;
|
2025-03-30 21:05:48 -05:00
|
|
|
}
|
|
|
|
|
}
|