Skip to content

Commit

Permalink
implemnted the api
Browse files Browse the repository at this point in the history
  • Loading branch information
CommanderStorm committed Sep 5, 2023
1 parent 3bc09ef commit 736390c
Show file tree
Hide file tree
Showing 4 changed files with 161 additions and 0 deletions.
42 changes: 42 additions & 0 deletions server/backend/movie.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package backend

import (
"context"
pb "github.com/TUM-Dev/Campus-Backend/server/api"
"github.com/TUM-Dev/Campus-Backend/server/model"
log "github.com/sirupsen/logrus"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
"google.golang.org/protobuf/types/known/timestamppb"
)

func (s *CampusServer) GetMovies(_ context.Context, req *pb.GetMoviesRequest) (*pb.GetMoviesReply, error) {
var movies []model.Kino
if err := s.db.Joins("Files").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")
}
var movieResponse []*pb.MovieMsgElement
for _, movie := range movies {
movieResponse = append(movieResponse, &pb.MovieMsgElement{
Id: movie.Id,
Date: timestamppb.New(movie.Date),
Created: timestamppb.New(movie.Created),
Title: movie.Title,
Year: movie.Year,
Runtime: movie.Runtime,
Genre: movie.Genre,
Director: movie.Director,
Actors: movie.Actors,
ImdbRating: movie.ImdbRating,
Description: movie.Description,
CoverName: movie.Files.Name,
CoverPath: movie.Files.Path,
CoverID: movie.Files.File,
Link: movie.Link,
})
}
return &pb.GetMoviesReply{
Movies: movieResponse,
}, nil
}
116 changes: 116 additions & 0 deletions server/backend/movie_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
package backend

import (
"context"
"database/sql"
"github.com/DATA-DOG/go-sqlmock"
pb "github.com/TUM-Dev/Campus-Backend/server/api"
"github.com/stretchr/testify/require"
"github.com/stretchr/testify/suite"
"google.golang.org/protobuf/types/known/timestamppb"
"gorm.io/driver/mysql"
"gorm.io/gorm"
"testing"
"time"
)

type MovieSuite struct {
suite.Suite
DB *gorm.DB
mock sqlmock.Sqlmock
}

func (s *MovieSuite) SetupSuite() {
var (
db *sql.DB
err error
)

db, s.mock, err = sqlmock.New()
require.NoError(s.T(), err)

dialector := mysql.New(mysql.Config{
Conn: db,
DriverName: "mysql",
})
s.mock.ExpectQuery("SELECT VERSION()").
WillReturnRows(sqlmock.NewRows([]string{"VERSION()"}).AddRow("10.11.4-MariaDB"))
s.DB, err = gorm.Open(dialector, &gorm.Config{})
require.NoError(s.T(), err)
}

var (
movie1 = pb.MovieMsgElement{
Id: 1,
Date: timestamppb.New(time.Now()),
Created: timestamppb.New(time.Now()),
Title: "Mission Impossible 4 - Ghost Protocol",
Year: "2011",
Runtime: "133 min",
Genre: "Action, Adventure, Thriller",
Director: "Brad Bird",
Actors: "Tom Cruise, Jeremy Renner, Simon Pegg, Paula Patton",
ImdbRating: "7.4",
Description: "The IMF is shut down when it's implicated in the bombing of the Kremlin, causing Ethan Hunt and his new team to go rogue to clear their organization's name.",
CoverName: "mission_impossible_4.jpg",
CoverPath: "movie/mission_impossible_4.jpg",
CoverID: 1,
Link: "https://www.imdb.com/title/tt1229238/",
}
movie2 = pb.MovieMsgElement{
Id: 2,
Date: timestamppb.New(time.Now()),
Created: timestamppb.New(time.Now()),
Title: "Mission Impossible 5 - Rogue Nation",
Year: "2015",
Runtime: "131 min",
Genre: "Action, Adventure, Thriller",
Director: "Christopher McQuarrie",
Actors: "Tom Cruise, Jeremy Renner, Simon Pegg, Rebecca Ferguson",
ImdbRating: "7.4",
Description: "Ethan and his team take on their most impossible mission yet when they have to eradicate an international rogue organization as highly skilled as they are and committed to destroying the IMF.",
CoverName: "mission_impossible_5.jpg",
CoverPath: "movie/mission_impossible_5.jpg",
CoverID: 2,
Link: "https://www.imdb.com/title/tt2381249/",
}
)

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 > ?").
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"}).
AddRow(movie2.Id, movie2.Date.AsTime(), movie2.Created.AsTime(), movie2.Title, movie2.Year, 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.Id, movie1.Date.AsTime(), movie1.Created.AsTime(), movie1.Title, movie1.Year, 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)
require.Equal(s.T(), &pb.GetMoviesReply{Movies: []*pb.MovieMsgElement{&movie2, &movie1}}, response)
}

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 > ?").
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"}).
AddRow(movie1.Id, movie1.Date.AsTime(), movie1.Created.AsTime(), movie1.Title, movie1.Year, 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)
require.Equal(s.T(), &pb.GetMoviesReply{Movies: []*pb.MovieMsgElement{&movie1}}, response)
}

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 > ?").
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"}))
response, err := server.GetMovies(context.Background(), &pb.GetMoviesRequest{LastId: 42})
require.NoError(s.T(), err)
require.Equal(s.T(), &pb.GetMoviesReply{Movies: []*pb.MovieMsgElement(nil)}, response)
}

// In order for 'go test' to run this suite, we need to create
// a normal test function and pass our suite to suite.Run
func TestExampleTestSuite(t *testing.T) {
suite.Run(t, new(MovieSuite))
}
1 change: 1 addition & 0 deletions server/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ module github.com/TUM-Dev/Campus-Backend/server
go 1.21

require (
github.com/DATA-DOG/go-sqlmock v1.5.0
github.com/disintegration/imaging v1.6.2
github.com/gabriel-vasile/mimetype v1.4.2
github.com/getsentry/sentry-go v0.23.0
Expand Down
2 changes: 2 additions & 0 deletions server/go.sum
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
github.com/DATA-DOG/go-sqlmock v1.5.0 h1:Shsta01QNfFxHCfpW6YH2STWB0MudeXXEWMr20OEh60=
github.com/DATA-DOG/go-sqlmock v1.5.0/go.mod h1:f/Ixk793poVmq4qj/V1dPUg2JEAKC73Q5eFN3EC/SaM=
github.com/PuerkitoBio/goquery v1.8.0 h1:PJTF7AmFCFKk1N6V6jmKfrNH9tV5pNE6lZMkG0gta/U=
github.com/PuerkitoBio/goquery v1.8.0/go.mod h1:ypIiRMtY7COPGk+I/YbZLbxsxn9g5ejnI2HSMtkjZvI=
github.com/andybalholm/cascadia v1.3.1 h1:nhxRkql1kdYCc8Snf7D5/D3spOX+dBgjA6u8x004T2c=
Expand Down

0 comments on commit 736390c

Please sign in to comment.