Skip to content

Commit

Permalink
Demonstrate non-gen Controller operation.
Browse files Browse the repository at this point in the history
  • Loading branch information
chullybun committed Oct 4, 2024
1 parent 7e70917 commit a4d1086
Show file tree
Hide file tree
Showing 12 changed files with 131 additions and 1 deletion.
38 changes: 38 additions & 0 deletions samples/Demo/Beef.Demo.Api/Controllers/PersonController.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
#nullable enable

using CoreEx.Results;

namespace Beef.Demo.Api.Controllers
{
public partial class PersonController
{
/// <summary>
/// Extend Response.
/// </summary>
/// <returns>A resultant <c>string</c>.</returns>
[HttpPost("api/v1/persons/extend-response", Name = "Person_ExtendResponse")]
[ProducesResponseType(typeof(string), (int)HttpStatusCode.OK)]
[ProducesResponseType((int)HttpStatusCode.NoContent)]
public Task<IActionResult> ExtendResponse(string? name)
=> _webApi.PostWithResultAsync(Request, p =>
{
return Result.GoAsync(() => _manager.ExtendResponseAsync(name)) // Execute the business logic.
.ThenAs(r => // Then handle response where Result.IsSuccess; otherwise, allow to bubble out.
{
var ia = ValueContentResult.CreateResult(r, HttpStatusCode.OK, null, _webApi.JsonSerializer, p.RequestOptions, false, null); // Use standard CoreEx ValueContentResult to handle the response.
if (ia is ValueContentResult vcr) // Ensure is a ValueContentResult, and if so, then manipulate.
{
vcr.BeforeExtension = hr => // Extend the reponse by adding a new header.
{
hr.Headers.Add("X-Beef-Test", "123");
return Task.CompletedTask;
};
}
return ia;
});
}, alternateStatusCode: HttpStatusCode.NoContent, operationType: CoreEx.OperationType.Unspecified);
}
}

#nullable restore
7 changes: 7 additions & 0 deletions samples/Demo/Beef.Demo.Business/Data/Generated/IPersonData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,13 @@ public partial interface IPersonData
/// <param name="id">The <see cref="Person"/> identifier.</param>
/// <returns>A resultant <see cref="string"/>.</returns>
Task<Result<string?>> SimulateWorkAsync(Guid id);

/// <summary>
/// Extend Response.
/// </summary>
/// <param name="name">The Name.</param>
/// <returns>A resultant <see cref="string"/>.</returns>
Task<Result<string?>> ExtendResponseAsync(string? name);
}

#pragma warning restore
Expand Down
3 changes: 3 additions & 0 deletions samples/Demo/Beef.Demo.Business/Data/Generated/PersonData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,9 @@ public Task DeleteWithEfAsync(Guid id) => DataInvoker.Current.InvokeAsync(this,
/// <inheritdoc/>
public Task<Result<string?>> SimulateWorkAsync(Guid id) => SimulateWorkOnImplementationAsync(id);

/// <inheritdoc/>
public Task<Result<string?>> ExtendResponseAsync(string? name) => ExtendResponseOnImplementationAsync(name);

/// <summary>
/// Provides the <see cref="Person"/> property and database column mapping.
/// </summary>
Expand Down
2 changes: 2 additions & 0 deletions samples/Demo/Beef.Demo.Business/Data/PersonData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -166,5 +166,7 @@ private static Task Add3OnImplementationAsync(Person value)
if (value == null) throw new ArgumentNullException(nameof(value));
return Task.CompletedTask;
}

private Task<Result<string?>> ExtendResponseOnImplementationAsync(string? name) => name is null ? Result<string?>.Fail("Name is needed dude!").AsTask() : Result.Ok<string?>(name).AsTask();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,13 @@ public partial interface IPersonDataSvc
/// <param name="id">The <see cref="Person"/> identifier.</param>
/// <returns>A resultant <see cref="string"/>.</returns>
Task<Result<string?>> SimulateWorkAsync(Guid id);

