less shit code but still not good
This commit is contained in:
parent
ddfb5eff54
commit
9e50b97bc9
6 changed files with 14 additions and 47 deletions
|
|
@ -1,6 +0,0 @@
|
||||||
namespace SurveyBackend.DTOs.Question;
|
|
||||||
|
|
||||||
public class OutputMultipleAnswerQuestionDto : OutputQuestionBaseDto
|
|
||||||
{
|
|
||||||
public List<OutputAnswerVariantDto> AnswerVariants { get; set; } = [];
|
|
||||||
}
|
|
||||||
|
|
@ -1,9 +1,10 @@
|
||||||
namespace SurveyBackend.DTOs.Question;
|
namespace SurveyBackend.DTOs.Question;
|
||||||
|
|
||||||
public abstract class OutputQuestionBaseDto
|
public class OutputQuestionDto
|
||||||
{
|
{
|
||||||
public required int Id { get; set; }
|
public required int Id { get; set; }
|
||||||
public required int SurveyId { get; set; }
|
public required int SurveyId { get; set; }
|
||||||
public required string Title { get; set; }
|
public required string Title { get; set; }
|
||||||
public required string QuestionType { get; set; }
|
public required string QuestionType { get; set; }
|
||||||
|
public List<OutputAnswerVariantDto>? AnswerVariants { get; set; }
|
||||||
}
|
}
|
||||||
|
|
@ -1,6 +0,0 @@
|
||||||
namespace SurveyBackend.DTOs.Question;
|
|
||||||
|
|
||||||
public class OutputSingleAnswerQuestionDto : OutputQuestionBaseDto
|
|
||||||
{
|
|
||||||
public List<OutputAnswerVariantDto> AnswerVariants { get; set; } = [];
|
|
||||||
}
|
|
||||||
|
|
@ -1,6 +0,0 @@
|
||||||
namespace SurveyBackend.DTOs.Question;
|
|
||||||
|
|
||||||
public class OutputTextQuestionDto : OutputQuestionBaseDto
|
|
||||||
{
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
@ -1,38 +1,21 @@
|
||||||
using SurveyBackend.DTOs.Question;
|
using SurveyBackend.DTOs.Question;
|
||||||
using SurveyLib.Core.Models;
|
using SurveyLib.Core.Models;
|
||||||
|
using SurveyLib.Core.Models.QuestionVariants;
|
||||||
|
|
||||||
namespace SurveyBackend.Mappers.QuestionDTOs;
|
namespace SurveyBackend.Mappers.QuestionDTOs;
|
||||||
|
|
||||||
public static class QuestionOutputMapper
|
public static class QuestionOutputMapper
|
||||||
{
|
{
|
||||||
public static object ModelToQuestionDTO(QuestionBase question)
|
public static OutputQuestionDto ModelToQuestionDTO(QuestionBase question)
|
||||||
{
|
{
|
||||||
return question.Discriminator.ToLower() switch
|
var withAnswerVariants = question.GetType() != typeof(TextQuestion);
|
||||||
{
|
return new OutputQuestionDto
|
||||||
"textquestion" => new OutputTextQuestionDto
|
|
||||||
{
|
{
|
||||||
Id = question.Id,
|
Id = question.Id,
|
||||||
Title = question.Title,
|
|
||||||
SurveyId = question.SurveyId,
|
SurveyId = question.SurveyId,
|
||||||
QuestionType = question.Discriminator
|
|
||||||
},
|
|
||||||
"singleanswerquestion" => new OutputSingleAnswerQuestionDto
|
|
||||||
{
|
|
||||||
Id = question.Id,
|
|
||||||
Title = question.Title,
|
Title = question.Title,
|
||||||
SurveyId = question.SurveyId,
|
|
||||||
QuestionType = question.Discriminator,
|
QuestionType = question.Discriminator,
|
||||||
AnswerVariants = GetAnswerVariants(question.AnswerVariants ?? Array.Empty<AnswerVariant>())
|
AnswerVariants = withAnswerVariants ? GetAnswerVariants(question.AnswerVariants) : null,
|
||||||
},
|
|
||||||
"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}")
|
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,3 +1,4 @@
|
||||||
|
using System.Text.Json.Serialization;
|
||||||
using Microsoft.AspNetCore.Authentication.JwtBearer;
|
using Microsoft.AspNetCore.Authentication.JwtBearer;
|
||||||
using Microsoft.EntityFrameworkCore;
|
using Microsoft.EntityFrameworkCore;
|
||||||
using Microsoft.IdentityModel.Tokens;
|
using Microsoft.IdentityModel.Tokens;
|
||||||
|
|
@ -66,7 +67,7 @@ public class Program
|
||||||
};
|
};
|
||||||
});
|
});
|
||||||
|
|
||||||
builder.Services.AddControllers();
|
builder.Services.AddControllers().AddJsonOptions(opts => { opts.JsonSerializerOptions.DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull; });
|
||||||
|
|
||||||
builder.Services.AddEndpointsApiExplorer();
|
builder.Services.AddEndpointsApiExplorer();
|
||||||
builder.Services.AddSwaggerGen(c =>
|
builder.Services.AddSwaggerGen(c =>
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue