using System.Security.Claims; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Mvc; using SurveyBackend.Core.Contexts; using SurveyBackend.DTOs.Survey; using SurveyLib.Core.Models; using SurveyLib.Core.Services; namespace SurveyBackend.Controllers; [ApiController] [Route("api/surveys")] public class SurveyController : ControllerBase { private readonly ISurveyService _surveyService; private readonly IUserContext _userContext; public SurveyController(ISurveyService surveyService, IUserContext userContext) { _surveyService = surveyService; _userContext = userContext; } [AllowAnonymous] [HttpGet] public async Task Get() { var result = await _surveyService.GetSurveysAsync(); return Ok(result); } [AllowAnonymous] [HttpGet("{id}")] public async Task Get(int id) { var result = await _surveyService.GetSurveyAsync(id); return result is not null ? Ok(result) : NotFound(); } [Authorize] [HttpPost] public async Task Post([FromBody] CreateSurveyDTO dto) { var userId = _userContext.UserId; var survey = new Survey { Title = dto.Title, Description = dto.Description, CreatedBy = userId, }; await _surveyService.AddSurveyAsync(survey); return Ok(); } [Authorize] [HttpDelete("{id}")] public async Task Delete(int id) { await _surveyService.DeleteSurveyAsync(id); return Ok(); } }