-
Notifications
You must be signed in to change notification settings - Fork 0
/
get_video.go
54 lines (47 loc) · 1.36 KB
/
get_video.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
44
45
46
47
48
49
50
51
52
53
54
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"
)
type VideoResponse struct {
VideoList *youtube.VideoListResponse `json:"video_list"`
IsFavorite bool `json:"is_favorite"`
}
func GetVideos() echo.HandlerFunc {
return func(c echo.Context) error {
yts := c.Get("yts").(*youtube.Service)
dbs := c.Get("dbs").(*middlewares.DatabaseClient)
token := c.Get("auth").(*auth.Token)
videoId := c.Param("id")
isFavorite := false
if token != nil {
favorite := models.Favorite{}
isFavoriteNotFound := dbs.DB.Table("favorites").
Joins("INNER JOIN users ON users.id = favorites.user_id").
Where(models.User{UID: token.UID}).
Where(models.Favorite{VideoId: videoId}).
First(&favorite).
RecordNotFound()
logrus.Debug("isFavoriteNotFound: ", isFavoriteNotFound)
if !isFavoriteNotFound {
isFavorite = true
}
}
lp := []string{"id", "snippet"}
call := yts.Videos.List(lp).Id(videoId)
res, err := call.Do()
if err != nil {
logrus.Fatalf("ErrorcallingYoutubeAPI:%v", err)
}
v := VideoResponse{
VideoList: res,
IsFavorite: isFavorite,
}
return c.JSON(fasthttp.StatusOK, v)
}
}