Add the Get Single Result Endpoint
In the TaskService.cs file add the following method
public async Task<TaskDto?> GetTaskByIdAsync(int id)
{
using var conn = await _connectionFactory.CreateConnectionAsync();
var parameters = new DynamicParameters();
parameters.Add("@Id", id);
var task = await conn.QuerySingleOrDefaultAsync<TaskDto>(
"GetTaskById",
parameters,
commandType: CommandType.StoredProcedure
);
return task;
}
In the TaskEndpoints.cs file add the following endpoint
app.MapGet("/api/tasks/{id:int}", async (int id, TaskService service) =>
{
var task = await service.GetTaskByIdAsync(id);
return task is not null ? Results.Ok(task) : Results.NotFound();
});
In Postman or Rider etc
GET /api/tasks/1