Poll answer and un-answer

This commit is contained in:
2025-08-02 18:18:48 +08:00
parent 700803f7a6
commit 71eccbb466
2 changed files with 101 additions and 21 deletions

View File

@@ -20,6 +20,28 @@ public class Poll : ModelBase
public Publisher.Publisher Publisher { get; set; } = null!; public Publisher.Publisher Publisher { get; set; } = null!;
} }
public class PollWithUserAnswer : Poll
{
public PollAnswer? UserAnswer { get; set; }
public static PollWithUserAnswer FromPoll(Poll poll, PollAnswer? userAnswer = null)
{
return new PollWithUserAnswer
{
Id = poll.Id,
Title = poll.Title,
Description = poll.Description,
EndedAt = poll.EndedAt,
PublisherId = poll.PublisherId,
Publisher = poll.Publisher,
Questions = poll.Questions,
CreatedAt = poll.CreatedAt,
UpdatedAt = poll.UpdatedAt,
UserAnswer = userAnswer
};
}
}
public enum PollQuestionType public enum PollQuestionType
{ {
SingleChoice, SingleChoice,

View File

@@ -1,3 +1,4 @@
using System.Text.Json;
using DysonNetwork.Shared.Proto; using DysonNetwork.Shared.Proto;
using DysonNetwork.Sphere.Publisher; using DysonNetwork.Sphere.Publisher;
using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Authorization;
@@ -11,6 +12,63 @@ namespace DysonNetwork.Sphere.Poll;
[Route("/api/polls")] [Route("/api/polls")]
public class PollController(AppDatabase db, PollService polls, PublisherService pub) : ControllerBase public class PollController(AppDatabase db, PollService polls, PublisherService pub) : ControllerBase
{ {
[HttpGet("{id:guid}")]
public async Task<ActionResult<PollWithUserAnswer>> GetPoll(Guid id)
{
var poll = await db.Polls
.Include(p => p.Questions)
.FirstOrDefaultAsync(p => p.Id == id);
if (poll is null) return NotFound("Poll not found");
var pollWithAnswer = PollWithUserAnswer.FromPoll(poll);
if (HttpContext.Items["CurrentUser"] is not Account currentUser) return Ok(pollWithAnswer);
var accountId = Guid.Parse(currentUser.Id);
var answer = await polls.GetPollAnswer(id, accountId);
if (answer is not null)
pollWithAnswer.UserAnswer = answer;
return Ok(pollWithAnswer);
}
public class PollAnswerRequest
{
public required Dictionary<string, JsonElement> Answer { get; set; }
}
[HttpPost("{id:guid}/answer")]
[Authorize]
public async Task<ActionResult<PollAnswer>> AnswerPoll(Guid id, [FromBody] PollAnswerRequest request)
{
if (HttpContext.Items["CurrentUser"] is not Account currentUser) return Unauthorized();
var accountId = Guid.Parse(currentUser.Id);
try
{
return await polls.AnswerPoll(id, accountId, request.Answer);
}
catch (Exception ex)
{
return BadRequest(ex.Message);
}
}
[HttpDelete("{id:guid}/answer")]
[Authorize]
public async Task<IActionResult> DeletePollAnswer(Guid id)
{
if (HttpContext.Items["CurrentUser"] is not Account currentUser) return Unauthorized();
var accountId = Guid.Parse(currentUser.Id);
try
{
await polls.UnAnswerPoll(id, accountId);
return NoContent();
}
catch (Exception ex)
{
return BadRequest(ex.Message);
}
}
[HttpGet("me")] [HttpGet("me")]
[Authorize] [Authorize]
public async Task<ActionResult<List<Poll>>> ListPolls( public async Task<ActionResult<List<Poll>>> ListPolls(