Files
OptixServe/OptixServe.Api/Endpoints/UserEndpoint.cs
Huxley Deng 6fd6c9f20d Enable API versioning with route group.
Fix: the API routing is now versioned with prefix `api/v1`, aligned to OpenAPI specifications.
2025-07-07 16:16:58 +08:00

37 lines
1.0 KiB
C#

using System.Text.Json.Serialization;
using OptixServe.Core.Services;
using OptixServe.Api.Dtos;
namespace OptixServe.Api.Endpoints;
[JsonSerializable(typeof(UserDto))]
[JsonSerializable(typeof(IEnumerable<UserDto>))]
public partial class UserJsonContext : JsonSerializerContext { }
public static class UserEndpoint
{
public static void Register(RouteGroupBuilder parentGroup)
{
var group = parentGroup.MapGroup("/users");
group.MapGet("/", GetAllUsers);
group.MapGet("/{id}", GetUserById);
}
public static IResult GetAllUsers(IUserService userService)
{
var users = userService.GetUsers()
.Select(u => new UserDto { Id = u.Id, UserName = u.UserName });
return Results.Ok(users);
}
public static IResult GetUserById(string id, IUserService userService)
{
var user = userService.GetUserById(id);
if (user == null)
return Results.NotFound();
return Results.Ok(new UserDto { Id = user.Id, UserName = user.UserName });
}
}