Skip to content

Commit

Permalink
Merge branch 'main' into feat/get-news
Browse files Browse the repository at this point in the history
# Conflicts:
#	server/backend/cron/news.go
#	server/model/files.go
  • Loading branch information
CommanderStorm committed Sep 18, 2023
2 parents 32de540 + d991966 commit 54f1cf8
Show file tree
Hide file tree
Showing 11 changed files with 34 additions and 49 deletions.
3 changes: 1 addition & 2 deletions server/backend/cafeteriaRatingDBInitializer.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package backend

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

Expand Down Expand Up @@ -57,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 Down
9 changes: 4 additions & 5 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 @@ -102,14 +101,14 @@ func (c *CronService) parseNewsFeed(source model.NewsSource) error {
break
}
}
var enclosureUrl null.String
var enclosureUrl = null.StringFrom("")
var file *model.Files
if pickedEnclosure != nil {
file, 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 @@ -146,8 +145,8 @@ func (c *CronService) saveImage(url string) (*model.Files, 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),
}
if err := c.db.Clauses(clause.OnConflict{
Columns: []clause.Column{{Name: "path"}},
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 @@ -53,30 +53,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:"index:idx_files_name,unique;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:"index:idx_files_name,unique;column:name;type:text;size:16777215;" json:"name"`
Path string `gorm:"index:idx_files_name,unique;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 54f1cf8

Please sign in to comment.