Skip to content

Commit

Permalink
Merge branch 'main' into chore/canteen-renaming-1
Browse files Browse the repository at this point in the history
  • Loading branch information
CommanderStorm authored Sep 18, 2023
2 parents fe258ab + d991966 commit 3fe7ecc
Show file tree
Hide file tree
Showing 12 changed files with 58 additions and 85 deletions.
1 change: 1 addition & 0 deletions server/api/generate.bash
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ echo making sure that this script is run from $BASEDIR
pushd $BASEDIR > /dev/null

echo updating the generated files
export PATH="$PATH:$(go env GOPATH)/bin"
buf mod update || exit 1
buf generate || exit 1

Expand Down
58 changes: 22 additions & 36 deletions server/backend/cafeteriaRatingDBInitializer.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
package backend

import (
"database/sql"
"embed"
"encoding/json"
"os"
"path/filepath"

"github.com/TUM-Dev/Campus-Backend/server/model"
"github.com/guregu/null"
Expand All @@ -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
Expand All @@ -55,7 +56,7 @@ func addEntriesForCronJob(db *gorm.DB, cronName string, interval int32) {
errCreate := db.Model(&model.Crontab{}).
Create(&model.Crontab{
Interval: interval,
Type: null.String{NullString: sql.NullString{String: cronName, Valid: true}},
Type: null.StringFrom(cronName),
LastRun: 0,
}).Error
if errCreate != nil {
Expand All @@ -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{}).
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -200,42 +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) {
if err := jsonFile.Close(); 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) {
if err := jsonFile.Close(); 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
}
13 changes: 6 additions & 7 deletions server/backend/cron/news.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package cron

import (
"crypto/md5"
"database/sql"
"errors"
"fmt"
"regexp"
Expand Down Expand Up @@ -91,7 +90,7 @@ func (c *CronService) parseNewsFeed(source model.NewsSource) error {
if !skipNews(existingNewsLinksForSource, item.Link) {
// pick the first enclosure that is an image (if any)
var pickedEnclosure *gofeed.Enclosure
var enclosureUrl = null.String{NullString: sql.NullString{Valid: true, String: ""}}
var enclosureUrl = null.StringFrom("")
for _, enclosure := range item.Enclosures {
if strings.HasSuffix(enclosure.URL, "jpg") ||
strings.HasSuffix(enclosure.URL, "jpeg") ||
Expand All @@ -101,13 +100,13 @@ func (c *CronService) parseNewsFeed(source model.NewsSource) error {
break
}
}
var fileId = null.Int{NullInt64: sql.NullInt64{Valid: false}}
var fileId = null.Int{}
if pickedEnclosure != nil {
fileId, err = c.saveImage(pickedEnclosure.URL)
if err != nil {
log.WithError(err).Error("can't save news image")
}
enclosureUrl = null.String{NullString: sql.NullString{String: pickedEnclosure.URL, Valid: true}}
enclosureUrl = null.StringFrom(pickedEnclosure.URL)
}
bm := bluemonday.StrictPolicy()
sanitizedDesc := bm.Sanitize(item.Description)
Expand Down Expand Up @@ -155,8 +154,8 @@ func (c *CronService) saveImage(url string) (null.Int, error) {
file := model.Files{
Name: targetFileName,
Path: ImageDirectory,
URL: sql.NullString{String: url, Valid: true},
Downloaded: sql.NullBool{Bool: false, Valid: true},
URL: null.StringFrom(url),
Downloaded: null.BoolFrom(false),
}
err := c.db.Transaction(func(tx *gorm.DB) error {
if err := tx.Create(&file).Error; err != nil {
Expand All @@ -169,7 +168,7 @@ func (c *CronService) saveImage(url string) (null.Int, error) {
return null.Int{}, err
}
// creating this int is annoying but i'm too afraid to use real ORM in the model
return null.Int{NullInt64: sql.NullInt64{Int64: int64(file.File), Valid: true}}, nil
return null.IntFrom(int64(file.File)), nil
}

// skipNews returns true if link is in existingLinks or link is invalid
Expand Down
8 changes: 3 additions & 5 deletions server/backend/migration/20210709193000.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package migration

import (
"database/sql"

"github.com/TUM-Dev/Campus-Backend/server/model"
"github.com/go-gormigrate/gormigrate/v2"
"github.com/guregu/null"
Expand All @@ -18,8 +16,8 @@ func (m TumDBMigrator) migrate20210709193000() *gormigrate.Migration {
ID: "20210709193000",
Migrate: func(tx *gorm.DB) error {
type Files struct {
URL sql.NullString `gorm:"column:url;default:null;" json:"url"` // URL of the file source (if any)
Downloaded sql.NullBool `gorm:"column:downloaded;type:boolean;default:1;" json:"downloaded"` // true when file is ready to be served, false when still being downloaded
URL null.String `gorm:"column:url;default:null;" json:"url"` // URL of the file source (if any)
Downloaded null.Bool `gorm:"column:downloaded;type:boolean;default:1;" json:"downloaded"` // true when file is ready to be served, false when still being downloaded
}
if err := tx.AutoMigrate(
&Files{},
Expand All @@ -29,7 +27,7 @@ func (m TumDBMigrator) migrate20210709193000() *gormigrate.Migration {
}
return tx.Create(&model.Crontab{
Interval: 300,
Type: null.String{NullString: sql.NullString{String: "fileDownload", Valid: true}},
Type: null.StringFrom("fileDownload"),
}).Error
},
Rollback: func(tx *gorm.DB) error {
Expand Down
10 changes: 2 additions & 8 deletions server/backend/migration/20221119131300.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package migration

import (
"database/sql"
_ "embed"
"encoding/json"

Expand Down Expand Up @@ -53,7 +52,7 @@ func (m TumDBMigrator) migrate20221119131300() *gormigrate.Migration {

err := tx.Create(&model.Crontab{
Interval: 60,
Type: null.String{NullString: sql.NullString{String: cron.IOSNotifications, Valid: true}},
Type: null.StringFrom(cron.IOSNotifications),
}).Error

if err != nil {
Expand All @@ -62,12 +61,7 @@ func (m TumDBMigrator) migrate20221119131300() *gormigrate.Migration {
}

return tx.Create(&model.Crontab{
Type: null.String{
NullString: sql.NullString{
String: cron.IOSActivityReset,
Valid: true,
},
},
Type: null.StringFrom(cron.IOSActivityReset),
Interval: 86400,
}).Error
},
Expand Down
4 changes: 1 addition & 3 deletions server/backend/migration/20221210000000.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package migration

import (
"database/sql"

"github.com/TUM-Dev/Campus-Backend/server/model"
"github.com/go-gormigrate/gormigrate/v2"
"github.com/guregu/null"
Expand Down Expand Up @@ -30,7 +28,7 @@ func (m TumDBMigrator) migrate20221210000000() *gormigrate.Migration {

return tx.Create(&model.Crontab{
Interval: 60 * 5, // Every 5 minutes
Type: null.String{NullString: sql.NullString{String: "canteenHeadCount", Valid: true}},
Type: null.StringFrom("canteenHeadCount"),
}).Error
},

Expand Down
4 changes: 1 addition & 3 deletions server/backend/migration/20230825000000.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package migration

import (
"database/sql"

"github.com/TUM-Dev/Campus-Backend/server/model"
"github.com/go-gormigrate/gormigrate/v2"
"github.com/guregu/null"
Expand All @@ -26,7 +24,7 @@ func (m TumDBMigrator) migrate20230825000000() *gormigrate.Migration {
}
return tx.Create(&model.Crontab{
Interval: 60 * 10, // Every 10 minutes
Type: null.String{NullString: sql.NullString{String: "chat", Valid: true}},
Type: null.StringFrom("chat"),
}).Error
},
}
Expand Down
4 changes: 1 addition & 3 deletions server/backend/migration/20230904000000.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package migration

import (
"database/sql"

"github.com/TUM-Dev/Campus-Backend/server/model"
"github.com/go-gormigrate/gormigrate/v2"
"github.com/guregu/null"
Expand Down Expand Up @@ -31,7 +29,7 @@ func (m TumDBMigrator) migrate20230904000000() *gormigrate.Migration {
}
return tx.Create(&model.Crontab{
Interval: 60 * 10, // Every 10 minutes
Type: null.String{NullString: sql.NullString{String: "ticketsales", Valid: true}},
Type: null.StringFrom("ticketsales"),
}).Error
},
}
Expand Down
8 changes: 4 additions & 4 deletions server/backend/newsAlerts_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,11 +60,11 @@ func (s *NewsAlertSuite) Test_GetTopNewsOne() {
Name: "Tournament_app_02-02.png",
Path: "newsalerts/",
Downloads: 0,
URL: sql.NullString{Valid: false},
Downloaded: sql.NullBool{Bool: true, Valid: true},
URL: null.String{},
Downloaded: null.Bool{},
},
Name: null.String{NullString: sql.NullString{String: "Exzellenzuniversität", Valid: true}},
Link: null.String{NullString: sql.NullString{String: "https://tum.de", Valid: true}},
Name: null.StringFrom("Exzellenzuniversität"),
Link: null.StringFrom("https://tum.de"),
Created: time.Time.Add(time.Now(), time.Hour*-4),
From: time.Time.Add(time.Now(), time.Hour*-2),
To: time.Time.Add(time.Now(), time.Hour*2),
Expand Down
12 changes: 6 additions & 6 deletions server/backend/news_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,30 +52,30 @@ func file(id int32) *model.Files {
Name: fmt.Sprintf("src_%d.png", id),
Path: "news/sources",
Downloads: 1,
URL: sql.NullString{Valid: false},
Downloaded: sql.NullBool{Bool: true, Valid: true},
URL: null.String{},
Downloaded: null.BoolFrom(true),
}
}

func source1() *model.NewsSource {
return &model.NewsSource{
Source: 1,
Title: "Amazing News 1",
URL: null.String{NullString: sql.NullString{String: "https://example.com/amazing1", Valid: true}},
URL: null.StringFrom("https://example.com/amazing1"),
FilesID: file(2).File,
Files: *file(2),
Hook: null.String{NullString: sql.NullString{String: "", Valid: true}},
Hook: null.StringFrom(""),
}
}

func source2() *model.NewsSource {
return &model.NewsSource{
Source: 2,
Title: "Amazing News 2",
URL: null.String{NullString: sql.NullString{String: "https://example.com/amazing2", Valid: true}},
URL: null.StringFrom("https://example.com/amazing2"),
FilesID: file(2).File,
Files: *file(2),
Hook: null.String{NullString: sql.NullString{String: "hook", Valid: true}},
Hook: null.StringFrom("hook"),
}
}

Expand Down
12 changes: 6 additions & 6 deletions server/model/files.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,12 @@ var (

// Files struct is a row record of the files table in the tca database
type Files struct {
File int32 `gorm:"primary_key;AUTO_INCREMENT;column:file;type:int;" json:"file"`
Name string `gorm:"column:name;type:text;size:16777215;" json:"name"`
Path string `gorm:"column:path;type:text;size:16777215;" json:"path"`
Downloads int32 `gorm:"column:downloads;type:int;default:0;" json:"downloads"`
URL sql.NullString `gorm:"column:url;default:null;" json:"url"` // URL of the files source (if any)
Downloaded sql.NullBool `gorm:"column:downloaded;type:boolean;default:1;" json:"downloaded"` // true when file is ready to be served, false when still being downloaded
File int32 `gorm:"primary_key;AUTO_INCREMENT;column:file;type:int;" json:"file"`
Name string `gorm:"column:name;type:text;size:16777215;" json:"name"`
Path string `gorm:"column:path;type:text;size:16777215;" json:"path"`
Downloads int32 `gorm:"column:downloads;type:int;default:0;" json:"downloads"`
URL null.String `gorm:"column:url;default:null;" json:"url"` // URL of the files source (if any)
Downloaded null.Bool `gorm:"column:downloaded;type:boolean;default:1;" json:"downloaded"` // true when file is ready to be served, false when still being downloaded
}

// TableName sets the insert table name for this struct type
Expand Down
9 changes: 5 additions & 4 deletions server/model/iosDeviceLastUpdated.go
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
package model

import (
"database/sql"
"fmt"

"github.com/guregu/null"
)

// IOSDeviceLastUpdated used as a result of a query that joins
// IOSDevice and IOSDeviceRequestLog tables.
type IOSDeviceLastUpdated struct {
DeviceID string `json:"deviceId"`
LastUpdated sql.NullTime `json:"lastUpdated"`
PublicKey string `json:"publicKey"`
DeviceID string `json:"deviceId"`
LastUpdated null.Time `json:"lastUpdated"`
PublicKey string `json:"publicKey"`
}

func (device *IOSDeviceLastUpdated) String() string {
Expand Down

0 comments on commit 3fe7ecc

Please sign in to comment.