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.
Dotnet-WebAPI/API/Controllers/AccountsController.cs

118 lines
3.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";
public record CountUsersResponse(int Value);
[HttpGet("/admin/users/count&search={search}")]
public async Task<CountUsersResponse> CountUsers(
[MaxLength(256, ErrorMessage = "Search string is too wide")]
string search
)
{
return new CountUsersResponse(await service.UsersCount(search));
}
[HttpGet("/admin/users/count")]
public async Task<CountUsersResponse> CountUsers()
{
return new CountUsersResponse(await service.UsersCount());
}
// [HttpGet("/admin/users/count")]
// public async Task<CountUsersResponse> CountUsers()
// {
// return new CountUsersResponse(await service.UsersCount());
// }
[HttpGet("/admin/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/users/{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);
}
public record AddUserRequest(
[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
);
[HttpPost("/admin/users")]
public Task<User> AddUser([FromBody] AddUserRequest req)
{
return service.CreateUser(req.Username, req.Email, req.Password, DefaultProfilePicture, req.IsAdmin);
}
public record RemoveUsersRequest(int[] Identifiers);
[HttpPost("/admin/users/remove-all")]
public async void RemoveUsers([FromBody] RemoveUsersRequest req)
{
await service.RemoveUsers(req.Identifiers);
}
public record UpdateUserRequest(
[MaxLength(256, ErrorMessage = "Username is too wide")]
string Username,
[MaxLength(256, ErrorMessage = "Email is too wide")]
[EmailAddress]
string Email,
bool IsAdmin
);
[HttpPut("/admin/users/{id:int}")]
public async Task<IActionResult> UpdateUser(
int id,
[FromBody] UpdateUserRequest req
)
{
try
{
await service.UpdateUser(new User(id, req.Username, req.Email, DefaultProfilePicture, req.IsAdmin));
return Ok();
}
catch (ServiceException e)
{
return BadRequest(e.Failures);
}
}
}