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")] [HttpGet("me")]
[Authorize] [Authorize]
public async Task<ActionResult<List<Poll>>> ListPolls( public async Task<ActionResult<List<Poll>>> ListPolls(
[FromQuery(Name = "pub")] string? pubName,
[FromQuery] bool active = false, [FromQuery] bool active = false,
[FromQuery] int offset = 0, [FromQuery] int offset = 0,
[FromQuery] int take = 20 [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(); if (HttpContext.Items["CurrentUser"] is not Account currentUser) return Unauthorized();
var accountId = Guid.Parse(currentUser.Id); 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 now = SystemClock.Instance.GetCurrentInstant();
var query = db.Polls var query = db.Polls
@@ -202,16 +246,16 @@ public class PollController(AppDatabase db, PollService polls, PublisherService
if (request.Questions != null) if (request.Questions != null)
{ {
await db.PollQuestions await db.PollQuestions
.Where(p => p.PollId == poll.Id) .Where(p => p.PollId == poll.Id)
.ExecuteDeleteAsync(); .ExecuteDeleteAsync();
var newQuestions = request.Questions var newQuestions = request.Questions
.Select(q => q.ToQuestion()) .Select(q => q.ToQuestion())
.Select(q => .Select(q =>
{ {
q.PollId = poll.Id; q.PollId = poll.Id;
return q; return q;
}) })
.ToList(); .ToList();
db.PollQuestions.AddRange(newQuestions); db.PollQuestions.AddRange(newQuestions);
await db.SaveChangesAsync(); await db.SaveChangesAsync();
poll.Questions = newQuestions; poll.Questions = newQuestions;
@@ -281,4 +325,4 @@ public class PollController(AppDatabase db, PollService polls, PublisherService
return StatusCode(500, "An error occurred while deleting the poll... " + ex.Message); return StatusCode(500, "An error occurred while deleting the poll... " + ex.Message);
} }
} }
} }