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 { /// /// Controller der das Melden von Feedback aus der App ermöglicht /// [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; /// /// Erstellt eine Instanz /// /// Instanz eines IMapper /// Instanz von LocalizationOptions /// Instanz eines IAppUserService /// Instanz eines IAppFeedbackService /// Instanz eines IUserService public ApiFeedbackController(IMapper mapper, IOptions localizationOptions, IAppUserService appUserService, IAppFeedbackService appFeedbackService, IUserService userService) : base(mapper, localizationOptions, appUserService) { _appFeedbackService = appFeedbackService; _userService = userService; } /// /// Anlegen eines Feedbacks /// /// AppFeedbackCreateDto /// HTTP 200 OK, Felher sonst [HttpPost] [Route("CreateFeedback")] public async Task CreateFeedback(AppFeedbackCreateDto model) { var clientOffset = GetClientDateOffset(); var result = new CreateResponseDto { 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(feedback); return Ok(result); } else { return NotFound(CommunicationErrors.Common_NotFound); } } } return BadRequest(CommunicationErrors.Common_Model_Invalid); } } }