diff --git a/SurveyBackend/SurveyBackend.API/Controllers/QuestionController.cs b/SurveyBackend/SurveyBackend.API/Controllers/QuestionController.cs new file mode 100644 index 0000000..64d1971 --- /dev/null +++ b/SurveyBackend/SurveyBackend.API/Controllers/QuestionController.cs @@ -0,0 +1,37 @@ +using Microsoft.AspNetCore.Authorization; +using Microsoft.AspNetCore.Mvc; +using SurveyBackend.Core.Contexts; +using SurveyBackend.DTOs.Question; +using SurveyBackend.Mappers.QuestionDTOs; +using SurveyLib.Core.Services; + +namespace SurveyBackend.Controllers; + +[ApiController] +[Route("api/questions")] +public class QuestionController : ControllerBase +{ + private readonly IQuestionService _questionService; + + public QuestionController(IQuestionService questionService, IUserContext userContext) + { + _questionService = questionService; + } + + [AllowAnonymous] + [HttpGet("by_survey/{id}")] + public async Task GetBySurveyId(int id) + { + var result = await _questionService.GetQuestionsBySurveyIdAsync(id); + return Ok(result); + } + + [Authorize] + [HttpPost] + public async Task AddQuestion(CreateQuestionDTO dto) + { + var model = QuestionCreationMapper.QuestionCreationToModel(dto); + await _questionService.AddQuestionAsync(model); + return Ok(); + } +} \ No newline at end of file diff --git a/SurveyBackend/SurveyBackend.API/DTOs/Question/CreateQuestionDTO.cs b/SurveyBackend/SurveyBackend.API/DTOs/Question/CreateQuestionDTO.cs new file mode 100644 index 0000000..e2846b2 --- /dev/null +++ b/SurveyBackend/SurveyBackend.API/DTOs/Question/CreateQuestionDTO.cs @@ -0,0 +1,10 @@ +namespace SurveyBackend.DTOs.Question; + +public class CreateQuestionDTO +{ + public required string Title { get; set; } + public required int SurveyId { get; set; } + public required string QuestionType { get; set; } + + public List? AnswerVariants { get; set; } +} \ No newline at end of file diff --git a/SurveyBackend/SurveyBackend.API/Mappers/QuestionDTOs/QuestionCreationMapper.cs b/SurveyBackend/SurveyBackend.API/Mappers/QuestionDTOs/QuestionCreationMapper.cs new file mode 100644 index 0000000..f407248 --- /dev/null +++ b/SurveyBackend/SurveyBackend.API/Mappers/QuestionDTOs/QuestionCreationMapper.cs @@ -0,0 +1,34 @@ +using SurveyBackend.DTOs.Question; +using SurveyBackend.Services.Exceptions; +using SurveyLib.Core.Models; +using SurveyLib.Core.Models.QuestionVariants; + +namespace SurveyBackend.Mappers.QuestionDTOs; + +public static class QuestionCreationMapper +{ + public static QuestionBase QuestionCreationToModel(CreateQuestionDTO dto) + { + return dto.QuestionType.ToLower() switch + { + "text" => new TextQuestion + { + Title = dto.Title, + SurveyId = dto.SurveyId, + }, + "single" => new SingleAnswerQuestion + { + Title = dto.Title, + SurveyId = dto.SurveyId, + AnswerVariants = dto.AnswerVariants?.Select(v => new AnswerVariant { Text = v }).ToList() ?? [], + }, + "multiple" => new MultipleAnswerQuestion + { + Title = dto.Title, + SurveyId = dto.SurveyId, + AnswerVariants = dto.AnswerVariants?.Select(v => new AnswerVariant { Text = v }).ToList() ?? [] + }, + _ => throw new BadRequestException("Unknown question type") + }; + } +} \ No newline at end of file diff --git a/SurveyBackend/SurveyBackend.API/Program.cs b/SurveyBackend/SurveyBackend.API/Program.cs index e5d9f51..36198e2 100644 --- a/SurveyBackend/SurveyBackend.API/Program.cs +++ b/SurveyBackend/SurveyBackend.API/Program.cs @@ -45,7 +45,10 @@ public class Program builder.Services.AddScoped(); builder.Services.AddScoped(); + builder.Services.AddScoped(); + builder.Services.AddScoped(); + builder.Services.AddScoped(); builder.Services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme) diff --git a/SurveyBackend/SurveyBackend.Services/Exceptions/BadRequestException.cs b/SurveyBackend/SurveyBackend.Services/Exceptions/BadRequestException.cs new file mode 100644 index 0000000..b80e69a --- /dev/null +++ b/SurveyBackend/SurveyBackend.Services/Exceptions/BadRequestException.cs @@ -0,0 +1,10 @@ +namespace SurveyBackend.Services.Exceptions; + +public class BadRequestException : ServiceException +{ + public override int StatusCode => 400; + + public BadRequestException(string message) : base(message) + { + } +} \ No newline at end of file diff --git a/SurveyBackend/SurveyBackend.Services/Services/QuestionService.cs b/SurveyBackend/SurveyBackend.Services/Services/QuestionService.cs new file mode 100644 index 0000000..ce3e11e --- /dev/null +++ b/SurveyBackend/SurveyBackend.Services/Services/QuestionService.cs @@ -0,0 +1,54 @@ +using SurveyBackend.Core.Contexts; +using SurveyBackend.Services.Exceptions; +using SurveyLib.Core.Models; +using SurveyLib.Core.Repositories; +using SurveyLib.Core.Services; + +namespace SurveyBackend.Services.Services; + +public class QuestionService : IQuestionService +{ + private readonly IQuestionRepository _questionRepository; + + public QuestionService(IQuestionRepository questionRepository, IUserContext userContext) + { + _questionRepository = questionRepository; + } + + public async Task AddQuestionAsync(QuestionBase question) + { + await _questionRepository.AddAsync(question); + } + + public async Task UpdateQuestionAsync(QuestionBase question) + { + await _questionRepository.UpdateAsync(question); + } + + public async Task DeleteQuestionAsync(int id) + { + var question = await _questionRepository.GetByIdAsync(id); + if (question is null) + { + throw new NotFoundException("Question not found"); + } + + await _questionRepository.DeleteAsync(question); + } + + public async Task GetQuestionByIdAsync(int id) + { + var question = await _questionRepository.GetByIdAsync(id); + if (question is null) + { + throw new NotFoundException("Question not found"); + } + + return question; + } + + public async Task> GetQuestionsBySurveyIdAsync(int surveyId) + { + return await _questionRepository.GetQuestionsBySurveyId(surveyId); + } +} \ No newline at end of file diff --git a/SurveyBackend/SurveyBackend.Services/Services/SurveyService.cs b/SurveyBackend/SurveyBackend.Services/Services/SurveyService.cs index 90f51c9..9911697 100644 --- a/SurveyBackend/SurveyBackend.Services/Services/SurveyService.cs +++ b/SurveyBackend/SurveyBackend.Services/Services/SurveyService.cs @@ -42,6 +42,8 @@ public class SurveyService : ISurveyService { throw new UnauthorizedAccessException("You are not authorized to delete this survey."); } + + await _surveyRepository.DeleteAsync(survey); } public async Task> GetSurveysAsync()