From 4a28db044bb812a7e60e55a860b92521369bf856 Mon Sep 17 00:00:00 2001 From: Mo Kweon Date: Tue, 22 Jun 2021 21:03:51 -0700 Subject: [PATCH] feat: add VideosResponseFromDB :: Database -> GetVideosResponse --- server/go.mod | 1 + server/pkg/handlers/handlers.go | 58 ++++++++++++++++++ server/pkg/handlers/handlers_test.go | 88 ++++++++++++++++++++++++++++ 3 files changed, 147 insertions(+) create mode 100644 server/pkg/handlers/handlers.go create mode 100644 server/pkg/handlers/handlers_test.go diff --git a/server/go.mod b/server/go.mod index ffebfd1b..8a6c85e9 100644 --- a/server/go.mod +++ b/server/go.mod @@ -6,6 +6,7 @@ require ( cloud.google.com/go v0.84.0 // indirect github.com/daixiang0/gci v0.2.8 github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect + github.com/google/go-cmp v0.5.6 github.com/grpc-ecosystem/go-grpc-middleware v1.3.0 github.com/grpc-ecosystem/go-grpc-prometheus v1.2.0 github.com/prometheus/client_golang v1.10.0 diff --git a/server/pkg/handlers/handlers.go b/server/pkg/handlers/handlers.go new file mode 100644 index 00000000..c03a22c6 --- /dev/null +++ b/server/pkg/handlers/handlers.go @@ -0,0 +1,58 @@ +package handlers + +import ( + "sort" + + "github.com/codingpot/pr12er/server/pkg/pr12er" +) + +// VideosResponseFromDB converts DB proto to GetVideosResponse. +func VideosResponseFromDB(db *pr12er.Database) *pr12er.GetVideosResponse { + videos := make([]*pr12er.Video, len(db.GetPrIdToVideo())) + i := 0 + + for prID, data := range db.GetPrIdToVideo() { + dataVideo := data.GetVideo() + videos[i] = &pr12er.Video{ + PrId: prID, + Title: dataVideo.GetVideoTitle(), + Link: getYouTubeLinkFromID(dataVideo.GetVideoId()), + Presenter: dataVideo.GetUploader(), + Category: getCategory(data), + NumberOfLike: dataVideo.GetNumberOfLikes(), + Keywords: getKeywords(data), + NumberOfViews: dataVideo.GetNumberOfViews(), + PublishedDate: dataVideo.GetPublishedDate(), + } + i++ + } + + sort.Slice(videos, func(i, j int) bool { + return videos[i].GetPrId() < videos[j].GetPrId() + }) + + return &pr12er.GetVideosResponse{ + Videos: videos, + } +} + +// getKeywords returns all keywords by merging each paper's methods. +func getKeywords(prVideo *pr12er.PrVideo) []string { + var ret []string + for _, p := range prVideo.GetPapers() { + for _, m := range p.GetMethods() { + ret = append(ret, m.GetName()) + } + } + return ret +} + +// TODO: Implement getCategory based on papers. +func getCategory(prVideo *pr12er.PrVideo) pr12er.Category { + return pr12er.Category_CATEGORY_UNSPECIFIED +} + +// getYouTubeLinkFromID returns the full URL. +func getYouTubeLinkFromID(videoID string) string { + return "https://youtu.be/" + videoID +} diff --git a/server/pkg/handlers/handlers_test.go b/server/pkg/handlers/handlers_test.go new file mode 100644 index 00000000..aa791a10 --- /dev/null +++ b/server/pkg/handlers/handlers_test.go @@ -0,0 +1,88 @@ +package handlers + +import ( + "testing" + + "github.com/codingpot/pr12er/server/pkg/pr12er" + "github.com/google/go-cmp/cmp" + "google.golang.org/protobuf/testing/protocmp" +) + +// nolint:funlen +func TestConvertToGet(t *testing.T) { + type args struct { + db *pr12er.Database + } + tests := []struct { + name string + args args + want *pr12er.GetVideosResponse + }{ + { + name: "convert pr12er.Database to GetVideosResponse in sorted order", + args: args{ + db: &pr12er.Database{ + PrIdToVideo: map[int32]*pr12er.PrVideo{ + 199: { + PrId: 199, + Video: &pr12er.YouTubeVideo{ + VideoId: "video-id-199", + VideoTitle: "Video Title", + NumberOfLikes: 3, + NumberOfViews: 4, + }, + }, + 2: { + PrId: 2, + Video: &pr12er.YouTubeVideo{ + VideoId: "video-id-1", + VideoTitle: "Video Title", + NumberOfLikes: 1, + NumberOfViews: 2, + }, + Papers: []*pr12er.Paper{ + { + PaperId: "paper-id-2", + Title: "paper-title-2", + Methods: []*pr12er.Method{ + { + Name: "Method 1", + }, + }, + }, + }, + }, + }, + }, + }, + want: &pr12er.GetVideosResponse{ + Videos: []*pr12er.Video{ + { + PrId: 2, + Title: "Video Title", + Link: "https://youtu.be/video-id-1", + NumberOfLike: 1, + NumberOfViews: 2, + Keywords: []string{"Method 1"}, + }, + { + PrId: 199, + Title: "Video Title", + Link: "https://youtu.be/video-id-199", + NumberOfLike: 3, + NumberOfViews: 4, + }, + }, + }, + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + got := VideosResponseFromDB(tt.args.db) + if got := cmp.Diff(tt.want, got, protocmp.Transform()); got != "" { + t.Error(got) + } + }) + } +}