// 2021 Rackspace, Inc. // // Licensed under the Apache License, Version 2.0 (the "License"): you may // not use this file except in compliance with the License. You may obtain // a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, WITHOUT // WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the // License for the specific language governing permissions and limitations // under the License. package tools import ( "context" "time" "github.rackspace.com/gorax/clients/goctk" "go.mongodb.org/mongo-driver/bson" "go.mongodb.org/mongo-driver/mongo" "go.mongodb.org/mongo-driver/mongo/options" ) // ProductGetBySku is a helper method to fetch a single product record. func (c *Client) ProductGetBySku(sku int) (product goctk.Product, err error) { ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second) defer cancel() cursor := c.db.Database(MigrationDB).Collection(ProductCollection).FindOne(ctx, bson.M{"_id": sku}) err = cursor.Decode(&product) return } // ProductGet is a helper method to fetch product records with a custom query. func (c *Client) ProductGet(query bson.M, projection []string) (products []goctk.Product, err error) { ctx, cancel := context.WithTimeout(context.Background(), 60*time.Second) defer cancel() opts := options.Find() if len(projection) != 0 { proj := bson.M{} for i := range projection { proj[projection[i]] = 1 } opts = options.Find().SetProjection(proj) } cursor, err := c.db.Database(MigrationDB).Collection(ProductCollection).Find(ctx, query, opts) if err != nil { return } products = make([]goctk.Product, 0) err = cursor.All(ctx, &products) return } // ProductCreateUpdate is a helper method to update or insert a platform into the platforms collection. func (c *Client) ProductCreateUpdate(product goctk.Product) (*mongo.UpdateResult, error) { ctx, cancel := context.WithTimeout(context.Background(), 60*time.Second) defer cancel() query := bson.M{ "_id": product.ID, } update := bson.M{ "$set": bson.M{ "category": product.Category, "sku": product.Sku, "description": product.Description, "name": product.Name, "active_date": product.ActiveDate, "sku_attribute_type_name": product.SkuAttributeTypeName, "inactive_date": product.InactiveDate, "active": product.Active, "item_quantity": product.ItemQuantity, "external_url": product.ExternalURL, "id": product.ID, "icon": product.Icon, }, } opts := options.Update().SetUpsert(true) result, err := c.db.Database(MigrationDB).Collection(ProductCollection).UpdateOne(ctx, query, update, opts) if err != nil { return nil, err } return result, nil } // ProductDelete is a helper method to remove a single platform record. func (c *Client) ProductDelete(sku int) (*mongo.DeleteResult, error) { ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second) defer cancel() return c.db.Database(MigrationDB).Collection(ProductCollection).DeleteOne(ctx, bson.M{"_id": sku}) }