From c144a98fd1cb8d7dcef0a5479cb2d188826f26a0 Mon Sep 17 00:00:00 2001 From: Fred Boniface Date: Tue, 26 Mar 2024 16:32:00 +0000 Subject: [PATCH] Add update time check function --- src/dbAccess/access.go | 34 +++++++++++++++++++++++++++++++++- src/dbAccess/client.go | 5 +++++ src/dbAccess/corpus.go | 25 +++++++++++++++++++++++++ 3 files changed, 63 insertions(+), 1 deletion(-) create mode 100644 src/dbAccess/corpus.go diff --git a/src/dbAccess/access.go b/src/dbAccess/access.go index 3fab5a9..e565848 100644 --- a/src/dbAccess/access.go +++ b/src/dbAccess/access.go @@ -8,10 +8,42 @@ import ( "git.fjla.uk/owlboard/timetable-mgr/log" "go.mongodb.org/mongo-driver/bson" "go.mongodb.org/mongo-driver/mongo/options" + "go.uber.org/zap" ) const timetableCollection string = "timetable" -const databaseName string = "owlboard" + +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 +} + +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 +} func PushVersionToDb() { version := database.Version{ diff --git a/src/dbAccess/client.go b/src/dbAccess/client.go index 7307f44..8f87d0d 100644 --- a/src/dbAccess/client.go +++ b/src/dbAccess/client.go @@ -12,6 +12,11 @@ import ( "go.mongodb.org/mongo-driver/mongo/options" ) +const databaseName string = "owlboard" +const CorpusCollection string = "corpus" +const StationsCollection string = "stations" +const metaCollection string = "meta" + // Provide the DB Connection to other functions var MongoClient (*mongo.Client) diff --git a/src/dbAccess/corpus.go b/src/dbAccess/corpus.go new file mode 100644 index 0000000..2511310 --- /dev/null +++ b/src/dbAccess/corpus.go @@ -0,0 +1,25 @@ +package dbAccess + +import ( + "context" +) + +const corpusCollection = "corpus" +const stationsCollection = "stations" + +func dropExistingCorpus() error { + database := MongoClient.Database(databaseName) + collection := database.Collection(corpusCollection) + err := collection.Drop(context.Background()) + if err != nil { + return err + } + + collection = database.Collection(stationsCollection) + err = collection.Drop(context.Background()) + if err != nil { + return err + } + + return nil +}