diff --git a/internal/repository/mongo/dal.go b/internal/repository/mongo/dal.go index 69e3997..807dd28 100644 --- a/internal/repository/mongo/dal.go +++ b/internal/repository/mongo/dal.go @@ -84,7 +84,7 @@ type ErrorCreateIndex struct { } func (d *DAL) CreateMessageIndex(ctx context.Context) error { - keys := bson.D{{"$**", "text"}} + keys := bson.D{{Key: "$**", Value: "text"}} opts := options.Index().SetDefaultLanguage("russian") @@ -98,7 +98,7 @@ func (d *DAL) CreateMessageIndex(ctx context.Context) error { } func (d *DAL) CreateTicketIndex(ctx context.Context) error { - keys := bson.D{{"$**", "text"}} + keys := bson.D{{Key: "$**", Value: "text"}} opts := options.Index().SetDefaultLanguage("russian") @@ -112,7 +112,7 @@ func (d *DAL) CreateTicketIndex(ctx context.Context) error { } func (d *DAL) CreateAccountIndex(ctx context.Context) error { - keys := bson.D{{"$**", "text"}} + keys := bson.D{{Key: "$**", Value: "text"}} opts := options.Index().SetDefaultLanguage("russian") @@ -321,8 +321,8 @@ func (d *DAL) WatchTickets( ctx context.Context, userID string, yield func(ticket model.Ticket) error) error { - operationTypes := []bson.D{{{"operationType", "insert"}}, - {{"operationType", "update"}}} + operationTypes := []bson.D{{{Key: "operationType", Value: "insert"}}, + {{Key: "operationType", Value: "update"}}} matchStage := bson.M{ "$and": bson.A{ @@ -332,7 +332,7 @@ func (d *DAL) WatchTickets( } matchPipeline := mongo.Pipeline{ - bson.D{{"$match", matchStage}}, + bson.D{{Key: "$match", Value: matchStage}}, } cs, err := d.colTck.Watch(ctx, matchPipeline, @@ -388,7 +388,7 @@ func (d *DAL) YieldActiveTickets( } func (d *DAL) YieldTickets(ctx context.Context, limit int64) ([]model.Ticket, int64, error) { - sort := bson.D{{"State", -1}, {"UpdatedAt", 1}} + sort := bson.D{{Key: "State", Value: -1}, {Key: "UpdatedAt", Value: 1}} cursor, err := d.colTck.Find(ctx, bson.M{}, options.Find().SetLimit(limit).SetSort(sort)) if err != nil { @@ -417,7 +417,7 @@ func (d *DAL) YieldUserTickets(ctx context.Context, userID string, limit, offset } fmt.Println("UserID", userID) - sort := bson.D{{"State", -1}, {"UpdatedAt", -1}} + sort := bson.D{{Key: "State", Value: -1}, {Key: "UpdatedAt", Value: -1}} cursor, err := d.colTck.Find(ctx, query, options.Find().SetSort(sort).SetLimit(limit).SetSkip(offset)) if err != nil { @@ -442,15 +442,15 @@ func (d *DAL) YieldUserTickets(ctx context.Context, userID string, limit, offset func (d *DAL) WatchAllTickets(ctx context.Context, yield func(ticket model.Ticket) error) error { operationTypes := []bson.D{ - {{"operationType", "insert"}}, - {{"operationType", "update"}}, + {{Key: "operationType", Value: "insert"}}, + {{Key: "operationType", Value: "update"}}, } - matchStage := bson.D{{"$or", operationTypes}} + matchStage := bson.D{{Key: "$or", Value: operationTypes}} matchPipeline := mongo.Pipeline{ bson.D{ - {"$match", matchStage}, + {Key: "$match", Value: matchStage}, }, } @@ -503,7 +503,7 @@ func (d *DAL) YieldMessages( yield func(ticket model.Message) error) error { cursor, err := d.colMsg.Find(ctx, bson.M{ "TicketID": ticketID, - }, options.Find().SetLimit(20).SetSort(bson.D{{"CreatedAt", -1}})) + }, options.Find().SetLimit(20).SetSort(bson.D{{Key: "CreatedAt", Value: -1}})) if err != nil { return err } @@ -571,22 +571,22 @@ type Change struct { func (d *DAL) WatchMessages( ctx context.Context, ticketID string, yield func(ticket model.Message) error) error { - operationTypes := []bson.D{{{"operationType", "insert"}}, - {{"operationType", "update"}}} + operationTypes := []bson.D{{{Key: "operationType", Value: "insert"}}, + {{Key: "operationType", Value: "update"}}} - matchStage := bson.D{{"$and", []bson.D{ + matchStage := bson.D{{Key: "$and", Value: []bson.D{ { - {"$or", operationTypes}, + {Key: "$or", Value: operationTypes}, }, { { - "fullDocument.TicketID", ticketID, + Key: "fullDocument.TicketID", Value: ticketID, }, }, }}} matchPipeline := mongo.Pipeline{ - bson.D{{"$match", matchStage}}, + bson.D{{Key: "$match", Value: matchStage}}, } cs, err := d.colMsg.Watch(ctx, matchPipeline, @@ -605,7 +605,10 @@ func (d *DAL) WatchMessages( return err } - bson.Unmarshal(change.FullDocument, &piece) + err := bson.Unmarshal(change.FullDocument, &piece) + if err != nil { + return err + } if err := yield(piece); err != nil { return err } @@ -655,7 +658,7 @@ func (d *DAL) GetTicketPage( } } - sort := bson.D{{"State", -1}, {"UpdatedAt", -1}} + sort := bson.D{{Key: "State", Value: -1}, {Key: "UpdatedAt", Value: -1}} cur, err := d.colTck.Find(ctx, query, options.Find().SetSort(sort).SetLimit(limit).SetSkip(skip*limit)) if err != nil { @@ -700,7 +703,7 @@ func (d *DAL) GetMessagesPage(ctx context.Context, } } - sort := bson.D{{"CreatedAt", -1}} + sort := bson.D{{Key: "CreatedAt", Value: -1}} cur, err := d.colMsg.Find(ctx, query, options.Find().SetLimit(limit).SetSkip(limit*offset).SetSort(sort)) if err != nil { @@ -773,7 +776,7 @@ func (d *DAL) GetAdditionalData(ctx context.Context, id string) (*Additional, er if err := d.client.Database("bb-identity").Collection("tp-users").FindOne(ctx, bson.M{ "_id": id, }, options.FindOne().SetProjection(bson.D{{ - "Identities", 1, + Key: "Identities", Value: 1, }})).Decode(&idens); err != nil { return nil, err } @@ -789,7 +792,7 @@ func (d *DAL) GetAdditionalData(ctx context.Context, id string) (*Additional, er if err := d.client.Database("profile").Collection("profile").FindOne(ctx, bson.M{ "_id": id, }, options.FindOne().SetProjection(bson.D{{ - "display_id", 1, + Key: "display_id", Value: 1, }})).Decode(&u); err != nil { return nil, err } @@ -882,7 +885,7 @@ func (d *DAL) GetAccountPage(ctx context.Context, search string, offset, limit i count, err := d.colAcc.CountDocuments(ctx, query) - sort := bson.D{{"CreatedAt", -1}} + sort := bson.D{{Key: "CreatedAt", Value: -1}} cur, err := d.colAcc.Find(ctx, query, options.Find().SetLimit(limit).SetSkip(limit*offset).SetSort(sort)) if err != nil { diff --git a/script/main.go b/script/main.go index 491d39d..3b97653 100644 --- a/script/main.go +++ b/script/main.go @@ -29,12 +29,12 @@ func main() { fmt.Println("Ошибка подключения к монго:", err) return } - defer func() { + defer func(client *mongo.Client, ctx context.Context) { err := client.Disconnect(ctx) if err != nil { - fmt.Println("CAN NOT DISCONNECT", err) + return } - }() + }(client, ctx) collection := client.Database(mongoDBName).Collection(collectionName)