timetable-mgr/dbAccess/access.go

63 lines
1.9 KiB
Go
Raw Normal View History

2023-07-17 12:48:36 +01:00
package dbAccess
import (
2023-07-19 21:31:00 +01:00
"context"
2023-07-18 00:25:13 +01:00
"git.fjla.uk/owlboard/go-types/pkg/database"
"git.fjla.uk/owlboard/timetable-mgr/helpers"
"git.fjla.uk/owlboard/timetable-mgr/log"
2023-07-19 21:31:00 +01:00
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/mongo/options"
2023-07-17 12:48:36 +01:00
)
2023-07-28 21:19:38 +01:00
const timetableCollection string = "timetable"
2024-03-26 16:32:00 +00:00
// Pushes the current version of this application to the data base 'versions' collection.
// Currently uses the old name of mq-client
func PushVersionToDb() {
2023-07-19 21:31:00 +01:00
version := database.Version{
2024-04-10 20:46:20 +01:00
Target: "timetable-mgr",
Component: "timetable-mgr",
2023-07-19 21:31:00 +01:00
Version: helpers.Version,
}
versionSelector := database.VersionSelector{
2024-04-10 20:46:20 +01:00
Target: "timetable-mgr",
Component: "timetable-mgr",
2023-07-19 21:31:00 +01:00
}
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 {
2024-04-14 19:03:13 +01:00
log.Warn("Unable to push version to database: " + err.Error())
2023-07-19 21:31:00 +01:00
} else {
2024-04-14 19:03:13 +01:00
log.Debug("Version up to date in Database")
2023-07-19 21:31:00 +01:00
}
2023-07-17 12:48:36 +01:00
}
// 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 {
2024-04-14 19:03:13 +01:00
log.Error("Unable to insert to database: " + err.Error())
return false
}
return true
2023-07-17 12:48:36 +01:00
}
// 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 {
2024-04-14 19:03:13 +01:00
log.Error("Unable to delete service: " + err.Error())
return false
}
return true
2023-07-18 00:25:13 +01:00
}