Files
Emergence/Emergence.api/Endpoints/UserEndpoints.cs
T

31 lines
1.2 KiB
C#

using Emergence.api.Interfaces;
using Emergence.models;
using Emergence.services.Interface;
using Microsoft.AspNetCore.Http.HttpResults;
namespace Emergence.api.Endpoints;
public class UserEndpoints : IEndpoint
{
private readonly IUserService _userService;
public UserEndpoints(IUserService userService)
{
_userService = userService;
}
public void MapEndPoint(IEndpointRouteBuilder app)
{
var endpoints = app.MapGroup("/users")
.WithTags("User Items");
endpoints.MapGet("/", GetAll).Produces<List<UserModel>>(StatusCodes.Status200OK).Produces(StatusCodes.Status404NotFound).WithName("Get All Users");
endpoints.MapGet("/{id}", GetById).Produces<string>(StatusCodes.Status200OK).Produces(StatusCodes.Status404NotFound).WithName("Get User By Id");
}
public async Task<Results<Ok<List<UserModel>>, NotFound>> GetAll() =>
await _userService.GetAllAsync() is List<UserModel> result ? TypedResults.Ok(result) : TypedResults.NotFound();
public async Task<Results<Ok<UserModel>, NotFound>> GetById(int id) =>
await _userService.GetByIdAsync(id) is UserModel result ? TypedResults.Ok(result) : TypedResults.NotFound();
}