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.
61 lines
2.3 KiB
C#
61 lines
2.3 KiB
C#
using LANCommander.Server.Services;
|
|
using Microsoft.AspNetCore.Authorization;
|
|
using LANCommander.SDK.Enums;
|
|
using LANCommander.Server.ImportExport.Factories;
|
|
|
|
namespace LANCommander.Server.Controllers
|
|
{
|
|
[Authorize(Roles = RoleService.AdministratorRoleName)]
|
|
public class GamesController : BaseController
|
|
{
|
|
private readonly GameService GameService;
|
|
private readonly ExportContextFactory ExportContextFactory;
|
|
|
|
public GamesController(
|
|
ILogger<GamesController> logger,
|
|
GameService gameService,
|
|
ExportContextFactory exportContextFactory) : base(logger)
|
|
{
|
|
GameService = gameService;
|
|
ExportContextFactory = exportContextFactory;
|
|
}
|
|
|
|
public async Task ExportAsync(Guid id, ImportRecordFlags flags)
|
|
{
|
|
using (var context = ExportContextFactory.Create())
|
|
{
|
|
var game = await GameService
|
|
.Include(g => g.Actions)
|
|
.Include(g => g.Archives)
|
|
.Include(g => g.BaseGame)
|
|
.Include(g => g.Categories)
|
|
.Include(g => g.Collections)
|
|
.Include(g => g.CustomFields)
|
|
.Include(g => g.DependentGames)
|
|
.Include(g => g.Developers)
|
|
.Include(g => g.Engine)
|
|
.Include(g => g.Genres)
|
|
.Include(g => g.Media)
|
|
.Include(g => g.MultiplayerModes)
|
|
.Include(g => g.Platforms)
|
|
.Include(g => g.Publishers)
|
|
.Include(g => g.Redistributables)
|
|
.Include(g => g.Scripts)
|
|
.Include(g => g.Tags)
|
|
.GetAsync(id);
|
|
|
|
if (game == null)
|
|
{
|
|
Response.StatusCode = StatusCodes.Status404NotFound;
|
|
return;
|
|
}
|
|
|
|
Response.ContentType = "application/octet-stream";
|
|
Response.Headers.Append("Content-Disposition", @$"attachment; filename=""{game.Title}.lcx""");
|
|
|
|
//await context.PrepareGameExportQueueAsync(game, flags);
|
|
await context.ExportQueueAsync(Response.BodyWriter.AsStream());
|
|
}
|
|
}
|
|
}
|
|
}
|