Fix: the API routing is now versioned with prefix `api/v1`, aligned to OpenAPI specifications.
37 lines
1.0 KiB
C#
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 });
|
|
}
|
|
} |