finished add movie method
This commit is contained in:
@@ -9,12 +9,15 @@ import (
|
|||||||
"github.com/captbrogers/MagicStreamServer/models"
|
"github.com/captbrogers/MagicStreamServer/models"
|
||||||
|
|
||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
|
"github.com/go-playground/validator/v10"
|
||||||
"go.mongodb.org/mongo-driver/v2/bson"
|
"go.mongodb.org/mongo-driver/v2/bson"
|
||||||
"go.mongodb.org/mongo-driver/v2/mongo"
|
"go.mongodb.org/mongo-driver/v2/mongo"
|
||||||
)
|
)
|
||||||
|
|
||||||
var movieCollection *mongo.Collection = database.OpenCollection("movies")
|
var movieCollection *mongo.Collection = database.OpenCollection("movies")
|
||||||
|
|
||||||
|
var validate = validator.New()
|
||||||
|
|
||||||
func GetMovies() gin.HandlerFunc {
|
func GetMovies() gin.HandlerFunc {
|
||||||
return func(ginCtx *gin.Context) {
|
return func(ginCtx *gin.Context) {
|
||||||
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
|
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
|
||||||
@@ -72,5 +75,18 @@ func AddMovie() gin.HandlersChain {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if err := validate.Struct(movie); err != nil {
|
||||||
|
c.JSON(http.StatusBadRequest, gin.H{"error": "Validation failed when binding struct", "details": err.Error()})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
result, err := movieCollection.InsertOne(ctx, movie)
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to insert the movie"})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
c.JSON(http.StatusCreated, result)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user