using System; 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> Authenticate(AuthenticateUser authenticateUser) { // Return test result return Ok(new { Expires = DateTime.Now.Add(TimeSpan.FromDays(7)), SessionToken = Guid.NewGuid().ToString(), Released = DateTime.Now }); } [HttpPost("user")] [SessionAuthorize("auth:user:create")] public async Task> CreateUser(AuthenticateUser authenticateUser) { Result 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")] [SessionAuthorize("auth:user:get")] public async Task>>> GetUsers(int take, int skip) { var usersAuth = await _authService.GetUsers(take, skip); List users = usersAuth.Value?.Select(Models.Auth.User.ToDto).ToList() ?? new List(); return Ok(new ApiListResult>(users.Count, await _authService.UserCount(), users)); } } }