diff --git a/server/backend/cafeteriaRatingDBInitializer.go b/server/backend/cafeteriaRatingDBInitializer.go index f51dd4db..55c636f1 100644 --- a/server/backend/cafeteriaRatingDBInitializer.go +++ b/server/backend/cafeteriaRatingDBInitializer.go @@ -2,10 +2,8 @@ package backend import ( "database/sql" + "embed" "encoding/json" - "os" - "path/filepath" - "github.com/TUM-Dev/Campus-Backend/server/model" "github.com/guregu/null" log "github.com/sirupsen/logrus" @@ -30,13 +28,16 @@ type nameTag struct { CanBeIncluded []string `json:"canbeincluded"` } +//go:embed static_data +var staticData embed.FS + /* Writes all available tags from the json file into tables in order to make them easier to use. Will be executed once while the server is started. */ func initTagRatingOptions(db *gorm.DB) { - updateTagTable("backend/static_data/dishRatingTags.json", db, DISH) - updateTagTable("backend/static_data/cafeteriaRatingTags.json", db, CAFETERIA) + updateTagTable("static_data/dishRatingTags.json", db, DISH) + updateTagTable("static_data/cafeteriaRatingTags.json", db, CAFETERIA) updateNameTagOptions(db) addEntriesForCronJob(db, "averageRatingComputation", 300) addEntriesForCronJob(db, "dishNameDownload", 302400) //run twice every week @@ -70,8 +71,7 @@ If a tag with the exact german and english name does not exist yet, it will be c Old tags won't be removed to prevent problems with foreign keys. */ func updateNameTagOptions(db *gorm.DB) { - absPathDishNames, _ := filepath.Abs("backend/static_data/dishNameTags.json") - tagsNames := generateNameTagListFromFile(absPathDishNames) + tagsNames := generateNameTagListFromFile("static_data/dishNameTags.json") for _, v := range tagsNames.MultiLanguageNameTags { var parentId int32 res := db.Model(&model.DishNameTagOption{}). @@ -154,8 +154,7 @@ If an entry with the same German and English name exists, the entry won't be add The TagType is used to identify the corresponding model */ func updateTagTable(path string, db *gorm.DB, tagType modelType) { - absPathDish, _ := filepath.Abs(path) - tagsDish := generateRatingTagListFromFile(absPathDish) + tagsDish := generateRatingTagListFromFile(path) insertModel := getTagModel(tagType, db) for _, v := range tagsDish.MultiLanguageTags { var count int64 @@ -200,44 +199,29 @@ func getTagModel(tagType modelType, db *gorm.DB) *gorm.DB { } func generateNameTagListFromFile(path string) multiLanguageNameTags { - file := readFromFile(path) + file, err := staticData.ReadFile(path) + if err != nil { + log.WithError(err).Error("Error including json.") + } var tags multiLanguageNameTags - errjson := json.NewDecoder(file).Decode(&tags) + errjson := json.Unmarshal(file, &tags) if errjson != nil { - log.WithError(errjson).Error("Error while reading the file.") + log.WithError(errjson).Error("Error parsing nameTagList to json.") } - defer func(jsonFile *os.File) { - err := jsonFile.Close() - if err != nil { - log.WithError(err).Error("Error in parsing json.") - } - }(file) return tags } func generateRatingTagListFromFile(path string) multiLanguageTags { - file := readFromFile(path) + file, err := staticData.ReadFile(path) + if err != nil { + log.WithError(err).Error("Error including json.") + } + var tags multiLanguageTags - errjson := json.NewDecoder(file).Decode(&tags) + errjson := json.Unmarshal(file, &tags) if errjson != nil { - log.WithError(errjson).Error("Error while reading or parsing the file.") + log.WithError(errjson).Error("Error parsing ratingTagList to json.") } - defer func(jsonFile *os.File) { - err := jsonFile.Close() - if err != nil { - log.WithError(err).Error("Error in parsing json.") - } - }(file) return tags } - -func readFromFile(path string) *os.File { - jsonFile, err := os.Open(path) - - if err != nil { - log.WithError(err).Error("Unable to open file with path: ", path) - } - - return jsonFile -} diff --git a/server/go.mod b/server/go.mod index 5b276183..5168b965 100644 --- a/server/go.mod +++ b/server/go.mod @@ -24,7 +24,7 @@ require ( google.golang.org/grpc/cmd/protoc-gen-go-grpc v1.3.0 google.golang.org/protobuf v1.31.0 gorm.io/driver/mysql v1.4.7 - gorm.io/gorm v1.25.0 + gorm.io/gorm v1.25.4 ) require ( diff --git a/server/go.sum b/server/go.sum index b78015e2..a0b0a8c3 100644 --- a/server/go.sum +++ b/server/go.sum @@ -362,3 +362,5 @@ gorm.io/gorm v1.23.8/go.mod h1:l2lP/RyAtc1ynaTjFksBde/O8v9oOGIApu2/xRitmZk= gorm.io/gorm v1.24.0/go.mod h1:DVrVomtaYTbqs7gB/x2uVvqnXzv0nqjB396B8cG4dBA= gorm.io/gorm v1.25.0 h1:+KtYtb2roDz14EQe4bla8CbQlmb9dN3VejSai3lprfU= gorm.io/gorm v1.25.0/go.mod h1:L4uxeKpfBml98NYqVqwAdmV1a2nBtAec/cf3fpucW/k= +gorm.io/gorm v1.25.4 h1:iyNd8fNAe8W9dvtlgeRI5zSVZPsq3OpcTu37cYcpCmw= +gorm.io/gorm v1.25.4/go.mod h1:L4uxeKpfBml98NYqVqwAdmV1a2nBtAec/cf3fpucW/k=