✨ Publisher rewarding
This commit is contained in:
@@ -8,8 +8,14 @@ namespace DysonNetwork.Pass.Credit;
|
||||
public class SocialCreditService(AppDatabase db, ICacheService cache)
|
||||
{
|
||||
private const string CacheKeyPrefix = "account:credits:";
|
||||
|
||||
public async Task<SnSocialCreditRecord> AddRecord(string reasonType, string reason, double delta, Guid accountId)
|
||||
|
||||
public async Task<SnSocialCreditRecord> AddRecord(
|
||||
string reasonType,
|
||||
string reason,
|
||||
double delta,
|
||||
Guid accountId,
|
||||
Instant? expiredAt
|
||||
)
|
||||
{
|
||||
var record = new SnSocialCreditRecord
|
||||
{
|
||||
@@ -17,21 +23,22 @@ public class SocialCreditService(AppDatabase db, ICacheService cache)
|
||||
Reason = reason,
|
||||
Delta = delta,
|
||||
AccountId = accountId,
|
||||
ExpiredAt = expiredAt
|
||||
};
|
||||
db.SocialCreditRecords.Add(record);
|
||||
await db.SaveChangesAsync();
|
||||
|
||||
|
||||
await db.AccountProfiles
|
||||
.Where(p => p.AccountId == accountId)
|
||||
.ExecuteUpdateAsync(p => p.SetProperty(v => v.SocialCredits, v => v.SocialCredits + record.Delta));
|
||||
|
||||
|
||||
await cache.RemoveAsync($"{CacheKeyPrefix}{accountId}");
|
||||
|
||||
|
||||
return record;
|
||||
}
|
||||
|
||||
|
||||
private const double BaseSocialCredit = 100;
|
||||
|
||||
|
||||
public async Task<double> GetSocialCredit(Guid accountId)
|
||||
{
|
||||
var cached = await cache.GetAsync<double?>($"{CacheKeyPrefix}{accountId}");
|
||||
@@ -61,7 +68,8 @@ public class SocialCreditService(AppDatabase db, ICacheService cache)
|
||||
{
|
||||
await db.AccountProfiles
|
||||
.Where(p => p.AccountId == accountId)
|
||||
.ExecuteUpdateAsync(p => p.SetProperty(v => v.SocialCredits, v => v.SocialCredits - totalDeltaSubtracted));
|
||||
.ExecuteUpdateAsync(p =>
|
||||
p.SetProperty(v => v.SocialCredits, v => v.SocialCredits - totalDeltaSubtracted));
|
||||
await cache.RemoveAsync($"{CacheKeyPrefix}{accountId}");
|
||||
}
|
||||
|
||||
@@ -69,4 +77,4 @@ public class SocialCreditService(AppDatabase db, ICacheService cache)
|
||||
.Where(r => r.Status == SocialCreditRecordStatus.Active && r.ExpiredAt.HasValue && r.ExpiredAt <= now)
|
||||
.ExecuteUpdateAsync(r => r.SetProperty(x => x.Status, SocialCreditRecordStatus.Expired));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,27 +1,33 @@
|
||||
using DysonNetwork.Shared.Proto;
|
||||
using Grpc.Core;
|
||||
using NodaTime.Serialization.Protobuf;
|
||||
|
||||
namespace DysonNetwork.Pass.Credit;
|
||||
|
||||
public class SocialCreditServiceGrpc(SocialCreditService creditService) : Shared.Proto.SocialCreditService.SocialCreditServiceBase
|
||||
public class SocialCreditServiceGrpc(SocialCreditService creditService)
|
||||
: Shared.Proto.SocialCreditService.SocialCreditServiceBase
|
||||
{
|
||||
public override async Task<Shared.Proto.SocialCreditRecord> AddRecord(AddSocialCreditRecordRequest request, ServerCallContext context)
|
||||
public override async Task<SocialCreditRecord> AddRecord(AddSocialCreditRecordRequest request,
|
||||
ServerCallContext context)
|
||||
{
|
||||
var accountId = Guid.Parse(request.AccountId);
|
||||
var record = await creditService.AddRecord(
|
||||
request.ReasonType,
|
||||
request.Reason,
|
||||
request.Delta,
|
||||
accountId);
|
||||
accountId,
|
||||
request.ExpiredAt.ToInstant()
|
||||
);
|
||||
|
||||
return record.ToProto();
|
||||
}
|
||||
|
||||
public override async Task<SocialCreditResponse> GetSocialCredit(GetSocialCreditRequest request, ServerCallContext context)
|
||||
public override async Task<SocialCreditResponse> GetSocialCredit(GetSocialCreditRequest request,
|
||||
ServerCallContext context)
|
||||
{
|
||||
var accountId = Guid.Parse(request.AccountId);
|
||||
var amount = await creditService.GetSocialCredit(accountId);
|
||||
|
||||
|
||||
return new SocialCreditResponse { Amount = amount };
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user