add basic versions of retrieving answers by question or completion
This commit is contained in:
parent
637e6c9824
commit
d73e0a104f
7 changed files with 95 additions and 16 deletions
|
|
@ -0,0 +1,37 @@
|
|||
using Microsoft.AspNetCore.Authorization;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using SurveyBackend.Mappers;
|
||||
using SurveyLib.Core.Services;
|
||||
|
||||
namespace SurveyBackend.Controllers;
|
||||
|
||||
[ApiController]
|
||||
public class AnswerController : ControllerBase
|
||||
{
|
||||
private readonly IAnswerService _answerService;
|
||||
|
||||
public AnswerController(IAnswerService answerService)
|
||||
{
|
||||
_answerService = answerService;
|
||||
}
|
||||
|
||||
[Authorize]
|
||||
[HttpGet]
|
||||
[Route("api/questions/{id:int}/answers")]
|
||||
public async Task<IActionResult> GetAnswersByQuestionId(int id)
|
||||
{
|
||||
var models = await _answerService.GetAnswersByQuestionIdAsync(id);
|
||||
var result = models.Select(AnswerMapper.ModelToOutputDto);
|
||||
return Ok(result);
|
||||
}
|
||||
|
||||
[Authorize]
|
||||
[HttpGet]
|
||||
[Route("api/completions/{id:int}/answers")]
|
||||
public async Task<IActionResult> GetAnswersByCompletionId(int id)
|
||||
{
|
||||
var models = await _answerService.GetAnswersByCompletionIdAsync(id);
|
||||
var result = models.Select(AnswerMapper.ModelToOutputDto);
|
||||
return Ok(result);
|
||||
}
|
||||
}
|
||||
|
|
@ -52,16 +52,15 @@ public class AnswerVariantsController : ControllerBase
|
|||
/// <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)
|
||||
[HttpPut]
|
||||
[Route("/api/answerVariants/{id:int}")]
|
||||
public async Task<IActionResult> Update(int id, [FromBody] AnswerVariantUpdateDto dto)
|
||||
{
|
||||
var model = AnswerVariantMapper.UpdateDtoToModel(dto, questionId, id);
|
||||
var model = AnswerVariantMapper.UpdateDtoToModel(dto, id);
|
||||
await _answerVariantsService.UpdateAnswerVariantAsync(model);
|
||||
var result = AnswerVariantMapper.ModelToOutputDto(model);
|
||||
return Ok(result);
|
||||
|
|
@ -70,13 +69,12 @@ public class AnswerVariantsController : ControllerBase
|
|||
/// <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)
|
||||
[HttpDelete]
|
||||
[Route("/api/answerVariants/{id:int}")]
|
||||
public async Task<IActionResult> Delete(int id)
|
||||
{
|
||||
await _answerVariantsService.DeleteAnswerVariantAsync(id);
|
||||
return Ok();
|
||||
|
|
|
|||
|
|
@ -11,10 +11,9 @@ public static class AnswerVariantMapper
|
|||
Text = dto.Text
|
||||
};
|
||||
|
||||
public static AnswerVariant UpdateDtoToModel(AnswerVariantUpdateDto dto, int questionId, int answerVariantId) =>
|
||||
public static AnswerVariant UpdateDtoToModel(AnswerVariantUpdateDto dto, int answerVariantId) =>
|
||||
new AnswerVariant
|
||||
{
|
||||
QuestionId = questionId,
|
||||
Id = answerVariantId,
|
||||
Text = dto.Text
|
||||
};
|
||||
|
|
|
|||
|
|
@ -69,21 +69,16 @@ public static class QuestionMapper
|
|||
{
|
||||
Id = questionId,
|
||||
Title = dto.Title,
|
||||
SurveyId = 0, // bad logic, need to do something
|
||||
},
|
||||
"singleanswerquestion" => new SingleAnswerQuestion
|
||||
{
|
||||
Id = questionId,
|
||||
Title = dto.Title,
|
||||
SurveyId = 0,
|
||||
AnswerVariants = [],
|
||||
},
|
||||
"multipleanswerquestion" => new MultipleAnswerQuestion
|
||||
{
|
||||
Id = questionId,
|
||||
Title = dto.Title,
|
||||
SurveyId = 0,
|
||||
AnswerVariants = []
|
||||
},
|
||||
_ => throw new BadRequestException("Unknown question type")
|
||||
};
|
||||
|
|
|
|||
|
|
@ -51,11 +51,13 @@ public class Program
|
|||
builder.Services.AddScoped<IQuestionRepository, QuestionRepository>();
|
||||
builder.Services.AddScoped<IAnswerVariantsRepository, AnswerVariantsRepository>();
|
||||
builder.Services.AddScoped<ICompletionRepository, CompletionRepository>();
|
||||
builder.Services.AddScoped<IAnswerRepository, AnswerRepository>();
|
||||
|
||||
builder.Services.AddScoped<ISurveyService, SurveyService>();
|
||||
builder.Services.AddScoped<IQuestionService, QuestionService>();
|
||||
builder.Services.AddScoped<IAnswerVariantsService, AnswerVariantsService>();
|
||||
builder.Services.AddScoped<ICompletionService, CompletionService>();
|
||||
builder.Services.AddScoped<IAnswerService, AnswerService>();
|
||||
|
||||
|
||||
builder.Services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
|
||||
|
|
|
|||
|
|
@ -0,0 +1,40 @@
|
|||
using SurveyLib.Core.Models;
|
||||
using SurveyLib.Core.Repositories;
|
||||
using SurveyLib.Core.Services;
|
||||
|
||||
namespace SurveyBackend.Services.Services;
|
||||
|
||||
public class AnswerService : IAnswerService
|
||||
{
|
||||
private readonly IAnswerRepository _answerRepository;
|
||||
|
||||
public AnswerService(IAnswerRepository answerRepository)
|
||||
{
|
||||
_answerRepository = answerRepository;
|
||||
}
|
||||
|
||||
public async Task AddAnswerAsync(Answer answer)
|
||||
{
|
||||
await _answerRepository.AddAsync(answer);
|
||||
}
|
||||
|
||||
public async Task UpdateAnswerAsync(Answer answer)
|
||||
{
|
||||
await _answerRepository.UpdateAsync(answer);
|
||||
}
|
||||
|
||||
public async Task DeleteAnswerAsync(int id)
|
||||
{
|
||||
await _answerRepository.DeleteAsync(id);
|
||||
}
|
||||
|
||||
public async Task<IEnumerable<Answer>> GetAnswersByCompletionIdAsync(int completionId)
|
||||
{
|
||||
return await _answerRepository.GetAnswersByCompletionIdAsync(completionId);
|
||||
}
|
||||
|
||||
public async Task<IEnumerable<Answer>> GetAnswersByQuestionIdAsync(int questionId)
|
||||
{
|
||||
return await _answerRepository.GetAnswersByQuestionIdAsync(questionId);
|
||||
}
|
||||
}
|
||||
|
|
@ -22,6 +22,14 @@ public class AnswerVariantsService : IAnswerVariantsService
|
|||
public async Task UpdateAnswerVariantAsync(AnswerVariant answerVariant)
|
||||
{
|
||||
// TODO: опять проверка существования, но еще и варианта
|
||||
var answerVariantBase = await _answerVariantsRepository.GetByIdAsNoTrackingAsync(answerVariant.Id);
|
||||
if (answerVariantBase is null)
|
||||
{
|
||||
throw new Exception("Answer Variant not found");
|
||||
}
|
||||
|
||||
answerVariant.QuestionId = answerVariantBase.QuestionId;
|
||||
|
||||
await _answerVariantsRepository.UpdateAsync(answerVariant);
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue