timetable-extension #1
@ -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{
 | 
			
		||||
 | 
			
		||||
@ -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)
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
							
								
								
									
										25
									
								
								src/dbAccess/corpus.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										25
									
								
								src/dbAccess/corpus.go
									
									
									
									
									
										Normal file
									
								
							@ -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
 | 
			
		||||
}
 | 
			
		||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user