package wishlist import ( "context" "time" "wish-list-api/api/presenter" "wish-list-api/pkg/entities" "go.mongodb.org/mongo-driver/bson" "go.mongodb.org/mongo-driver/bson/primitive" "go.mongodb.org/mongo-driver/mongo" ) type Repository interface { CreateWishList(wishList *entities.WishList) (*entities.WishList, error) ReadWishList(ID string) (*entities.WishList, error) ReadAllWishLists(userID string) (*[]presenter.WishList, error) ReadPublicWishLists() (*[]presenter.WishList, error) UpdateWishList(wishList *entities.WishList) (*entities.WishList, error) DeleteWishList(ID string) error CreateWishListItem(item *entities.WishListItem) (*entities.WishListItem, error) ReadWishListItem(ID string) (*entities.WishListItem, error) ReadAllWishListItems(wishListID string) (*[]presenter.WishListItem, error) UpdateWishListItem(item *entities.WishListItem) (*entities.WishListItem, error) DeleteWishListItem(ID string) error } type repository struct { WishListCollection *mongo.Collection WishListItemCollection *mongo.Collection } func NewRepo(wishListCollection, wishListItemCollection *mongo.Collection) Repository { return &repository{ WishListCollection: wishListCollection, WishListItemCollection: wishListItemCollection, } } func NewMongoRepository(wishListCollection, wishListItemCollection *mongo.Collection) Repository { return NewRepo(wishListCollection, wishListItemCollection) } func (r *repository) CreateWishList(wishList *entities.WishList) (*entities.WishList, error) { wishList.ID = primitive.NewObjectID().Hex() wishList.CreatedAt = time.Now() wishList.UpdatedAt = time.Now() _, err := r.WishListCollection.InsertOne(context.Background(), wishList) if err != nil { return nil, err } return wishList, nil } func (r *repository) ReadWishList(ID string) (*entities.WishList, error) { var wishList entities.WishList err := r.WishListCollection.FindOne(context.Background(), bson.M{"_id": ID}).Decode(&wishList) if err != nil { return nil, err } return &wishList, nil } func (r *repository) ReadAllWishLists(userID string) (*[]presenter.WishList, error) { var wishLists []presenter.WishList cursor, err := r.WishListCollection.Find(context.Background(), bson.M{"user_id": userID}) if err != nil { return nil, err } for cursor.Next(context.TODO()) { var wishList presenter.WishList _ = cursor.Decode(&wishList) wishLists = append(wishLists, wishList) } return &wishLists, nil } func (r *repository) ReadPublicWishLists() (*[]presenter.WishList, error) { var wishLists []presenter.WishList cursor, err := r.WishListCollection.Find(context.Background(), bson.M{"is_public": true}) if err != nil { return nil, err } for cursor.Next(context.TODO()) { var wishList presenter.WishList _ = cursor.Decode(&wishList) wishLists = append(wishLists, wishList) } return &wishLists, nil } func (r *repository) UpdateWishList(wishList *entities.WishList) (*entities.WishList, error) { wishList.UpdatedAt = time.Now() _, err := r.WishListCollection.UpdateOne( context.Background(), bson.M{"_id": wishList.ID}, bson.M{"$set": wishList}, ) if err != nil { return nil, err } return wishList, nil } func (r *repository) DeleteWishList(ID string) error { _, err := r.WishListCollection.DeleteOne(context.Background(), bson.M{"_id": ID}) if err != nil { return err } _, err = r.WishListItemCollection.DeleteMany(context.Background(), bson.M{"wish_list_id": ID}) if err != nil { return err } return nil } func (r *repository) CreateWishListItem(item *entities.WishListItem) (*entities.WishListItem, error) { item.ID = primitive.NewObjectID().Hex() item.CreatedAt = time.Now() item.UpdatedAt = time.Now() _, err := r.WishListItemCollection.InsertOne(context.Background(), item) if err != nil { return nil, err } return item, nil } func (r *repository) ReadWishListItem(ID string) (*entities.WishListItem, error) { var item entities.WishListItem err := r.WishListItemCollection.FindOne(context.Background(), bson.M{"_id": ID}).Decode(&item) if err != nil { return nil, err } return &item, nil } func (r *repository) ReadAllWishListItems(wishListID string) (*[]presenter.WishListItem, error) { var items []presenter.WishListItem cursor, err := r.WishListItemCollection.Find(context.Background(), bson.M{"wish_list_id": wishListID}) if err != nil { return nil, err } for cursor.Next(context.TODO()) { var item presenter.WishListItem _ = cursor.Decode(&item) items = append(items, item) } return &items, nil } func (r *repository) UpdateWishListItem(item *entities.WishListItem) (*entities.WishListItem, error) { item.UpdatedAt = time.Now() _, err := r.WishListItemCollection.UpdateOne( context.Background(), bson.M{"_id": item.ID}, bson.M{"$set": item}, ) if err != nil { return nil, err } return item, nil } func (r *repository) DeleteWishListItem(ID string) error { _, err := r.WishListItemCollection.DeleteOne(context.Background(), bson.M{"_id": ID}) if err != nil { return err } return nil }