Skip to content

Commit

Permalink
renamed Files -> File to be consistent with other models
Browse files Browse the repository at this point in the history
  • Loading branch information
CommanderStorm committed Sep 22, 2023
1 parent 7fef4c3 commit a75a33f
Show file tree
Hide file tree
Showing 16 changed files with 89 additions and 94 deletions.
6 changes: 3 additions & 3 deletions server/backend/cron/fileDownload.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand All @@ -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
}
Expand All @@ -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
}
Expand Down
6 changes: 3 additions & 3 deletions server/backend/cron/movies.go
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand All @@ -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 {
Expand Down
12 changes: 6 additions & 6 deletions server/backend/cron/news.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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)
}
Expand All @@ -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) {
Expand All @@ -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),
Expand Down
10 changes: 5 additions & 5 deletions server/backend/migration/20230904100000.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
2 changes: 1 addition & 1 deletion server/backend/migration/migration.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{},
Expand Down
8 changes: 4 additions & 4 deletions server/backend/movie.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
Expand All @@ -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,
})
}
Expand Down
12 changes: 6 additions & 6 deletions server/backend/movie_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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})
Expand All @@ -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)
Expand All @@ -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)
Expand Down
6 changes: 3 additions & 3 deletions server/backend/news.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
Expand All @@ -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
Expand All @@ -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)
}
Expand Down
4 changes: 2 additions & 2 deletions server/backend/newsAlerts.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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),
Expand Down
12 changes: 6 additions & 6 deletions server/backend/newsAlerts_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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/",
Expand All @@ -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),
Expand Down
Loading

0 comments on commit a75a33f

Please sign in to comment.