/// <summary>
/// Extend Response.
/// </summary>
/// <param name="name">The Name.</param>
/// <returns>A resultant <see cref="string"/>.</returns>
Task<Result<string?>> ExtendResponseAsync(string? name);
}

#pragma warning restore
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ public partial class PersonDataSvc : IPersonDataSvc
private Func<Guid, Task>? _deleteWithEfOnAfterAsync;
private Func<FileContentResult, Guid, Task>? _getDocumentationOnAfterAsync;
private Func<string?, Guid, Task<Result>>? _simulateWorkOnAfterAsync;
private Func<string?, string?, Task<Result>>? _extendResponseOnAfterAsync;

#endregion

Expand Down Expand Up @@ -309,6 +310,13 @@ public async Task<FileContentResult> GetDocumentationAsync(Guid id)
.ThenAsync(r => _simulateWorkOnAfterAsync?.Invoke(r, id) ?? Result.SuccessTask)
.Then(r => _events.PublishValueEvent("WorkIt", new Uri($"/person/", UriKind.Relative), $"Work", "Simulated"));
}, new InvokerArgs { IncludeTransactionScope = true, EventPublisher = _events });

/// <inheritdoc/>
public Task<Result<string?>> ExtendResponseAsync(string? name)
{
return Result.GoAsync(_data.ExtendResponseAsync(name))
.ThenAsync(r => _extendResponseOnAfterAsync?.Invoke(r, name) ?? Result.SuccessTask);
}
}

#pragma warning restore
Expand Down
7 changes: 7 additions & 0 deletions samples/Demo/Beef.Demo.Business/Generated/IPersonManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,13 @@ public partial interface IPersonManager
/// <param name="id">The <see cref="Person"/> identifier.</param>
/// <returns>A resultant <see cref="string"/>.</returns>
Task<Result<string?>> SimulateWorkAsync(Guid id);

/// <summary>
/// Extend Response.
/// </summary>
/// <param name="name">The Name.</param>
/// <returns>A resultant <see cref="string"/>.</returns>
Task<Result<string?>> ExtendResponseAsync(string? name);
}

#pragma warning restore
Expand Down
19 changes: 19 additions & 0 deletions samples/Demo/Beef.Demo.Business/Generated/PersonManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,11 @@ public partial class PersonManager : IPersonManager
private Func<Guid, Task<Result>>? _simulateWorkOnBeforeAsync;
private Func<string?, Guid, Task<Result>>? _simulateWorkOnAfterAsync;

private Func<string?, Task<Result>>? _extendResponseOnPreValidateAsync;
private Action<MultiValidator, string?>? _extendResponseOnValidate;
private Func<string?, Task<Result>>? _extendResponseOnBeforeAsync;
private Func<string?, string?, Task<Result>>? _extendResponseOnAfterAsync;

#endregion

/// <summary>
Expand Down Expand Up @@ -674,6 +679,20 @@ await MultiValidator.Create()
.ThenAsync(r => _simulateWorkOnAfterAsync?.Invoke(r, id) ?? Result.SuccessTask)
.Then(r => Cleaner.Clean(r));
}, InvokerArgs.Unspecified);

/// <inheritdoc/>
public Task<Result<string?>> ExtendResponseAsync(string? name) => ManagerInvoker.Current.InvokeAsync(this, (_, ct) =>
{
return Result.Go()
.Then(() => Cleaner.CleanUp(name))
.ThenAsync(() => _extendResponseOnPreValidateAsync?.Invoke(name) ?? Result.SuccessTask)
.ValidateAsync(() => MultiValidator.Create()
.Additional(mv => _extendResponseOnValidate?.Invoke(mv, name)), cancellationToken: ct)
.ThenAsync(() => _extendResponseOnBeforeAsync?.Invoke(name) ?? Result.SuccessTask)
.ThenAsAsync(() => _dataService.ExtendResponseAsync(name))
.ThenAsync(r => _extendResponseOnAfterAsync?.Invoke(r, name) ?? Result.SuccessTask)
.Then(r => Cleaner.Clean(r));
}, InvokerArgs.Unspecified);
}

