72 lines
No EOL
1.8 KiB
C#
72 lines
No EOL
1.8 KiB
C#
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<IActionResult> Get()
|
|
{
|
|
var result = await _surveyService.GetSurveysAsync();
|
|
return Ok(result);
|
|
}
|
|
|
|
[AllowAnonymous]
|
|
[HttpGet("{id}")]
|
|
public async Task<IActionResult> Get(int id)
|
|
{
|
|
var result = await _surveyService.GetSurveyAsync(id);
|
|
return result is not null ? Ok(result) : NotFound();
|
|
}
|
|
|
|
[Authorize]
|
|
[HttpPost]
|
|
public async Task<IActionResult> 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<IActionResult> Delete(int id)
|
|
{
|
|
await _surveyService.DeleteSurveyAsync(id);
|
|
return Ok();
|
|
}
|
|
|
|
[Authorize]
|
|
[HttpGet("my_surveys")]
|
|
public async Task<IActionResult> GetMySurveys()
|
|
{
|
|
var userId = _userContext.UserId;
|
|
var result = await _surveyService.GetSurveysByUserIdAsync(userId);
|
|
return Ok(result);
|
|
}
|
|
} |