2023-07-17 12:48:36 +01:00
|
|
|
package dbAccess
|
|
|
|
|
|
|
|
import (
|
2023-07-19 21:31:00 +01:00
|
|
|
"context"
|
2024-03-26 22:33:11 +00:00
|
|
|
"time"
|
2023-07-18 00:25:13 +01:00
|
|
|
|
|
|
|
"git.fjla.uk/owlboard/go-types/pkg/database"
|
2024-03-25 11:26:07 +00:00
|
|
|
"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"
|
2024-03-26 16:32:00 +00:00
|
|
|
"go.uber.org/zap"
|
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
|
|
|
|
2024-03-29 14:01:57 +00:00
|
|
|
// CAUTION: Drops the collection named in collectionName
|
2024-03-26 16:32:00 +00:00
|
|
|
func DropCollection(collectionName string) error {
|
|
|
|
database := MongoClient.Database(databaseName)
|
|
|
|
collection := database.Collection(collectionName)
|
|
|
|
|
|
|
|
err := collection.Drop(context.Background())
|
|
|
|
if err != nil {
|
|
|
|
log.Msg.Error("Error dropping collection", zap.String("Collection Name", collectionName), zap.Error(err))
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-03-29 14:01:57 +00:00
|
|
|
// Checks the update time (unix timestamp) of the collection named in collectionName, uses 'type: collection' entries in the meta collection
|
2024-03-26 16:32:00 +00:00
|
|
|
func CheckUpdateTime(collectionName string) (int64, error) {
|
|
|
|
database := MongoClient.Database(databaseName)
|
|
|
|
collection := database.Collection(metaCollection)
|
|
|
|
filter := bson.D{
|
|
|
|
{Key: "target", Value: collectionName},
|
|
|
|
{Key: "type", Value: "collection"},
|
|
|
|
}
|
|
|
|
var result struct {
|
|
|
|
Updated int64 `bson:"updated"`
|
|
|
|
}
|
|
|
|
err := collection.FindOne(context.Background(), filter).Decode(&result)
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
return result.Updated, nil
|
|
|
|
}
|
2023-07-27 20:51:21 +01:00
|
|
|
|
2024-03-29 14:01:57 +00:00
|
|
|
// Sets a new update time (unix timestamp) of the collection named in collectionName. The update time is calculated within the function.
|
2024-03-26 22:33:11 +00:00
|
|
|
func SetUpdateTime(collectionName string) error {
|
|
|
|
log.Msg.Info("Setting update time", zap.String("collection", collectionName))
|
|
|
|
database := MongoClient.Database(databaseName)
|
|
|
|
collection := database.Collection("meta")
|
|
|
|
options := options.Update().SetUpsert(true)
|
|
|
|
updateTime := time.Now().Unix()
|
|
|
|
filter := bson.M{
|
|
|
|
"target": collectionName,
|
|
|
|
"type": "collection",
|
|
|
|
}
|
|
|
|
update := bson.M{
|
|
|
|
"$set": bson.M{
|
|
|
|
"updated": updateTime,
|
|
|
|
"target": collectionName,
|
|
|
|
"type": "collection",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
_, err := collection.UpdateOne(context.Background(), filter, update, options)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
log.Msg.Error("Error setting update time", zap.String("collection", collectionName), zap.Error(err))
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-03-29 14:01:57 +00:00
|
|
|
// Pushes the current version of this application to the data base 'versions' collection.
|
|
|
|
// Currently uses the old name of mq-client
|
2024-03-25 12:21:59 +00:00
|
|
|
func PushVersionToDb() {
|
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
|
|
|
}
|
|
|
|
|
2024-03-29 14:01:57 +00:00
|
|
|
// Puts one item of the type `database.Service` to the database, used by the VSTP package which receives services one at a time
|
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
|
|
|
}
|
|
|
|
|
2024-03-29 14:01:57 +00:00
|
|
|
// Deletes one service from the database.
|
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
|
|
|
}
|