aboutsummaryrefslogtreecommitdiff
path: root/src/Costasdev.Busurbano.Backend/SantiagoController.cs
blob: 51a76c604c8e8ce0e38ea0d3f408d1ec0d1568c9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
using System.Text.Json;
using Costasdev.VigoTransitApi.Types;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Caching.Memory;

namespace Costasdev.Busurbano.Backend;

[ApiController]
[Route("api/santiago")]
public class SantiagoController : ControllerBase
{
    private readonly IMemoryCache _cache;
    private readonly HttpClient _httpClient;

    public SantiagoController(HttpClient http, IMemoryCache cache)
    {
        _cache = cache;
        _httpClient = http;
    }

    [HttpGet("GetStopEstimates")]
    public async Task<IActionResult> Run()
    {
        var argumentAvailable = Request.Query.TryGetValue("id", out var requestedStopIdString);
        if (!argumentAvailable)
        {
            return BadRequest("Please provide a stop id as a query parameter with the name 'id'.");
        }

        var argumentNumber = int.TryParse(requestedStopIdString, out var requestedStopId);
        if (!argumentNumber)
        {
            return BadRequest("The provided stop id is not a valid number.");
        }

        try
        {
            var obj = await _httpClient.GetFromJsonAsync<JsonDocument>(
                $"https://app.tussa.org/tussa/api/paradas/{requestedStopId}");

            if (obj is null)
            {
                return BadRequest("No response returned from the API, or whatever");
            }

            var root = obj.RootElement;

            List<StopEstimate> estimates = root
                .GetProperty("lineas")
                .EnumerateArray()
                .Select(el => new StopEstimate(
                    el.GetProperty("sinoptico").GetString() ?? string.Empty,
                    el.GetProperty("nombre").GetString() ?? string.Empty,
                    el.GetProperty("minutosProximoPaso").GetInt32(),
                    0
                )).ToList();

            return new OkObjectResult(new StopEstimateResponse
            {
                Stop = new StopEstimateResponse.StopInfo
                {
                    Name = root.GetProperty("nombre").GetString() ?? string.Empty,
                    Id = root.GetProperty("id").GetInt32(),
                    Latitude = root.GetProperty("coordenadas").GetProperty("latitud").GetDecimal(),
                    Longitude = root.GetProperty("coordenadas").GetProperty("longitud").GetDecimal()
                },
                Estimates = estimates
            });
        }
        catch (InvalidOperationException)
        {
            return new BadRequestObjectResult("Stop not found");
        }
    }

    [HttpGet("GetStopTimetable")]
    public async Task<IActionResult> GetStopTimetable()
    {
        throw new NotImplementedException();
    }
}