survey-webapp/SurveyBackend/SurveyBackend.API/Controllers/QuestionController.cs
2025-04-21 04:15:40 +05:00

42 lines
No EOL
1.4 KiB
C#

using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using SurveyBackend.Core.Contexts;
using SurveyBackend.DTOs.Question;
using SurveyBackend.Mappers;
using SurveyLib.Core.Services;
namespace SurveyBackend.Controllers;
[ApiController]
[Route("api/surveys/{surveyId}/questions")]
public class QuestionController : ControllerBase
{
private readonly IQuestionService _questionService;
public QuestionController(IQuestionService questionService, IUserContext userContext)
{
_questionService = questionService;
}
[AllowAnonymous]
[HttpGet]
[ProducesResponseType(StatusCodes.Status404NotFound)]
[ProducesResponseType(typeof(List<OutputQuestionDto>), StatusCodes.Status200OK)]
public async Task<IActionResult> GetBySurveyId([FromRoute] int surveyId)
{
var questions = await _questionService.GetQuestionsBySurveyIdAsync(surveyId);
var result = questions.Select(QuestionMapper.ModelToQuestionDto).ToList();
return Ok(result);
}
[Authorize]
[HttpPost]
[ProducesResponseType(StatusCodes.Status404NotFound)]
[ProducesResponseType(StatusCodes.Status201Created)]
public async Task<IActionResult> AddQuestion(CreateQuestionDto dto, [FromRoute] int surveyId)
{
var model = QuestionMapper.QuestionCreationToModel(dto, surveyId);
await _questionService.AddQuestionAsync(model);
return Created();
}
}