parent
2f9975cc2a
commit
7bf6f9c5ec
@ -0,0 +1,83 @@
|
||||
using System.ComponentModel.DataAnnotations;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Model;
|
||||
using Services;
|
||||
|
||||
namespace API.Controllers;
|
||||
|
||||
public class AccountsController(UserService service) : ControllerBase
|
||||
{
|
||||
private static 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);
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,3 @@
|
||||
namespace Model;
|
||||
|
||||
public record User(int Id, string Name, string Email, string ProfilePicture, bool IsAdmin);
|
@ -0,0 +1,9 @@
|
||||
namespace Services.Failures;
|
||||
|
||||
public record Failure(string Name, string Message)
|
||||
{
|
||||
public static Failure NotFound(string message)
|
||||
{
|
||||
return new("not found", message);
|
||||
}
|
||||
}
|
@ -0,0 +1,13 @@
|
||||
using Services.Failures;
|
||||
|
||||
namespace Services;
|
||||
|
||||
public class ServiceException : Exception
|
||||
{
|
||||
public List<Failure> Failures { get; init; }
|
||||
|
||||
public ServiceException(params Failure[] failures)
|
||||
{
|
||||
Failures = new List<Failure>(failures);
|
||||
}
|
||||
}
|
Loading…
Reference in new issue