lineResponse.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. // SPDX-FileCopyrightText: Adam Evyčędo
  2. //
  3. // SPDX-License-Identifier: AGPL-3.0-or-later
  4. package api
  5. import (
  6. "apiote.xyz/p/szczanieckiej/traffic"
  7. "fmt"
  8. )
  9. func MakeLineResponse(line traffic.Line, context traffic.Context, t *traffic.Traffic, accept map[uint]struct{}) (LineResponse, error) {
  10. if _, ok := accept[0]; ok {
  11. response := LineResponseDev{
  12. Line: LineV3{},
  13. }
  14. line, err := makeLineV3(line, context, t)
  15. response.Line = line
  16. return response, err
  17. }
  18. if _, ok := accept[3]; ok {
  19. response := LineResponseV3{
  20. Line: LineV3{},
  21. }
  22. line, err := makeLineV3(line, context, t)
  23. response.Line = line
  24. return response, err
  25. }
  26. if _, ok := accept[2]; ok {
  27. response := LineResponseV2{
  28. Line: LineV2{},
  29. }
  30. line, err := makeLineV2(line, context, t)
  31. response.Line = line
  32. return response, err
  33. }
  34. if _, ok := accept[1]; ok {
  35. response := LineResponseV1{
  36. Line: LineV1{},
  37. }
  38. line, err := makeLineV1(line, context, t)
  39. response.Line = line
  40. return response, err
  41. }
  42. return LineResponseDev{}, AcceptError
  43. }
  44. func makeLineV3(line traffic.Line, context traffic.Context, t *traffic.Traffic) (LineV3, error) {
  45. l := convertTrafficLineV3(line, context.FeedID)
  46. lG, err := convertTrafficLineGraphsV1(line, context, t)
  47. if err != nil {
  48. return LineV3{}, fmt.Errorf("while converting graph: %w", err)
  49. }
  50. l.Graphs = lG
  51. return l, nil
  52. }
  53. func makeLineV2(line traffic.Line, context traffic.Context, t *traffic.Traffic) (LineV2, error) {
  54. l := convertTrafficLineV2(line, context.FeedID)
  55. lG, err := convertTrafficLineGraphsV1(line, context, t)
  56. if err != nil {
  57. return LineV2{}, fmt.Errorf("while converting graph: %w", err)
  58. }
  59. l.Graphs = lG
  60. return l, nil
  61. }
  62. func makeLineV1(line traffic.Line, context traffic.Context, t *traffic.Traffic) (LineV1, error) {
  63. l := convertTrafficLine(line, context.FeedID)
  64. lG, err := convertTrafficLineGraphsV1(line, context, t)
  65. if err != nil {
  66. return LineV1{}, fmt.Errorf("while converting graph: %w", err)
  67. }
  68. l.Graphs = lG
  69. return l, nil
  70. }
  71. func convertTrafficLineV3(line traffic.Line, feedID string) LineV3 {
  72. l := LineV3{
  73. Id: line.Id,
  74. Name: line.Name,
  75. Colour: fromColor(line.Colour),
  76. Kind: makeLineTypeV3(line),
  77. FeedID: feedID,
  78. Headsigns: line.Headsigns,
  79. }
  80. return l
  81. }
  82. func convertTrafficLineV2(line traffic.Line, feedID string) LineV2 {
  83. l := LineV2{
  84. Name: line.Name,
  85. Colour: fromColor(line.Colour),
  86. Kind: makeLineTypeV3(line),
  87. FeedID: feedID,
  88. Headsigns: line.Headsigns,
  89. }
  90. return l
  91. }
  92. func convertTrafficLine(line traffic.Line, feedID string) LineV1 {
  93. l := LineV1{
  94. Name: line.Name,
  95. Colour: fromColor(line.Colour),
  96. Kind: makeLineTypeV2(line),
  97. FeedID: feedID,
  98. Headsigns: line.Headsigns,
  99. }
  100. return l
  101. }
  102. func convertTrafficLineGraphsV1(trafficLine traffic.Line, context traffic.Context, t *traffic.Traffic) ([]LineGraphV1, error) {
  103. lineGraphs := []LineGraphV1{}
  104. for _, graph := range trafficLine.Graphs {
  105. if len(graph.StopCodes) != 0 {
  106. lineGraph := LineGraphV1{
  107. Stops: make([]StopStubV1, len(graph.StopCodes)),
  108. NextNodes: graph.NextNodes,
  109. }
  110. for i, code := range graph.StopCodes {
  111. stopStub, err := traffic.GetStopStub(code, trafficLine.Id, context, t)
  112. if err != nil {
  113. return lineGraphs, fmt.Errorf("while getting stopStub for %s: %w", code, err)
  114. }
  115. lineGraph.Stops[i] = convertTrafficStopStub(stopStub)
  116. }
  117. lineGraphs = append(lineGraphs, lineGraph)
  118. }
  119. }
  120. return lineGraphs, nil
  121. }