You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

43 lines
1.3 KiB
Go

package model
import (
"context"
"time"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
)
type AgentUrl struct {
ID string `json:"_id" bson:"_id"`
AgentID string `json:"agent_id" bson:"agent_id"`
Url string `json:"url" bson:"url"`
ExpireTime int `json:"expire_time" bson:"expire_time"`
}
func GetAgentUrlByAgengId(collection *mongo.Collection, agent_id string) (AgentUrl, error) {
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
filter := bson.D{{Key: "agent_id", Value: agent_id}}
var result AgentUrl
err := collection.FindOne(ctx, filter).Decode(&result)
if err != nil && err != mongo.ErrNoDocuments {
return result, err
}
return result, nil
}
func UpdateAgentUrl(collection *mongo.Collection, id, agent_id, url string, expire_time int) error {
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
filter := bson.D{{Key: "_id", Value: id}}
update := bson.D{{Key: "$set", Value: bson.D{{Key: "agent_id", Value: agent_id}, {Key: "url", Value: url}, {Key: "expire_time", Value: expire_time}}}}
opts := options.Update().SetUpsert(true)
_, err := collection.UpdateOne(ctx, filter, update, opts)
if err != nil {
return err
}
return nil
}