You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
81 lines
1.7 KiB
81 lines
1.7 KiB
package customer
|
|
|
|
import (
|
|
"context"
|
|
"log"
|
|
"time"
|
|
|
|
arango_db "git.slaventius.ru/test3k/authDB/internal/transport/arango"
|
|
"git.slaventius.ru/test3k/umate/pkg/kafka"
|
|
|
|
"github.com/arangodb/go-driver"
|
|
)
|
|
|
|
type CustomerRepository struct {
|
|
ctx context.Context
|
|
database driver.Database
|
|
collection driver.Collection
|
|
Customers map[string]Customer
|
|
}
|
|
|
|
// Хранилище покупателей
|
|
func NewCustomerRepository(ctx context.Context, database *arango_db.DataBase) *CustomerRepository {
|
|
collName := "customers"
|
|
collection, err := database.AddCollection(collName)
|
|
if err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
|
|
return &CustomerRepository{
|
|
ctx: ctx,
|
|
database: database.DB,
|
|
collection: collection,
|
|
Customers: make(map[string]Customer),
|
|
}
|
|
}
|
|
|
|
// Новый покупатель
|
|
func (r *CustomerRepository) NewCustomer(login string, password string, email string, code string) Customer {
|
|
return Customer{
|
|
SimpleRow: *NewSimpleRow(r.ctx, r.database, r.collection),
|
|
Login: login,
|
|
Password: password,
|
|
Time: time.Now().Add(time.Minute * 15),
|
|
MessageRegistration: kafka.MessageRegistration{
|
|
Email: email,
|
|
Code: code,
|
|
},
|
|
}
|
|
}
|
|
|
|
func (r *CustomerRepository) FetchAll() error {
|
|
params := make(map[string]interface{})
|
|
params["@coll"] = r.collection.Name()
|
|
|
|
//
|
|
query := "for el in @@coll return el"
|
|
cursor, err := r.database.Query(r.ctx, query, params)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
defer cursor.Close()
|
|
|
|
//
|
|
for {
|
|
rec := Customer{}
|
|
meta, era := cursor.ReadDocument(r.ctx, &rec)
|
|
if driver.IsNoMoreDocuments(era) {
|
|
break
|
|
}
|
|
|
|
//
|
|
rec.ctx = r.ctx
|
|
rec.collection = r.collection
|
|
rec.database = r.database
|
|
rec.key = meta.Key
|
|
|
|
r.Customers[rec.Login] = rec
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|