Update SurveyLib and start refactoring endpoint paths

This commit is contained in:
Вячеслав 2025-05-31 01:03:12 +05:00
parent c2f6ba6dde
commit 637e6c9824
5 changed files with 22 additions and 15 deletions

View file

@ -1,10 +1,8 @@
using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc;
using SurveyBackend.Contexts;
using SurveyBackend.Core.Contexts; using SurveyBackend.Core.Contexts;
using SurveyBackend.DTOs.Completion; using SurveyBackend.DTOs.Completion;
using SurveyBackend.Mappers; using SurveyBackend.Mappers;
using SurveyLib.Core.Models;
using SurveyLib.Core.Services; using SurveyLib.Core.Services;
namespace SurveyBackend.Controllers; namespace SurveyBackend.Controllers;
@ -31,7 +29,8 @@ public class CompletionController : ControllerBase
return Ok(result); return Ok(result);
} }
[HttpGet("{id:int}")] [HttpGet]
[Route("/api/completions/{id:int}")]
[Authorize] [Authorize]
public async Task<IActionResult> GetCompletionAsync(int id) public async Task<IActionResult> GetCompletionAsync(int id)
{ {

View file

@ -64,11 +64,11 @@ public class QuestionController : ControllerBase
/// <param name="surveyId"></param> /// <param name="surveyId"></param>
/// <returns></returns> /// <returns></returns>
[Authorize] [Authorize]
[HttpPut("{id}")] [HttpPut]
public async Task<IActionResult> UpdateQuestion([FromBody] QuestionUpdateDto dto, [FromRoute] int id, [Route("/api/questions/{id:int}")]
[FromRoute] int surveyId) public async Task<IActionResult> UpdateQuestion([FromBody] QuestionUpdateDto dto, [FromRoute] int id)
{ {
var question = QuestionMapper.QuestionUpdateToModel(dto, surveyId, id); var question = QuestionMapper.QuestionUpdateToModel(dto, id);
await _questionService.UpdateQuestionAsync(question); await _questionService.UpdateQuestionAsync(question);
var result = QuestionMapper.ModelToQuestionDto(question); var result = QuestionMapper.ModelToQuestionDto(question);
return Ok(result); return Ok(result);
@ -81,8 +81,9 @@ public class QuestionController : ControllerBase
/// <param name="surveyId"></param> /// <param name="surveyId"></param>
/// <returns></returns> /// <returns></returns>
[Authorize] [Authorize]
[HttpDelete("{id}")] [HttpDelete]
public async Task<IActionResult> DeleteQuestion([FromRoute] int id, [FromRoute] int surveyId) [Route("/api/questions/{id:int}")]
public async Task<IActionResult> DeleteQuestion([FromRoute] int id)
{ {
await _questionService.DeleteQuestionAsync(id); await _questionService.DeleteQuestionAsync(id);
return Ok(); return Ok();

View file

@ -61,7 +61,7 @@ public static class QuestionMapper
}; };
} }
public static QuestionBase QuestionUpdateToModel(QuestionCreateDto dto, int surveyId, int questionId) public static QuestionBase QuestionUpdateToModel(QuestionCreateDto dto, int questionId)
{ {
return dto.QuestionType.ToLower() switch return dto.QuestionType.ToLower() switch
{ {
@ -69,20 +69,20 @@ public static class QuestionMapper
{ {
Id = questionId, Id = questionId,
Title = dto.Title, Title = dto.Title,
SurveyId = surveyId, SurveyId = 0, // bad logic, need to do something
}, },
"singleanswerquestion" => new SingleAnswerQuestion "singleanswerquestion" => new SingleAnswerQuestion
{ {
Id = questionId, Id = questionId,
Title = dto.Title, Title = dto.Title,
SurveyId = surveyId, SurveyId = 0,
AnswerVariants = [], AnswerVariants = [],
}, },
"multipleanswerquestion" => new MultipleAnswerQuestion "multipleanswerquestion" => new MultipleAnswerQuestion
{ {
Id = questionId, Id = questionId,
Title = dto.Title, Title = dto.Title,
SurveyId = surveyId, SurveyId = 0,
AnswerVariants = [] AnswerVariants = []
}, },
_ => throw new BadRequestException("Unknown question type") _ => throw new BadRequestException("Unknown question type")

View file

@ -28,7 +28,14 @@ public class QuestionService : IQuestionService
public async Task UpdateQuestionAsync(QuestionBase question) public async Task UpdateQuestionAsync(QuestionBase question)
{ {
// TODO: проверить существование вопроса var questionBase = await _questionRepository.GetByIdAsNoTrackingAsync(question.Id);
if (questionBase is null)
{
throw new NotFoundException("Question not found");
}
question.SurveyId = questionBase.SurveyId;
await _questionRepository.UpdateAsync(question); await _questionRepository.UpdateAsync(question);
} }

@ -1 +1 @@
Subproject commit 6349edf237839ed67b596e25b7c11510e97df325 Subproject commit f02a36dd85184c31a58e1d84081220a08b1c5d99