From a75a33f336e489ca6343f8fe605446b72acf9b59 Mon Sep 17 00:00:00 2001 From: Frank Elsinga Date: Fri, 22 Sep 2023 13:02:19 +0200 Subject: [PATCH] renamed `Files` -> `File` to be consistent with other models --- server/backend/cron/fileDownload.go | 6 +- server/backend/cron/movies.go | 6 +- server/backend/cron/news.go | 12 ++-- server/backend/migration/20230904100000.go | 10 +-- server/backend/migration/migration.go | 2 +- server/backend/movie.go | 8 +-- server/backend/movie_test.go | 12 ++-- server/backend/news.go | 6 +- server/backend/newsAlerts.go | 4 +- server/backend/newsAlerts_test.go | 12 ++-- server/backend/news_test.go | 72 +++++++++++----------- server/model/files.go | 9 +-- server/model/kino.go | 4 +- server/model/news.go | 4 +- server/model/news_alert.go | 4 +- server/model/news_source.go | 12 ++-- 16 files changed, 89 insertions(+), 94 deletions(-) diff --git a/server/backend/cron/fileDownload.go b/server/backend/cron/fileDownload.go index 3e16f5a0..8b2706a0 100644 --- a/server/backend/cron/fileDownload.go +++ b/server/backend/cron/fileDownload.go @@ -18,7 +18,7 @@ import ( // fileDownloadCron downloads all files that are not marked as finished in the database func (c *CronService) fileDownloadCron() error { return c.db.Transaction(func(tx *gorm.DB) error { - var files []model.Files + var files []model.File err := tx.Find(&files, "downloaded = 0 AND url IS NOT NULL").Error if err != nil && !errors.Is(err, gorm.ErrRecordNotFound) { log.WithError(err).Error("Could not get files from database") @@ -30,7 +30,7 @@ func (c *CronService) fileDownloadCron() error { fields := log.Fields{"url": file.URL.String, "dstPath": dstPath} log.WithFields(fields).Info("downloading file") - if err = tx.Model(&model.Files{File: file.File}).Update("downloads", file.Downloads+1).Error; err != nil { + if err = tx.Model(&model.File{File: file.File}).Update("downloads", file.Downloads+1).Error; err != nil { log.WithError(err).WithFields(fields).Error("Could not set update the download-count") continue } @@ -48,7 +48,7 @@ func (c *CronService) fileDownloadCron() error { continue } // everything went well => we can mark the file as downloaded - if err = tx.Model(&model.Files{URL: file.URL}).Update("downloaded", true).Error; err != nil { + if err = tx.Model(&model.File{URL: file.URL}).Update("downloaded", true).Error; err != nil { log.WithError(err).WithFields(fields).Error("Could not set image to downloaded.") continue } diff --git a/server/backend/cron/movies.go b/server/backend/cron/movies.go index ebba1075..9c89ced7 100644 --- a/server/backend/cron/movies.go +++ b/server/backend/cron/movies.go @@ -78,7 +78,7 @@ func (c *CronService) movieCron() error { } // add a file to preview (downloaded in another cronjob) - file := model.Files{ + file := model.File{ Name: item.Title, Path: MovieImageDirectory, URL: null.StringFrom(item.Enclosure.Url), @@ -99,8 +99,8 @@ func (c *CronService) movieCron() error { Actors: omdbMovie.Actors, ImdbRating: omdbMovie.ImdbRating, Description: omdbMovie.Plot, // we get this from imdb as tu-fim does truncate their plot - FilesID: file.File, - Files: file, + FileID: file.File, + File: file, Link: item.Link, } if err := c.db.Create(&movie).Error; err != nil { diff --git a/server/backend/cron/news.go b/server/backend/cron/news.go index 43ed0f4b..640754e2 100644 --- a/server/backend/cron/news.go +++ b/server/backend/cron/news.go @@ -100,7 +100,7 @@ func (c *CronService) parseNewsFeed(source model.NewsSource) error { } } var enclosureUrl = null.StringFrom("") - var file *model.Files + var file *model.File if pickedEnclosure != nil { file, err = c.saveImage(pickedEnclosure.URL) if err != nil { @@ -119,8 +119,8 @@ func (c *CronService) parseNewsFeed(source model.NewsSource) error { Src: source.Source, Link: item.Link, Image: enclosureUrl, - FilesID: null.IntFrom(file.File), - Files: file, + FileID: null.IntFrom(file.File), + File: file, } newNews = append(newNews, newsItem) } @@ -138,9 +138,9 @@ func (c *CronService) parseNewsFeed(source model.NewsSource) error { } // saveImage saves an image to the database, so it can be downloaded by another cronjob and returns its id -func (c *CronService) saveImage(url string) (*model.Files, error) { +func (c *CronService) saveImage(url string) (*model.File, error) { targetFileName := fmt.Sprintf("%x.jpg", md5.Sum([]byte(url))) - file := model.Files{ + file := model.File{ Name: targetFileName, // path intentionally omitted } if err := c.db.First(&file).Error; err != nil && !errors.Is(err, gorm.ErrRecordNotFound) { @@ -151,7 +151,7 @@ func (c *CronService) saveImage(url string) (*model.Files, error) { } // does not exist, store in database - file = model.Files{ + file = model.File{ Name: targetFileName, Path: NewsImageDirectory, URL: null.StringFrom(url), diff --git a/server/backend/migration/20230904100000.go b/server/backend/migration/20230904100000.go index e439ddfb..0af99f79 100644 --- a/server/backend/migration/20230904100000.go +++ b/server/backend/migration/20230904100000.go @@ -45,11 +45,11 @@ func (m TumDBMigrator) migrate20230904100000() *gormigrate.Migration { return err } if err := tx.Create(&model.NewsSource{ - Source: 2, - Title: "TU Film", - URL: null.StringFrom("http://www.tu-film.de/programm/index/upcoming.rss"), - FilesID: 2, - Hook: null.String{}, + Source: 2, + Title: "TU Film", + URL: null.StringFrom("http://www.tu-film.de/programm/index/upcoming.rss"), + FileID: 2, + Hook: null.String{}, }).Error; err != nil { return err } diff --git a/server/backend/migration/migration.go b/server/backend/migration/migration.go index 2b34e95f..b5dcbcf5 100644 --- a/server/backend/migration/migration.go +++ b/server/backend/migration/migration.go @@ -26,7 +26,7 @@ func (m TumDBMigrator) Migrate() error { err := m.database.AutoMigrate( &model.TopNews{}, &model.Crontab{}, - &model.Files{}, + &model.File{}, &model.NewsSource{}, &model.NewsAlert{}, &model.News{}, diff --git a/server/backend/movie.go b/server/backend/movie.go index 71be0023..ff5b177e 100644 --- a/server/backend/movie.go +++ b/server/backend/movie.go @@ -13,7 +13,7 @@ import ( func (s *CampusServer) GetMovies(ctx context.Context, req *pb.GetMoviesRequest) (*pb.GetMoviesReply, error) { var movies []model.Kino - if err := s.db.WithContext(ctx).Joins("Files").Find(&movies, "kino > ?", req.LastId).Error; err != nil { + if err := s.db.WithContext(ctx).Joins("File").Find(&movies, "kino > ?", req.LastId).Error; err != nil { log.WithError(err).Error("Error while fetching movies from database") return nil, status.Error(codes.Internal, "Error while fetching movies from database") } @@ -31,9 +31,9 @@ func (s *CampusServer) GetMovies(ctx context.Context, req *pb.GetMoviesRequest) Actors: movie.Actors, ImdbRating: movie.ImdbRating, Description: movie.Description, - CoverName: movie.Files.Name, - CoverPath: movie.Files.Path, - CoverId: movie.Files.File, + CoverName: movie.File.Name, + CoverPath: movie.File.Path, + CoverId: movie.File.File, Link: movie.Link, }) } diff --git a/server/backend/movie_test.go b/server/backend/movie_test.go index 0cab874e..0c2dd31d 100644 --- a/server/backend/movie_test.go +++ b/server/backend/movie_test.go @@ -79,9 +79,9 @@ var ( func (s *MovieSuite) Test_GetMoviesAll() { server := CampusServer{db: s.DB} - s.mock.ExpectQuery("SELECT `kino`.`kino`,`kino`.`date`,`kino`.`created`,`kino`.`title`,`kino`.`year`,`kino`.`runtime`,`kino`.`genre`,`kino`.`director`,`kino`.`actors`,`kino`.`rating`,`kino`.`description`,`kino`.`trailer`,`kino`.`cover`,`kino`.`link`,`Files`.`file` AS `Files__file`,`Files`.`name` AS `Files__name`,`Files`.`path` AS `Files__path`,`Files`.`downloads` AS `Files__downloads`,`Files`.`url` AS `Files__url`,`Files`.`downloaded` AS `Files__downloaded` FROM `kino` LEFT JOIN `files` `Files` ON `kino`.`cover` = `Files`.`file` WHERE kino > ?"). + s.mock.ExpectQuery("SELECT `kino`.`kino`,`kino`.`date`,`kino`.`created`,`kino`.`title`,`kino`.`year`,`kino`.`runtime`,`kino`.`genre`,`kino`.`director`,`kino`.`actors`,`kino`.`rating`,`kino`.`description`,`kino`.`trailer`,`kino`.`cover`,`kino`.`link`,`File`.`file` AS `File__file`,`File`.`name` AS `File__name`,`File`.`path` AS `File__path`,`File`.`downloads` AS `File__downloads`,`File`.`url` AS `File__url`,`File`.`downloaded` AS `File__downloaded` FROM `kino` LEFT JOIN `files` `File` ON `kino`.`cover` = `File`.`file` WHERE kino > ?"). WithArgs(-1). - WillReturnRows(sqlmock.NewRows([]string{"kino", "date", "created", "title", "year", "runtime", "genre", "director", "actors", "rating", "description", "trailer", "cover", "link", "Files__file", "Files__name", "Files__path", "Files__downloads", "Files__url", "Files__downloaded"}). + WillReturnRows(sqlmock.NewRows([]string{"kino", "date", "created", "title", "year", "runtime", "genre", "director", "actors", "rating", "description", "trailer", "cover", "link", "File__file", "File__name", "File__path", "File__downloads", "File__url", "File__downloaded"}). AddRow(movie2.MovieId, movie2.Date.AsTime(), movie2.Created.AsTime(), movie2.Title, movie2.ReleaseYear, movie2.Runtime, movie2.Genre, movie2.Director, movie2.Actors, movie2.ImdbRating, movie2.Description, nil, movie2.CoverId, movie2.Link, movie2.CoverId, movie2.CoverName, movie2.CoverPath, 1, "", 1). AddRow(movie1.MovieId, movie1.Date.AsTime(), movie1.Created.AsTime(), movie1.Title, movie1.ReleaseYear, movie1.Runtime, movie1.Genre, movie1.Director, movie1.Actors, movie1.ImdbRating, movie1.Description, nil, movie1.CoverId, movie1.Link, movie1.CoverId, movie1.CoverName, movie1.CoverPath, 1, "", 1)) response, err := server.GetMovies(context.Background(), &pb.GetMoviesRequest{LastId: -1}) @@ -91,9 +91,9 @@ func (s *MovieSuite) Test_GetMoviesAll() { func (s *MovieSuite) Test_GetMoviesOne() { server := CampusServer{db: s.DB} - s.mock.ExpectQuery("SELECT `kino`.`kino`,`kino`.`date`,`kino`.`created`,`kino`.`title`,`kino`.`year`,`kino`.`runtime`,`kino`.`genre`,`kino`.`director`,`kino`.`actors`,`kino`.`rating`,`kino`.`description`,`kino`.`trailer`,`kino`.`cover`,`kino`.`link`,`Files`.`file` AS `Files__file`,`Files`.`name` AS `Files__name`,`Files`.`path` AS `Files__path`,`Files`.`downloads` AS `Files__downloads`,`Files`.`url` AS `Files__url`,`Files`.`downloaded` AS `Files__downloaded` FROM `kino` LEFT JOIN `files` `Files` ON `kino`.`cover` = `Files`.`file` WHERE kino > ?"). + s.mock.ExpectQuery("SELECT `kino`.`kino`,`kino`.`date`,`kino`.`created`,`kino`.`title`,`kino`.`year`,`kino`.`runtime`,`kino`.`genre`,`kino`.`director`,`kino`.`actors`,`kino`.`rating`,`kino`.`description`,`kino`.`trailer`,`kino`.`cover`,`kino`.`link`,`File`.`file` AS `File__file`,`File`.`name` AS `File__name`,`File`.`path` AS `File__path`,`File`.`downloads` AS `File__downloads`,`File`.`url` AS `File__url`,`File`.`downloaded` AS `File__downloaded` FROM `kino` LEFT JOIN `files` `File` ON `kino`.`cover` = `File`.`file` WHERE kino > ?"). WithArgs(1). - WillReturnRows(sqlmock.NewRows([]string{"kino", "date", "created", "title", "year", "runtime", "genre", "director", "actors", "rating", "description", "trailer", "cover", "link", "Files__file", "Files__name", "Files__path", "Files__downloads", "Files__url", "Files__downloaded"}). + WillReturnRows(sqlmock.NewRows([]string{"kino", "date", "created", "title", "year", "runtime", "genre", "director", "actors", "rating", "description", "trailer", "cover", "link", "File__file", "File__name", "File__path", "File__downloads", "File__url", "File__downloaded"}). AddRow(movie1.MovieId, movie1.Date.AsTime(), movie1.Created.AsTime(), movie1.Title, movie1.ReleaseYear, movie1.Runtime, movie1.Genre, movie1.Director, movie1.Actors, movie1.ImdbRating, movie1.Description, nil, movie1.CoverId, movie1.Link, movie1.CoverId, movie1.CoverName, movie1.CoverPath, 1, "", 1)) response, err := server.GetMovies(context.Background(), &pb.GetMoviesRequest{LastId: 1}) require.NoError(s.T(), err) @@ -102,9 +102,9 @@ func (s *MovieSuite) Test_GetMoviesOne() { func (s *MovieSuite) Test_GetMoviesNone() { server := CampusServer{db: s.DB} - s.mock.ExpectQuery("SELECT `kino`.`kino`,`kino`.`date`,`kino`.`created`,`kino`.`title`,`kino`.`year`,`kino`.`runtime`,`kino`.`genre`,`kino`.`director`,`kino`.`actors`,`kino`.`rating`,`kino`.`description`,`kino`.`trailer`,`kino`.`cover`,`kino`.`link`,`Files`.`file` AS `Files__file`,`Files`.`name` AS `Files__name`,`Files`.`path` AS `Files__path`,`Files`.`downloads` AS `Files__downloads`,`Files`.`url` AS `Files__url`,`Files`.`downloaded` AS `Files__downloaded` FROM `kino` LEFT JOIN `files` `Files` ON `kino`.`cover` = `Files`.`file` WHERE kino > ?"). + s.mock.ExpectQuery("SELECT `kino`.`kino`,`kino`.`date`,`kino`.`created`,`kino`.`title`,`kino`.`year`,`kino`.`runtime`,`kino`.`genre`,`kino`.`director`,`kino`.`actors`,`kino`.`rating`,`kino`.`description`,`kino`.`trailer`,`kino`.`cover`,`kino`.`link`,`File`.`file` AS `File__file`,`File`.`name` AS `File__name`,`File`.`path` AS `File__path`,`File`.`downloads` AS `File__downloads`,`File`.`url` AS `File__url`,`File`.`downloaded` AS `File__downloaded` FROM `kino` LEFT JOIN `files` `File` ON `kino`.`cover` = `File`.`file` WHERE kino > ?"). WithArgs(42). - WillReturnRows(sqlmock.NewRows([]string{"kino", "date", "created", "title", "year", "runtime", "genre", "director", "actors", "rating", "description", "trailer", "cover", "link", "Files__file", "Files__name", "Files__path", "Files__downloads", "Files__url", "Files__downloaded"})) + WillReturnRows(sqlmock.NewRows([]string{"kino", "date", "created", "title", "year", "runtime", "genre", "director", "actors", "rating", "description", "trailer", "cover", "link", "File__file", "File__name", "File__path", "File__downloads", "File__url", "File__downloaded"})) response, err := server.GetMovies(context.Background(), &pb.GetMoviesRequest{LastId: 42}) require.NoError(s.T(), err) require.Equal(s.T(), &pb.GetMoviesReply{Movies: []*pb.Movie(nil)}, response) diff --git a/server/backend/news.go b/server/backend/news.go index f2e3a58a..23acd183 100644 --- a/server/backend/news.go +++ b/server/backend/news.go @@ -19,7 +19,7 @@ func (s *CampusServer) GetNewsSources(ctx context.Context, _ *pb.GetNewsSourcesR } var sources []model.NewsSource - if err := s.db.WithContext(ctx).Joins("Files").Find(&sources).Error; err != nil { + if err := s.db.WithContext(ctx).Joins("File").Find(&sources).Error; err != nil { log.WithError(err).Error("could not find newsSources") return nil, status.Error(codes.Internal, "could not GetNewsSources") } @@ -30,7 +30,7 @@ func (s *CampusServer) GetNewsSources(ctx context.Context, _ *pb.GetNewsSourcesR resp = append(resp, &pb.NewsSource{ Source: fmt.Sprintf("%d", source.Source), Title: source.Title, - Icon: source.Files.URL.String, + Icon: source.File.URL.String, }) } return &pb.GetNewsSourcesReply{Sources: resp}, nil @@ -42,7 +42,7 @@ func (s *CampusServer) GetNews(ctx context.Context, req *pb.GetNewsRequest) (*pb } var newsEntries []model.News - tx := s.db.WithContext(ctx).Joins("Files") + tx := s.db.WithContext(ctx).Joins("File") if req.NewsSource != 0 { tx = tx.Where("src = ?", req.NewsSource) } diff --git a/server/backend/newsAlerts.go b/server/backend/newsAlerts.go index 6189b256..2501ab21 100644 --- a/server/backend/newsAlerts.go +++ b/server/backend/newsAlerts.go @@ -19,7 +19,7 @@ func (s *CampusServer) GetTopNews(ctx context.Context, _ *pb.GetTopNewsRequest) } var res *model.NewsAlert - err := s.db.WithContext(ctx).Joins("Files").Where("NOW() between `from` and `to`").First(&res).Error + err := s.db.WithContext(ctx).Joins("File").Where("NOW() between `from` and `to`").First(&res).Error if errors.Is(err, gorm.ErrRecordNotFound) { return nil, status.Error(codes.NotFound, "no current active top news") } else if err != nil { @@ -28,7 +28,7 @@ func (s *CampusServer) GetTopNews(ctx context.Context, _ *pb.GetTopNewsRequest) } return &pb.GetTopNewsReply{ - ImageUrl: res.Files.URL.String, + ImageUrl: res.File.URL.String, Link: res.Link.String, Created: timestamppb.New(res.Created), From: timestamppb.New(res.From), diff --git a/server/backend/newsAlerts_test.go b/server/backend/newsAlerts_test.go index ffbb0579..36e24196 100644 --- a/server/backend/newsAlerts_test.go +++ b/server/backend/newsAlerts_test.go @@ -49,13 +49,13 @@ func (s *NewsAlertSuite) SetupSuite() { s.deviceBuf = newDeviceBuffer() } -const ExpectedGetTopNewsQuery = "SELECT `news_alert`.`news_alert`,`news_alert`.`file`,`news_alert`.`name`,`news_alert`.`link`,`news_alert`.`created`,`news_alert`.`from`,`news_alert`.`to`,`Files`.`file` AS `Files__file`,`Files`.`name` AS `Files__name`,`Files`.`path` AS `Files__path`,`Files`.`downloads` AS `Files__downloads`,`Files`.`url` AS `Files__url`,`Files`.`downloaded` AS `Files__downloaded` FROM `news_alert` LEFT JOIN `files` `Files` ON `news_alert`.`file` = `Files`.`file` WHERE NOW() between `from` and `to` ORDER BY `news_alert`.`news_alert` LIMIT 1" +const ExpectedGetTopNewsQuery = "SELECT `news_alert`.`news_alert`,`news_alert`.`file`,`news_alert`.`name`,`news_alert`.`link`,`news_alert`.`created`,`news_alert`.`from`,`news_alert`.`to`,`File`.`file` AS `File__file`,`File`.`name` AS `File__name`,`File`.`path` AS `File__path`,`File`.`downloads` AS `File__downloads`,`File`.`url` AS `File__url`,`File`.`downloaded` AS `File__downloaded` FROM `news_alert` LEFT JOIN `files` `File` ON `news_alert`.`file` = `File`.`file` WHERE NOW() between `from` and `to` ORDER BY `news_alert`.`news_alert` LIMIT 1" func (s *NewsAlertSuite) Test_GetTopNewsOne() { expectedAlert := model.NewsAlert{ NewsAlert: 1, - FilesID: 3001, - Files: model.Files{ + FileID: 3001, + File: model.File{ File: 3001, Name: "Tournament_app_02-02.png", Path: "newsalerts/", @@ -70,15 +70,15 @@ func (s *NewsAlertSuite) Test_GetTopNewsOne() { To: time.Time.Add(time.Now(), time.Hour*2), } s.mock.ExpectQuery(regexp.QuoteMeta(ExpectedGetTopNewsQuery)). - WillReturnRows(sqlmock.NewRows([]string{"news_alert", "file", "name", "link", "created", "from", "to", "Files__file", "Files__name", "Files__path", "Files__downloads", "Files__url", "Files__downloaded"}). - AddRow(expectedAlert.NewsAlert, expectedAlert.FilesID, expectedAlert.Name, expectedAlert.Link, expectedAlert.Created, expectedAlert.From, expectedAlert.To, expectedAlert.Files.File, expectedAlert.Files.Name, expectedAlert.Files.Path, expectedAlert.Files.Downloads, expectedAlert.Files.URL, expectedAlert.Files.Downloaded)) + WillReturnRows(sqlmock.NewRows([]string{"news_alert", "file", "name", "link", "created", "from", "to", "File__file", "File__name", "File__path", "File__downloads", "File__url", "File__downloaded"}). + AddRow(expectedAlert.NewsAlert, expectedAlert.FileID, expectedAlert.Name, expectedAlert.Link, expectedAlert.Created, expectedAlert.From, expectedAlert.To, expectedAlert.File.File, expectedAlert.File.Name, expectedAlert.File.Path, expectedAlert.File.Downloads, expectedAlert.File.URL, expectedAlert.File.Downloaded)) meta := metadata.MD{} server := CampusServer{db: s.DB, deviceBuf: s.deviceBuf} response, err := server.GetTopNews(metadata.NewIncomingContext(context.Background(), meta), nil) require.NoError(s.T(), err) require.Equal(s.T(), &pb.GetTopNewsReply{ - ImageUrl: expectedAlert.Files.URL.String, + ImageUrl: expectedAlert.File.URL.String, Link: expectedAlert.Link.String, Created: timestamppb.New(expectedAlert.Created), From: timestamppb.New(expectedAlert.From), diff --git a/server/backend/news_test.go b/server/backend/news_test.go index 2bbafbf6..37ef4be3 100644 --- a/server/backend/news_test.go +++ b/server/backend/news_test.go @@ -48,8 +48,8 @@ func (s *NewsSuite) SetupSuite() { s.deviceBuf = newDeviceBuffer() } -func file(id int64) *model.Files { - return &model.Files{ +func file(id int64) *model.File { + return &model.File{ File: id, Name: fmt.Sprintf("src_%d.png", id), Path: "news/sources", @@ -61,33 +61,33 @@ func file(id int64) *model.Files { func source1() *model.NewsSource { return &model.NewsSource{ - Source: 1, - Title: "Amazing News 1", - URL: null.StringFrom("https://example.com/amazing1"), - FilesID: file(2).File, - Files: *file(2), - Hook: null.StringFrom(""), + Source: 1, + Title: "Amazing News 1", + URL: null.StringFrom("https://example.com/amazing1"), + FileID: file(2).File, + File: *file(2), + Hook: null.StringFrom(""), } } func source2() *model.NewsSource { return &model.NewsSource{ - Source: 2, - Title: "Amazing News 2", - URL: null.StringFrom("https://example.com/amazing2"), - FilesID: file(2).File, - Files: *file(2), - Hook: null.StringFrom("hook"), + Source: 2, + Title: "Amazing News 2", + URL: null.StringFrom("https://example.com/amazing2"), + FileID: file(2).File, + File: *file(2), + Hook: null.StringFrom("hook"), } } -const ExpectedGetSourceQuery = "SELECT `newsSource`.`source`,`newsSource`.`title`,`newsSource`.`url`,`newsSource`.`icon`,`newsSource`.`hook`,`Files`.`file` AS `Files__file`,`Files`.`name` AS `Files__name`,`Files`.`path` AS `Files__path`,`Files`.`downloads` AS `Files__downloads`,`Files`.`url` AS `Files__url`,`Files`.`downloaded` AS `Files__downloaded` FROM `newsSource` LEFT JOIN `files` `Files` ON `newsSource`.`icon` = `Files`.`file`" +const ExpectedGetSourceQuery = "SELECT `newsSource`.`source`,`newsSource`.`title`,`newsSource`.`url`,`newsSource`.`icon`,`newsSource`.`hook`,`File`.`file` AS `File__file`,`File`.`name` AS `File__name`,`File`.`path` AS `File__path`,`File`.`downloads` AS `File__downloads`,`File`.`url` AS `File__url`,`File`.`downloaded` AS `File__downloaded` FROM `newsSource` LEFT JOIN `files` `File` ON `newsSource`.`icon` = `File`.`file`" func (s *NewsSuite) Test_GetNewsSourcesMultiple() { s.mock.ExpectQuery(regexp.QuoteMeta(ExpectedGetSourceQuery)). - WillReturnRows(sqlmock.NewRows([]string{"source", "title", "url", "icon", "hook", "Files__file", "Files__name", "Files__path", "Files__downloads", "Files__url", "Files__downloaded"}). - AddRow(source1().Source, source1().Title, source1().URL, source1().FilesID, source1().Hook, source1().Files.File, source1().Files.Name, source1().Files.Path, source1().Files.Downloads, source1().Files.URL, source1().Files.Downloaded). - AddRow(source2().Source, source2().Title, source2().URL, source2().FilesID, source2().Hook, source2().Files.File, source2().Files.Name, source2().Files.Path, source2().Files.Downloads, source2().Files.URL, source2().Files.Downloaded)) + WillReturnRows(sqlmock.NewRows([]string{"source", "title", "url", "icon", "hook", "File__file", "File__name", "File__path", "File__downloads", "File__url", "File__downloaded"}). + AddRow(source1().Source, source1().Title, source1().URL, source1().FileID, source1().Hook, source1().File.File, source1().File.Name, source1().File.Path, source1().File.Downloads, source1().File.URL, source1().File.Downloaded). + AddRow(source2().Source, source2().Title, source2().URL, source2().FileID, source2().Hook, source2().File.File, source2().File.Name, source2().File.Path, source2().File.Downloads, source2().File.URL, source2().File.Downloaded)) meta := metadata.MD{} server := CampusServer{db: s.DB, deviceBuf: s.deviceBuf} @@ -95,8 +95,8 @@ func (s *NewsSuite) Test_GetNewsSourcesMultiple() { require.NoError(s.T(), err) expectedResp := &pb.GetNewsSourcesReply{ Sources: []*pb.NewsSource{ - {Source: fmt.Sprintf("%d", source1().Source), Title: source1().Title, Icon: source1().Files.URL.String}, - {Source: fmt.Sprintf("%d", source2().Source), Title: source2().Title, Icon: source2().Files.URL.String}, + {Source: fmt.Sprintf("%d", source1().Source), Title: source1().Title, Icon: source1().File.URL.String}, + {Source: fmt.Sprintf("%d", source2().Source), Title: source2().Title, Icon: source2().File.URL.String}, }, } require.Equal(s.T(), expectedResp, response) @@ -104,27 +104,27 @@ func (s *NewsSuite) Test_GetNewsSourcesMultiple() { func news1() *model.News { return &model.News{ - News: 1, - Title: "Amazing News 1", - Link: "https://example.com/amazing2", - FilesID: null.IntFrom(file(1).File), - Files: file(1), + News: 1, + Title: "Amazing News 1", + Link: "https://example.com/amazing2", + FileID: null.IntFrom(file(1).File), + File: file(1), } } func news2() *model.News { return &model.News{ - News: 2, - Title: "Amazing News 2", - Link: "https://example.com/amazing2", - FilesID: null.Int{}, - Files: nil, + News: 2, + Title: "Amazing News 2", + Link: "https://example.com/amazing2", + FileID: null.Int{}, + File: nil, } } func (s *NewsSuite) Test_GetNewsSourcesNone() { s.mock.ExpectQuery(regexp.QuoteMeta(ExpectedGetSourceQuery)). - WillReturnRows(sqlmock.NewRows([]string{"source", "title", "url", "icon", "hook", "Files__file", "Files__name", "Files__path", "Files__downloads", "Files__url", "Files__downloaded"})) + WillReturnRows(sqlmock.NewRows([]string{"source", "title", "url", "icon", "hook", "File__file", "File__name", "File__path", "File__downloads", "File__url", "File__downloaded"})) meta := metadata.MD{} server := CampusServer{db: s.DB, deviceBuf: s.deviceBuf} @@ -136,12 +136,12 @@ func (s *NewsSuite) Test_GetNewsSourcesNone() { require.Equal(s.T(), expectedResp, response) } -const ExpectedGetNewsQuery = "SELECT `news`.`news`,`news`.`date`,`news`.`created`,`news`.`title`,`news`.`description`,`news`.`src`,`news`.`link`,`news`.`image`,`news`.`file`,`Files`.`file` AS `Files__file`,`Files`.`name` AS `Files__name`,`Files`.`path` AS `Files__path`,`Files`.`downloads` AS `Files__downloads`,`Files`.`url` AS `Files__url`,`Files`.`downloaded` AS `Files__downloaded` FROM `news` LEFT JOIN `files` `Files` ON `news`.`file` = `Files`.`file`" +const ExpectedGetNewsQuery = "SELECT `news`.`news`,`news`.`date`,`news`.`created`,`news`.`title`,`news`.`description`,`news`.`src`,`news`.`link`,`news`.`image`,`news`.`file`,`File`.`file` AS `File__file`,`File`.`name` AS `File__name`,`File`.`path` AS `File__path`,`File`.`downloads` AS `File__downloads`,`File`.`url` AS `File__url`,`File`.`downloaded` AS `File__downloaded` FROM `news` LEFT JOIN `files` `File` ON `news`.`file` = `File`.`file`" func (s *NewsSuite) Test_GetNewsNone_withFilters() { s.mock.ExpectQuery(regexp.QuoteMeta(ExpectedGetNewsQuery+" WHERE src = ? AND news > ?")). WithArgs(1, 2). - WillReturnRows(sqlmock.NewRows([]string{"news", "date", "created", "title", "description", "src", "link", "image", "file", "Files__file", "Files__name", "Files__path", "Files__downloads", "Files__url", "Files__downloaded"})) + WillReturnRows(sqlmock.NewRows([]string{"news", "date", "created", "title", "description", "src", "link", "image", "file", "File__file", "File__name", "File__path", "File__downloads", "File__url", "File__downloaded"})) meta := metadata.NewIncomingContext(context.Background(), metadata.MD{}) server := CampusServer{db: s.DB, deviceBuf: s.deviceBuf} @@ -154,7 +154,7 @@ func (s *NewsSuite) Test_GetNewsNone_withFilters() { } func (s *NewsSuite) Test_GetNewsNone() { s.mock.ExpectQuery(regexp.QuoteMeta(ExpectedGetNewsQuery)). - WillReturnRows(sqlmock.NewRows([]string{"news", "date", "created", "title", "description", "src", "link", "image", "file", "Files__file", "Files__name", "Files__path", "Files__downloads", "Files__url", "Files__downloaded"})) + WillReturnRows(sqlmock.NewRows([]string{"news", "date", "created", "title", "description", "src", "link", "image", "file", "File__file", "File__name", "File__path", "File__downloads", "File__url", "File__downloaded"})) meta := metadata.NewIncomingContext(context.Background(), metadata.MD{}) server := CampusServer{db: s.DB, deviceBuf: s.deviceBuf} @@ -169,8 +169,8 @@ func (s *NewsSuite) Test_GetNewsMultiple() { n1 := news1() n2 := news2() s.mock.ExpectQuery(regexp.QuoteMeta(" ")). - WillReturnRows(sqlmock.NewRows([]string{"news", "date", "created", "title", "description", "src", "link", "image", "file", "Files__file", "Files__name", "Files__path", "Files__downloads", "Files__url", "Files__downloaded"}). - AddRow(n1.News, n1.Date, n1.Created, n1.Title, n1.Description, n1.Src, n1.Link, n1.Image, n1.FilesID, n1.Files.File, n1.Files.Name, n1.Files.Path, n1.Files.Downloads, n1.Files.URL, n1.Files.Downloaded). + WillReturnRows(sqlmock.NewRows([]string{"news", "date", "created", "title", "description", "src", "link", "image", "file", "File__file", "File__name", "File__path", "File__downloads", "File__url", "File__downloaded"}). + AddRow(n1.News, n1.Date, n1.Created, n1.Title, n1.Description, n1.Src, n1.Link, n1.Image, n1.FileID, n1.File.File, n1.File.Name, n1.File.Path, n1.File.Downloads, n1.File.URL, n1.File.Downloaded). AddRow(n2.News, n2.Date, n2.Created, n2.Title, n2.Description, n2.Src, n2.Link, n2.Image, nil, nil, nil, nil, nil, nil, nil)) meta := metadata.NewIncomingContext(context.Background(), metadata.MD{}) diff --git a/server/model/files.go b/server/model/files.go index 430bb247..f9d27ddd 100644 --- a/server/model/files.go +++ b/server/model/files.go @@ -15,8 +15,8 @@ var ( _ = uuid.UUID{} ) -// Files struct is a row record of the files table in the tca database -type Files struct { +// File struct is a row record of the files table in the tca database +type File struct { File int64 `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"` @@ -24,8 +24,3 @@ type Files struct { 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 -func (f *Files) TableName() string { - return "files" -} diff --git a/server/model/kino.go b/server/model/kino.go index a4b6f645..f8eec7a0 100644 --- a/server/model/kino.go +++ b/server/model/kino.go @@ -20,8 +20,8 @@ type Kino struct { ImdbRating string `gorm:"column:rating;type:varchar(4);not null;"` Description string `gorm:"column:description;type:text;not null;"` Trailer null.String `gorm:"column:trailer"` - FilesID int64 `gorm:"column:cover"` - Files Files `gorm:"foreignKey:FilesID;references:file"` + FileID int64 `gorm:"column:cover"` + File File `gorm:"foreignKey:FileID;references:file"` Link string `gorm:"column:link;type:varchar(190);not null;unique;"` } diff --git a/server/model/news.go b/server/model/news.go index 4cabb46c..4fa2158f 100755 --- a/server/model/news.go +++ b/server/model/news.go @@ -23,8 +23,8 @@ type News struct { Src int64 `gorm:"column:src;type:int;"` Link string `gorm:"column:link;type:varchar(190);"` Image null.String `gorm:"column:image;type:text;size:65535;"` - FilesID null.Int `gorm:"column:file;type:int;"` - Files *Files `gorm:"foreignKey:FilesID;references:file;constraint:OnUpdate:CASCADE,OnDelete:CASCADE;"` + FileID null.Int `gorm:"column:file;type:int;"` + File *File `gorm:"foreignKey:FileID;references:file;constraint:OnUpdate:CASCADE,OnDelete:CASCADE;"` } // TableName sets the insert table name for this struct type diff --git a/server/model/news_alert.go b/server/model/news_alert.go index 55a2a65f..076c1ae1 100644 --- a/server/model/news_alert.go +++ b/server/model/news_alert.go @@ -18,8 +18,8 @@ var ( // NewsAlert struct is a row record of the news_alert table in the tca database type NewsAlert struct { NewsAlert int64 `gorm:"primary_key;AUTO_INCREMENT;column:news_alert;type:int;" json:"news_alert"` - FilesID int64 `gorm:"column:file;not null"` - Files Files `gorm:"foreignKey:FilesID;references:file;constraint:OnUpdate:CASCADE,OnDelete:CASCADE;"` + FileID int64 `gorm:"column:file;not null"` + File File `gorm:"foreignKey:FileID;references:file;constraint:OnUpdate:CASCADE,OnDelete:CASCADE;"` Name null.String `gorm:"column:name;type:varchar(100);" json:"name"` Link null.String `gorm:"column:link;type:text;size:65535;" json:"link"` Created time.Time `gorm:"column:created;type:timestamp;default:CURRENT_TIMESTAMP;" json:"created"` diff --git a/server/model/news_source.go b/server/model/news_source.go index 94d06a2b..79d05e0e 100644 --- a/server/model/news_source.go +++ b/server/model/news_source.go @@ -17,12 +17,12 @@ var ( // NewsSource struct is a row record of the newsSource table in the tca database type NewsSource struct { - Source int64 `gorm:"primary_key;AUTO_INCREMENT;column:source;type:int;"` - Title string `gorm:"column:title;type:text;size:16777215;"` - URL null.String `gorm:"column:url;type:text;size:16777215;"` - FilesID int64 `gorm:"column:icon;not null"` - Files Files `gorm:"foreignKey:FilesID;references:file;constraint:OnUpdate:CASCADE,OnDelete:CASCADE;"` - Hook null.String `gorm:"column:hook;type:char;size:12;"` + Source int64 `gorm:"primary_key;AUTO_INCREMENT;column:source;type:int;"` + Title string `gorm:"column:title;type:text;size:16777215;"` + URL null.String `gorm:"column:url;type:text;size:16777215;"` + FileID int64 `gorm:"column:icon;not null"` + File File `gorm:"foreignKey:FileID;references:file;constraint:OnUpdate:CASCADE,OnDelete:CASCADE;"` + Hook null.String `gorm:"column:hook;type:char;size:12;"` } // TableName sets the insert table name for this struct type