diff --git a/dri/download/download.go b/dri/download/download.go index 4d84680..ede6a81 100644 --- a/dri/download/download.go +++ b/dri/download/download.go @@ -18,13 +18,13 @@ func Down(url, path, name string, e ...*Error) bool { } out, err := os.Create(path + name) - if err != nil && e[0] != nil { + if err != nil && len(e) != 0 { e[0].SetError(err) return false } defer out.Close() resp, err := http.Get(url) - if err != nil && e[0] != nil { + if err != nil && len(e) != 0 { e[0].SetError(err) return false } diff --git a/dri/mongodb/mongodb.go b/dri/mongodb/mongodb.go index 24c1964..93252f1 100644 --- a/dri/mongodb/mongodb.go +++ b/dri/mongodb/mongodb.go @@ -1,4 +1,4 @@ -package mongodb +package libs import ( . "code.hoteas.com/golang/hotime/common" @@ -8,7 +8,7 @@ import ( "go.mongodb.org/mongo-driver/mongo/options" ) -type mongoDb struct { +type MongoDb struct { Client *mongo.Client Ctx context.Context DataBase *mongo.Database @@ -16,12 +16,12 @@ type mongoDb struct { LastErr error } -func Init(database, url string) (*mongoDb, error) { - db := mongoDb{} +func GetMongoDb(database, url string) (*MongoDb, error) { + db := MongoDb{} clientOptions := options.Client().ApplyURI(url) db.Ctx = context.TODO() - // Connect to MongoDB + // Connect to MongoDb var err error db.Client, err = mongo.Connect(db.Ctx, clientOptions) if err != nil { @@ -32,7 +32,7 @@ func Init(database, url string) (*mongoDb, error) { if err != nil { return nil, err } - fmt.Println("Connected to MongoDB!") + fmt.Println("Connected to MongoDb!") //databases, err := db.Client.ListDatabaseNames(db.Ctx, bson.M{}) //if err != nil { // return nil, err @@ -43,7 +43,7 @@ func Init(database, url string) (*mongoDb, error) { } -func (that *mongoDb) Insert(table string, data interface{}) string { +func (that *MongoDb) Insert(table string, data interface{}) string { collection := that.DataBase.Collection(table) re, err := collection.InsertOne(that.Ctx, data) if err != nil { @@ -53,7 +53,7 @@ func (that *mongoDb) Insert(table string, data interface{}) string { return ObjToStr(re.InsertedID) } -func (that *mongoDb) InsertMany(table string, data ...interface{}) Slice { +func (that *MongoDb) InsertMany(table string, data ...interface{}) Slice { collection := that.DataBase.Collection(table) re, err := collection.InsertMany(that.Ctx, data) if err != nil { @@ -65,7 +65,7 @@ func (that *mongoDb) InsertMany(table string, data ...interface{}) Slice { } -func (that *mongoDb) Update(table string, data Map, where Map) int64 { +func (that *MongoDb) Update(table string, data Map, where Map) int64 { collection := that.DataBase.Collection(table) re, err := collection.UpdateMany(that.Ctx, where, data) @@ -78,7 +78,7 @@ func (that *mongoDb) Update(table string, data Map, where Map) int64 { } -func (that *mongoDb) Delete(table string, where Map) int64 { +func (that *MongoDb) Delete(table string, where Map) int64 { collection := that.DataBase.Collection(table) re, err := collection.DeleteMany(that.Ctx, where) @@ -91,7 +91,7 @@ func (that *mongoDb) Delete(table string, where Map) int64 { } -func (that *mongoDb) Get(table string, where Map) Map { +func (that *MongoDb) Get(table string, where Map) Map { results := []Map{} var cursor *mongo.Cursor var err error @@ -119,7 +119,7 @@ func (that *mongoDb) Get(table string, where Map) Map { return nil } -func (that mongoDb) Select(table string, where Map, page, pageRow int64) []Map { +func (that MongoDb) Select(table string, where Map, page, pageRow int64) []Map { page = (page - 1) * pageRow if page < 0 { page = 0