life quality changes
- refactored SurveyController.cs - added ProducesResponseType to every endpoint in every controller - remade mappers
This commit is contained in:
parent
9e50b97bc9
commit
6692a91821
11 changed files with 146 additions and 93 deletions
|
|
@ -1,10 +1,9 @@
|
|||
using SurveyBackend.Core.Models;
|
||||
using SurveyBackend.Core.Services;
|
||||
using SurveyBackend.DTOs;
|
||||
|
||||
namespace SurveyBackend.Mappers.UserDTOs;
|
||||
namespace SurveyBackend.Mappers;
|
||||
|
||||
public static class UserRegistrationMapper
|
||||
public static class AuthMapper
|
||||
{
|
||||
public static User UserRegistrationToModel(UserRegistrationDto dto) => new User
|
||||
{
|
||||
|
|
@ -1,34 +0,0 @@
|
|||
using SurveyBackend.DTOs.Question;
|
||||
using SurveyBackend.Services.Exceptions;
|
||||
using SurveyLib.Core.Models;
|
||||
using SurveyLib.Core.Models.QuestionVariants;
|
||||
|
||||
namespace SurveyBackend.Mappers.QuestionDTOs;
|
||||
|
||||
public static class QuestionCreationMapper
|
||||
{
|
||||
public static QuestionBase QuestionCreationToModel(CreateQuestionDto dto)
|
||||
{
|
||||
return dto.QuestionType.ToLower() switch
|
||||
{
|
||||
"textquestion" => new TextQuestion
|
||||
{
|
||||
Title = dto.Title,
|
||||
SurveyId = dto.SurveyId,
|
||||
},
|
||||
"singleanswerquestion" => new SingleAnswerQuestion
|
||||
{
|
||||
Title = dto.Title,
|
||||
SurveyId = dto.SurveyId,
|
||||
AnswerVariants = dto.AnswerVariants?.Select(v => new AnswerVariant { Text = v }).ToList() ?? [],
|
||||
},
|
||||
"multipleanswerquestion" => new MultipleAnswerQuestion
|
||||
{
|
||||
Title = dto.Title,
|
||||
SurveyId = dto.SurveyId,
|
||||
AnswerVariants = dto.AnswerVariants?.Select(v => new AnswerVariant { Text = v }).ToList() ?? []
|
||||
},
|
||||
_ => throw new BadRequestException("Unknown question type")
|
||||
};
|
||||
}
|
||||
}
|
||||
|
|
@ -1,31 +0,0 @@
|
|||
using SurveyBackend.DTOs.Question;
|
||||
using SurveyLib.Core.Models;
|
||||
using SurveyLib.Core.Models.QuestionVariants;
|
||||
|
||||
namespace SurveyBackend.Mappers.QuestionDTOs;
|
||||
|
||||
public static class QuestionOutputMapper
|
||||
{
|
||||
public static OutputQuestionDto ModelToQuestionDTO(QuestionBase question)
|
||||
{
|
||||
var withAnswerVariants = question.GetType() != typeof(TextQuestion);
|
||||
return new OutputQuestionDto
|
||||
{
|
||||
Id = question.Id,
|
||||
SurveyId = question.SurveyId,
|
||||
Title = question.Title,
|
||||
QuestionType = question.Discriminator,
|
||||
AnswerVariants = withAnswerVariants ? GetAnswerVariants(question.AnswerVariants) : null,
|
||||
};
|
||||
}
|
||||
|
||||
private static List<OutputAnswerVariantDto> GetAnswerVariants(IEnumerable<AnswerVariant> answerVariants)
|
||||
{
|
||||
return answerVariants.Select(av => new OutputAnswerVariantDto
|
||||
{
|
||||
Id = av.Id,
|
||||
QuestionId = av.QuestionId,
|
||||
Text = av.Text,
|
||||
}).ToList();
|
||||
}
|
||||
}
|
||||
57
SurveyBackend/SurveyBackend.API/Mappers/QuestionMapper.cs
Normal file
57
SurveyBackend/SurveyBackend.API/Mappers/QuestionMapper.cs
Normal file
|
|
@ -0,0 +1,57 @@
|
|||
using SurveyBackend.DTOs.Question;
|
||||
using SurveyBackend.Services.Exceptions;
|
||||
using SurveyLib.Core.Models;
|
||||
using SurveyLib.Core.Models.QuestionVariants;
|
||||
|
||||
namespace SurveyBackend.Mappers;
|
||||
|
||||
public static class QuestionMapper
|
||||
{
|
||||
public static QuestionBase QuestionCreationToModel(CreateQuestionDto dto, int surveyId)
|
||||
{
|
||||
return dto.QuestionType.ToLower() switch
|
||||
{
|
||||
"textquestion" => new TextQuestion
|
||||
{
|
||||
Title = dto.Title,
|
||||
SurveyId = surveyId,
|
||||
},
|
||||
"singleanswerquestion" => new SingleAnswerQuestion
|
||||
{
|
||||
Title = dto.Title,
|
||||
SurveyId = surveyId,
|
||||
AnswerVariants = dto.AnswerVariants?.Select(v => new AnswerVariant { Text = v }).ToList() ?? [],
|
||||
},
|
||||
"multipleanswerquestion" => new MultipleAnswerQuestion
|
||||
{
|
||||
Title = dto.Title,
|
||||
SurveyId = surveyId,
|
||||
AnswerVariants = dto.AnswerVariants?.Select(v => new AnswerVariant { Text = v }).ToList() ?? []
|
||||
},
|
||||
_ => throw new BadRequestException("Unknown question type")
|
||||
};
|
||||
}
|
||||
|
||||
public static OutputQuestionDto ModelToQuestionDto(QuestionBase question)
|
||||
{
|
||||
var withAnswerVariants = question.GetType() != typeof(TextQuestion);
|
||||
return new OutputQuestionDto
|
||||
{
|
||||
Id = question.Id,
|
||||
SurveyId = question.SurveyId,
|
||||
Title = question.Title,
|
||||
QuestionType = question.Discriminator,
|
||||
AnswerVariants = withAnswerVariants ? AnswerVariantsToDto(question.AnswerVariants) : null,
|
||||
};
|
||||
}
|
||||
|
||||
private static List<OutputAnswerVariantDto> AnswerVariantsToDto(IEnumerable<AnswerVariant> answerVariants)
|
||||
{
|
||||
return answerVariants.Select(av => new OutputAnswerVariantDto
|
||||
{
|
||||
Id = av.Id,
|
||||
QuestionId = av.QuestionId,
|
||||
Text = av.Text,
|
||||
}).ToList();
|
||||
}
|
||||
}
|
||||
28
SurveyBackend/SurveyBackend.API/Mappers/SurveyMapper.cs
Normal file
28
SurveyBackend/SurveyBackend.API/Mappers/SurveyMapper.cs
Normal file
|
|
@ -0,0 +1,28 @@
|
|||
using SurveyBackend.DTOs.Survey;
|
||||
using SurveyLib.Core.Models;
|
||||
|
||||
namespace SurveyBackend.Mappers;
|
||||
|
||||
public static class SurveyMapper
|
||||
{
|
||||
public static Survey CreateDtoToModel(CreateSurveyDto dto, int userId)
|
||||
{
|
||||
return new Survey
|
||||
{
|
||||
Title = dto.Title,
|
||||
Description = dto.Description,
|
||||
CreatedBy = userId
|
||||
};
|
||||
}
|
||||
|
||||
public static OutputSurveyDto ModelToOutputDto(Survey survey)
|
||||
{
|
||||
return new OutputSurveyDto
|
||||
{
|
||||
Id = survey.Id,
|
||||
Title = survey.Title,
|
||||
Description = survey.Description,
|
||||
CreatedBy = survey.CreatedBy,
|
||||
};
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue