Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore:cleaned null-ability usage #229

Merged
merged 1 commit into from
Sep 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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