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.
46 lines
1.3 KiB
Go
46 lines
1.3 KiB
Go
package model
|
|
|
|
import (
|
|
"context"
|
|
"time"
|
|
|
|
"go.mongodb.org/mongo-driver/bson"
|
|
"go.mongodb.org/mongo-driver/mongo"
|
|
)
|
|
|
|
type ActivityConfig struct {
|
|
ID int `json:"_id" bson:"_id"`
|
|
Name string `json:"name" bson:"name"`
|
|
Status int `json:"status" bson:"status"`
|
|
StartTime string `json:"start_time" bson:"start_time"`
|
|
EndTime string `json:"end_time" bson:"end_time"`
|
|
}
|
|
|
|
func GetActivityList(collection *mongo.Collection) ([]ActivityConfig, error) {
|
|
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
|
|
defer cancel()
|
|
cur, err := collection.Find(ctx, bson.D{})
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer cur.Close(ctx)
|
|
list := []ActivityConfig{}
|
|
err = cur.All(context.Background(), &list)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return list, nil
|
|
}
|
|
|
|
func UpdateActivity(collection *mongo.Collection, id int64, status int64, start_time string, end_time string) 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: "status", Value: status}, {Key: "start_time", Value: start_time}, {Key: "end_time", Value: end_time}}}}
|
|
_, err := collection.UpdateOne(ctx, filter, update)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
return nil
|
|
}
|