🐛 Trying to fix poll answer cache

This commit is contained in:
2025-08-06 02:59:17 +08:00
parent b0af3af059
commit a52b09b787
2 changed files with 13 additions and 10 deletions

View File

@@ -10,12 +10,12 @@ public class Poll : ModelBase
{ {
public Guid Id { get; set; } = Guid.NewGuid(); public Guid Id { get; set; } = Guid.NewGuid();
public List<PollQuestion> Questions { get; set; } = new(); public List<PollQuestion> Questions { get; set; } = new();
[MaxLength(1024)] public string? Title { get; set; } [MaxLength(1024)] public string? Title { get; set; }
[MaxLength(4096)] public string? Description { get; set; } [MaxLength(4096)] public string? Description { get; set; }
public Instant? EndedAt { get; set; } public Instant? EndedAt { get; set; }
public Guid PublisherId { get; set; } public Guid PublisherId { get; set; }
[JsonIgnore] public Publisher.Publisher? Publisher { get; set; } [JsonIgnore] public Publisher.Publisher? Publisher { get; set; }
} }
@@ -32,15 +32,15 @@ public enum PollQuestionType
public class PollQuestion : ModelBase public class PollQuestion : ModelBase
{ {
public Guid Id { get; set; } = Guid.NewGuid(); public Guid Id { get; set; } = Guid.NewGuid();
public PollQuestionType Type { get; set; } public PollQuestionType Type { get; set; }
[Column(TypeName = "jsonb")] public List<PollOption>? Options { get; set; } [Column(TypeName = "jsonb")] public List<PollOption>? Options { get; set; }
[MaxLength(1024)] public string Title { get; set; } = null!; [MaxLength(1024)] public string Title { get; set; } = null!;
[MaxLength(4096)] public string? Description { get; set; } [MaxLength(4096)] public string? Description { get; set; }
public int Order { get; set; } = 0; public int Order { get; set; } = 0;
public bool IsRequired { get; set; } public bool IsRequired { get; set; }
public Guid PollId { get; set; } public Guid PollId { get; set; }
[JsonIgnore] public Poll Poll { get; set; } = null!; [JsonIgnore] public Poll Poll { get; set; } = null!;
} }
@@ -48,7 +48,7 @@ public class PollQuestion : ModelBase
public class PollOption public class PollOption
{ {
public Guid Id { get; set; } = Guid.NewGuid(); public Guid Id { get; set; } = Guid.NewGuid();
[Required] [MaxLength(1024)] public string Label { get; set; } = null!; [Required][MaxLength(1024)] public string Label { get; set; } = null!;
[MaxLength(4096)] public string? Description { get; set; } [MaxLength(4096)] public string? Description { get; set; }
public int Order { get; set; } = 0; public int Order { get; set; } = 0;
} }
@@ -57,8 +57,8 @@ public class PollAnswer : ModelBase
{ {
public Guid Id { get; set; } = Guid.NewGuid(); public Guid Id { get; set; } = Guid.NewGuid();
[Column(TypeName = "jsonb")] public Dictionary<string, JsonElement> Answer { get; set; } = null!; [Column(TypeName = "jsonb")] public Dictionary<string, JsonElement> Answer { get; set; } = null!;
public Guid AccountId { get; set; } public Guid AccountId { get; set; }
public Guid PollId { get; set; } public Guid PollId { get; set; }
[JsonIgnore] public Poll Poll { get; set; } = null!; [JsonIgnore] public Poll? Poll { get; set; }
} }

View File

@@ -51,7 +51,10 @@ public class PollService(AppDatabase db, ICacheService cache)
var answer = await db.PollAnswers var answer = await db.PollAnswers
.Where(e => e.PollId == pollId && e.AccountId == accountId) .Where(e => e.PollId == pollId && e.AccountId == accountId)
.AsNoTracking()
.FirstOrDefaultAsync(); .FirstOrDefaultAsync();
if (answer is not null)
answer.Poll = null;
// Set the answer even it is null, which stands for unanswered // Set the answer even it is null, which stands for unanswered
await cache.SetAsync(cacheKey, answer, TimeSpan.FromMinutes(30)); await cache.SetAsync(cacheKey, answer, TimeSpan.FromMinutes(30));