2025-04-11 02:31:29 -05:00
|
|
|
using AutoMapper;
|
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.Exceptions;
|
|
|
|
|
using LANCommander.Server.ImportExport.Models;
|
|
|
|
|
using LANCommander.Server.Services;
|
2025-03-29 17:58:00 -05:00
|
|
|
|
2025-07-27 17:51:50 -05:00
|
|
|
namespace LANCommander.Server.ImportExport.Importers;
|
2025-03-29 17:58:00 -05:00
|
|
|
|
2025-04-10 19:45:41 -05:00
|
|
|
public class DeveloperImporter(
|
2025-04-11 02:31:29 -05:00
|
|
|
IMapper mapper,
|
2025-07-26 19:19:38 -05:00
|
|
|
CompanyService companyService,
|
|
|
|
|
GameService gameService) : BaseImporter<Company, Data.Models.Company>
|
2025-03-29 17:58:00 -05:00
|
|
|
{
|
2025-07-19 17:39:55 -05:00
|
|
|
public override async Task<ImportItemInfo> GetImportInfoAsync(Company record)
|
2025-04-12 02:37:14 -05:00
|
|
|
{
|
|
|
|
|
return new ImportItemInfo
|
|
|
|
|
{
|
2025-08-01 02:27:06 -05:00
|
|
|
Type = ImportExportRecordType.Developer,
|
2025-04-12 02:37:14 -05:00
|
|
|
Name = record.Name,
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
2025-07-19 17:39:55 -05:00
|
|
|
public override bool CanImport(Company record) => ImportContext.DataRecord is Data.Models.Game;
|
2025-04-11 02:31:29 -05:00
|
|
|
|
2025-07-19 17:39:55 -05:00
|
|
|
public override async Task<Data.Models.Company> AddAsync(Company record)
|
2025-03-29 17:58:00 -05:00
|
|
|
{
|
2025-07-26 19:19:38 -05:00
|
|
|
if (ImportContext.DataRecord is not Data.Models.Game)
|
2025-07-19 17:39:55 -05:00
|
|
|
throw new ImportSkippedException<Company>(record, $"Cannot import developers for a {ImportContext.DataRecord.GetType().Name}");
|
2025-03-29 17:58:00 -05:00
|
|
|
|
|
|
|
|
try
|
|
|
|
|
{
|
2025-07-26 19:19:38 -05:00
|
|
|
var game = ImportContext.DataRecord as Data.Models.Game;
|
|
|
|
|
|
2025-03-29 17:58:00 -05:00
|
|
|
var company = new Data.Models.Company
|
|
|
|
|
{
|
2025-07-26 19:19:38 -05:00
|
|
|
DevelopedGames = [await gameService.GetAsync(game.Id)],
|
2025-03-29 17:58:00 -05:00
|
|
|
Name = record.Name,
|
|
|
|
|
};
|
|
|
|
|
|
2025-04-09 19:44:07 -05:00
|
|
|
company = await companyService.AddAsync(company);
|
2025-03-29 17:58:00 -05:00
|
|
|
|
2025-03-29 20:10:35 -05:00
|
|
|
return company;
|
2025-03-29 17:58:00 -05:00
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
throw new ImportSkippedException<Company>(record, "An unknown error occured while importing developer", ex);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-07-19 17:39:55 -05:00
|
|
|
public override async Task<Data.Models.Company> UpdateAsync(Company record)
|
2025-03-29 17:58:00 -05:00
|
|
|
{
|
2025-07-26 19:19:38 -05:00
|
|
|
if (ImportContext.DataRecord is not Data.Models.Game)
|
2025-07-19 17:39:55 -05:00
|
|
|
throw new ImportSkippedException<Company>(record, $"Cannot import developers for a {ImportContext.DataRecord.GetType().Name}");
|
2025-03-29 17:58:00 -05:00
|
|
|
|
2025-04-09 19:44:07 -05:00
|
|
|
var existing = await companyService.Include(g => g.DevelopedGames).FirstOrDefaultAsync(c => c.Name == record.Name);
|
2025-03-29 17:58:00 -05:00
|
|
|
|
|
|
|
|
try
|
|
|
|
|
{
|
2025-07-26 19:19:38 -05:00
|
|
|
var game = ImportContext.DataRecord as Data.Models.Game;
|
|
|
|
|
|
2025-03-29 17:58:00 -05:00
|
|
|
if (existing.DevelopedGames == null)
|
2025-03-30 21:05:48 -05:00
|
|
|
existing.DevelopedGames = new List<Data.Models.Game>();
|
2025-07-26 19:19:38 -05:00
|
|
|
|
|
|
|
|
if (!existing.DevelopedGames.Any(g => g.Id == game.Id))
|
|
|
|
|
{
|
|
|
|
|
existing.DevelopedGames.Add(await gameService.GetAsync(game.Id));
|
2025-03-29 17:58:00 -05:00
|
|
|
|
2025-07-26 19:19:38 -05:00
|
|
|
existing = await companyService.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<Company>(record, "An unknown error occured while importing developer", ex);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-07-19 17:39:55 -05:00
|
|
|
public override async Task<bool> ExistsAsync(Company record)
|
2025-03-29 17:58:00 -05:00
|
|
|
{
|
2025-07-19 17:39:55 -05:00
|
|
|
if (ImportContext.DataRecord is not Data.Models.Game game)
|
|
|
|
|
throw new ImportSkippedException<Company>(record, $"Cannot import developers for a {ImportContext.DataRecord.GetType().Name}");
|
2025-03-29 17:58:00 -05:00
|
|
|
|
2025-04-09 19:44:07 -05:00
|
|
|
return await companyService.ExistsAsync(c => c.Name == record.Name);
|
2025-03-29 17:58:00 -05:00
|
|
|
}
|
|
|
|
|
}
|