-
Notifications
You must be signed in to change notification settings - Fork 0
/
fetch_favorite_videos.go
43 lines (36 loc) · 1.04 KB
/
fetch_favorite_videos.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
package api
import (
"github.com/ham357/youtube-manager-go/middlewares"
"github.com/ham357/youtube-manager-go/models"
"firebase.google.com/go/auth"
"github.com/labstack/echo"
"github.com/sirupsen/logrus"
"github.com/valyala/fasthttp"
"google.golang.org/api/youtube/v3"
)
func FetchFavoriteVideos() echo.HandlerFunc {
return func(c echo.Context) error {
dbs := c.Get("dbs").(*middlewares.DatabaseClient)
token := c.Get("auth").(*auth.Token)
user := models.User{}
dbs.DB.Table("users").Where(models.User{UID: token.UID}).First(&user)
favorites := []models.Favorite{}
dbs.DB.Model(&user).Related(&favorites)
videoIds := ""
for _, f := range favorites {
if len(videoIds) == 0 {
videoIds += f.VideoId
} else {
videoIds += "," + f.VideoId
}
}
yts := c.Get("yts").(*youtube.Service)
lp := []string{"id", "snippet"}
call := yts.Videos.List(lp).Id(videoIds).MaxResults(10)
res, err := call.Do()
if err != nil {
logrus.Fatalf("Error calling Youtube API: %v", err)
}
return c.JSON(fasthttp.StatusOK, res)
}
}