-
-
Notifications
You must be signed in to change notification settings - Fork 462
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adding the ability to use strong typed identifiers with Json.WriteByI…
…d and Json.FindByIdAsync. Closes GH-3608
- Loading branch information
1 parent
436f3b7
commit 5c4b552
Showing
6 changed files
with
194 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
using System; | ||
using System.Text.Json.Serialization; | ||
using System.Threading.Tasks; | ||
using Marten; | ||
using Marten.AspNetCore; | ||
using Microsoft.AspNetCore.Mvc; | ||
using StronglyTypedIds; | ||
|
||
namespace IssueService.Controllers; | ||
|
||
public class PaymentController : ControllerBase | ||
{ | ||
[HttpGet("/payment/{paymentId}")] | ||
public Task WritePayment(Guid paymentId, [FromServices] IQuerySession session) | ||
{ | ||
return session.Json.WriteById<Payment>(new PaymentId(paymentId), HttpContext); | ||
} | ||
} | ||
|
||
[StronglyTypedId(Template.Guid)] | ||
public readonly partial struct PaymentId; | ||
|
||
public class Payment | ||
{ | ||
[JsonInclude] public PaymentId? Id { get; set; } | ||
|
||
[JsonInclude] public DateTimeOffset CreatedAt { get; set; } | ||
|
||
[JsonInclude] public PaymentState State { get; set; } | ||
|
||
|
||
} | ||
|
||
public enum PaymentState | ||
{ | ||
Created, | ||
Initialized, | ||
Canceled, | ||
Verified | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
48 changes: 48 additions & 0 deletions
48
src/Marten.AspNetCore.Testing/using_strong_typed_identifiers.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
using System; | ||
using System.Text.Json.Serialization; | ||
using System.Threading.Tasks; | ||
using Alba; | ||
using IssueService.Controllers; | ||
using Shouldly; | ||
using StronglyTypedIds; | ||
using Xunit; | ||
|
||
namespace Marten.AspNetCore.Testing; | ||
|
||
[Collection("integration")] | ||
public class using_strong_typed_identifiers : IntegrationContext | ||
{ | ||
public using_strong_typed_identifiers(AppFixture fixture) : base(fixture) | ||
{ | ||
} | ||
|
||
[Fact] | ||
public async Task stream_json_hit() | ||
{ | ||
var payment = new Payment | ||
{ | ||
CreatedAt = DateTime.Today, | ||
State = PaymentState.Created | ||
}; | ||
|
||
using var session = Host.DocumentStore().LightweightSession(); | ||
session.Store(payment); | ||
await session.SaveChangesAsync(); | ||
|
||
var json = await session.Json.FindByIdAsync<Payment>(payment.Id); | ||
json.ShouldContain(payment.Id.ToString()); | ||
|
||
var result = await Host.Scenario(s => | ||
{ | ||
s.Get.Url($"/payment/{payment.Id}"); | ||
s.StatusCodeShouldBe(200); | ||
s.ContentTypeShouldBe("application/json"); | ||
}); | ||
|
||
var read = result.ReadAsJson<Payment>(); | ||
|
||
read.State.ShouldBe(payment.State); | ||
} | ||
} | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters