Poll feedback

This commit is contained in:
2025-08-06 14:46:21 +08:00
parent f2bba64ee5
commit 1105d6f11e

View File

@@ -71,9 +71,43 @@ public class PollController(AppDatabase db, PollService polls, PublisherService
}
}
[HttpGet("{id:guid}/feedback")]
public async Task<ActionResult<List<PollAnswer>>> GetPollFeedback(
Guid id,
[FromQuery] int offset = 0,
[FromQuery] int take = 20
)
{
if (HttpContext.Items["CurrentUser"] is not Account currentUser) return Unauthorized();
var accountId = Guid.Parse(currentUser.Id);
var poll = await db.Polls
.FirstOrDefaultAsync(p => p.Id == id);
if (poll is null) return NotFound("Poll not found");
if (!await pub.IsMemberWithRole(poll.PublisherId, accountId, PublisherMemberRole.Viewer))
return StatusCode(403, "You need to be a viewer to view this poll's feedback.");
var answerQuery = db.PollAnswers
.Where(a => a.PollId == id)
.AsQueryable();
var total = await answerQuery.CountAsync();
Response.Headers.Append("X-Total", total.ToString());
var answers = await answerQuery
.OrderByDescending(a => a.CreatedAt)
.Skip(offset)
.Take(take)
.ToListAsync();
return Ok(answers);
}
[HttpGet("me")]
[Authorize]
public async Task<ActionResult<List<Poll>>> ListPolls(
[FromQuery(Name = "pub")] string? pubName,
[FromQuery] bool active = false,
[FromQuery] int offset = 0,
[FromQuery] int take = 20
@@ -81,7 +115,17 @@ public class PollController(AppDatabase db, PollService polls, PublisherService
{
if (HttpContext.Items["CurrentUser"] is not Account currentUser) return Unauthorized();
var accountId = Guid.Parse(currentUser.Id);
var publishers = (await pub.GetUserPublishers(accountId)).Select(p => p.Id).ToList();
List<Guid> publishers;
if (pubName is null) publishers = (await pub.GetUserPublishers(accountId)).Select(p => p.Id).ToList();
else
{
publishers = await db.PublisherMembers
.Include(p => p.Publisher)
.Where(p => p.Publisher.Name == pubName && p.AccountId == accountId)
.Select(p => p.PublisherId)
.ToListAsync();
}
var now = SystemClock.Instance.GetCurrentInstant();
var query = db.Polls