98 lines
2.7 KiB
Go
98 lines
2.7 KiB
Go
|
|
package repository
|
||
|
|
|
||
|
|
import (
|
||
|
|
"context"
|
||
|
|
|
||
|
|
libmongo "apps/backend/internal/lib/mongo"
|
||
|
|
"apps/backend/internal/module/usage/domain"
|
||
|
|
|
||
|
|
"github.com/zeromicro/go-zero/core/stores/mon"
|
||
|
|
"go.mongodb.org/mongo-driver/bson"
|
||
|
|
"go.mongodb.org/mongo-driver/mongo/options"
|
||
|
|
)
|
||
|
|
|
||
|
|
type MonStore struct {
|
||
|
|
events *mon.Model
|
||
|
|
prefs *mon.Model
|
||
|
|
purchases *mon.Model
|
||
|
|
}
|
||
|
|
|
||
|
|
func NewMonStore(uri, database string) *MonStore {
|
||
|
|
uri = libmongo.MustMongoURI(uri)
|
||
|
|
return &MonStore{
|
||
|
|
events: mon.MustNewModel(uri, database, "usage_events"),
|
||
|
|
prefs: mon.MustNewModel(uri, database, "usage_prefs"),
|
||
|
|
purchases: mon.MustNewModel(uri, database, "usage_purchases"),
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
func (s *MonStore) InsertEvent(ctx context.Context, e *domain.Event) error {
|
||
|
|
_, err := s.events.InsertOne(ctx, e)
|
||
|
|
return err
|
||
|
|
}
|
||
|
|
|
||
|
|
func (s *MonStore) ListEvents(ctx context.Context, uid int64, monthKey, keyMode string, limit int) ([]*domain.Event, error) {
|
||
|
|
filter := bson.M{"uid": uid}
|
||
|
|
if monthKey != "" {
|
||
|
|
filter["month_key"] = monthKey
|
||
|
|
}
|
||
|
|
if keyMode != "" && keyMode != "all" {
|
||
|
|
filter["key_mode"] = keyMode
|
||
|
|
}
|
||
|
|
opts := options.Find().SetSort(bson.D{{Key: "created_at", Value: -1}})
|
||
|
|
if limit > 0 {
|
||
|
|
opts.SetLimit(int64(limit))
|
||
|
|
}
|
||
|
|
var list []*domain.Event
|
||
|
|
err := s.events.Find(ctx, &list, filter, opts)
|
||
|
|
return list, err
|
||
|
|
}
|
||
|
|
|
||
|
|
func (s *MonStore) ListEventsAll(ctx context.Context, monthKey string, limit int) ([]*domain.Event, error) {
|
||
|
|
filter := bson.M{}
|
||
|
|
if monthKey != "" {
|
||
|
|
filter["month_key"] = monthKey
|
||
|
|
}
|
||
|
|
opts := options.Find().SetSort(bson.D{{Key: "created_at", Value: -1}})
|
||
|
|
if limit > 0 {
|
||
|
|
opts.SetLimit(int64(limit))
|
||
|
|
}
|
||
|
|
var list []*domain.Event
|
||
|
|
err := s.events.Find(ctx, &list, filter, opts)
|
||
|
|
return list, err
|
||
|
|
}
|
||
|
|
|
||
|
|
func (s *MonStore) GetPrefs(ctx context.Context, uid int64) (*domain.MemberPrefs, error) {
|
||
|
|
var p domain.MemberPrefs
|
||
|
|
err := s.prefs.FindOne(ctx, &p, bson.M{"uid": uid})
|
||
|
|
if err != nil {
|
||
|
|
if err == mon.ErrNotFound {
|
||
|
|
return &domain.MemberPrefs{UID: uid, PlanID: domain.PlanFree}, nil
|
||
|
|
}
|
||
|
|
return nil, err
|
||
|
|
}
|
||
|
|
return &p, nil
|
||
|
|
}
|
||
|
|
|
||
|
|
func (s *MonStore) SavePrefs(ctx context.Context, p *domain.MemberPrefs) error {
|
||
|
|
_, err := s.prefs.ReplaceOne(ctx, bson.M{"uid": p.UID}, p, options.Replace().SetUpsert(true))
|
||
|
|
return err
|
||
|
|
}
|
||
|
|
|
||
|
|
func (s *MonStore) InsertPurchase(ctx context.Context, p *domain.Purchase) error {
|
||
|
|
_, err := s.purchases.InsertOne(ctx, p)
|
||
|
|
return err
|
||
|
|
}
|
||
|
|
|
||
|
|
func (s *MonStore) ListPurchases(ctx context.Context, uid int64, limit int) ([]*domain.Purchase, error) {
|
||
|
|
opts := options.Find().SetSort(bson.D{{Key: "created_at", Value: -1}})
|
||
|
|
if limit > 0 {
|
||
|
|
opts.SetLimit(int64(limit))
|
||
|
|
}
|
||
|
|
var list []*domain.Purchase
|
||
|
|
err := s.purchases.Find(ctx, &list, bson.M{"uid": uid}, opts)
|
||
|
|
return list, err
|
||
|
|
}
|
||
|
|
|
||
|
|
var _ domain.Repository = (*MonStore)(nil)
|