SharpRSS/SharpRSS.API/Controllers/AuthController.cs

82 lines
3.8 KiB
C#
Raw Normal View History

using System.Threading.Tasks;
2023-10-08 00:46:42 +02:00
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
2023-09-17 21:41:31 +02:00
using SharpRSS.API.Auth;
2023-09-10 21:32:25 +02:00
using SharpRSS.API.Contracts.DTO;
2023-10-08 00:46:42 +02:00
using SharpRSS.API.Contracts.Models;
using SharpRSS.API.Contracts.Payloads;
2023-09-10 21:32:25 +02:00
using SharpRSS.API.Data;
2023-10-08 00:46:42 +02:00
using ToolQit;
using ToolQit.Logging;
namespace SharpRSS.API.Controllers
{
[ApiController]
2023-09-17 21:41:31 +02:00
[SessionAuthorize]
[Route("api/[controller]")]
public class AuthController : ControllerBase
{
2023-09-10 21:32:25 +02:00
public AuthController(AuthService authService)
{
2023-09-10 21:32:25 +02:00
_authService = authService;
2023-10-08 00:46:42 +02:00
_log = LogManager.CreateLogger(typeof(AuthController));
2023-09-10 21:32:25 +02:00
}
2023-10-08 00:46:42 +02:00
private readonly ILog _log;
2023-09-10 21:32:25 +02:00
private readonly AuthService _authService;
2023-09-17 21:41:31 +02:00
2023-10-08 00:46:42 +02:00
[HttpPost("createuser")]
[Produces("application/json")]
[ProducesResponseType(StatusCodes.Status201Created)]
[ProducesResponseType(StatusCodes.Status400BadRequest)]
[ProducesResponseType(StatusCodes.Status500InternalServerError)]
public async Task<ActionResult<ResultOr<User>>> CreateUser(ModifyUser payload)
{
var createdUserResult = await _authService.CreateUser(payload);
return createdUserResult.Success ? Created("", createdUserResult) : createdUserResult.Status == ResultStatus.Failed ? BadRequest(createdUserResult) : StatusCode(StatusCodes.Status500InternalServerError, createdUserResult);
2023-09-17 21:41:31 +02:00
}
2023-10-08 00:46:42 +02:00
[HttpPost("updateuser")]
[Produces("application/json")]
[ProducesResponseType(StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status400BadRequest)]
[ProducesResponseType(StatusCodes.Status500InternalServerError)]
public async Task<ActionResult<ResultOr<User>>> UpdateUser(ModifyUser payload)
{
var updatedUserResult = await _authService.UpdateUser(payload);
return updatedUserResult.Success ? Ok(updatedUserResult) : updatedUserResult.Status == ResultStatus.Failed ? BadRequest(updatedUserResult) : StatusCode(StatusCodes.Status500InternalServerError, updatedUserResult);
}
[HttpDelete("deleteuser")]
[Produces("application/json")]
[ProducesResponseType(StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status400BadRequest)]
[ProducesResponseType(StatusCodes.Status500InternalServerError)]
public async Task<ActionResult<Result>> DeleteUser(string userId)
2023-09-10 21:32:25 +02:00
{
2023-10-08 00:46:42 +02:00
var removedUserResult = await _authService.RemoveUserAsync(userId);
return removedUserResult.Success ? Ok(removedUserResult) : removedUserResult.Status == ResultStatus.Failed ? BadRequest(removedUserResult) : StatusCode(StatusCodes.Status500InternalServerError, removedUserResult);
}
2023-09-17 21:41:31 +02:00
[HttpGet("user")]
2023-10-08 00:46:42 +02:00
[Produces("application/json")]
[ProducesResponseType(StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status400BadRequest)]
public async Task<ActionResult<ResultOr<User>>> GetUser(string userId)
{
var userResult = await _authService.GetUserAsync(userId);
return userResult.Success ? Ok(userResult) : BadRequest(userResult);
}
[HttpGet("users")]
[Produces("application/json")]
[ProducesResponseType(StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status400BadRequest)]
[ProducesResponseType(StatusCodes.Status500InternalServerError)]
public async Task<ActionResult<ListResult<User>>> GetUsers(string search = "", int results = 20, int skip = 0)
2023-09-17 21:41:31 +02:00
{
2023-10-08 00:46:42 +02:00
var usersResult = await _authService.GetUsersAsync(results, skip, search);
return usersResult.Success ? Ok(usersResult) : usersResult.Status == ResultStatus.Failed ? BadRequest(usersResult) : StatusCode(StatusCodes.Status500InternalServerError, usersResult);
2023-09-17 21:41:31 +02:00
}
}
}