add AnswerVariant
This commit is contained in:
parent
8a80013c94
commit
ae43e8c74c
10 changed files with 139 additions and 5 deletions
|
|
@ -0,0 +1,84 @@
|
|||
using Microsoft.AspNetCore.Authorization;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using SurveyBackend.DTOs.AnswerVariant;
|
||||
using SurveyBackend.Mappers;
|
||||
using SurveyLib.Core.Services;
|
||||
|
||||
namespace SurveyBackend.Controllers;
|
||||
|
||||
[ApiController]
|
||||
[Route("api/surveys/{surveyId}/questions/{questionId}/answerVariants")]
|
||||
public class AnswerVariantsController : ControllerBase
|
||||
{
|
||||
private readonly IAnswerVariantsService _answerVariantsService;
|
||||
|
||||
public AnswerVariantsController(IAnswerVariantsService answerVariantsService)
|
||||
{
|
||||
_answerVariantsService = answerVariantsService;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Получить варианты ответа на вопрос
|
||||
/// </summary>
|
||||
/// <param name="surveyId"></param>
|
||||
/// <param name="questionId"></param>
|
||||
/// <returns></returns>
|
||||
[AllowAnonymous]
|
||||
[HttpGet]
|
||||
public async Task<IActionResult> Get(int surveyId, int questionId)
|
||||
{
|
||||
var answerVariants = await _answerVariantsService.GetAnswerVariantsByQuestionIdAsync(questionId);
|
||||
var result = answerVariants.Select(AnswerVariantMapper.ModelToOutputDto);
|
||||
return Ok(result);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Добавить новый вариант ответа для вопроса
|
||||
/// </summary>
|
||||
/// <param name="surveyId">Идентификатор опроса</param>
|
||||
/// <param name="questionId">Идентификатор вопроса</param>
|
||||
/// <param name="dto">Объект с данными для создания варианта ответа</param>
|
||||
/// <returns>Результат операции добавления</returns>
|
||||
[Authorize]
|
||||
[HttpPost]
|
||||
public async Task<IActionResult> Add(int surveyId, int questionId, [FromBody] AnswerVariantCreateDto dto)
|
||||
{
|
||||
var model = AnswerVariantMapper.CreateDtoToModel(dto, questionId);
|
||||
await _answerVariantsService.AddAnswerVariantAsync(model);
|
||||
var result = AnswerVariantMapper.ModelToOutputDto(model);
|
||||
return Ok(result);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Обновить вариант ответа на вопрос
|
||||
/// </summary>
|
||||
/// <param name="surveyId">Идентификатор опроса</param>
|
||||
/// <param name="questionId">Идентификатор вопроса</param>
|
||||
/// <param name="id">Идентификатор варианта ответа</param>
|
||||
/// <param name="dto">Объект с данными для обновления варианта ответа</param>
|
||||
/// <returns>Результат обновленного варианта ответа</returns>
|
||||
[Authorize]
|
||||
[HttpPut("{id}")]
|
||||
public async Task<IActionResult> Update(int surveyId, int questionId, int id, [FromBody] AnswerVariantUpdateDto dto)
|
||||
{
|
||||
var model = AnswerVariantMapper.UpdateDtoToModel(dto, questionId, id);
|
||||
await _answerVariantsService.UpdateAnswerVariantAsync(model);
|
||||
var result = AnswerVariantMapper.ModelToOutputDto(model);
|
||||
return Ok(result);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Удалить вариант ответа на вопрос
|
||||
/// </summary>
|
||||
/// <param name="surveyId">Идентификатор опроса</param>
|
||||
/// <param name="questionId">Идентификатор вопроса</param>
|
||||
/// <param name="id">Идентификатор варианта ответа</param>
|
||||
/// <returns>Результат операции удаления</returns>
|
||||
[Authorize]
|
||||
[HttpDelete("{id}")]
|
||||
public async Task<IActionResult> Delete(int surveyId, int questionId, int id)
|
||||
{
|
||||
await _answerVariantsService.DeleteAnswerVariantAsync(id);
|
||||
return Ok();
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,6 @@
|
|||
namespace SurveyBackend.DTOs.AnswerVariant;
|
||||
|
||||
public class AnswerVariantCreateDto
|
||||
{
|
||||
public required string Text { get; set; }
|
||||
}
|
||||
|
|
@ -1,9 +1,9 @@
|
|||
namespace SurveyBackend.DTOs.Question;
|
||||
namespace SurveyBackend.DTOs.AnswerVariant;
|
||||
|
||||
/// <summary>
|
||||
/// Выходная схема вариантов ответа
|
||||
/// </summary>
|
||||
public class OutputAnswerVariantDto
|
||||
public class AnswerVariantOutputDto
|
||||
{
|
||||
/// <summary>
|
||||
/// ID варианта ответа
|
||||
|
|
@ -0,0 +1,5 @@
|
|||
namespace SurveyBackend.DTOs.AnswerVariant;
|
||||
|
||||
public class AnswerVariantUpdateDto : AnswerVariantCreateDto
|
||||
{
|
||||
}
|
||||
|
|
@ -1,3 +1,5 @@
|
|||
using SurveyBackend.DTOs.AnswerVariant;
|
||||
|
||||
namespace SurveyBackend.DTOs.Question;
|
||||
|
||||
/// <summary>
|
||||
|
|
@ -28,5 +30,5 @@ public class QuestionOutputDto
|
|||
/// <summary>
|
||||
/// Варианты ответа
|
||||
/// </summary>
|
||||
public List<OutputAnswerVariantDto>? AnswerVariants { get; set; }
|
||||
public List<AnswerVariantOutputDto>? AnswerVariants { get; set; }
|
||||
}
|
||||
|
|
@ -9,16 +9,21 @@ public class SurveyOutputDto
|
|||
/// ID опроса
|
||||
/// </summary>
|
||||
public required int Id { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Название опроса
|
||||
/// </summary>
|
||||
public required string Title { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Описание опроса
|
||||
/// </summary>
|
||||
public required string Description { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Создатель опроса (опционально)
|
||||
/// </summary>
|
||||
public int? CreatedBy { get; set; }
|
||||
|
||||
public required DateTime CreatedAt { get; set; }
|
||||
}
|
||||
|
|
@ -0,0 +1,28 @@
|
|||
using SurveyBackend.DTOs.AnswerVariant;
|
||||
using SurveyLib.Core.Models;
|
||||
|
||||
namespace SurveyBackend.Mappers;
|
||||
|
||||
public static class AnswerVariantMapper
|
||||
{
|
||||
public static AnswerVariant CreateDtoToModel(AnswerVariantCreateDto dto, int questionId) => new AnswerVariant
|
||||
{
|
||||
QuestionId = questionId,
|
||||
Text = dto.Text
|
||||
};
|
||||
|
||||
public static AnswerVariant UpdateDtoToModel(AnswerVariantUpdateDto dto, int answerVariantId, int questionId) =>
|
||||
new AnswerVariant
|
||||
{
|
||||
QuestionId = questionId,
|
||||
Id = answerVariantId,
|
||||
Text = dto.Text
|
||||
};
|
||||
|
||||
public static AnswerVariantOutputDto ModelToOutputDto(AnswerVariant model) => new AnswerVariantOutputDto
|
||||
{
|
||||
Id = model.Id,
|
||||
QuestionId = model.QuestionId,
|
||||
Text = model.Text
|
||||
};
|
||||
}
|
||||
|
|
@ -1,3 +1,4 @@
|
|||
using SurveyBackend.DTOs.AnswerVariant;
|
||||
using SurveyBackend.DTOs.Question;
|
||||
using SurveyBackend.Services.Exceptions;
|
||||
using SurveyLib.Core.Models;
|
||||
|
|
@ -88,9 +89,9 @@ public static class QuestionMapper
|
|||
};
|
||||
}
|
||||
|
||||
private static List<OutputAnswerVariantDto> AnswerVariantsToDto(IEnumerable<AnswerVariant> answerVariants)
|
||||
private static List<AnswerVariantOutputDto> AnswerVariantsToDto(IEnumerable<AnswerVariant> answerVariants)
|
||||
{
|
||||
return answerVariants.Select(av => new OutputAnswerVariantDto
|
||||
return answerVariants.Select(av => new AnswerVariantOutputDto
|
||||
{
|
||||
Id = av.Id,
|
||||
QuestionId = av.QuestionId,
|
||||
|
|
|
|||
|
|
@ -45,6 +45,7 @@ public static class SurveyMapper
|
|||
Title = survey.Title,
|
||||
Description = survey.Description,
|
||||
CreatedBy = survey.CreatedBy,
|
||||
CreatedAt = survey.CreatedAt,
|
||||
};
|
||||
}
|
||||
}
|
||||
|
|
@ -48,9 +48,11 @@ public class Program
|
|||
|
||||
builder.Services.AddScoped<ISurveyRepository, SurveyRepository>();
|
||||
builder.Services.AddScoped<IQuestionRepository, QuestionRepository>();
|
||||
builder.Services.AddScoped<IAnswerVariantsRepository, AnswerVariantsRepository>();
|
||||
|
||||
builder.Services.AddScoped<ISurveyService, SurveyService>();
|
||||
builder.Services.AddScoped<IQuestionService, QuestionService>();
|
||||
builder.Services.AddScoped<IAnswerVariantsService, AnswerVariantsService>();
|
||||
|
||||
|
||||
builder.Services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue