mirror of
https://gitlab.com/foxixus/neomovies-api.git
synced 2025-10-27 17:38:51 +05:00
Bug fix: favourites route
This commit is contained in:
@@ -5,6 +5,7 @@ import (
|
|||||||
"strconv"
|
"strconv"
|
||||||
|
|
||||||
"go.mongodb.org/mongo-driver/bson"
|
"go.mongodb.org/mongo-driver/bson"
|
||||||
|
"go.mongodb.org/mongo-driver/bson/primitive"
|
||||||
"go.mongodb.org/mongo-driver/mongo"
|
"go.mongodb.org/mongo-driver/mongo"
|
||||||
|
|
||||||
"neomovies-api/pkg/models"
|
"neomovies-api/pkg/models"
|
||||||
@@ -57,24 +58,32 @@ func (s *MovieService) GetSimilar(id, page int, language string) (*models.TMDBRe
|
|||||||
func (s *MovieService) AddToFavorites(userID string, movieID string) error {
|
func (s *MovieService) AddToFavorites(userID string, movieID string) error {
|
||||||
collection := s.db.Collection("users")
|
collection := s.db.Collection("users")
|
||||||
|
|
||||||
filter := bson.M{"_id": userID}
|
objID, err := primitive.ObjectIDFromHex(userID)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
filter := bson.M{"_id": objID}
|
||||||
update := bson.M{
|
update := bson.M{
|
||||||
"$addToSet": bson.M{"favorites": movieID},
|
"$addToSet": bson.M{"favorites": movieID},
|
||||||
}
|
}
|
||||||
|
|
||||||
_, err := collection.UpdateOne(context.Background(), filter, update)
|
_, err = collection.UpdateOne(context.Background(), filter, update)
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *MovieService) RemoveFromFavorites(userID string, movieID string) error {
|
func (s *MovieService) RemoveFromFavorites(userID string, movieID string) error {
|
||||||
collection := s.db.Collection("users")
|
collection := s.db.Collection("users")
|
||||||
|
|
||||||
filter := bson.M{"_id": userID}
|
objID, err := primitive.ObjectIDFromHex(userID)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
filter := bson.M{"_id": objID}
|
||||||
update := bson.M{
|
update := bson.M{
|
||||||
"$pull": bson.M{"favorites": movieID},
|
"$pull": bson.M{"favorites": movieID},
|
||||||
}
|
}
|
||||||
|
|
||||||
_, err := collection.UpdateOne(context.Background(), filter, update)
|
_, err = collection.UpdateOne(context.Background(), filter, update)
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -82,7 +91,11 @@ func (s *MovieService) GetFavorites(userID string, language string) ([]models.Mo
|
|||||||
collection := s.db.Collection("users")
|
collection := s.db.Collection("users")
|
||||||
|
|
||||||
var user models.User
|
var user models.User
|
||||||
err := collection.FindOne(context.Background(), bson.M{"_id": userID}).Decode(&user)
|
objID, err := primitive.ObjectIDFromHex(userID)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
err = collection.FindOne(context.Background(), bson.M{"_id": objID}).Decode(&user)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user