LANCommander/LANCommander.Server.ImportExport/Importers/RedistributableImporter.cs
Pat Hartl 6ec919d1be Separate import and export functionality
This refactor splits the import and export functionality out of the ImportContext and adds an ExportContext. This also introduced separate importer and exporter implementations.

Additionally, adding archive, save, scripts, and server files to the final export archive has been implemented at the exporter implementation for each instead of handling it in the context. This should be a good pattern if other files need to be added down the road.
2025-07-27 17:51:50 -05:00

68 lines
No EOL
2.3 KiB
C#

using AutoMapper;
using LANCommander.SDK.Models.Manifest;
using LANCommander.Server.ImportExport.Exceptions;
using LANCommander.Server.ImportExport.Models;
using LANCommander.Server.Services;
namespace LANCommander.Server.ImportExport.Importers;
public class RedistributableImporter(
IMapper mapper,
RedistributableService redistributableService,
UserService userService) : BaseImporter<Redistributable, Data.Models.Redistributable>
{
public override async Task<ImportItemInfo> GetImportInfoAsync(Redistributable record)
{
return new ImportItemInfo()
{
Name = record.Name,
};
}
public override bool CanImport(Redistributable record) => true;
public override async Task<Data.Models.Redistributable> AddAsync(Redistributable record)
{
var redistributable = mapper.Map<Data.Models.Redistributable>(record);
try
{
return await redistributableService.AddAsync(redistributable);
}
catch (Exception ex)
{
throw new ImportSkippedException<Redistributable>(record,
"An unknown error occurred while trying to add redistributable", ex);
}
}
public override async Task<Data.Models.Redistributable> UpdateAsync(Redistributable record)
{
var existing = await redistributableService.FirstOrDefaultAsync(r => r.Id == record.Id || r.Name == record.Name);
try
{
existing.Name = record.Name;
existing.Description = record.Description;
existing.Notes = record.Notes;
existing.CreatedOn = record.CreatedOn;
existing.CreatedBy = await userService.GetAsync(record.CreatedBy);
existing.UpdatedOn = record.UpdatedOn;
existing.UpdatedBy = await userService.GetAsync(record.UpdatedBy);
existing = await redistributableService.UpdateAsync(existing);
return existing;
}
catch (Exception ex)
{
throw new ImportSkippedException<Redistributable>(record,
"An unknown error occurred while trying to add redistributable", ex);
}
}
public override async Task<bool> ExistsAsync(Redistributable record)
{
return await redistributableService.ExistsAsync(r => r.Id == record.Id || r.Name == record.Name);
}
}