🐛 Trying to fix validation issue in poll
This commit is contained in:
@@ -1,3 +1,4 @@
|
|||||||
|
using System.ComponentModel.DataAnnotations;
|
||||||
using System.Text.Json;
|
using System.Text.Json;
|
||||||
using DysonNetwork.Shared.Proto;
|
using DysonNetwork.Shared.Proto;
|
||||||
using DysonNetwork.Sphere.Publisher;
|
using DysonNetwork.Sphere.Publisher;
|
||||||
@@ -102,12 +103,37 @@ public class PollController(AppDatabase db, PollService polls, PublisherService
|
|||||||
public string? Title { get; set; }
|
public string? Title { get; set; }
|
||||||
public string? Description { get; set; }
|
public string? Description { get; set; }
|
||||||
public Instant? EndedAt { get; set; }
|
public Instant? EndedAt { get; set; }
|
||||||
public List<PollQuestion>? Questions { get; set; }
|
public List<PollRequestQuestion>? Questions { get; set; }
|
||||||
|
}
|
||||||
|
|
||||||
|
public class PollRequestQuestion
|
||||||
|
{
|
||||||
|
public Guid Id { get; set; } = Guid.NewGuid();
|
||||||
|
|
||||||
|
public PollQuestionType Type { get; set; }
|
||||||
|
public List<PollOption>? Options { get; set; }
|
||||||
|
|
||||||
|
[MaxLength(1024)] public string Title { get; set; } = null!;
|
||||||
|
[MaxLength(4096)] public string? Description { get; set; }
|
||||||
|
public int Order { get; set; } = 0;
|
||||||
|
public bool IsRequired { get; set; }
|
||||||
|
|
||||||
|
public PollQuestion ToQuestion() => new()
|
||||||
|
{
|
||||||
|
Id = Id,
|
||||||
|
Type = Type,
|
||||||
|
Options = Options,
|
||||||
|
Title = Title,
|
||||||
|
Description = Description,
|
||||||
|
Order = Order,
|
||||||
|
IsRequired = IsRequired
|
||||||
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
[HttpPost]
|
[HttpPost]
|
||||||
[Authorize]
|
[Authorize]
|
||||||
public async Task<ActionResult<Poll>> CreatePoll([FromBody] PollRequest request, [FromQuery(Name = "pub")] string pubName)
|
public async Task<ActionResult<Poll>> CreatePoll([FromBody] PollRequest request,
|
||||||
|
[FromQuery(Name = "pub")] string pubName)
|
||||||
{
|
{
|
||||||
if (request.Questions is null) return BadRequest("Questions are required.");
|
if (request.Questions is null) return BadRequest("Questions are required.");
|
||||||
if (HttpContext.Items["CurrentUser"] is not Account currentUser) return Unauthorized();
|
if (HttpContext.Items["CurrentUser"] is not Account currentUser) return Unauthorized();
|
||||||
@@ -124,7 +150,7 @@ public class PollController(AppDatabase db, PollService polls, PublisherService
|
|||||||
Description = request.Description,
|
Description = request.Description,
|
||||||
EndedAt = request.EndedAt,
|
EndedAt = request.EndedAt,
|
||||||
PublisherId = publisher.Id,
|
PublisherId = publisher.Id,
|
||||||
Questions = request.Questions
|
Questions = request.Questions.Select(q => q.ToQuestion()).ToList()
|
||||||
};
|
};
|
||||||
|
|
||||||
try
|
try
|
||||||
@@ -173,9 +199,8 @@ public class PollController(AppDatabase db, PollService polls, PublisherService
|
|||||||
{
|
{
|
||||||
// Remove existing questions
|
// Remove existing questions
|
||||||
db.PollQuestions.RemoveRange(poll.Questions);
|
db.PollQuestions.RemoveRange(poll.Questions);
|
||||||
|
|
||||||
// Add new questions
|
// Add new questions
|
||||||
poll.Questions = request.Questions;
|
poll.Questions = request.Questions.Select(q => q.ToQuestion()).ToList();
|
||||||
}
|
}
|
||||||
|
|
||||||
polls.ValidatePoll(poll);
|
polls.ValidatePoll(poll);
|
||||||
|
Reference in New Issue
Block a user