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> 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 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 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 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); } } }