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.Mvc;
|
||||
using SurveyBackend.DTOs;
|
||||
using SurveyBackend.Mappers.UserDTOs;
|
||||
using SurveyBackend.Mappers;
|
||||
using IAuthorizationService = SurveyBackend.Core.Services.IAuthorizationService;
|
||||
|
||||
namespace SurveyBackend.Controllers;
|
||||
|
|
@ -19,6 +19,8 @@ public class AuthController : ControllerBase
|
|||
|
||||
[AllowAnonymous]
|
||||
[HttpPost("login")]
|
||||
[ProducesResponseType(StatusCodes.Status401Unauthorized)]
|
||||
[ProducesResponseType(StatusCodes.Status200OK)]
|
||||
public async Task<IActionResult> LogIn([FromBody] UserLoginDto loginData)
|
||||
{
|
||||
var token = await _authorizationService.LogInUser(loginData.Email, loginData.Password);
|
||||
|
|
@ -27,10 +29,12 @@ public class AuthController : ControllerBase
|
|||
|
||||
[AllowAnonymous]
|
||||
[HttpPost("register")]
|
||||
[ProducesResponseType(StatusCodes.Status401Unauthorized)]
|
||||
[ProducesResponseType(StatusCodes.Status200OK)]
|
||||
public async Task<IActionResult> Register([FromBody] UserRegistrationDto registerData)
|
||||
{
|
||||
var token = await _authorizationService.RegisterUser(
|
||||
UserRegistrationMapper.UserRegistrationToModel(registerData));
|
||||
AuthMapper.UserRegistrationToModel(registerData));
|
||||
return Ok(new { token = token });
|
||||
}
|
||||
}
|
||||
|
|
@ -2,13 +2,13 @@ using Microsoft.AspNetCore.Authorization;
|
|||
using Microsoft.AspNetCore.Mvc;
|
||||
using SurveyBackend.Core.Contexts;
|
||||
using SurveyBackend.DTOs.Question;
|
||||
using SurveyBackend.Mappers.QuestionDTOs;
|
||||
using SurveyBackend.Mappers;
|
||||
using SurveyLib.Core.Services;
|
||||
|
||||
namespace SurveyBackend.Controllers;
|
||||
|
||||
[ApiController]
|
||||
[Route("api/questions")]
|
||||
[Route("surveys/{surveyId}/questions")]
|
||||
public class QuestionController : ControllerBase
|
||||
{
|
||||
private readonly IQuestionService _questionService;
|
||||
|
|
@ -19,20 +19,24 @@ public class QuestionController : ControllerBase
|
|||
}
|
||||
|
||||
[AllowAnonymous]
|
||||
[HttpGet("by_survey/{id}")]
|
||||
public async Task<IActionResult> GetBySurveyId(int id)
|
||||
[HttpGet]
|
||||
[ProducesResponseType(StatusCodes.Status404NotFound)]
|
||||
[ProducesResponseType(typeof(List<OutputQuestionDto>), StatusCodes.Status200OK)]
|
||||
public async Task<IActionResult> GetBySurveyId([FromRoute] int surveyId)
|
||||
{
|
||||
var questions = await _questionService.GetQuestionsBySurveyIdAsync(id);
|
||||
var result = questions.Select(QuestionOutputMapper.ModelToQuestionDTO).ToList();
|
||||
var questions = await _questionService.GetQuestionsBySurveyIdAsync(surveyId);
|
||||
var result = questions.Select(QuestionMapper.ModelToQuestionDto).ToList();
|
||||
return Ok(result);
|
||||
}
|
||||
|
||||
[Authorize]
|
||||
[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);
|
||||
return Ok();
|
||||
return Created();
|
||||
}
|
||||
}
|
||||
|
|
@ -3,13 +3,15 @@ using Microsoft.AspNetCore.Authorization;
|
|||
using Microsoft.AspNetCore.Mvc;
|
||||
using SurveyBackend.Core.Contexts;
|
||||
using SurveyBackend.DTOs.Survey;
|
||||
using SurveyBackend.Mappers;
|
||||
using SurveyBackend.Services.Exceptions;
|
||||
using SurveyLib.Core.Models;
|
||||
using SurveyLib.Core.Services;
|
||||
|
||||
namespace SurveyBackend.Controllers;
|
||||
|
||||
[ApiController]
|
||||
[Route("api/surveys")]
|
||||
[Route("surveys")]
|
||||
public class SurveyController : ControllerBase
|
||||
{
|
||||
private readonly ISurveyService _surveyService;
|
||||
|
|
@ -23,38 +25,42 @@ public class SurveyController : ControllerBase
|
|||
|
||||
[AllowAnonymous]
|
||||
[HttpGet]
|
||||
[ProducesResponseType(typeof(List<OutputSurveyDto>), StatusCodes.Status200OK)]
|
||||
public async Task<IActionResult> Get()
|
||||
{
|
||||
var result = await _surveyService.GetSurveysAsync();
|
||||
var surveys = await _surveyService.GetSurveysAsync();
|
||||
var result = surveys.Select(SurveyMapper.ModelToOutputDto);
|
||||
return Ok(result);
|
||||
}
|
||||
|
||||
[AllowAnonymous]
|
||||
[HttpGet("{id}")]
|
||||
[ProducesResponseType(StatusCodes.Status404NotFound)]
|
||||
[ProducesResponseType(typeof(OutputSurveyDto), StatusCodes.Status200OK)]
|
||||
public async Task<IActionResult> Get(int id)
|
||||
{
|
||||
var result = await _surveyService.GetSurveyAsync(id);
|
||||
return result is not null ? Ok(result) : NotFound();
|
||||
var survey = await _surveyService.GetSurveyAsync(id);
|
||||
var result = SurveyMapper.ModelToOutputDto(survey);
|
||||
return Ok(result);
|
||||
}
|
||||
|
||||
[Authorize]
|
||||
[HttpPost]
|
||||
[ProducesResponseType(StatusCodes.Status201Created)]
|
||||
public async Task<IActionResult> Post([FromBody] CreateSurveyDto dto)
|
||||
{
|
||||
var userId = _userContext.UserId;
|
||||
|
||||
var survey = new Survey
|
||||
{
|
||||
Title = dto.Title,
|
||||
Description = dto.Description,
|
||||
CreatedBy = userId,
|
||||
};
|
||||
var survey = SurveyMapper.CreateDtoToModel(dto, userId);
|
||||
await _surveyService.AddSurveyAsync(survey);
|
||||
return Ok();
|
||||
return Created();
|
||||
}
|
||||
|
||||
[Authorize]
|
||||
[HttpDelete("{id}")]
|
||||
[ProducesResponseType(StatusCodes.Status404NotFound)]
|
||||
[ProducesResponseType(StatusCodes.Status401Unauthorized)]
|
||||
[ProducesResponseType(StatusCodes.Status200OK)]
|
||||
public async Task<IActionResult> Delete(int id)
|
||||
{
|
||||
await _surveyService.DeleteSurveyAsync(id);
|
||||
|
|
@ -62,10 +68,13 @@ public class SurveyController : ControllerBase
|
|||
}
|
||||
|
||||
[Authorize]
|
||||
[HttpGet("my_surveys")]
|
||||
[HttpGet("my")]
|
||||
[ProducesResponseType(StatusCodes.Status401Unauthorized)]
|
||||
[ProducesResponseType(typeof(List<OutputSurveyDto>), StatusCodes.Status200OK)]
|
||||
public async Task<IActionResult> GetMySurveys()
|
||||
{
|
||||
var userId = _userContext.UserId;
|
||||
|
||||
var result = await _surveyService.GetSurveysByUserIdAsync(userId);
|
||||
return Ok(result);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -3,7 +3,6 @@ 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<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.Services;
|
||||
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
|
||||
{
|
||||
|
|
@ -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,
|
||||
};
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue