♻️ Finish centerlizing the data models

This commit is contained in:
2025-09-27 15:14:05 +08:00
parent e70d8371f8
commit 9ce31c4dd8
167 changed files with 780 additions and 42880 deletions

View File

@@ -1,6 +1,5 @@
using System.ComponentModel.DataAnnotations;
using System.Text.Json;
using DysonNetwork.Shared.Data;
using DysonNetwork.Shared.Models;
using DysonNetwork.Shared.Proto;
using DysonNetwork.Shared.Registry;
@@ -127,7 +126,7 @@ public class PollController(
[HttpGet("me")]
[Authorize]
public async Task<ActionResult<List<Poll>>> ListPolls(
public async Task<ActionResult<List<SnPoll>>> ListPolls(
[FromQuery(Name = "pub")] string? pubName,
[FromQuery] bool active = false,
[FromQuery] int offset = 0,
@@ -199,7 +198,7 @@ public class PollController(
[HttpPost]
[Authorize]
public async Task<ActionResult<Poll>> CreatePoll([FromBody] PollRequest request,
public async Task<ActionResult<SnPoll>> CreatePoll([FromBody] PollRequest request,
[FromQuery(Name = "pub")] string pubName)
{
if (request.Questions is null) return BadRequest("Questions are required.");
@@ -211,7 +210,7 @@ public class PollController(
if (!await pub.IsMemberWithRole(publisher.Id, accountId, Shared.Models.PublisherMemberRole.Editor))
return StatusCode(403, "You need at least be an editor to create polls as this publisher.");
var poll = new Poll
var poll = new SnPoll
{
Title = request.Title,
Description = request.Description,
@@ -237,7 +236,7 @@ public class PollController(
[HttpPatch("{id:guid}")]
[Authorize]
public async Task<ActionResult<Poll>> UpdatePoll(Guid id, [FromBody] PollRequest request)
public async Task<ActionResult<SnPoll>> UpdatePoll(Guid id, [FromBody] PollRequest request)
{
if (HttpContext.Items["CurrentUser"] is not Account currentUser) return Unauthorized();
var accountId = Guid.Parse(currentUser.Id);

View File

@@ -3,12 +3,12 @@ using DysonNetwork.Sphere.WebReader;
namespace DysonNetwork.Sphere.Poll;
public class PollWithStats : Poll
public class PollWithStats : SnPoll
{
public SnPollAnswer? UserAnswer { get; set; }
public Dictionary<Guid, Dictionary<string, int>> Stats { get; set; } = new(); // question id -> (option id -> count)
public static PollWithStats FromPoll(Poll poll, SnPollAnswer? userAnswer = null)
public static PollWithStats FromPoll(SnPoll poll, SnPollAnswer? userAnswer = null)
{
return new PollWithStats
{

View File

@@ -8,7 +8,7 @@ namespace DysonNetwork.Sphere.Poll;
public class PollService(AppDatabase db, ICacheService cache)
{
public void ValidatePoll(Poll poll)
public void ValidatePoll(SnPoll poll)
{
if (poll.Questions.Count == 0)
throw new Exception("Poll must have at least one question");
@@ -32,7 +32,7 @@ public class PollService(AppDatabase db, ICacheService cache)
}
}
public async Task<Poll?> GetPoll(Guid id)
public async Task<SnPoll?> GetPoll(Guid id)
{
var poll = await db.Polls
.Where(e => e.Id == id)
@@ -307,9 +307,7 @@ public class PollService(AppDatabase db, ICacheService cache)
public async Task<PollEmbed> LoadPollEmbed(Guid pollId, Guid? accountId)
{
var poll = await GetPoll(pollId);
if (poll is null)
throw new Exception("Poll not found");
var poll = await GetPoll(pollId) ?? throw new Exception("Poll not found");
var pollWithStats = PollWithStats.FromPoll(poll);
pollWithStats.Stats = await GetPollStats(poll.Id);
if (accountId is not null)