#pragma warning restore
Expand Down
3 changes: 2 additions & 1 deletion samples/Demo/Beef.Demo.CodeGen/entity.beef-5.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,8 @@ entities:
parameters: [
{ name: Id, text: '{{Person}} identifier', type: Guid, isMandatory: true }
]},
{ name: SimulateWork, type: Custom, withResult: true, returnType: string?, webApiRoute: simulate, webApiMethod: HttpGet, webApiStatus: OK, primaryKey: true, eventSubject: Work:Simulated, eventValue: '"WorkIt"' }
{ name: SimulateWork, type: Custom, withResult: true, returnType: string?, webApiRoute: simulate, webApiMethod: HttpGet, webApiStatus: OK, primaryKey: true, eventSubject: Work:Simulated, eventValue: '"WorkIt"' },
{ name: ExtendResponse, type: Custom, returnType: string?, excludeWebApi: true, webApiRoute: extend-response, withResult: true, parameters: [ { name: Name } ] }
]
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -349,6 +349,15 @@ public partial interface IPersonAgent
/// <param name="cancellationToken">The <see cref="CancellationToken"/>.</param>
/// <returns>A <see cref="HttpResult"/>.</returns>
Task<HttpResult<string?>> SimulateWorkAsync(Guid id, HttpRequestOptions? requestOptions = null, CancellationToken cancellationToken = default);

/// <summary>
/// Extend Response.
/// </summary>
/// <param name="name">The Name.</param>
/// <param name="requestOptions">The optional <see cref="HttpRequestOptions"/>.</param>
/// <param name="cancellationToken">The <see cref="CancellationToken"/>.</param>
/// <returns>A <see cref="HttpResult"/>.</returns>
Task<HttpResult<string?>> ExtendResponseAsync(string? name, HttpRequestOptions? requestOptions = null, CancellationToken cancellationToken = default);
}
}

Expand Down
4 changes: 4 additions & 0 deletions samples/Demo/Beef.Demo.Common/Agents/Generated/PersonAgent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,10 @@ public Task<HttpResult> GetDocumentationAsync(Guid id, HttpRequestOptions? reque
/// <inheritdoc/>
public Task<HttpResult<string?>> SimulateWorkAsync(Guid id, HttpRequestOptions? requestOptions = null, CancellationToken cancellationToken = default)
=> GetAsync<string?>("api/v1/persons/simulate", requestOptions: requestOptions, args: HttpArgs.Create(new HttpArg<Guid>("id", id)), cancellationToken: cancellationToken);

/// <inheritdoc/>
public Task<HttpResult<string?>> ExtendResponseAsync(string? name, HttpRequestOptions? requestOptions = null, CancellationToken cancellationToken = default)
=> PostAsync<string?>("api/v1/persons/extend-response", requestOptions: requestOptions, args: HttpArgs.Create(new HttpArg<string?>("name", name)), cancellationToken: cancellationToken);
}
}

Expand Down
25 changes: 25 additions & 0 deletions samples/Demo/Beef.Demo.Test/PersonTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1312,5 +1312,30 @@ public void I420_Simulate_Perf()
}

#endregion

#region ExtendResponse

[Test, TestSetUp]
public void J110_ExtendResponse_Error()
{
AgentTester.Test<PersonAgent>()
.ExpectStatusCode(HttpStatusCode.BadRequest)
.ExpectError("Name is needed dude!")
.Run(a => a.ExtendResponseAsync(null));
}

[Test, TestSetUp]
public void J120_ExtendResponse_Success()
{
var resp = AgentTester.Test<PersonAgent, string>()
.ExpectStatusCode(HttpStatusCode.OK)
.ExpectValue("Sweet!")
.Run(a => a.ExtendResponseAsync("Sweet!"));

if (resp.Response.Headers.TryGetValues("X-Beef-Test", out var values))
Assert.That(values, Is.EquivalentTo(new string[] { "123" }));
}

#endregion
}
}

0 comments on commit a4d1086

Please sign in to comment.