LANCommander/LANCommander.Server.ImportExport/Importers/SavePathImporter.cs

75 lines
2.4 KiB
C#
Raw Permalink Normal View History

2025-04-11 02:31:29 -05:00
using AutoMapper;
using LANCommander.SDK.Enums;
using LANCommander.SDK.Models.Manifest;
using LANCommander.Server.ImportExport.Exceptions;
using LANCommander.Server.ImportExport.Models;
using LANCommander.Server.Services;
2025-03-29 17:58:00 -05:00
namespace LANCommander.Server.ImportExport.Importers;
2025-03-29 17:58:00 -05:00
public class SavePathImporter(
2025-04-11 02:31:29 -05:00
IMapper mapper,
SavePathService savePathService) : BaseImporter<SavePath, Data.Models.SavePath>
2025-03-29 17:58:00 -05:00
{
public override async Task<ImportItemInfo> GetImportInfoAsync(SavePath record)
{
return new ImportItemInfo
{
Type = ImportExportRecordType.SavePath,
Name = record.Path,
};
}
public override bool CanImport(SavePath record) => ImportContext.DataRecord is Data.Models.Game;
2025-03-29 17:58:00 -05:00
public override async Task<Data.Models.SavePath> AddAsync(SavePath record)
{
2025-03-29 17:58:00 -05:00
try
{
var savePath = new Data.Models.SavePath
{
Id = record.Id,
Game = ImportContext.DataRecord as Data.Models.Game,
2025-03-29 17:58:00 -05:00
Path = record.Path,
WorkingDirectory = record.WorkingDirectory,
IsRegex = record.IsRegex,
Type = record.Type,
};
savePath = await savePathService.AddAsync(savePath);
2025-03-29 17:58:00 -05:00
2025-03-29 20:10:35 -05:00
return savePath;
2025-03-29 17:58:00 -05:00
}
catch (Exception ex)
{
throw new ImportSkippedException<SavePath>(record, "An unknown error occured while importing save path", ex);
}
}
public override async Task<Data.Models.SavePath> UpdateAsync(SavePath record)
2025-03-29 17:58:00 -05:00
{
var existing = await savePathService.FirstOrDefaultAsync(p => p.Id == record.Id);
2025-03-29 17:58:00 -05:00
try
{
existing.Game = ImportContext.DataRecord as Data.Models.Game;
2025-03-29 17:58:00 -05:00
existing.Path = record.Path;
existing.WorkingDirectory = record.WorkingDirectory;
existing.IsRegex = record.IsRegex;
existing.Type = record.Type;
existing = await savePathService.UpdateAsync(existing);
2025-03-29 17:58:00 -05:00
2025-03-29 20:10:35 -05:00
return existing;
2025-03-29 17:58:00 -05:00
}
catch (Exception ex)
{
throw new ImportSkippedException<SavePath>(record, "An unknown error occured while importing save path", ex);
}
}
public override async Task<bool> ExistsAsync(SavePath record)
2025-03-29 17:58:00 -05:00
{
return await savePathService.ExistsAsync(p => p.Id == record.Id);
2025-03-29 17:58:00 -05:00
}
}