Skip to content

Commit

Permalink
movie handler
Browse files Browse the repository at this point in the history
  • Loading branch information
sayem314 committed May 14, 2023
1 parent 5a9df49 commit 39e5b2a
Show file tree
Hide file tree
Showing 5 changed files with 115 additions and 0 deletions.
4 changes: 4 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ module github.com/lunarr-app/lunarr-go
go 1.20

require (
github.com/cyruzin/golang-tmdb v1.5.0
github.com/go-playground/validator/v10 v10.13.0
github.com/kataras/iris/v12 v12.2.0
github.com/rs/zerolog v1.29.1
Expand Down Expand Up @@ -30,6 +31,7 @@ require (
github.com/gorilla/css v1.0.0 // indirect
github.com/iris-contrib/schema v0.0.6 // indirect
github.com/josharian/intern v1.0.0 // indirect
github.com/json-iterator/go v1.1.12 // indirect
github.com/kataras/blocks v0.0.7 // indirect
github.com/kataras/golog v0.1.8 // indirect
github.com/kataras/pio v0.0.11 // indirect
Expand All @@ -42,6 +44,8 @@ require (
github.com/mattn/go-colorable v0.1.13 // indirect
github.com/mattn/go-isatty v0.0.18 // indirect
github.com/microcosm-cc/bluemonday v1.0.23 // indirect
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/montanaflynn/stats v0.0.0-20171201202039-1bf9dbcd8cbe // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
Expand Down
11 changes: 11 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ github.com/aymerick/douceur v0.2.0 h1:Mv+mAeH1Q+n9Fr+oyamOlAkUNPWPlA8PPGR0QAaYuP
github.com/aymerick/douceur v0.2.0/go.mod h1:wlT5vV2O3h55X9m7iVYN0TBM0NH/MmbLnd30/FjWUq4=
github.com/cheekybits/is v0.0.0-20150225183255-68e9c0620927/go.mod h1:h/aW8ynjgkuj+NQRlZcDbAbM1ORAbXjXX77sX7T289U=
github.com/coreos/go-systemd/v22 v22.5.0/go.mod h1:Y58oyj3AT4RCenI/lSvhwexgC+NSVTIJ3seZv2GcEnc=
github.com/cyruzin/golang-tmdb v1.5.0 h1:a8KT9gfTDCwVjfOg+oSGSjtZbZe/VgOEz5ri4s7RLOU=
github.com/cyruzin/golang-tmdb v1.5.0/go.mod h1:ZSryJLCcY+9TiKU+LbouXKns++YBrM8Tizannr05c+I=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
Expand Down Expand Up @@ -45,6 +47,7 @@ github.com/google/go-cmp v0.5.2/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/
github.com/google/go-cmp v0.5.5 h1:Khx7svrCpmxxtHBq5j2mp/xVjsi8hQMfNLvJFAlrGgU=
github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/go-querystring v1.1.0 h1:AnCroh3fv4ZBgVIf1Iwtovgjaw/GiKJo8M8yD/fhyJ8=
github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
github.com/google/uuid v1.3.0 h1:t6JiXgmwXMjEs8VusXIJk2BXHsn+wx8BZdTaoZ5fu7I=
github.com/google/uuid v1.3.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/gorilla/css v1.0.0 h1:BQqNyPTi50JCFMTw/b67hByjMVXZRwGha6wxVGkeihY=
Expand All @@ -56,6 +59,8 @@ github.com/iris-contrib/schema v0.0.6 h1:CPSBLyx2e91H2yJzPuhGuifVRnZBBJ3pCOMbOvP
github.com/iris-contrib/schema v0.0.6/go.mod h1:iYszG0IOsuIsfzjymw1kMzTL8YQcCWlm65f3wX8J5iA=
github.com/josharian/intern v1.0.0 h1:vlS4z54oSdjm0bgjRigI+G1HpF+tI+9rE5LLzOg8HmY=
github.com/josharian/intern v1.0.0/go.mod h1:5DoeVV0s6jJacbCEi61lwdGj/aVlrQvzHFFd8Hwg//Y=
github.com/json-iterator/go v1.1.12 h1:PV8peI4a0ysnczrg+LtxykD8LfKY9ML6u2jnxaEnrnM=
github.com/json-iterator/go v1.1.12/go.mod h1:e30LSqwooZae/UwlEbR2852Gd8hjQvJoHmT4TnhNGBo=
github.com/kataras/blocks v0.0.7 h1:cF3RDY/vxnSRezc7vLFlQFTYXG/yAr1o7WImJuZbzC4=
github.com/kataras/blocks v0.0.7/go.mod h1:UJIU97CluDo0f+zEjbnbkeMRlvYORtmc1304EeyXf4I=
github.com/kataras/golog v0.1.8 h1:isP8th4PJH2SrbkciKnylaND9xoTtfxv++NB+DF0l9g=
Expand Down Expand Up @@ -93,6 +98,11 @@ github.com/mattn/go-isatty v0.0.18/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D
github.com/microcosm-cc/bluemonday v1.0.23 h1:SMZe2IGa0NuHvnVNAZ+6B38gsTbi5e4sViiWJyDDqFY=
github.com/microcosm-cc/bluemonday v1.0.23/go.mod h1:mN70sk7UkkF8TUr2IGBpNN0jAgStuPzlK76QuruE/z4=
github.com/mitchellh/go-wordwrap v1.0.1 h1:TLuKupo69TCn6TQSyGxwI1EblZZEsQ0vMlAFQflz0v0=
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd h1:TRLaZ9cD/w8PVh93nsPXa1VrQ6jlwL5oN8l14QlcNfg=
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
github.com/modern-go/reflect2 v1.0.2 h1:xBagoLtFs94CBntxluKeaWgTMpvLxC4ur3nMaC9Gz0M=
github.com/modern-go/reflect2 v1.0.2/go.mod h1:yWuevngMOJpCy52FWWMvUC8ws7m/LJsjYzDa0/r8luk=
github.com/montanaflynn/stats v0.0.0-20171201202039-1bf9dbcd8cbe h1:iruDEfMl2E6fbMZ9s0scYfZQ84/6SPL6zC8ACM2oIL0=
github.com/montanaflynn/stats v0.0.0-20171201202039-1bf9dbcd8cbe/go.mod h1:wL8QJuTMNUDYhXwkmfOly8iTdp5TEcJFWZD2D7SIkUc=
github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e h1:fD57ERR4JtEqsWbfPhv4DMiApHyliiK5xCTNVSPiaAs=
Expand All @@ -117,6 +127,7 @@ github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
Expand Down
91 changes: 91 additions & 0 deletions internal/handlers/movies/lists.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
package movies

import (
"net/http"

tmdb "github.com/cyruzin/golang-tmdb"
"github.com/go-playground/validator/v10"
"github.com/kataras/iris/v12"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/mongo/options"

"github.com/lunarr-app/lunarr-go/internal/db"
"github.com/lunarr-app/lunarr-go/internal/models"
)

func ListsHandler(ctx iris.Context) {
var query models.SearchQueryParams
if err := ctx.ReadQuery(&query); err != nil {
ctx.StopWithJSON(http.StatusBadRequest, iris.Map{
"status": http.StatusText(http.StatusBadRequest),
"message": err.Error(),
})
return
}

// Validate user input
validate := validator.New()
if err := validate.Struct(query); err != nil {
ctx.StopWithJSON(http.StatusBadRequest, iris.Map{
"status": http.StatusText(http.StatusBadRequest),
"message": err.Error(),
})
return
}

// Build query object based on search query
search := bson.M{}
if query.Query != "" {
search = bson.M{
"$text": bson.M{
"$search": query.Query,
"$caseSensitive": false,
"$diacriticSensitive": false,
},
}
}

// Find movies in the database based on query and pagination
opts := options.Find().SetSort(bson.M{"title": 1})
opts.SetSkip(int64(query.Limit * (query.Page - 1)))
opts.SetLimit(int64(query.Limit))

totalMovies, err := db.MoviesLists.CountDocuments(ctx.Request().Context(), search)
if err != nil {
ctx.StopWithJSON(http.StatusInternalServerError, iris.Map{
"status": http.StatusText(http.StatusInternalServerError),
"message": "Failed to count movies",
})
return
}

cur, err := db.MoviesLists.Find(ctx.Request().Context(), search, opts)
if err != nil {
ctx.StopWithJSON(http.StatusInternalServerError, iris.Map{
"status": http.StatusText(http.StatusInternalServerError),
"message": "Failed to find movies",
})
return
}

var movieList []tmdb.MovieDetails
for cur.Next(ctx.Request().Context()) {
var movie tmdb.MovieDetails
if err := cur.Decode(&movie); err != nil {
ctx.StopWithJSON(http.StatusInternalServerError, iris.Map{
"status": http.StatusText(http.StatusInternalServerError),
"message": "Failed to decode movie",
})
return
}
movieList = append(movieList, movie)
}

ctx.StatusCode(http.StatusOK)
ctx.JSON(map[string]interface{}{
"results": movieList,
"limit": query.Limit,
"page": query.Page,
"total": int(totalMovies)/query.Limit + 1,
})
}
7 changes: 7 additions & 0 deletions internal/models/request.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package models

type SearchQueryParams struct {
Limit int `json:"limit,omitempty" validate:"gte=1" default:"1"`
Page int `json:"page,omitempty" validate:"gte=1" default:"1"`
Query string `json:"query,omitempty"`
}
2 changes: 2 additions & 0 deletions internal/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (

"github.com/lunarr-app/lunarr-go/internal/handlers"
"github.com/lunarr-app/lunarr-go/internal/handlers/auth"
"github.com/lunarr-app/lunarr-go/internal/handlers/movies"
"github.com/lunarr-app/lunarr-go/internal/server/middleware"
)

Expand Down Expand Up @@ -37,6 +38,7 @@ func New() *iris.Application {

// Register authenticated API routes
api.Get("/", handlers.RootHandler)
api.Get("/movies", movies.ListsHandler)

// Return the application instance
return app
Expand Down

0 comments on commit 39e5b2a

Please sign in to comment.