using System.Threading.Tasks; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Mvc; using BasicDotnetTemplate.MainProject.Core.Attributes; using BasicDotnetTemplate.MainProject.Models.Api.Request.Auth; using BasicDotnetTemplate.MainProject.Models.Api.Response; using BasicDotnetTemplate.MainProject.Models.Api.Response.Auth; using BasicDotnetTemplate.MainProject.Services; namespace BasicDotnetTemplate.MainProject.Controllers { [Route("[controller]")] public class AuthController : BaseController { private readonly IAuthService _authService; public AuthController( IConfiguration configuration, IAuthService authService ) : base(configuration) { this._authService = authService; } [HttpPost("authenticate")] [ProducesResponseType(StatusCodes.Status200OK)] [ProducesResponseType(StatusCodes.Status404NotFound)] [ProducesResponseType(StatusCodes.Status400BadRequest)] [ProducesResponseType(StatusCodes.Status500InternalServerError)] public async Task AuthenticateAsync([FromBody] AuthenticateRequest request) { try { if (!ModelState.IsValid) { return BadRequest("Request is not well formed"); } if ( request == null || request.Data == null || String.IsNullOrEmpty(request.Data.Username) || String.IsNullOrEmpty(request.Data.Password) ) { return BadRequest("Request is not well formed"); } var data = await this._authService.AuthenticateAsync(request.Data); if (data == null) { return NotFound(); } return Success(String.Empty, data); } catch (Exception exception) { var message = "Something went wrong"; if (!String.IsNullOrEmpty(exception.Message)) { message += $". {exception.Message}"; } return InternalServerError(message); } } } }