parent
6814a13a9b
commit
a13c9d6b49
6 changed files with 257 additions and 59 deletions
@ -0,0 +1,39 @@ |
||||
package customer |
||||
|
||||
import ( |
||||
"time" |
||||
|
||||
apiKafka "git.slaventius.ru/test3k/umate/pkg/kafka" |
||||
) |
||||
|
||||
// Покупатель
|
||||
type Customer struct { |
||||
SimpleRow |
||||
Login string |
||||
Password string |
||||
Confirmed bool |
||||
apiKafka.MessageRegistration |
||||
Time time.Time |
||||
} |
||||
|
||||
func (c *Customer) Update() error { |
||||
_, err := c.collection.UpdateDocument(c.ctx, c.key, c) |
||||
|
||||
return err |
||||
} |
||||
|
||||
func (c *Customer) Delete() error { |
||||
return c.SimpleRow.Delete() |
||||
} |
||||
|
||||
func (c *Customer) Refresh() error { |
||||
c.Time = time.Now().Add(time.Minute * 15) |
||||
|
||||
return c.Update() |
||||
} |
||||
|
||||
func (c *Customer) Confirm() error { |
||||
c.Confirmed = true |
||||
|
||||
return c.Update() |
||||
} |
@ -0,0 +1,58 @@ |
||||
package customer |
||||
|
||||
import ( |
||||
"context" |
||||
"strconv" |
||||
"time" |
||||
|
||||
driver "github.com/arangodb/go-driver" |
||||
) |
||||
|
||||
const ( |
||||
DateTemplate string = "2006-01-02" // Шаблон даты
|
||||
TimeTemplate string = "15:04:05" // Шаблон времени
|
||||
StampTemplate string = DateTemplate + " " + TimeTemplate // Шаблон метки времени
|
||||
DateTemplatePast string = "1900-01-01" + " " + TimeTemplate // Далекое прошлое
|
||||
) |
||||
|
||||
type Row struct { |
||||
ctx context.Context |
||||
collection driver.Collection |
||||
database driver.Database |
||||
key string |
||||
} |
||||
|
||||
// Базовые поля
|
||||
type SimpleRow struct { |
||||
Row |
||||
ID string `json:"id"` |
||||
CreatedAt string `json:"created_at"` |
||||
UpdatedAt string `json:"updated_at"` |
||||
DeletedAt string `json:"deleted_at"` |
||||
} |
||||
|
||||
func NewSimpleRow(ctx context.Context, database driver.Database, collection driver.Collection) *SimpleRow { |
||||
now := time.Now() |
||||
nowStr := now.Format(StampTemplate) |
||||
key := strconv.Itoa(now.Nanosecond()) |
||||
|
||||
return &SimpleRow{ |
||||
Row: Row{ |
||||
ctx: ctx, |
||||
collection: collection, |
||||
database: database, |
||||
key: key, |
||||
}, |
||||
ID: key, |
||||
CreatedAt: nowStr, |
||||
UpdatedAt: nowStr, |
||||
DeletedAt: DateTemplatePast, |
||||
} |
||||
} |
||||
|
||||
// Удаление
|
||||
func (r *SimpleRow) Delete() error { |
||||
_, err := r.collection.RemoveDocument(r.ctx, r.key) |
||||
|
||||
return err |
||||
} |
@ -0,0 +1,91 @@ |
||||
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, error) { |
||||
customer := 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, |
||||
}, |
||||
} |
||||
|
||||
//
|
||||
meta, err := r.collection.CreateDocument(r.ctx, customer) |
||||
if err != nil { |
||||
return customer, err |
||||
} else { |
||||
customer.key = meta.Key |
||||
} |
||||
|
||||
return customer, nil |
||||
} |
||||
|
||||
func (r *CustomerRepository) Fetch() 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 |
||||
} |
Loading…
Reference in new issue