life quality changes
- refactored SurveyController.cs - added ProducesResponseType to every endpoint in every controller - remade mappers
This commit is contained in:
parent
9e50b97bc9
commit
6692a91821
11 changed files with 146 additions and 93 deletions
|
|
@ -1,7 +1,7 @@
|
||||||
using Microsoft.AspNetCore.Authorization;
|
using Microsoft.AspNetCore.Authorization;
|
||||||
using Microsoft.AspNetCore.Mvc;
|
using Microsoft.AspNetCore.Mvc;
|
||||||
using SurveyBackend.DTOs;
|
using SurveyBackend.DTOs;
|
||||||
using SurveyBackend.Mappers.UserDTOs;
|
using SurveyBackend.Mappers;
|
||||||
using IAuthorizationService = SurveyBackend.Core.Services.IAuthorizationService;
|
using IAuthorizationService = SurveyBackend.Core.Services.IAuthorizationService;
|
||||||
|
|
||||||
namespace SurveyBackend.Controllers;
|
namespace SurveyBackend.Controllers;
|
||||||
|
|
@ -19,6 +19,8 @@ public class AuthController : ControllerBase
|
||||||
|
|
||||||
[AllowAnonymous]
|
[AllowAnonymous]
|
||||||
[HttpPost("login")]
|
[HttpPost("login")]
|
||||||
|
[ProducesResponseType(StatusCodes.Status401Unauthorized)]
|
||||||
|
[ProducesResponseType(StatusCodes.Status200OK)]
|
||||||
public async Task<IActionResult> LogIn([FromBody] UserLoginDto loginData)
|
public async Task<IActionResult> LogIn([FromBody] UserLoginDto loginData)
|
||||||
{
|
{
|
||||||
var token = await _authorizationService.LogInUser(loginData.Email, loginData.Password);
|
var token = await _authorizationService.LogInUser(loginData.Email, loginData.Password);
|
||||||
|
|
@ -27,10 +29,12 @@ public class AuthController : ControllerBase
|
||||||
|
|
||||||
[AllowAnonymous]
|
[AllowAnonymous]
|
||||||
[HttpPost("register")]
|
[HttpPost("register")]
|
||||||
|
[ProducesResponseType(StatusCodes.Status401Unauthorized)]
|
||||||
|
[ProducesResponseType(StatusCodes.Status200OK)]
|
||||||
public async Task<IActionResult> Register([FromBody] UserRegistrationDto registerData)
|
public async Task<IActionResult> Register([FromBody] UserRegistrationDto registerData)
|
||||||
{
|
{
|
||||||
var token = await _authorizationService.RegisterUser(
|
var token = await _authorizationService.RegisterUser(
|
||||||
UserRegistrationMapper.UserRegistrationToModel(registerData));
|
AuthMapper.UserRegistrationToModel(registerData));
|
||||||
return Ok(new { token = token });
|
return Ok(new { token = token });
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -2,13 +2,13 @@ using Microsoft.AspNetCore.Authorization;
|
||||||
using Microsoft.AspNetCore.Mvc;
|
using Microsoft.AspNetCore.Mvc;
|
||||||
using SurveyBackend.Core.Contexts;
|
using SurveyBackend.Core.Contexts;
|
||||||
using SurveyBackend.DTOs.Question;
|
using SurveyBackend.DTOs.Question;
|
||||||
using SurveyBackend.Mappers.QuestionDTOs;
|
using SurveyBackend.Mappers;
|
||||||
using SurveyLib.Core.Services;
|
using SurveyLib.Core.Services;
|
||||||
|
|
||||||
namespace SurveyBackend.Controllers;
|
namespace SurveyBackend.Controllers;
|
||||||
|
|
||||||
[ApiController]
|
[ApiController]
|
||||||
[Route("api/questions")]
|
[Route("surveys/{surveyId}/questions")]
|
||||||
public class QuestionController : ControllerBase
|
public class QuestionController : ControllerBase
|
||||||
{
|
{
|
||||||
private readonly IQuestionService _questionService;
|
private readonly IQuestionService _questionService;
|
||||||
|
|
@ -19,20 +19,24 @@ public class QuestionController : ControllerBase
|
||||||
}
|
}
|
||||||
|
|
||||||
[AllowAnonymous]
|
[AllowAnonymous]
|
||||||
[HttpGet("by_survey/{id}")]
|
[HttpGet]
|
||||||
public async Task<IActionResult> GetBySurveyId(int id)
|
[ProducesResponseType(StatusCodes.Status404NotFound)]
|
||||||
|
[ProducesResponseType(typeof(List<OutputQuestionDto>), StatusCodes.Status200OK)]
|
||||||
|
public async Task<IActionResult> GetBySurveyId([FromRoute] int surveyId)
|
||||||
{
|
{
|
||||||
var questions = await _questionService.GetQuestionsBySurveyIdAsync(id);
|
var questions = await _questionService.GetQuestionsBySurveyIdAsync(surveyId);
|
||||||
var result = questions.Select(QuestionOutputMapper.ModelToQuestionDTO).ToList();
|
var result = questions.Select(QuestionMapper.ModelToQuestionDto).ToList();
|
||||||
return Ok(result);
|
return Ok(result);
|
||||||
}
|
}
|
||||||
|
|
||||||
[Authorize]
|
[Authorize]
|
||||||
[HttpPost]
|
[HttpPost]
|
||||||
public async Task<IActionResult> AddQuestion(CreateQuestionDto dto)
|
[ProducesResponseType(StatusCodes.Status404NotFound)]
|
||||||
|
[ProducesResponseType(StatusCodes.Status201Created)]
|
||||||
|
public async Task<IActionResult> AddQuestion(CreateQuestionDto dto, [FromRoute] int surveyId)
|
||||||
{
|
{
|
||||||
var model = QuestionCreationMapper.QuestionCreationToModel(dto);
|
var model = QuestionMapper.QuestionCreationToModel(dto, surveyId);
|
||||||
await _questionService.AddQuestionAsync(model);
|
await _questionService.AddQuestionAsync(model);
|
||||||
return Ok();
|
return Created();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -3,13 +3,15 @@ using Microsoft.AspNetCore.Authorization;
|
||||||
using Microsoft.AspNetCore.Mvc;
|
using Microsoft.AspNetCore.Mvc;
|
||||||
using SurveyBackend.Core.Contexts;
|
using SurveyBackend.Core.Contexts;
|
||||||
using SurveyBackend.DTOs.Survey;
|
using SurveyBackend.DTOs.Survey;
|
||||||
|
using SurveyBackend.Mappers;
|
||||||
|
using SurveyBackend.Services.Exceptions;
|
||||||
using SurveyLib.Core.Models;
|
using SurveyLib.Core.Models;
|
||||||
using SurveyLib.Core.Services;
|
using SurveyLib.Core.Services;
|
||||||
|
|
||||||
namespace SurveyBackend.Controllers;
|
namespace SurveyBackend.Controllers;
|
||||||
|
|
||||||
[ApiController]
|
[ApiController]
|
||||||
[Route("api/surveys")]
|
[Route("surveys")]
|
||||||
public class SurveyController : ControllerBase
|
public class SurveyController : ControllerBase
|
||||||
{
|
{
|
||||||
private readonly ISurveyService _surveyService;
|
private readonly ISurveyService _surveyService;
|
||||||
|
|
@ -23,38 +25,42 @@ public class SurveyController : ControllerBase
|
||||||
|
|
||||||
[AllowAnonymous]
|
[AllowAnonymous]
|
||||||
[HttpGet]
|
[HttpGet]
|
||||||
|
[ProducesResponseType(typeof(List<OutputSurveyDto>), StatusCodes.Status200OK)]
|
||||||
public async Task<IActionResult> Get()
|
public async Task<IActionResult> Get()
|
||||||
{
|
{
|
||||||
var result = await _surveyService.GetSurveysAsync();
|
var surveys = await _surveyService.GetSurveysAsync();
|
||||||
|
var result = surveys.Select(SurveyMapper.ModelToOutputDto);
|
||||||
return Ok(result);
|
return Ok(result);
|
||||||
}
|
}
|
||||||
|
|
||||||
[AllowAnonymous]
|
[AllowAnonymous]
|
||||||
[HttpGet("{id}")]
|
[HttpGet("{id}")]
|
||||||
|
[ProducesResponseType(StatusCodes.Status404NotFound)]
|
||||||
|
[ProducesResponseType(typeof(OutputSurveyDto), StatusCodes.Status200OK)]
|
||||||
public async Task<IActionResult> Get(int id)
|
public async Task<IActionResult> Get(int id)
|
||||||
{
|
{
|
||||||
var result = await _surveyService.GetSurveyAsync(id);
|
var survey = await _surveyService.GetSurveyAsync(id);
|
||||||
return result is not null ? Ok(result) : NotFound();
|
var result = SurveyMapper.ModelToOutputDto(survey);
|
||||||
|
return Ok(result);
|
||||||
}
|
}
|
||||||
|
|
||||||
[Authorize]
|
[Authorize]
|
||||||
[HttpPost]
|
[HttpPost]
|
||||||
|
[ProducesResponseType(StatusCodes.Status201Created)]
|
||||||
public async Task<IActionResult> Post([FromBody] CreateSurveyDto dto)
|
public async Task<IActionResult> Post([FromBody] CreateSurveyDto dto)
|
||||||
{
|
{
|
||||||
var userId = _userContext.UserId;
|
var userId = _userContext.UserId;
|
||||||
|
|
||||||
var survey = new Survey
|
var survey = SurveyMapper.CreateDtoToModel(dto, userId);
|
||||||
{
|
|
||||||
Title = dto.Title,
|
|
||||||
Description = dto.Description,
|
|
||||||
CreatedBy = userId,
|
|
||||||
};
|
|
||||||
await _surveyService.AddSurveyAsync(survey);
|
await _surveyService.AddSurveyAsync(survey);
|
||||||
return Ok();
|
return Created();
|
||||||
}
|
}
|
||||||
|
|
||||||
[Authorize]
|
[Authorize]
|
||||||
[HttpDelete("{id}")]
|
[HttpDelete("{id}")]
|
||||||
|
[ProducesResponseType(StatusCodes.Status404NotFound)]
|
||||||
|
[ProducesResponseType(StatusCodes.Status401Unauthorized)]
|
||||||
|
[ProducesResponseType(StatusCodes.Status200OK)]
|
||||||
public async Task<IActionResult> Delete(int id)
|
public async Task<IActionResult> Delete(int id)
|
||||||
{
|
{
|
||||||
await _surveyService.DeleteSurveyAsync(id);
|
await _surveyService.DeleteSurveyAsync(id);
|
||||||
|
|
@ -62,10 +68,13 @@ public class SurveyController : ControllerBase
|
||||||
}
|
}
|
||||||
|
|
||||||
[Authorize]
|
[Authorize]
|
||||||
[HttpGet("my_surveys")]
|
[HttpGet("my")]
|
||||||
|
[ProducesResponseType(StatusCodes.Status401Unauthorized)]
|
||||||
|
[ProducesResponseType(typeof(List<OutputSurveyDto>), StatusCodes.Status200OK)]
|
||||||
public async Task<IActionResult> GetMySurveys()
|
public async Task<IActionResult> GetMySurveys()
|
||||||
{
|
{
|
||||||
var userId = _userContext.UserId;
|
var userId = _userContext.UserId;
|
||||||
|
|
||||||
var result = await _surveyService.GetSurveysByUserIdAsync(userId);
|
var result = await _surveyService.GetSurveysByUserIdAsync(userId);
|
||||||
return Ok(result);
|
return Ok(result);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -3,7 +3,6 @@ namespace SurveyBackend.DTOs.Question;
|
||||||
public class CreateQuestionDto
|
public class CreateQuestionDto
|
||||||
{
|
{
|
||||||
public required string Title { get; set; }
|
public required string Title { get; set; }
|
||||||
public required int SurveyId { get; set; }
|
|
||||||
public required string QuestionType { get; set; }
|
public required string QuestionType { get; set; }
|
||||||
|
|
||||||
public List<string>? AnswerVariants { get; set; }
|
public List<string>? AnswerVariants { get; set; }
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,9 @@
|
||||||
|
namespace SurveyBackend.DTOs.Survey;
|
||||||
|
|
||||||
|
public class OutputSurveyDto
|
||||||
|
{
|
||||||
|
public required int Id { get; set; }
|
||||||
|
public required string Title { get; set; }
|
||||||
|
public required string Description { get; set; }
|
||||||
|
public int? CreatedBy { get; set; }
|
||||||
|
}
|
||||||
|
|
@ -1,10 +1,9 @@
|
||||||
using SurveyBackend.Core.Models;
|
using SurveyBackend.Core.Models;
|
||||||
using SurveyBackend.Core.Services;
|
|
||||||
using SurveyBackend.DTOs;
|
using SurveyBackend.DTOs;
|
||||||
|
|
||||||
namespace SurveyBackend.Mappers.UserDTOs;
|
namespace SurveyBackend.Mappers;
|
||||||
|
|
||||||
public static class UserRegistrationMapper
|
public static class AuthMapper
|
||||||
{
|
{
|
||||||
public static User UserRegistrationToModel(UserRegistrationDto dto) => new User
|
public static User UserRegistrationToModel(UserRegistrationDto dto) => new User
|
||||||
{
|
{
|
||||||
|
|
@ -1,34 +0,0 @@
|
||||||
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
|
|
||||||
{
|
|
||||||
"textquestion" => new TextQuestion
|
|
||||||
{
|
|
||||||
Title = dto.Title,
|
|
||||||
SurveyId = dto.SurveyId,
|
|
||||||
},
|
|
||||||
"singleanswerquestion" => new SingleAnswerQuestion
|
|
||||||
{
|
|
||||||
Title = dto.Title,
|
|
||||||
SurveyId = dto.SurveyId,
|
|
||||||
AnswerVariants = dto.AnswerVariants?.Select(v => new AnswerVariant { Text = v }).ToList() ?? [],
|
|
||||||
},
|
|
||||||
"multipleanswerquestion" => new MultipleAnswerQuestion
|
|
||||||
{
|
|
||||||
Title = dto.Title,
|
|
||||||
SurveyId = dto.SurveyId,
|
|
||||||
AnswerVariants = dto.AnswerVariants?.Select(v => new AnswerVariant { Text = v }).ToList() ?? []
|
|
||||||
},
|
|
||||||
_ => throw new BadRequestException("Unknown question type")
|
|
||||||
};
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
@ -1,31 +0,0 @@
|
||||||
using SurveyBackend.DTOs.Question;
|
|
||||||
using SurveyLib.Core.Models;
|
|
||||||
using SurveyLib.Core.Models.QuestionVariants;
|
|
||||||
|
|
||||||
namespace SurveyBackend.Mappers.QuestionDTOs;
|
|
||||||
|
|
||||||
public static class QuestionOutputMapper
|
|
||||||
{
|
|
||||||
public static OutputQuestionDto ModelToQuestionDTO(QuestionBase question)
|
|
||||||
{
|
|
||||||
var withAnswerVariants = question.GetType() != typeof(TextQuestion);
|
|
||||||
return new OutputQuestionDto
|
|
||||||
{
|
|
||||||
Id = question.Id,
|
|
||||||
SurveyId = question.SurveyId,
|
|
||||||
Title = question.Title,
|
|
||||||
QuestionType = question.Discriminator,
|
|
||||||
AnswerVariants = withAnswerVariants ? GetAnswerVariants(question.AnswerVariants) : null,
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
private static List<OutputAnswerVariantDto> GetAnswerVariants(IEnumerable<AnswerVariant> answerVariants)
|
|
||||||
{
|
|
||||||
return answerVariants.Select(av => new OutputAnswerVariantDto
|
|
||||||
{
|
|
||||||
Id = av.Id,
|
|
||||||
QuestionId = av.QuestionId,
|
|
||||||
Text = av.Text,
|
|
||||||
}).ToList();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
57
SurveyBackend/SurveyBackend.API/Mappers/QuestionMapper.cs
Normal file
57
SurveyBackend/SurveyBackend.API/Mappers/QuestionMapper.cs
Normal file
|
|
@ -0,0 +1,57 @@
|
||||||
|
using SurveyBackend.DTOs.Question;
|
||||||
|
using SurveyBackend.Services.Exceptions;
|
||||||
|
using SurveyLib.Core.Models;
|
||||||
|
using SurveyLib.Core.Models.QuestionVariants;
|
||||||
|
|
||||||
|
namespace SurveyBackend.Mappers;
|
||||||
|
|
||||||
|
public static class QuestionMapper
|
||||||
|
{
|
||||||
|
public static QuestionBase QuestionCreationToModel(CreateQuestionDto dto, int surveyId)
|
||||||
|
{
|
||||||
|
return dto.QuestionType.ToLower() switch
|
||||||
|
{
|
||||||
|
"textquestion" => new TextQuestion
|
||||||
|
{
|
||||||
|
Title = dto.Title,
|
||||||
|
SurveyId = surveyId,
|
||||||
|
},
|
||||||
|
"singleanswerquestion" => new SingleAnswerQuestion
|
||||||
|
{
|
||||||
|
Title = dto.Title,
|
||||||
|
SurveyId = surveyId,
|
||||||
|
AnswerVariants = dto.AnswerVariants?.Select(v => new AnswerVariant { Text = v }).ToList() ?? [],
|
||||||
|
},
|
||||||
|
"multipleanswerquestion" => new MultipleAnswerQuestion
|
||||||
|
{
|
||||||
|
Title = dto.Title,
|
||||||
|
SurveyId = surveyId,
|
||||||
|
AnswerVariants = dto.AnswerVariants?.Select(v => new AnswerVariant { Text = v }).ToList() ?? []
|
||||||
|
},
|
||||||
|
_ => throw new BadRequestException("Unknown question type")
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
public static OutputQuestionDto ModelToQuestionDto(QuestionBase question)
|
||||||
|
{
|
||||||
|
var withAnswerVariants = question.GetType() != typeof(TextQuestion);
|
||||||
|
return new OutputQuestionDto
|
||||||
|
{
|
||||||
|
Id = question.Id,
|
||||||
|
SurveyId = question.SurveyId,
|
||||||
|
Title = question.Title,
|
||||||
|
QuestionType = question.Discriminator,
|
||||||
|
AnswerVariants = withAnswerVariants ? AnswerVariantsToDto(question.AnswerVariants) : null,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
private static List<OutputAnswerVariantDto> AnswerVariantsToDto(IEnumerable<AnswerVariant> answerVariants)
|
||||||
|
{
|
||||||
|
return answerVariants.Select(av => new OutputAnswerVariantDto
|
||||||
|
{
|
||||||
|
Id = av.Id,
|
||||||
|
QuestionId = av.QuestionId,
|
||||||
|
Text = av.Text,
|
||||||
|
}).ToList();
|
||||||
|
}
|
||||||
|
}
|
||||||
28
SurveyBackend/SurveyBackend.API/Mappers/SurveyMapper.cs
Normal file
28
SurveyBackend/SurveyBackend.API/Mappers/SurveyMapper.cs
Normal file
|
|
@ -0,0 +1,28 @@
|
||||||
|
using SurveyBackend.DTOs.Survey;
|
||||||
|
using SurveyLib.Core.Models;
|
||||||
|
|
||||||
|
namespace SurveyBackend.Mappers;
|
||||||
|
|
||||||
|
public static class SurveyMapper
|
||||||
|
{
|
||||||
|
public static Survey CreateDtoToModel(CreateSurveyDto dto, int userId)
|
||||||
|
{
|
||||||
|
return new Survey
|
||||||
|
{
|
||||||
|
Title = dto.Title,
|
||||||
|
Description = dto.Description,
|
||||||
|
CreatedBy = userId
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
public static OutputSurveyDto ModelToOutputDto(Survey survey)
|
||||||
|
{
|
||||||
|
return new OutputSurveyDto
|
||||||
|
{
|
||||||
|
Id = survey.Id,
|
||||||
|
Title = survey.Title,
|
||||||
|
Description = survey.Description,
|
||||||
|
CreatedBy = survey.CreatedBy,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -9,10 +9,13 @@ namespace SurveyBackend.Services.Services;
|
||||||
public class QuestionService : IQuestionService
|
public class QuestionService : IQuestionService
|
||||||
{
|
{
|
||||||
private readonly IQuestionRepository _questionRepository;
|
private readonly IQuestionRepository _questionRepository;
|
||||||
|
private readonly ISurveyRepository _surveyRepository;
|
||||||
|
|
||||||
public QuestionService(IQuestionRepository questionRepository, IUserContext userContext)
|
public QuestionService(IQuestionRepository questionRepository, ISurveyRepository surveyRepository,
|
||||||
|
IUserContext userContext)
|
||||||
{
|
{
|
||||||
_questionRepository = questionRepository;
|
_questionRepository = questionRepository;
|
||||||
|
_surveyRepository = surveyRepository;
|
||||||
}
|
}
|
||||||
|
|
||||||
public async Task AddQuestionAsync(QuestionBase question)
|
public async Task AddQuestionAsync(QuestionBase question)
|
||||||
|
|
@ -49,6 +52,12 @@ public class QuestionService : IQuestionService
|
||||||
|
|
||||||
public async Task<IEnumerable<QuestionBase>> GetQuestionsBySurveyIdAsync(int surveyId)
|
public async Task<IEnumerable<QuestionBase>> GetQuestionsBySurveyIdAsync(int surveyId)
|
||||||
{
|
{
|
||||||
|
var survey = await _surveyRepository.GetByIdAsync(surveyId);
|
||||||
|
if (survey is null)
|
||||||
|
{
|
||||||
|
throw new NotFoundException("Survey not found");
|
||||||
|
}
|
||||||
|
|
||||||
return await _questionRepository.GetQuestionsBySurveyId(surveyId);
|
return await _questionRepository.GetQuestionsBySurveyId(surveyId);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Loading…
Add table
Add a link
Reference in a new issue