add repo methods
This commit is contained in:
parent
96c8f59b48
commit
e26d82d91c
@ -1,8 +1,12 @@
|
||||
package models
|
||||
|
||||
import "go.mongodb.org/mongo-driver/bson/primitive"
|
||||
|
||||
type Message struct {
|
||||
AccountID string // id аккаунта который что то сделал
|
||||
Email string // его mail
|
||||
ServiceKey string // сервис с которого пришло сообщение
|
||||
EventType string // тип события вызвавшее отправку данных в кафку
|
||||
ID primitive.ObjectID `bson:"_id"`
|
||||
AccountID string `bson:"accountID"` // id аккаунта который что то сделал
|
||||
Email string `bson:"email"` // его mail
|
||||
ServiceKey string `bson:"serviceKey"` // сервис с которого пришло сообщение
|
||||
EventType string `bson:"eventType"` // тип события вызвавшее отправку данных в кафку
|
||||
Send bool `bson:"send"`
|
||||
}
|
||||
|
@ -1,6 +1,12 @@
|
||||
package repository
|
||||
|
||||
import "go.mongodb.org/mongo-driver/mongo"
|
||||
import (
|
||||
"context"
|
||||
"go.mongodb.org/mongo-driver/bson"
|
||||
"go.mongodb.org/mongo-driver/bson/primitive"
|
||||
"go.mongodb.org/mongo-driver/mongo"
|
||||
"mailnotifier/internal/models"
|
||||
)
|
||||
|
||||
type Repository struct {
|
||||
mdb *mongo.Collection
|
||||
@ -12,6 +18,30 @@ func NewRepository(mdb *mongo.Collection) *Repository {
|
||||
}
|
||||
}
|
||||
|
||||
// метод вставки в коллекцию
|
||||
// записываем каждый месседж по одному
|
||||
func (r *Repository) Insert(ctx context.Context, mes models.Message) error {
|
||||
mes.ID = primitive.NewObjectID()
|
||||
mes.Send = false
|
||||
_, err := r.mdb.InsertOne(ctx, mes)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// метод получения из коллекции
|
||||
return nil
|
||||
}
|
||||
|
||||
// получаем сразу все в tools метод распределения
|
||||
func (r *Repository) GetMany(ctx context.Context) ([]models.Message, error) {
|
||||
cursor, err := r.mdb.Find(ctx, bson.D{})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer cursor.Close(ctx)
|
||||
|
||||
var messages []models.Message
|
||||
if err := cursor.All(ctx, &messages); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return messages, nil
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user