timetable-mgr/dbAccess/corpus.go
Fred Boniface 9496c9aae0
All checks were successful
Go Test / test (push) Successful in 31s
Add pipeline for tiploc/stanox
2024-06-30 19:36:37 +01:00

112 lines
2.7 KiB
Go

package dbAccess
import (
"context"
"fmt"
"git.fjla.uk/owlboard/go-types/pkg/database"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
)
// Puts an array of Corpus Documents into the database
func PutManyCorpus(corpusData *[]database.CorpusEntry) error {
collection := MongoClient.Database(DatabaseName).Collection(CorpusCollection)
documents := convertCorpusToInterfaceSlice(corpusData)
_, err := collection.InsertMany(context.Background(), *documents)
if err != nil {
return err
}
SetUpdateTime(CorpusCollection)
return nil
}
// Puts an array of Stations documents into the database
func PutManyStations(stationsData *[]database.StationEntry) error {
collection := MongoClient.Database(DatabaseName).Collection(StationsCollection)
documents := convertStationsToInterfaceSlice(stationsData)
_, err := collection.InsertMany(context.Background(), *documents)
if err != nil {
return err
}
SetUpdateTime(StationsCollection)
return nil
}
// Converts []database.CorpusEntry types into interface slices required to put them into the database
func convertCorpusToInterfaceSlice(corpusData *[]database.CorpusEntry) *[]interface{} {
interfaceSlice := make([]interface{}, len(*corpusData))
for i, doc := range *corpusData {
interfaceSlice[i] = doc
}
return &interfaceSlice
}
// Converts []database.StationEntry types into interface slices required to put them into the database
func convertStationsToInterfaceSlice(stationsData *[]database.StationEntry) *[]interface{} {
interfaceSlice := make([]interface{}, len(*stationsData))
for i, doc := range *stationsData {
interfaceSlice[i] = doc
}
return &interfaceSlice
}
func CreateCorpusIndexes() error {
coll := MongoClient.Database(DatabaseName).Collection(CorpusCollection)
indexModels := []mongo.IndexModel{
{
Keys: bson.M{
"tiploc": 1,
},
Options: options.Index().SetName("tiploc"),
},
{
Keys: bson.M{
"3alpha": 1,
},
Options: options.Index().SetName("3alpha"),
},
}
_, err := coll.Indexes().CreateMany(context.Background(), indexModels)
if err != nil {
return err
}
return nil
}
func GetTiplocFromCrs(crs string) (tiploc string, err error) {
// Return TIPLOC from CRS code
// PIPELINE:
/*
bson.A{
bson.D{{"$match", bson.D{{"3ALPHA", "BTH"}}}},
bson.D{
{"$project",
bson.D{
{"TIPLOC", 1},
{"STANOX", 1},
{"_id", 0},
},
},
},
}
*/
err = fmt.Errorf("not yet written")
return "", err
}
func GetStanoxFromCrs(crs string) (stanox string, err error) {
// Return STANOX from CRS code
err = fmt.Errorf("not yet written")
return "", err
}