Skip to content

Commit

Permalink
use new dbtx helper func
Browse files Browse the repository at this point in the history
  • Loading branch information
0xor1 committed Apr 1, 2024
1 parent ddd05cd commit b13d8bc
Showing 1 changed file with 33 additions and 42 deletions.
75 changes: 33 additions & 42 deletions Dnsk.Eps/CounterEps.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,53 +38,44 @@ internal static class CounterEps
public static IReadOnlyList<IEp> Eps { get; } =
new List<IEp>()
{
new Ep<Get, Counter>(
Ep<Get, Counter>.DbTx<DnskDb>(
CounterRpcs.Get,
async (ctx, req) =>
await ctx.DbTx<DnskDb, Counter>(
async (db, ses) =>
{
var counter = await GetCounter(ctx, db, ses, req);
return counter.ToApi();
},
false
)
async (ctx, db, ses, req) =>
{
var counter = await GetCounter(ctx, db, ses, req);
return counter.ToApi();
},
false
),
new Ep<Nothing, Counter>(
Ep<Nothing, Counter>.DbTx<DnskDb>(
CounterRpcs.Increment,
async (ctx, _) =>
await ctx.DbTx<DnskDb, Counter>(
async (db, ses) =>
{
var counter = await GetCounter(ctx, db, ses);
if (counter.Value < uint.MaxValue)
{
counter.Value++;
}
var fcm = ctx.Get<IFcmClient>();
var res = counter.ToApi();
await fcm.SendTopic(ctx, db, ses, new List<string>() { ses.Id }, res);
return res;
}
)
async (ctx, db, ses, _) =>
{
var counter = await GetCounter(ctx, db, ses);
if (counter.Value < uint.MaxValue)
{
counter.Value++;
}
var fcm = ctx.Get<IFcmClient>();
var res = counter.ToApi();
await fcm.SendTopic(ctx, db, ses, new List<string>() { ses.Id }, res);
return res;
}
),
new Ep<Nothing, Counter>(
Ep<Nothing, Counter>.DbTx<DnskDb>(
CounterRpcs.Decrement,
async (ctx, _) =>
await ctx.DbTx<DnskDb, Counter>(
async (db, ses) =>
{
var counter = await GetCounter(ctx, db, ses);
if (counter.Value > uint.MinValue)
{
counter.Value--;
}
var fcm = ctx.Get<IFcmClient>();
var res = counter.ToApi();
await fcm.SendTopic(ctx, db, ses, new List<string>() { ses.Id }, res);
return res;
}
)
async (ctx, db, ses, req) =>
{
var counter = await GetCounter(ctx, db, ses);
if (counter.Value > uint.MinValue)
{
counter.Value--;
}
var fcm = ctx.Get<IFcmClient>();
var res = counter.ToApi();
await fcm.SendTopic(ctx, db, ses, new List<string>() { ses.Id }, res);
return res;
}
)
};

Expand Down

0 comments on commit b13d8bc

Please sign in to comment.