using System;
using System.Threading;
using System.Threading.Tasks;
using FastEndpoints;
using GB5Shared.Draft;
using GB5Shared.DTO.Draft;
using GB5Shared.DTO.Framework.Login;
using GB5Shared.DTO.Framework.ResponseStandard;
using GB5Shared.FastEndPoint;
using static GB5Shared.GB5Constant.Constant;
namespace FrameworkSL.Endpoints.Draft
{
///
/// Transfers a draft to another user. Dual-layer ownership check:
/// BLL checks application-layer, SP checks at DB level.
/// Returns 401-style error if the caller is not the owner.
///
public class ShareDraft
: BaseEndpoint>
{
private readonly IDraftRowBLL _bll;
public ShareDraft(IDraftRowBLL bll) => _bll = bll;
public override void Configure()
{
Post("/Draft/ShareDraft");
AllowAnonymous();
}
public record Parameters(
[property: FromHeader] string Login,
[property: FromBody] DraftShareDTO DraftShare
);
protected override string? GetCacheKey(Parameters req, LoginDTO login) => null;
protected override async Task> ExecuteAsync(
Parameters req, LoginDTO login, CancellationToken ct)
{
try
{
var result = await _bll.ShareDraftAsync(req.DraftShare, login, ct);
return await GB5Shared.ResponseStandard.Response
.CreateSuccessResponse(result, CacheKeyLevel.NOT_REQUIRED, login);
}
catch (UnauthorizedAccessException uex)
{
return await GB5Shared.ResponseStandard.Response
.CreateExceptionError(uex, CacheKeyLevel.NOT_REQUIRED, login, uex.Message, 403);
}
catch (Exception ex)
{
return await GB5Shared.ResponseStandard.Response
.CreateExceptionError(ex, CacheKeyLevel.NOT_REQUIRED, login, ex.Message, 500);
}
}
}
}