|
@@ -20,6 +20,8 @@ type RepoSession struct {
|
|
const (
|
|
const (
|
|
CollectionCategory = "category"
|
|
CollectionCategory = "category"
|
|
CollectionUser = "users"
|
|
CollectionUser = "users"
|
|
|
|
+ CollectionLearnLog = "learn_logs"
|
|
|
|
+ CollectionTest = "tests"
|
|
)
|
|
)
|
|
|
|
|
|
type Map map[string]interface{}
|
|
type Map map[string]interface{}
|
|
@@ -32,6 +34,7 @@ type PageResult struct {
|
|
}
|
|
}
|
|
|
|
|
|
type PageSearchOptions struct {
|
|
type PageSearchOptions struct {
|
|
|
|
+ Db string
|
|
CollectName string
|
|
CollectName string
|
|
Page int64
|
|
Page int64
|
|
Size int64
|
|
Size int64
|
|
@@ -64,6 +67,15 @@ func RepoAddDoc(ctx *RepoSession, collectName string, doc interface{}) (string,
|
|
return result.InsertedID.(primitive.ObjectID).Hex(), nil
|
|
return result.InsertedID.(primitive.ObjectID).Hex(), nil
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+func RepoAddDbDoc(ctx *RepoSession, Db string, collectName string, doc interface{}) (string, error) {
|
|
|
|
+ coll := ctx.Client.GetDbCollection(Db, collectName)
|
|
|
|
+ result, err := coll.InsertOne(ctx.Ctx, doc)
|
|
|
|
+ if err != nil {
|
|
|
|
+ return "", err
|
|
|
|
+ }
|
|
|
|
+ return result.InsertedID.(primitive.ObjectID).Hex(), nil
|
|
|
|
+}
|
|
|
|
+
|
|
func RepoDeleteDoc(ctx *RepoSession, collectName string, id string) (interface{}, error) {
|
|
func RepoDeleteDoc(ctx *RepoSession, collectName string, id string) (interface{}, error) {
|
|
|
|
|
|
uid, _ := primitive.ObjectIDFromHex(id)
|
|
uid, _ := primitive.ObjectIDFromHex(id)
|
|
@@ -73,6 +85,15 @@ func RepoDeleteDoc(ctx *RepoSession, collectName string, id string) (interface{}
|
|
return colls.DeleteOne(ctx.Ctx, &bson.M{"_id": uid})
|
|
return colls.DeleteOne(ctx.Ctx, &bson.M{"_id": uid})
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+func RepoDeleteDbDoc(ctx *RepoSession, Db string, collectName string, id string) (interface{}, error) {
|
|
|
|
+
|
|
|
|
+ uid, _ := primitive.ObjectIDFromHex(id)
|
|
|
|
+
|
|
|
|
+ colls := ctx.Client.GetDbCollection(Db, collectName)
|
|
|
|
+
|
|
|
|
+ return colls.DeleteOne(ctx.Ctx, &bson.M{"_id": uid})
|
|
|
|
+}
|
|
|
|
+
|
|
func RepoDeleteDocs(ctx *RepoSession, collectName string, query interface{}) (interface{}, error) {
|
|
func RepoDeleteDocs(ctx *RepoSession, collectName string, query interface{}) (interface{}, error) {
|
|
colls := ctx.Client.GetCollection(collectName)
|
|
colls := ctx.Client.GetCollection(collectName)
|
|
|
|
|
|
@@ -99,6 +120,17 @@ func RepoUpdateSetDoc(ctx *RepoSession, collectName string, idstr string, model
|
|
return colls.UpdateByID(ctx.Ctx, uid, update)
|
|
return colls.UpdateByID(ctx.Ctx, uid, update)
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+func RepoUpdateDbSetDoc(ctx *RepoSession, Db string, collectName string, idstr string, model interface{}) (*mongo.UpdateResult, error) {
|
|
|
|
+
|
|
|
|
+ colls := ctx.Client.GetDbCollection(Db, collectName)
|
|
|
|
+
|
|
|
|
+ update := bson.M{"$set": model}
|
|
|
|
+
|
|
|
|
+ uid, _ := primitive.ObjectIDFromHex(idstr)
|
|
|
|
+
|
|
|
|
+ return colls.UpdateByID(ctx.Ctx, uid, update)
|
|
|
|
+}
|
|
|
|
+
|
|
func RepoUpsertSetDoc(ctx *RepoSession, collectName string, filter interface{}, model interface{}) (*mongo.UpdateResult, error) {
|
|
func RepoUpsertSetDoc(ctx *RepoSession, collectName string, filter interface{}, model interface{}) (*mongo.UpdateResult, error) {
|
|
|
|
|
|
coll := ctx.Client.GetCollection(collectName)
|
|
coll := ctx.Client.GetCollection(collectName)
|
|
@@ -291,6 +323,64 @@ func RepoPageSearch(ctx *RepoSession, para *PageSearchOptions) (out *PageResult,
|
|
return
|
|
return
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+func RepoDbPageSearch(ctx *RepoSession, para *PageSearchOptions) (out *PageResult, err error) {
|
|
|
|
+
|
|
|
|
+ colls := ctx.Client.GetDbCollection(para.Db, para.CollectName)
|
|
|
|
+
|
|
|
|
+ findoptions := &options.FindOptions{}
|
|
|
|
+
|
|
|
|
+ if para.Size > 0 {
|
|
|
|
+ findoptions.SetLimit(para.Size)
|
|
|
|
+ findoptions.SetSkip(para.Size * (para.Page - 1))
|
|
|
|
+ }
|
|
|
|
+ if para.Sort != nil {
|
|
|
|
+ findoptions.SetSort(para.Sort)
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ if len(para.Project) > 0 {
|
|
|
|
+ prj := bson.M{}
|
|
|
|
+ for _, v := range para.Project {
|
|
|
|
+ prj[v] = 1
|
|
|
|
+ }
|
|
|
|
+ findoptions.SetProjection(prj)
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ filter := bson.M{}
|
|
|
|
+ if len(para.Query) > 0 {
|
|
|
|
+ for k, v := range para.Query {
|
|
|
|
+
|
|
|
|
+ if value, ok := v.(string); ok {
|
|
|
|
+ if len(value) > 0 {
|
|
|
|
+ filter[k] = v
|
|
|
|
+ continue
|
|
|
|
+ }
|
|
|
|
+ } else if v != nil {
|
|
|
|
+ filter[k] = v
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ cur, err := colls.Find(ctx.Ctx, filter, findoptions)
|
|
|
|
+
|
|
|
|
+ out = &PageResult{
|
|
|
|
+ List: []map[string]interface{}{},
|
|
|
|
+ Total: 0,
|
|
|
|
+ Page: para.Page,
|
|
|
|
+ Size: para.Size,
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ if err != nil {
|
|
|
|
+ return out, err
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ defer cur.Close(ctx.Ctx)
|
|
|
|
+
|
|
|
|
+ err = cur.All(ctx.Ctx, &out.List)
|
|
|
|
+
|
|
|
|
+ out.Total, _ = colls.CountDocuments(ctx.Ctx, filter)
|
|
|
|
+ return
|
|
|
|
+}
|
|
|
|
+
|
|
// PageSearch 单表分页查询
|
|
// PageSearch 单表分页查询
|
|
func RepoCountDoc(ctx *RepoSession, collectionName string, Query Map) (int64, error) {
|
|
func RepoCountDoc(ctx *RepoSession, collectionName string, Query Map) (int64, error) {
|
|
|
|
|