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"
|
2023-07-19 21:31:00 +01:00
|
|
|
"git.fjla.uk/owlboard/mq-client/helpers"
|
2023-07-19 01:18:55 +01:00
|
|
|
"git.fjla.uk/owlboard/mq-client/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"
|
2023-07-27 20:51:21 +01:00
|
|
|
const databaseName string = "owlboard"
|
|
|
|
|
2023-07-17 12:48:36 +01:00
|
|
|
func init() {
|
2023-07-19 21:31:00 +01:00
|
|
|
version := database.Version{
|
|
|
|
Target: "mq-client",
|
|
|
|
Component: "mq-client",
|
|
|
|
Version: helpers.Version,
|
|
|
|
}
|
|
|
|
versionSelector := database.VersionSelector{
|
|
|
|
Target: "mq-client",
|
|
|
|
Component: "mq-client",
|
|
|
|
}
|
|
|
|
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.Msg.Warn("Unable to push version to database: " + err.Error())
|
|
|
|
} else {
|
|
|
|
log.Msg.Debug("Version up to date in Database")
|
|
|
|
}
|
2023-07-17 12:48:36 +01:00
|
|
|
}
|
|
|
|
|
2023-07-27 20:51:21 +01:00
|
|
|
func PutOneService(data database.Service) bool {
|
|
|
|
coll := MongoClient.Database(databaseName).Collection(timetableCollection)
|
|
|
|
_, err := coll.InsertOne(context.TODO(), data)
|
|
|
|
if err != nil {
|
|
|
|
log.Msg.Error("Unable to insert to database: " + err.Error())
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
return true
|
2023-07-17 12:48:36 +01:00
|
|
|
}
|
|
|
|
|
2023-07-27 20:51:21 +01:00
|
|
|
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.Msg.Error("Unable to delete service: " + err.Error())
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
return true
|
2023-07-18 00:25:13 +01:00
|
|
|
}
|