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), StatusCodes.Status200OK)] public async Task 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 AddQuestion(CreateQuestionDto dto, [FromRoute] int surveyId) { var model = QuestionMapper.QuestionCreationToModel(dto, surveyId); await _questionService.AddQuestionAsync(model); return Created(); } }