Skip to content

Commit

Permalink
Add export download endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
sei-bstein committed Jan 16, 2025
1 parent d1a1cd3 commit dee881d
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 6 deletions.
8 changes: 8 additions & 0 deletions src/Gameboard.Api/Features/Game/GameController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,14 @@ public Task<GamePlayState> GetGamePlayState(string gameId)
public Task<ExportGamesResult> ExportGames([FromBody] ExportGameCommand request, CancellationToken cancellationToken)
=> _mediator.Send(request, cancellationToken);

[HttpGet("/api/games/export/{exportBatchId}")]
[ProducesResponseType(typeof(FileContentResult), 200)]
public async Task<FileContentResult> DownloadExportPackage(string exportBatchId, CancellationToken cancellationToken)
{
var bytes = await _mediator.Send(new DownloadExportPackageRequest(exportBatchId), cancellationToken);
return new FileContentResult(bytes, "application/zip");
}

[HttpPost("/api/games/import")]
public async Task<ImportedGame[]> ImportGames([FromForm] IFormFile packageFile, CancellationToken cancellationToken)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -124,3 +124,8 @@ public sealed class ImportedGame
public required string Id { get; set; }
public required string Name { get; set; }
}

public sealed class ExportPackageNotFound : GameboardException
{
public ExportPackageNotFound(string exportBatchId) : base($"Export package {exportBatchId} doesn't exist.") { }
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net.Http;
using System.Threading;
using System.Threading.Tasks;
using Gameboard.Api.Common.Services;
Expand All @@ -19,6 +18,7 @@ namespace Gameboard.Api.Features.Games;

public interface IGameImportExportService
{
Task<byte[]> GetExportedPackageContent(string exportBatchId, CancellationToken cancellationToken);
Task<GameImportExportBatch> ExportPackage(string[] gameIds, bool includePracticeAreaTemplate, CancellationToken cancellationToken);
Task<ImportedGame[]> ImportPackage(byte[] package, CancellationToken cancellationToken);
}
Expand All @@ -28,21 +28,28 @@ internal sealed class GameImportExportService
IActingUserService actingUser,
CoreOptions coreOptions,
IGuidService guids,
HttpClient http,
IJsonService json,
IPracticeService practice,
IStore store,
IZipService zip
IStore store
) : IGameImportExportService
{
private readonly IActingUserService _actingUser = actingUser;
private readonly CoreOptions _coreOptions = coreOptions;
private readonly IGuidService _guids = guids;
private readonly HttpClient _http = http;
private readonly IJsonService _json = json;
private readonly IPracticeService _practice = practice;
private readonly IStore _store = store;
private readonly IZipService _zip = zip;

public async Task<byte[]> GetExportedPackageContent(string exportBatchId, CancellationToken cancellationToken)
{
var path = GetExportBatchPackagePath(exportBatchId);
if (!File.Exists(path))
{
throw new ExportPackageNotFound(exportBatchId);
}

return await File.ReadAllBytesAsync(path, cancellationToken);
}

public async Task<GameImportExportBatch> ExportPackage(string[] gameIds, bool includePracticeAreaTemplate, CancellationToken cancellationToken)
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
using System.Threading;
using System.Threading.Tasks;
using Gameboard.Api.Features.Users;
using Gameboard.Api.Structure.MediatR;
using MediatR;

namespace Gameboard.Api.Features.Games;

public record DownloadExportPackageRequest(string ExportBatchId) : IRequest<byte[]>;

internal sealed class DownloadExportPackageHandler
(
IGameImportExportService importExport,
IValidatorService validator
) : IRequestHandler<DownloadExportPackageRequest, byte[]>
{
private readonly IGameImportExportService _importExport = importExport;
private readonly IValidatorService _validator = validator;

public async Task<byte[]> Handle(DownloadExportPackageRequest request, CancellationToken cancellationToken)
{
await _validator
.Auth(c => c.Require(PermissionKey.Games_CreateEditDelete))
.Validate(cancellationToken);

return await _importExport.GetExportedPackageContent(request.ExportBatchId, cancellationToken);
}
}

0 comments on commit dee881d

Please sign in to comment.