C
C#3mo ago
Joellao

Saving state in time - "snapshot"

Hello everyone. I'm adding some gameification to a project that I'm currently working on really sporadically. Let's say we have Users and Posts. A User can have multiple Posts and one post belongs to only one user. I want to create a Leaderboard for this. Every day at 19.00 I'd like to create a Leaderboard with the User and their number of posts. Not all the users are part of a leaderboard, they need to be added to one to be calculated. How would you guys implement this? My idea was to have a Leaderboard entity class basically like this
public class LeaderBoardEntity {
[Key]
public int Id { get; set; }
public IEnumerable<UserSnapshot> { get; set; }
public DateTime CreatedAt { get; set; }
}

public class UserSnapshotEntity {
[Key]
public int Id { get; set; }
public int PostCount { get; set; }
public IEnumerable<PostEntity> { get; set; }
}
public class LeaderBoardEntity {
[Key]
public int Id { get; set; }
public IEnumerable<UserSnapshot> { get; set; }
public DateTime CreatedAt { get; set; }
}

public class UserSnapshotEntity {
[Key]
public int Id { get; set; }
public int PostCount { get; set; }
public IEnumerable<PostEntity> { get; set; }
}
This way when I retrieve a specific Leaderboard, I can see directly the amount of posts that the user has created. Another way would be to create the leaderboard and through SQL get the posts that were created <= LeaderBoardEntity.CreatedAt. Anyone thinks of anything else?
1 Reply
Joellao
Joellao3mo ago
Any suggestion on this one?