Compare commits

..
3 Commits
Author SHA1 Message Date
brian 61330f8036 adding functions for getting and adding a movie 2026-07-17 19:30:52 -06:00
brian caa26b5f12 adding a route to show a single movie 2026-07-17 19:30:10 -06:00
brian e4d0fd3f4a removing logging stuff 2026-06-11 21:03:16 -06:00
2 changed files with 42 additions and 3 deletions
@@ -4,7 +4,6 @@ import (
"context"
"net/http"
"time"
"log"
"github.com/captbrogers/MagicStreamServer/database"
"github.com/captbrogers/MagicStreamServer/models"
@@ -18,7 +17,7 @@ var movieCollection *mongo.Collection = database.OpenCollection("movies")
func GetMovies() gin.HandlerFunc {
return func(ginCtx *gin.Context) {
ctx, cancel := context.WithTimeout(context.Background(), 100*time.Second)
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
var movies []models.Movie
@@ -26,14 +25,52 @@ func GetMovies() gin.HandlerFunc {
cursor, err := movieCollection.Find(ctx, bson.M{})
if err != nil {
ginCtx.JSON(http.StatusInternalServerError, gin.H{"error": "failed to fetch movies"})
return
}
defer cursor.Close(ctx)
if err = cursor.All(ctx, &movies); err != nil {
log.Fatalf("Failed to decode: %v", err)
ginCtx.JSON(http.StatusInternalServerError, gin.H{"error": "failed to decode movies"})
return
}
ginCtx.JSON(http.StatusOK, movies)
}
}
func GetMovie() gin.HandlerFunc {
return func(ginCtx *gin.Context) {
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
imdbID := ginCtx.Param("imdb_id")
if imdbID == "" {
ginCtx.JSON(http.StatusBadRequest, gin.H{"error": "A valid IMDB id is required"})
return
}
var movie models.Movie
err := movieCollection.FindOne(ctx, bson.M{"imdb_id": imdbID}).Decode(&movie)
if err != nil {
ginCtx.JSON(http.StatusInternalServerError, gin.H{"error": "failed to get movie by IMDB id"})
return
}
ginCtx.JSON(http.StatusOK, movie)
}
}
func AddMovie() gin.HandlersChain {
return func(c *gin.Context) {
ctx, cancel := context.WithTimeout(context.Background(), 100*time.Second)
defer cancel()
var movie models.Movie
if err := c.ShouldBindJSON(&movie); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid input"})
return
}
}
}
+2
View File
@@ -18,6 +18,8 @@ func main() {
router.GET("/movies", controller.GetMovies())
router.GET("/movie/:imdb_id", controller.GetMovie())
if err := router.Run(":5180"); err != nil {
log.Fatal("failure to start the server")
}