Content search from tmdb

- Rename lists to watched
- Implement content search from tmdb
This commit is contained in:
IRHM
2023-03-25 18:57:26 +00:00
parent 2741696035
commit 17c5d8d800
6 changed files with 148 additions and 40 deletions
+2 -2
View File
@@ -18,10 +18,10 @@ import (
type User struct {
gorm.Model
ID int `json:"id"`
ID uint `json:"id"`
Username string `gorm:"notNull,unique" json:"username" binding:"required"`
Password string `gorm:"notNnull" json:"password" binding:"required"`
// Lists []*List `gorm:"rel:has-many,join:id=user_id"`
Watched []Watched
}
type AuthResponse struct {
+93
View File
@@ -0,0 +1,93 @@
package main
import (
"encoding/json"
"errors"
"io"
"net/http"
"net/url"
"gorm.io/gorm"
)
type Content struct {
gorm.Model
ID int `json:"id"`
Type string `json:"type"`
Name string `json:"name"`
}
type TMDBSearchMultiResponse struct {
Page int `json:"page"`
Results []struct {
Adult bool `json:"adult"`
BackdropPath string `json:"backdrop_path"`
ID int `json:"id"`
Title string `json:"title,omitempty"`
OriginalLanguage string `json:"original_language"`
OriginalTitle string `json:"original_title,omitempty"`
Overview string `json:"overview"`
PosterPath string `json:"poster_path"`
MediaType string `json:"media_type"`
GenreIds []int `json:"genre_ids"`
Popularity float64 `json:"popularity"`
ReleaseDate string `json:"release_date,omitempty"`
Video bool `json:"video,omitempty"`
VoteAverage float64 `json:"vote_average"`
VoteCount int `json:"vote_count"`
Name string `json:"name,omitempty"`
OriginalName string `json:"original_name,omitempty"`
FirstAirDate string `json:"first_air_date,omitempty"`
OriginCountry []string `json:"origin_country,omitempty"`
} `json:"results"`
TotalPages int `json:"total_pages"`
TotalResults int `json:"total_results"`
}
func tmdbRequest(ep string, p map[string]string, resp interface{}) error {
base, err := url.Parse("https://api.themoviedb.org/3")
if err != nil {
return errors.New("failed to parse api uri")
}
// Path params
base.Path += ep
// Query params
params := url.Values{}
params.Add("api_key", "")
params.Add("language", "en-US")
for k, v := range p {
params.Add(k, v)
}
// Add params to url
base.RawQuery = params.Encode()
// Run get request
res, err := http.Get(base.String())
if err != nil {
return err
}
body, err := io.ReadAll(res.Body)
res.Body.Close()
if err != nil {
return err
}
err = json.Unmarshal([]byte(body), &resp)
if err != nil {
return err
}
return nil
}
func searchContent(query string) (TMDBSearchMultiResponse, error) {
resp := new(TMDBSearchMultiResponse)
err := tmdbRequest("/search/multi", map[string]string{"query": query, "page": "1"}, &resp)
if err != nil {
return TMDBSearchMultiResponse{}, errors.New("failed to complete multi search request")
}
return *resp, nil
}
-34
View File
@@ -1,34 +0,0 @@
package main
import (
"github.com/uptrace/bun"
"gorm.io/gorm"
)
type Content struct {
bun.BaseModel `bun:"table:content"`
ID int `bun:"id,pk,autoincrement" json:"id"`
Type string `json:"-"`
Name string `json:"name"`
}
type List struct {
bun.BaseModel `bun:"table:lists"`
ID int `bun:"id,pk,autoincrement" json:"id"`
Watched bool `bun:"watched" json:"watched"`
UserID int `bun:"user_id" json:"-"`
ContentID int
Content *Content `bun:"rel:belongs-to,join:content_id=id" json:"content"`
}
func getContent(db *gorm.DB) List {
list := new(List)
// err := db.NewSelect().Model(list).Relation("Content").Where("user_id = ?", 8).Scan(ctx)
// if err != nil {
// panic(err)
// }
// fmt.Println(list.ID)
return *list
}
+26 -2
View File
@@ -26,8 +26,32 @@ func newBaseRouter(db *gorm.DB, rg *gin.Engine) *BaseRouter {
func (b *BaseRouter) addContentRoutes() {
content := b.rg.Group("/content")
content.GET("/", func(c *gin.Context) {
c.JSON(http.StatusOK, getContent(b.db))
// Get trending content
// content.GET("/", func(c *gin.Context) {
// c.JSON(http.StatusOK, getWatched(b.db))
// })
// Search for content
content.GET("/:query", func(c *gin.Context) {
println(c.Param("query"))
if c.Param("query") == "" {
c.Status(400)
return
}
content, err := searchContent(c.Param("query"))
if err != nil {
c.JSON(http.StatusBadRequest, ErrorResponse{Error: err.Error()})
return
}
c.JSON(http.StatusOK, content)
})
}
func (b *BaseRouter) addWatchedRoutes() {
watched := b.rg.Group("/watched")
watched.GET("/", func(c *gin.Context) {
c.JSON(http.StatusOK, getWatched(b.db))
})
}
+3 -2
View File
@@ -27,7 +27,7 @@ func main() {
panic("failed to connect to database")
}
db.AutoMigrate(&User{})
db.AutoMigrate(&User{}, &Content{}, &Watched{})
gin := gin.Default()
gin.Use(cors.New(cors.Config{
@@ -40,7 +40,8 @@ func main() {
}))
br := newBaseRouter(db, gin)
br.addAuthRoutes()
// br.addContentRoutes()
br.addContentRoutes()
br.addWatchedRoutes()
gin.Run("localhost:3080")
}
+24
View File
@@ -0,0 +1,24 @@
package main
import (
"gorm.io/gorm"
)
type Watched struct {
gorm.Model
ID int `json:"id"`
Finished bool `json:"watched"`
UserID uint `json:"-"`
ContentID int `json:"-"`
Content Content `json:"content"`
}
func getWatched(db *gorm.DB) Watched {
watched := new(Watched)
res := db.Where("user_id = ?", 1).Find(&watched)
if res.Error != nil {
panic(res.Error)
}
return *watched
}