You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
84 lines
2.4 KiB
84 lines
2.4 KiB
using System.ComponentModel.DataAnnotations;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
using Model;
|
|
using Services;
|
|
|
|
namespace API.Controllers;
|
|
|
|
[ApiController]
|
|
public class AccountsController(IUserService service) : ControllerBase
|
|
{
|
|
private const string DefaultProfilePicture =
|
|
"https://cdn.pixabay.com/photo/2015/10/05/22/37/blank-profile-picture-973460_960_720.png";
|
|
|
|
[HttpGet("/admin/list-users")]
|
|
public async Task<IEnumerable<User>> ListUsers(
|
|
[Range(0, int.MaxValue, ErrorMessage = "Only positive number allowed")]
|
|
int start,
|
|
[Range(0, int.MaxValue, ErrorMessage = "Only positive number allowed")]
|
|
int n,
|
|
[MaxLength(256, ErrorMessage = "Search string is too wide")]
|
|
string? search
|
|
)
|
|
{
|
|
var result = search == null
|
|
? await service.ListUsers(search!)
|
|
: await service.ListUsers();
|
|
|
|
return result.Skip(start).Take(n);
|
|
}
|
|
|
|
[HttpGet("/admin/user/{id:int}")]
|
|
public async Task<IActionResult> GetUser(
|
|
[Range(0, int.MaxValue, ErrorMessage = "Only positive number allowed")]
|
|
int id
|
|
)
|
|
{
|
|
var result = await service.GetUser(id);
|
|
if (result == null)
|
|
return NotFound();
|
|
|
|
return Ok(result);
|
|
}
|
|
|
|
[HttpPost("/admin/user")]
|
|
public Task<User> AddUser(
|
|
[MaxLength(256, ErrorMessage = "Username is too wide")]
|
|
string username,
|
|
[Range(4, 256, ErrorMessage = "Password must length be between 4 and 256")]
|
|
string password,
|
|
[MaxLength(256, ErrorMessage = "Email is too wide")] [EmailAddress]
|
|
string email,
|
|
bool isAdmin = false
|
|
)
|
|
{
|
|
return service.CreateUser(username, email, password, DefaultProfilePicture, isAdmin);
|
|
}
|
|
|
|
[HttpDelete("/admin/user")]
|
|
public async void RemoveUsers(int[] identifiers)
|
|
{
|
|
await service.RemoveUsers(identifiers);
|
|
}
|
|
|
|
[HttpPut("/admin/user/{id:int}")]
|
|
public async Task<IActionResult> UpdateUser(
|
|
int id,
|
|
[MaxLength(256, ErrorMessage = "Username is too wide")]
|
|
string username,
|
|
[MaxLength(256, ErrorMessage = "Email is too wide")] [EmailAddress]
|
|
string email,
|
|
bool isAdmin
|
|
)
|
|
{
|
|
try
|
|
{
|
|
await service.UpdateUser(new User(id, username, email, DefaultProfilePicture, isAdmin));
|
|
return Ok();
|
|
}
|
|
catch (ServiceException e)
|
|
{
|
|
return BadRequest(e.Failures);
|
|
}
|
|
}
|
|
} |