63 lines
1.9 KiB
Go
63 lines
1.9 KiB
Go
package dbAccess
|
|
|
|
import (
|
|
"context"
|
|
|
|
"git.fjla.uk/owlboard/go-types/pkg/database"
|
|
"git.fjla.uk/owlboard/timetable-mgr/helpers"
|
|
"git.fjla.uk/owlboard/timetable-mgr/log"
|
|
"go.mongodb.org/mongo-driver/bson"
|
|
"go.mongodb.org/mongo-driver/mongo/options"
|
|
)
|
|
|
|
const timetableCollection string = "timetable"
|
|
|
|
// Pushes the current version of this application to the data base 'versions' collection.
|
|
// Currently uses the old name of mq-client
|
|
func PushVersionToDb() {
|
|
version := database.Version{
|
|
Target: "timetable-mgr",
|
|
Component: "timetable-mgr",
|
|
Version: helpers.Version,
|
|
}
|
|
versionSelector := database.VersionSelector{
|
|
Target: "timetable-mgr",
|
|
Component: "timetable-mgr",
|
|
}
|
|
opts := options.Update().SetUpsert(true)
|
|
coll := MongoClient.Database("owlboard").Collection("versions")
|
|
_, err := coll.UpdateOne(context.TODO(), versionSelector, bson.M{"$set": version}, opts)
|
|
if err != nil {
|
|
log.Warn("Unable to push version to database: " + err.Error())
|
|
} else {
|
|
log.Debug("Version up to date in Database")
|
|
}
|
|
}
|
|
|
|
// Puts one item of the type `database.Service` to the database, used by the VSTP package which receives services one at a time
|
|
func PutOneService(data database.Service) bool {
|
|
coll := MongoClient.Database(databaseName).Collection(timetableCollection)
|
|
_, err := coll.InsertOne(context.TODO(), data)
|
|
if err != nil {
|
|
log.Error("Unable to insert to database: " + err.Error())
|
|
return false
|
|
}
|
|
return true
|
|
}
|
|
|
|
// Deletes one service from the database.
|
|
func DeleteOneService(data database.DeleteQuery) bool {
|
|
coll := MongoClient.Database(databaseName).Collection(timetableCollection)
|
|
var filter = bson.D{
|
|
{Key: "trainUid", Value: data.TrainUid},
|
|
{Key: "stpIndicator", Value: data.StpIndicator},
|
|
{Key: "scheduleStartDate", Value: data.ScheduleStartDate},
|
|
}
|
|
_, err := coll.DeleteOne(context.TODO(), filter)
|
|
if err != nil {
|
|
log.Error("Unable to delete service: " + err.Error())
|
|
return false
|
|
}
|
|
return true
|
|
}
|