going to question management
This commit is contained in:
parent
00e93fde2e
commit
585d6ac6e3
7 changed files with 150 additions and 0 deletions
|
|
@ -0,0 +1,37 @@
|
|||
using Microsoft.AspNetCore.Authorization;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using SurveyBackend.Core.Contexts;
|
||||
using SurveyBackend.DTOs.Question;
|
||||
using SurveyBackend.Mappers.QuestionDTOs;
|
||||
using SurveyLib.Core.Services;
|
||||
|
||||
namespace SurveyBackend.Controllers;
|
||||
|
||||
[ApiController]
|
||||
[Route("api/questions")]
|
||||
public class QuestionController : ControllerBase
|
||||
{
|
||||
private readonly IQuestionService _questionService;
|
||||
|
||||
public QuestionController(IQuestionService questionService, IUserContext userContext)
|
||||
{
|
||||
_questionService = questionService;
|
||||
}
|
||||
|
||||
[AllowAnonymous]
|
||||
[HttpGet("by_survey/{id}")]
|
||||
public async Task<IActionResult> GetBySurveyId(int id)
|
||||
{
|
||||
var result = await _questionService.GetQuestionsBySurveyIdAsync(id);
|
||||
return Ok(result);
|
||||
}
|
||||
|
||||
[Authorize]
|
||||
[HttpPost]
|
||||
public async Task<IActionResult> AddQuestion(CreateQuestionDTO dto)
|
||||
{
|
||||
var model = QuestionCreationMapper.QuestionCreationToModel(dto);
|
||||
await _questionService.AddQuestionAsync(model);
|
||||
return Ok();
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,10 @@
|
|||
namespace SurveyBackend.DTOs.Question;
|
||||
|
||||
public class CreateQuestionDTO
|
||||
{
|
||||
public required string Title { get; set; }
|
||||
public required int SurveyId { get; set; }
|
||||
public required string QuestionType { get; set; }
|
||||
|
||||
public List<string>? AnswerVariants { get; set; }
|
||||
}
|
||||
|
|
@ -0,0 +1,34 @@
|
|||
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
|
||||
{
|
||||
"text" => new TextQuestion
|
||||
{
|
||||
Title = dto.Title,
|
||||
SurveyId = dto.SurveyId,
|
||||
},
|
||||
"single" => new SingleAnswerQuestion
|
||||
{
|
||||
Title = dto.Title,
|
||||
SurveyId = dto.SurveyId,
|
||||
AnswerVariants = dto.AnswerVariants?.Select(v => new AnswerVariant { Text = v }).ToList() ?? [],
|
||||
},
|
||||
"multiple" => new MultipleAnswerQuestion
|
||||
{
|
||||
Title = dto.Title,
|
||||
SurveyId = dto.SurveyId,
|
||||
AnswerVariants = dto.AnswerVariants?.Select(v => new AnswerVariant { Text = v }).ToList() ?? []
|
||||
},
|
||||
_ => throw new BadRequestException("Unknown question type")
|
||||
};
|
||||
}
|
||||
}
|
||||
|
|
@ -45,7 +45,10 @@ public class Program
|
|||
builder.Services.AddScoped<IAuthorizationService, AuthorizationService>();
|
||||
|
||||
builder.Services.AddScoped<ISurveyRepository, SurveyRepository>();
|
||||
builder.Services.AddScoped<IQuestionRepository, QuestionRepository>();
|
||||
|
||||
builder.Services.AddScoped<ISurveyService, SurveyService>();
|
||||
builder.Services.AddScoped<IQuestionService, QuestionService>();
|
||||
|
||||
|
||||
builder.Services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue