added questions output (SHIT CODE)
This commit is contained in:
parent
7d76a9f67c
commit
ddfb5eff54
11 changed files with 93 additions and 9 deletions
|
|
@ -22,13 +22,14 @@ public class QuestionController : ControllerBase
|
||||||
[HttpGet("by_survey/{id}")]
|
[HttpGet("by_survey/{id}")]
|
||||||
public async Task<IActionResult> GetBySurveyId(int id)
|
public async Task<IActionResult> 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);
|
return Ok(result);
|
||||||
}
|
}
|
||||||
|
|
||||||
[Authorize]
|
[Authorize]
|
||||||
[HttpPost]
|
[HttpPost]
|
||||||
public async Task<IActionResult> AddQuestion(CreateQuestionDTO dto)
|
public async Task<IActionResult> AddQuestion(CreateQuestionDto dto)
|
||||||
{
|
{
|
||||||
var model = QuestionCreationMapper.QuestionCreationToModel(dto);
|
var model = QuestionCreationMapper.QuestionCreationToModel(dto);
|
||||||
await _questionService.AddQuestionAsync(model);
|
await _questionService.AddQuestionAsync(model);
|
||||||
|
|
|
||||||
|
|
@ -39,7 +39,7 @@ public class SurveyController : ControllerBase
|
||||||
|
|
||||||
[Authorize]
|
[Authorize]
|
||||||
[HttpPost]
|
[HttpPost]
|
||||||
public async Task<IActionResult> Post([FromBody] CreateSurveyDTO dto)
|
public async Task<IActionResult> Post([FromBody] CreateSurveyDto dto)
|
||||||
{
|
{
|
||||||
var userId = _userContext.UserId;
|
var userId = _userContext.UserId;
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,6 @@
|
||||||
namespace SurveyBackend.DTOs.Question;
|
namespace SurveyBackend.DTOs.Question;
|
||||||
|
|
||||||
public class CreateQuestionDTO
|
public class CreateQuestionDto
|
||||||
{
|
{
|
||||||
public required string Title { get; set; }
|
public required string Title { get; set; }
|
||||||
public required int SurveyId { get; set; }
|
public required int SurveyId { get; set; }
|
||||||
|
|
|
||||||
|
|
@ -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; }
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,6 @@
|
||||||
|
namespace SurveyBackend.DTOs.Question;
|
||||||
|
|
||||||
|
public class OutputMultipleAnswerQuestionDto : OutputQuestionBaseDto
|
||||||
|
{
|
||||||
|
public List<OutputAnswerVariantDto> AnswerVariants { get; set; } = [];
|
||||||
|
}
|
||||||
|
|
@ -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; }
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,6 @@
|
||||||
|
namespace SurveyBackend.DTOs.Question;
|
||||||
|
|
||||||
|
public class OutputSingleAnswerQuestionDto : OutputQuestionBaseDto
|
||||||
|
{
|
||||||
|
public List<OutputAnswerVariantDto> AnswerVariants { get; set; } = [];
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,6 @@
|
||||||
|
namespace SurveyBackend.DTOs.Question;
|
||||||
|
|
||||||
|
public class OutputTextQuestionDto : OutputQuestionBaseDto
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
||||||
|
|
@ -1,6 +1,6 @@
|
||||||
namespace SurveyBackend.DTOs.Survey;
|
namespace SurveyBackend.DTOs.Survey;
|
||||||
|
|
||||||
public class CreateSurveyDTO
|
public class CreateSurveyDto
|
||||||
{
|
{
|
||||||
public required string Title { get; set; }
|
public required string Title { get; set; }
|
||||||
public string Description { get; set; } = string.Empty;
|
public string Description { get; set; } = string.Empty;
|
||||||
|
|
|
||||||
|
|
@ -7,22 +7,22 @@ namespace SurveyBackend.Mappers.QuestionDTOs;
|
||||||
|
|
||||||
public static class QuestionCreationMapper
|
public static class QuestionCreationMapper
|
||||||
{
|
{
|
||||||
public static QuestionBase QuestionCreationToModel(CreateQuestionDTO dto)
|
public static QuestionBase QuestionCreationToModel(CreateQuestionDto dto)
|
||||||
{
|
{
|
||||||
return dto.QuestionType.ToLower() switch
|
return dto.QuestionType.ToLower() switch
|
||||||
{
|
{
|
||||||
"text" => new TextQuestion
|
"textquestion" => new TextQuestion
|
||||||
{
|
{
|
||||||
Title = dto.Title,
|
Title = dto.Title,
|
||||||
SurveyId = dto.SurveyId,
|
SurveyId = dto.SurveyId,
|
||||||
},
|
},
|
||||||
"single" => new SingleAnswerQuestion
|
"singleanswerquestion" => new SingleAnswerQuestion
|
||||||
{
|
{
|
||||||
Title = dto.Title,
|
Title = dto.Title,
|
||||||
SurveyId = dto.SurveyId,
|
SurveyId = dto.SurveyId,
|
||||||
AnswerVariants = dto.AnswerVariants?.Select(v => new AnswerVariant { Text = v }).ToList() ?? [],
|
AnswerVariants = dto.AnswerVariants?.Select(v => new AnswerVariant { Text = v }).ToList() ?? [],
|
||||||
},
|
},
|
||||||
"multiple" => new MultipleAnswerQuestion
|
"multipleanswerquestion" => new MultipleAnswerQuestion
|
||||||
{
|
{
|
||||||
Title = dto.Title,
|
Title = dto.Title,
|
||||||
SurveyId = dto.SurveyId,
|
SurveyId = dto.SurveyId,
|
||||||
|
|
|
||||||
|
|
@ -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<AnswerVariant>())
|
||||||
|
},
|
||||||
|
"multipleanswerquestion" => new OutputMultipleAnswerQuestionDto
|
||||||
|
{
|
||||||
|
Id = question.Id,
|
||||||
|
Title = question.Title,
|
||||||
|
SurveyId = question.SurveyId,
|
||||||
|
QuestionType = question.Discriminator,
|
||||||
|
AnswerVariants = GetAnswerVariants(question.AnswerVariants ?? Array.Empty<AnswerVariant>())
|
||||||
|
},
|
||||||
|
_ => throw new Exception($"Unknown question type: {question.Discriminator}")
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
private static List<OutputAnswerVariantDto> GetAnswerVariants(IEnumerable<AnswerVariant> answerVariants)
|
||||||
|
{
|
||||||
|
return answerVariants.Select(av => new OutputAnswerVariantDto
|
||||||
|
{
|
||||||
|
Id = av.Id,
|
||||||
|
QuestionId = av.QuestionId,
|
||||||
|
Text = av.Text,
|
||||||
|
}).ToList();
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Add table
Add a link
Reference in a new issue