gehgassi_backend/gehGassi.Web/Controllers/Api/ApiFeedbackController.cs

98 lines
3.6 KiB
C#

using System;
using System.Threading.Tasks;
using Asp.Versioning;
using AutoMapper;
using gehGassi.Core.Interfaces;
using gehGassi.Domain.Common;
using gehGassi.Domain.Ratings;
using gehGassi.Dto;
using gehGassi.Dto.Common;
using gehGassi.Dto.Feedback;
using gehGassi.Dto.Ratings;
using gehGassi.Web.Helper;
using Microsoft.AspNetCore.Authentication.JwtBearer;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Options;
namespace gehGassi.Web.Controllers.Api
{
/// <summary>
/// Controller der das Melden von Feedback aus der App ermöglicht
/// </summary>
[Authorize(AuthenticationSchemes = JwtBearerDefaults.AuthenticationScheme)]
[ApiController]
[ApiVersion(1)]
[Route("api/feedback")]
[Route("api/v{v:apiVersion}/feedback")]
public class ApiFeedbackController : ApiBaseController
{
private readonly IAppFeedbackService _appFeedbackService;
private readonly IUserService _userService;
/// <summary>
/// Erstellt eine Instanz
/// </summary>
/// <param name="mapper">Instanz eines IMapper</param>
/// <param name="localizationOptions">Instanz von LocalizationOptions</param>
/// <param name="appUserService">Instanz eines IAppUserService</param>
/// <param name="appFeedbackService">Instanz eines IAppFeedbackService</param>
/// <param name="userService">Instanz eines IUserService</param>
public ApiFeedbackController(IMapper mapper, IOptions<LocalizationOptions> localizationOptions, IAppUserService appUserService, IAppFeedbackService appFeedbackService,
IUserService userService) : base(mapper, localizationOptions, appUserService)
{
_appFeedbackService = appFeedbackService;
_userService = userService;
}
/// <summary>
/// Anlegen eines Feedbacks
/// </summary>
/// <param name="model">AppFeedbackCreateDto</param>
/// <returns>HTTP 200 OK, Felher sonst</returns>
[HttpPost]
[Route("CreateFeedback")]
public async Task<IActionResult> CreateFeedback(AppFeedbackCreateDto model)
{
var clientOffset = GetClientDateOffset();
var result = new CreateResponseDto<AppFeedbackDto>
{
Status = CreateStatusDto.Error,
Value = null
};
if (User?.Identity != null && User.Identity.IsAuthenticated)
{
if (ModelState.IsValid)
{
var user = await _userService.GetByUsernameAsync(User.Identity.Name);
if (user != null)
{
var feedback = _appFeedbackService.Create();
Mapper.Map(model, feedback);
feedback.Created = DateTimeOffset.Now;
feedback.UserName = user.UserName;
feedback.Name = $"{user.FirstName} {user.LastName}";
feedback.Status = AppFeedbackStatus.Open;
_appFeedbackService.Add(feedback);
await _appFeedbackService.CommitAsync(User.Identity.Name);
result.Status = CreateStatusDto.Success;
result.Value = Mapper.Map<AppFeedbackDto>(feedback);
return Ok(result);
}
else
{
return NotFound(CommunicationErrors.Common_NotFound);
}
}
}
return BadRequest(CommunicationErrors.Common_Model_Invalid);
}
}
}