add filter to repo method
This commit is contained in:
parent
d143270b92
commit
f0c6bdc6a4
@ -3,7 +3,7 @@ package models
|
||||
import "time"
|
||||
|
||||
type Account struct {
|
||||
ID string `json:"id" bson:"_id,omitempty"`
|
||||
ID string `json:"id" bson:"_id"`
|
||||
UserID string `json:"userId" bson:"userId"`
|
||||
Cart []string `json:"cart" bson:"cart"`
|
||||
Wallet Wallet `json:"wallet" bson:"wallet"`
|
||||
|
@ -6,6 +6,7 @@ import (
|
||||
"go.mongodb.org/mongo-driver/bson/primitive"
|
||||
"go.mongodb.org/mongo-driver/mongo"
|
||||
"mailnotifier/internal/models"
|
||||
"time"
|
||||
)
|
||||
|
||||
type Repository struct {
|
||||
@ -32,10 +33,30 @@ func (r *Repository) Insert(ctx context.Context, mes models.Message) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// todo стоит лучше передавать фильтр сразу для того чтобы разгрузить запрос
|
||||
// получаем сразу все в tools метод распределения
|
||||
func (r *Repository) GetMany(ctx context.Context) ([]models.Message, error) {
|
||||
cursor, err := r.mdb.Find(ctx, bson.D{})
|
||||
to := time.Now().AddDate(0, 0, -15)
|
||||
from := time.Now().AddDate(0, 0, -7)
|
||||
|
||||
filter := bson.D{
|
||||
{"$and", bson.A{
|
||||
bson.D{
|
||||
{"$or", bson.A{
|
||||
bson.D{{"sendRegistration", false}},
|
||||
bson.D{{"sendNoneCreated", false}},
|
||||
bson.D{{"sendUnpublished", false}},
|
||||
}},
|
||||
},
|
||||
bson.D{
|
||||
{"sendAt", bson.D{
|
||||
{"$gte", to},
|
||||
{"$lte", from},
|
||||
}},
|
||||
},
|
||||
}},
|
||||
}
|
||||
|
||||
cursor, err := r.mdb.Find(ctx, filter)
|
||||
if err != nil {
|
||||
if err == mongo.ErrNoDocuments {
|
||||
return []models.Message{}, nil
|
||||
|
96
test/repo_test.go
Normal file
96
test/repo_test.go
Normal file
@ -0,0 +1,96 @@
|
||||
package test
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"mailnotifier/internal/initialize"
|
||||
"mailnotifier/internal/models"
|
||||
"mailnotifier/internal/repository"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
func TestInsertAndGetMany(t *testing.T) {
|
||||
crx := context.Background()
|
||||
mdb, err := initialize.MongoInit(crx, initialize.Config{
|
||||
MongoHost: "127.0.0.1",
|
||||
MongoPort: "27020",
|
||||
MongoUser: "test",
|
||||
MongoPassword: "test",
|
||||
MongoDatabase: "admin",
|
||||
MongoAuth: "admin",
|
||||
})
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
repo := repository.NewRepository(mdb.Collection("notify"))
|
||||
ctx := context.Background()
|
||||
testMessages := []models.Message{
|
||||
{
|
||||
AccountID: "1",
|
||||
Email: "test",
|
||||
ServiceKey: "1",
|
||||
SendRegistration: false,
|
||||
SendNoneCreated: false,
|
||||
SendUnpublished: true,
|
||||
SendPaid: false,
|
||||
SendAt: time.Now().AddDate(0, 0, -12),
|
||||
},
|
||||
{
|
||||
AccountID: "2",
|
||||
Email: "test2",
|
||||
ServiceKey: "2",
|
||||
SendRegistration: false,
|
||||
SendNoneCreated: true,
|
||||
SendUnpublished: false,
|
||||
SendPaid: true,
|
||||
SendAt: time.Now().AddDate(0, 0, -7),
|
||||
},
|
||||
{
|
||||
AccountID: "3",
|
||||
Email: "test3",
|
||||
ServiceKey: "3",
|
||||
SendRegistration: false,
|
||||
SendNoneCreated: false,
|
||||
SendUnpublished: false,
|
||||
SendPaid: false,
|
||||
SendAt: time.Now().AddDate(0, 0, -14),
|
||||
},
|
||||
{
|
||||
AccountID: "4",
|
||||
Email: "test4",
|
||||
ServiceKey: "4",
|
||||
SendRegistration: true,
|
||||
SendNoneCreated: true,
|
||||
SendUnpublished: true,
|
||||
SendPaid: true,
|
||||
SendAt: time.Now(),
|
||||
},
|
||||
{
|
||||
AccountID: "5",
|
||||
Email: "test5",
|
||||
ServiceKey: "5",
|
||||
SendRegistration: true,
|
||||
SendNoneCreated: true,
|
||||
SendUnpublished: true,
|
||||
SendPaid: true,
|
||||
SendAt: time.Now().AddDate(0, 0, -15),
|
||||
},
|
||||
}
|
||||
|
||||
for _, msg := range testMessages {
|
||||
if err := repo.Insert(ctx, msg); err != nil {
|
||||
fmt.Println(err)
|
||||
}
|
||||
}
|
||||
|
||||
result, err := repo.GetMany(ctx)
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
}
|
||||
|
||||
if len(result) != 3 {
|
||||
t.Errorf("ожидал %d , получил %d", 3, len(result))
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user