SharpRSS/SharpRSS.API/Controllers/AuthController.cs
2023-09-17 21:41:31 +02:00

52 lines
1.7 KiB
C#

using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using SharpRSS.API.Auth;
using SharpRSS.API.Contracts;
using SharpRSS.API.Contracts.DTO;
using SharpRSS.API.Contracts.Models.User;
using SharpRSS.API.Data;
using SharpRSS.API.Models;
using SharpRSS.API.Models.Auth;
namespace SharpRSS.API.Controllers
{
[ApiController]
[SessionAuthorize]
[Route("api/[controller]")]
public class AuthController : ControllerBase
{
public AuthController(AuthService authService)
{
_authService = authService;
}
private readonly AuthService _authService;
[HttpPost("[action]")]
[AllowAnonymous]
public async Task<ActionResult<string>> Authenticate(AuthenticateUser authenticateUser)
{
return Ok("Ok!");
}
[HttpPost("user")]
public async Task<ActionResult<UserDto>> CreateUser(AuthenticateUser authenticateUser)
{
Result<User> result = await _authService.CreateUser(authenticateUser);
if (result.Success)
return Ok(Models.Auth.User.ToDto(result.Value ?? new User()));
return BadRequest(new ApiResult(result.Message, ApiResults.Error));
}
[HttpGet("user")]
public async Task<ActionResult<ApiListResult<IEnumerable<UserDto>>>> GetUsers(int take, int skip)
{
var usersAuth = await _authService.GetUsers(take, skip);
List<UserDto> users = usersAuth.Value?.Select(Models.Auth.User.ToDto).ToList() ?? new List<UserDto>();
return Ok(new ApiListResult<IEnumerable<UserDto>>(users.Count, await _authService.UserCount(), users));
}
}
}