diff --git a/SurveyBackend/SurveyBackend.API/Controllers/QuestionController.cs b/SurveyBackend/SurveyBackend.API/Controllers/QuestionController.cs index 64d1971..0a522c5 100644 --- a/SurveyBackend/SurveyBackend.API/Controllers/QuestionController.cs +++ b/SurveyBackend/SurveyBackend.API/Controllers/QuestionController.cs @@ -22,13 +22,14 @@ public class QuestionController : ControllerBase [HttpGet("by_survey/{id}")] public async Task GetBySurveyId(int id) { - var result = await _questionService.GetQuestionsBySurveyIdAsync(id); + var questions = await _questionService.GetQuestionsBySurveyIdAsync(id); + var result = questions.Select(QuestionOutputMapper.ModelToQuestionDTO).ToList(); return Ok(result); } [Authorize] [HttpPost] - public async Task AddQuestion(CreateQuestionDTO dto) + public async Task AddQuestion(CreateQuestionDto dto) { var model = QuestionCreationMapper.QuestionCreationToModel(dto); await _questionService.AddQuestionAsync(model); diff --git a/SurveyBackend/SurveyBackend.API/Controllers/SurveyController.cs b/SurveyBackend/SurveyBackend.API/Controllers/SurveyController.cs index 938b2b7..7b2379e 100644 --- a/SurveyBackend/SurveyBackend.API/Controllers/SurveyController.cs +++ b/SurveyBackend/SurveyBackend.API/Controllers/SurveyController.cs @@ -39,7 +39,7 @@ public class SurveyController : ControllerBase [Authorize] [HttpPost] - public async Task Post([FromBody] CreateSurveyDTO dto) + public async Task Post([FromBody] CreateSurveyDto dto) { var userId = _userContext.UserId; diff --git a/SurveyBackend/SurveyBackend.API/DTOs/Question/CreateQuestionDTO.cs b/SurveyBackend/SurveyBackend.API/DTOs/Question/CreateQuestionDTO.cs index e2846b2..e295fc2 100644 --- a/SurveyBackend/SurveyBackend.API/DTOs/Question/CreateQuestionDTO.cs +++ b/SurveyBackend/SurveyBackend.API/DTOs/Question/CreateQuestionDTO.cs @@ -1,6 +1,6 @@ namespace SurveyBackend.DTOs.Question; -public class CreateQuestionDTO +public class CreateQuestionDto { public required string Title { get; set; } public required int SurveyId { get; set; } diff --git a/SurveyBackend/SurveyBackend.API/DTOs/Question/OutputAnswerVariantDTO.cs b/SurveyBackend/SurveyBackend.API/DTOs/Question/OutputAnswerVariantDTO.cs new file mode 100644 index 0000000..9112795 --- /dev/null +++ b/SurveyBackend/SurveyBackend.API/DTOs/Question/OutputAnswerVariantDTO.cs @@ -0,0 +1,8 @@ +namespace SurveyBackend.DTOs.Question; + +public class OutputAnswerVariantDto +{ + public required int Id { get; set; } + public required int QuestionId { get; set; } + public required string Text { get; set; } +} \ No newline at end of file diff --git a/SurveyBackend/SurveyBackend.API/DTOs/Question/OutputMultipleAnswerQuestionDTO.cs b/SurveyBackend/SurveyBackend.API/DTOs/Question/OutputMultipleAnswerQuestionDTO.cs new file mode 100644 index 0000000..606c9a0 --- /dev/null +++ b/SurveyBackend/SurveyBackend.API/DTOs/Question/OutputMultipleAnswerQuestionDTO.cs @@ -0,0 +1,6 @@ +namespace SurveyBackend.DTOs.Question; + +public class OutputMultipleAnswerQuestionDto : OutputQuestionBaseDto +{ + public List AnswerVariants { get; set; } = []; +} \ No newline at end of file diff --git a/SurveyBackend/SurveyBackend.API/DTOs/Question/OutputQuestionBaseDTO.cs b/SurveyBackend/SurveyBackend.API/DTOs/Question/OutputQuestionBaseDTO.cs new file mode 100644 index 0000000..b522001 --- /dev/null +++ b/SurveyBackend/SurveyBackend.API/DTOs/Question/OutputQuestionBaseDTO.cs @@ -0,0 +1,9 @@ +namespace SurveyBackend.DTOs.Question; + +public abstract class OutputQuestionBaseDto +{ + public required int Id { get; set; } + public required int SurveyId { get; set; } + public required string Title { get; set; } + public required string QuestionType { get; set; } +} \ No newline at end of file diff --git a/SurveyBackend/SurveyBackend.API/DTOs/Question/OutputSingleAnswerQuestionDTO.cs b/SurveyBackend/SurveyBackend.API/DTOs/Question/OutputSingleAnswerQuestionDTO.cs new file mode 100644 index 0000000..2084f10 --- /dev/null +++ b/SurveyBackend/SurveyBackend.API/DTOs/Question/OutputSingleAnswerQuestionDTO.cs @@ -0,0 +1,6 @@ +namespace SurveyBackend.DTOs.Question; + +public class OutputSingleAnswerQuestionDto : OutputQuestionBaseDto +{ + public List AnswerVariants { get; set; } = []; +} \ No newline at end of file diff --git a/SurveyBackend/SurveyBackend.API/DTOs/Question/OutputTextQuestionDTO.cs b/SurveyBackend/SurveyBackend.API/DTOs/Question/OutputTextQuestionDTO.cs new file mode 100644 index 0000000..d955b8d --- /dev/null +++ b/SurveyBackend/SurveyBackend.API/DTOs/Question/OutputTextQuestionDTO.cs @@ -0,0 +1,6 @@ +namespace SurveyBackend.DTOs.Question; + +public class OutputTextQuestionDto : OutputQuestionBaseDto +{ + +} \ No newline at end of file diff --git a/SurveyBackend/SurveyBackend.API/DTOs/Survey/CreateSurveyDTO.cs b/SurveyBackend/SurveyBackend.API/DTOs/Survey/CreateSurveyDTO.cs index 5b25182..67bfaf6 100644 --- a/SurveyBackend/SurveyBackend.API/DTOs/Survey/CreateSurveyDTO.cs +++ b/SurveyBackend/SurveyBackend.API/DTOs/Survey/CreateSurveyDTO.cs @@ -1,6 +1,6 @@ namespace SurveyBackend.DTOs.Survey; -public class CreateSurveyDTO +public class CreateSurveyDto { public required string Title { get; set; } public string Description { get; set; } = string.Empty; diff --git a/SurveyBackend/SurveyBackend.API/Mappers/QuestionDTOs/QuestionCreationMapper.cs b/SurveyBackend/SurveyBackend.API/Mappers/QuestionDTOs/QuestionCreationMapper.cs index f407248..aeacd4f 100644 --- a/SurveyBackend/SurveyBackend.API/Mappers/QuestionDTOs/QuestionCreationMapper.cs +++ b/SurveyBackend/SurveyBackend.API/Mappers/QuestionDTOs/QuestionCreationMapper.cs @@ -7,22 +7,22 @@ namespace SurveyBackend.Mappers.QuestionDTOs; public static class QuestionCreationMapper { - public static QuestionBase QuestionCreationToModel(CreateQuestionDTO dto) + public static QuestionBase QuestionCreationToModel(CreateQuestionDto dto) { return dto.QuestionType.ToLower() switch { - "text" => new TextQuestion + "textquestion" => new TextQuestion { Title = dto.Title, SurveyId = dto.SurveyId, }, - "single" => new SingleAnswerQuestion + "singleanswerquestion" => new SingleAnswerQuestion { Title = dto.Title, SurveyId = dto.SurveyId, AnswerVariants = dto.AnswerVariants?.Select(v => new AnswerVariant { Text = v }).ToList() ?? [], }, - "multiple" => new MultipleAnswerQuestion + "multipleanswerquestion" => new MultipleAnswerQuestion { Title = dto.Title, SurveyId = dto.SurveyId, diff --git a/SurveyBackend/SurveyBackend.API/Mappers/QuestionDTOs/QuestionOutputMapper.cs b/SurveyBackend/SurveyBackend.API/Mappers/QuestionDTOs/QuestionOutputMapper.cs new file mode 100644 index 0000000..2f7a205 --- /dev/null +++ b/SurveyBackend/SurveyBackend.API/Mappers/QuestionDTOs/QuestionOutputMapper.cs @@ -0,0 +1,48 @@ +using SurveyBackend.DTOs.Question; +using SurveyLib.Core.Models; + +namespace SurveyBackend.Mappers.QuestionDTOs; + +public static class QuestionOutputMapper +{ + public static object ModelToQuestionDTO(QuestionBase question) + { + return question.Discriminator.ToLower() switch + { + "textquestion" => new OutputTextQuestionDto + { + Id = question.Id, + Title = question.Title, + SurveyId = question.SurveyId, + QuestionType = question.Discriminator + }, + "singleanswerquestion" => new OutputSingleAnswerQuestionDto + { + Id = question.Id, + Title = question.Title, + SurveyId = question.SurveyId, + QuestionType = question.Discriminator, + AnswerVariants = GetAnswerVariants(question.AnswerVariants ?? Array.Empty()) + }, + "multipleanswerquestion" => new OutputMultipleAnswerQuestionDto + { + Id = question.Id, + Title = question.Title, + SurveyId = question.SurveyId, + QuestionType = question.Discriminator, + AnswerVariants = GetAnswerVariants(question.AnswerVariants ?? Array.Empty()) + }, + _ => throw new Exception($"Unknown question type: {question.Discriminator}") + }; + } + + private static List GetAnswerVariants(IEnumerable answerVariants) + { + return answerVariants.Select(av => new OutputAnswerVariantDto + { + Id = av.Id, + QuestionId = av.QuestionId, + Text = av.Text, + }).ToList(); + } +} \ No newline at end of file