2023-07-19 21:31:00 +01:00
|
|
|
package vstp
|
|
|
|
|
2023-07-21 12:12:16 +01:00
|
|
|
import (
|
2023-07-21 21:20:44 +01:00
|
|
|
"fmt"
|
|
|
|
|
2023-07-21 12:12:16 +01:00
|
|
|
"git.fjla.uk/owlboard/go-types/pkg/database"
|
2024-04-15 20:03:48 +01:00
|
|
|
"git.fjla.uk/owlboard/go-types/pkg/upstreamApi"
|
|
|
|
"git.fjla.uk/owlboard/timetable-mgr/cif"
|
2024-03-25 11:26:07 +00:00
|
|
|
"git.fjla.uk/owlboard/timetable-mgr/dbAccess"
|
2023-07-21 12:12:16 +01:00
|
|
|
)
|
|
|
|
|
2024-04-15 20:03:48 +01:00
|
|
|
// Converts to the correct struct for database insertion, then processes accordingly
|
|
|
|
func processCifData(s *upstreamApi.JsonScheduleV1) error {
|
2023-07-22 09:01:45 +01:00
|
|
|
|
2024-04-15 20:03:48 +01:00
|
|
|
if s.TransactionType == "Create" {
|
|
|
|
service, err := cif.ConvertServiceType(s, true)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
// Create slice as required by CreateCifEntries()
|
|
|
|
services := []database.Service{*service}
|
|
|
|
err = dbAccess.CreateCifEntries(services)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
2023-07-21 12:12:16 +01:00
|
|
|
|
2024-04-15 20:03:48 +01:00
|
|
|
} else if s.TransactionType == "Delete" {
|
|
|
|
query := database.DeleteQuery{
|
|
|
|
TrainUid: s.CifTrainUid,
|
|
|
|
ScheduleStartDate: cif.ParseCifDate(&s.ScheduleStartDate, "start"),
|
|
|
|
StpIndicator: s.CifStpIndicator,
|
|
|
|
}
|
|
|
|
// Create slice as required by DeleteCifEntries()
|
|
|
|
queries := []database.DeleteQuery{query}
|
|
|
|
err := dbAccess.DeleteCifEntries(queries)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
2023-07-27 20:51:21 +01:00
|
|
|
} else {
|
2024-04-15 20:36:33 +01:00
|
|
|
return fmt.Errorf("unknown transaction type: %s", s.TransactionType)
|
2023-07-27 20:51:21 +01:00
|
|
|
}
|
2023-07-21 12:12:16 +01:00
|
|
|
}
|