From 06f59fcfea36f5472768a15ceb8e39229e4f02ac Mon Sep 17 00:00:00 2001 From: Fred Boniface Date: Thu, 27 Jul 2023 20:51:21 +0100 Subject: [PATCH] Add 'Create' & 'Delete' database actions --- src/dbAccess/access.go | 29 ++++++++++++++++++++++++----- src/go.mod | 2 +- src/go.sum | 4 ++++ src/vstp/actions.go | 20 +++++++++++++++++++- 4 files changed, 48 insertions(+), 7 deletions(-) diff --git a/src/dbAccess/access.go b/src/dbAccess/access.go index d68a5a5..e93d19e 100644 --- a/src/dbAccess/access.go +++ b/src/dbAccess/access.go @@ -2,7 +2,6 @@ package dbAccess import ( "context" - "fmt" "git.fjla.uk/owlboard/go-types/pkg/database" "git.fjla.uk/owlboard/mq-client/helpers" @@ -11,6 +10,9 @@ import ( "go.mongodb.org/mongo-driver/mongo/options" ) +const timetableCollection string = "timetable_test" +const databaseName string = "owlboard" + func init() { version := database.Version{ Target: "mq-client", @@ -31,10 +33,27 @@ func init() { } } -func PutManyServices(collection string, data []database.Service) bool { - return false +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 } -func PrintFromDbPackage() { - fmt.Println("hello from the dbAccess package") +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 } diff --git a/src/go.mod b/src/go.mod index 8e94012..a241005 100644 --- a/src/go.mod +++ b/src/go.mod @@ -3,7 +3,7 @@ module git.fjla.uk/owlboard/mq-client go 1.19 require ( - git.fjla.uk/owlboard/go-types v0.0.0-20230721082911-9a574276d572 + git.fjla.uk/owlboard/go-types v0.0.0-20230727192011-171bd3eafd83 github.com/go-stomp/stomp/v3 v3.0.5 go.mongodb.org/mongo-driver v1.12.0 go.uber.org/zap v1.24.0 diff --git a/src/go.sum b/src/go.sum index 81ca724..41565c3 100644 --- a/src/go.sum +++ b/src/go.sum @@ -1,5 +1,9 @@ git.fjla.uk/owlboard/go-types v0.0.0-20230721082911-9a574276d572 h1:shnlNyIV1jG+xQsg5zCt2fEjiDzCQQeDTjTFuKZa97c= git.fjla.uk/owlboard/go-types v0.0.0-20230721082911-9a574276d572/go.mod h1:kG+BX9UF+yJaAVnln/QSKlTdrtKRRReezMeSk1ZLMzY= +git.fjla.uk/owlboard/go-types v0.0.0-20230727191457-d15ddc556312 h1:IolAJJTttdcmykOI73Zjfh3V8Gd01l9TrM+OmliM4h0= +git.fjla.uk/owlboard/go-types v0.0.0-20230727191457-d15ddc556312/go.mod h1:kG+BX9UF+yJaAVnln/QSKlTdrtKRRReezMeSk1ZLMzY= +git.fjla.uk/owlboard/go-types v0.0.0-20230727192011-171bd3eafd83 h1:q+I66M4YVRnKwdyYqcwou7TTniM1uwUSh3Bpa8SDLuM= +git.fjla.uk/owlboard/go-types v0.0.0-20230727192011-171bd3eafd83/go.mod h1:kG+BX9UF+yJaAVnln/QSKlTdrtKRRReezMeSk1ZLMzY= github.com/benbjohnson/clock v1.1.0 h1:Q92kusRqC1XV2MjkWETPvjJVqKetz1OzxZB7mHJLju8= github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= diff --git a/src/vstp/actions.go b/src/vstp/actions.go index 39cfb36..84d727d 100644 --- a/src/vstp/actions.go +++ b/src/vstp/actions.go @@ -4,6 +4,7 @@ import ( "fmt" "git.fjla.uk/owlboard/go-types/pkg/database" + "git.fjla.uk/owlboard/mq-client/dbAccess" "git.fjla.uk/owlboard/mq-client/log" ) @@ -25,6 +26,12 @@ func processEntryType(entry database.Service) { func createEntry(entry database.Service) { log.Msg.Info("Entry Creation requested for: " + entry.TrainUid + " - " + entry.Headcode + " - " + entry.Operator) + status := dbAccess.PutOneService(entry) + if status { + log.Msg.Info("Database entry created") + } else { + log.Msg.Error("Database entry failed, skipped service") + } } func updateEntry(entry database.Service) { @@ -33,5 +40,16 @@ func updateEntry(entry database.Service) { func deleteEntry(entry database.Service) { log.Msg.Info("Entry DELETE requested for: " + entry.TrainUid + " - " + entry.Headcode) - fmt.Printf("%+v\n", entry) + var deletionQuery = database.DeleteQuery{ + TrainUid: entry.TrainUid, + ScheduleStartDate: entry.ScheduleStartDate, + StpIndicator: entry.StpIndicator, + } + status := dbAccess.DeleteOneService(deletionQuery) + if status { + log.Msg.Info("Database entry deleted") + } else { + log.Msg.Error("Database deletion failed, skipped deletion") + fmt.Printf("%+v\n", deletionQuery) + } }