survey-webapp/SurveyBackend/SurveyBackend.API/Controllers/SurveyController.cs

52 lines
No EOL
1.3 KiB
C#

using Microsoft.AspNetCore.Mvc;
using SurveyBackend.DTOs.Survey;
using SurveyLib.Core.Models;
using SurveyLib.Core.Services;
using SurveyLib.Infrastructure.EFCore.Services;
namespace SurveyBackend.Controllers;
[ApiController]
[Route("api/surveys")]
public class SurveyController : ControllerBase
{
private readonly ISurveyService _surveyService;
public SurveyController(ISurveyService surveyService)
{
_surveyService = surveyService;
}
[HttpGet]
public async Task<IActionResult> Get()
{
var result = await _surveyService.GetSurveysAsync();
return Ok(result);
}
[HttpGet("{id}")]
public async Task<IActionResult> Get(int id)
{
var result = await _surveyService.GetSurveyAsync(id);
return result is not null ? Ok(result) : NotFound();
}
[HttpPost]
public async Task<IActionResult> Post([FromBody] CreateSurveyDTO dto)
{
var survey = new Survey
{
Title = dto.Title,
Description = dto.Description,
};
await _surveyService.AddSurveyAsync(survey);
return Ok();
}
[HttpDelete("{id}")]
public async Task<IActionResult> Delete(int id)
{
await _surveyService.DeleteSurveyAsync(id);
return Ok();
}
}