fix: refs for cassandra db

This commit is contained in:
Lakhan Samani
2023-08-01 16:09:17 +05:30
parent 15a4be5431
commit c2defdbaac
47 changed files with 103 additions and 255 deletions

View File

@@ -43,22 +43,18 @@ func (p *provider) UpdateEmailTemplate(ctx context.Context, emailTemplate *model
// ListEmailTemplates to list EmailTemplate
func (p *provider) ListEmailTemplate(ctx context.Context, pagination *model.Pagination) (*model.EmailTemplates, error) {
var emailTemplate *models.EmailTemplate
var iter dynamo.PagingIter
var lastEval dynamo.PagingKey
var iteration int64 = 0
collection := p.db.Table(models.Collections.EmailTemplate)
emailTemplates := []*model.EmailTemplate{}
paginationClone := pagination
scanner := collection.Scan()
count, err := scanner.Count()
if err != nil {
return nil, err
}
for (paginationClone.Offset + paginationClone.Limit) > iteration {
iter = scanner.StartFrom(lastEval).Limit(paginationClone.Limit).Iter()
for iter.NextWithContext(ctx, &emailTemplate) {
@@ -69,9 +65,7 @@ func (p *provider) ListEmailTemplate(ctx context.Context, pagination *model.Pagi
lastEval = iter.LastEvaluatedKey()
iteration += paginationClone.Limit
}
paginationClone.Total = count
return &model.EmailTemplates{
Pagination: paginationClone,
EmailTemplates: emailTemplates,
@@ -111,7 +105,6 @@ func (p *provider) GetEmailTemplateByEventName(ctx context.Context, eventName st
func (p *provider) DeleteEmailTemplate(ctx context.Context, emailTemplate *model.EmailTemplate) error {
collection := p.db.Table(models.Collections.EmailTemplate)
err := collection.Delete("id", emailTemplate.ID).RunWithContext(ctx)
if err != nil {
return err
}

View File

@@ -13,22 +13,16 @@ import (
// AddEnv to save environment information in database
func (p *provider) AddEnv(ctx context.Context, env *models.Env) (*models.Env, error) {
collection := p.db.Table(models.Collections.Env)
if env.ID == "" {
env.ID = uuid.New().String()
}
env.Key = env.ID
env.CreatedAt = time.Now().Unix()
env.UpdatedAt = time.Now().Unix()
err := collection.Put(env).RunWithContext(ctx)
if err != nil {
return env, err
}
return env, nil
}
@@ -36,9 +30,7 @@ func (p *provider) AddEnv(ctx context.Context, env *models.Env) (*models.Env, er
func (p *provider) UpdateEnv(ctx context.Context, env *models.Env) (*models.Env, error) {
collection := p.db.Table(models.Collections.Env)
env.UpdatedAt = time.Now().Unix()
err := UpdateByHashKey(collection, "id", env.ID, env)
if err != nil {
return env, err
}
@@ -48,24 +40,19 @@ func (p *provider) UpdateEnv(ctx context.Context, env *models.Env) (*models.Env,
// GetEnv to get environment information from database
func (p *provider) GetEnv(ctx context.Context) (*models.Env, error) {
var env *models.Env
collection := p.db.Table(models.Collections.Env)
// As there is no Findone supported.
iter := collection.Scan().Limit(1).Iter()
for iter.NextWithContext(ctx, &env) {
if env.ID == "" {
if env == nil {
return env, errors.New("no documets found")
} else {
return env, nil
}
}
err := iter.Err()
if err != nil {
return env, fmt.Errorf("config not found")
}
return env, nil
}

View File

@@ -91,7 +91,6 @@ func (p *provider) GetOTPByPhoneNumber(ctx context.Context, phoneNumber string)
// DeleteOTP to delete otp
func (p *provider) DeleteOTP(ctx context.Context, otp *models.OTP) error {
collection := p.db.Table(models.Collections.OTP)
if otp.ID != "" {
err := collection.Delete("id", otp.ID).RunWithContext(ctx)
if err != nil {

View File

@@ -42,10 +42,8 @@ func NewProvider() (*provider, error) {
} else {
log.Debugf("%s or %s or %s not found. Trying to load default credentials from aws config", constants.EnvAwsRegion, constants.EnvAwsAccessKeyID, constants.EnvAwsSecretAccessKey)
}
session := session.Must(session.NewSession(&config))
db := dynamo.New(session)
db.CreateTable(models.Collections.User, models.User{}).Wait()
db.CreateTable(models.Collections.Session, models.Session{}).Wait()
db.CreateTable(models.Collections.EmailTemplate, models.EmailTemplate{}).Wait()
@@ -54,7 +52,6 @@ func NewProvider() (*provider, error) {
db.CreateTable(models.Collections.VerificationRequest, models.VerificationRequest{}).Wait()
db.CreateTable(models.Collections.Webhook, models.Webhook{}).Wait()
db.CreateTable(models.Collections.WebhookLog, models.WebhookLog{}).Wait()
return &provider{
db: db,
}, nil

View File

@@ -11,11 +11,9 @@ import (
// AddSession to save session information in database
func (p *provider) AddSession(ctx context.Context, session *models.Session) error {
collection := p.db.Table(models.Collections.Session)
if session.ID == "" {
session.ID = uuid.New().String()
}
session.CreatedAt = time.Now().Unix()
session.UpdatedAt = time.Now().Unix()
err := collection.Put(session).RunWithContext(ctx)

View File

@@ -9,16 +9,13 @@ import (
func UpdateByHashKey(table dynamo.Table, hashKey string, hashValue string, item interface{}) error {
existingValue, err := dynamo.MarshalItem(item)
var i interface{}
if err != nil {
return err
}
nullableValue, err := dynamodbattribute.MarshalMap(item)
if err != nil {
return err
}
u := table.Update(hashKey, hashValue)
for k, v := range existingValue {
if k == hashKey {
@@ -26,7 +23,6 @@ func UpdateByHashKey(table dynamo.Table, hashKey string, hashValue string, item
}
u = u.Set(k, v)
}
for k, v := range nullableValue {
if k == hashKey {
continue
@@ -36,11 +32,9 @@ func UpdateByHashKey(table dynamo.Table, hashKey string, hashValue string, item
u = u.SetNullable(k, v)
}
}
err = u.Run()
if err != nil {
return err
}
return nil
}

View File

@@ -13,7 +13,6 @@ import (
// AddVerification to save verification request in database
func (p *provider) AddVerificationRequest(ctx context.Context, verificationRequest *models.VerificationRequest) (*models.VerificationRequest, error) {
collection := p.db.Table(models.Collections.VerificationRequest)
if verificationRequest.ID == "" {
verificationRequest.ID = uuid.New().String()
verificationRequest.CreatedAt = time.Now().Unix()
@@ -23,7 +22,6 @@ func (p *provider) AddVerificationRequest(ctx context.Context, verificationReque
return verificationRequest, err
}
}
return verificationRequest, nil
}
@@ -31,12 +29,10 @@ func (p *provider) AddVerificationRequest(ctx context.Context, verificationReque
func (p *provider) GetVerificationRequestByToken(ctx context.Context, token string) (*models.VerificationRequest, error) {
collection := p.db.Table(models.Collections.VerificationRequest)
var verificationRequest *models.VerificationRequest
iter := collection.Scan().Filter("'token' = ?", token).Iter()
for iter.NextWithContext(ctx, &verificationRequest) {
return verificationRequest, nil
}
err := iter.Err()
if err != nil {
return verificationRequest, err
@@ -52,7 +48,6 @@ func (p *provider) GetVerificationRequestByEmail(ctx context.Context, email stri
for iter.NextWithContext(ctx, &verificationRequest) {
return verificationRequest, nil
}
err := iter.Err()
if err != nil {
return verificationRequest, err
@@ -67,17 +62,13 @@ func (p *provider) ListVerificationRequests(ctx context.Context, pagination *mod
var lastEval dynamo.PagingKey
var iter dynamo.PagingIter
var iteration int64 = 0
collection := p.db.Table(models.Collections.VerificationRequest)
paginationClone := pagination
scanner := collection.Scan()
count, err := scanner.Count()
if err != nil {
return nil, err
}
for (paginationClone.Offset + paginationClone.Limit) > iteration {
iter = scanner.StartFrom(lastEval).Limit(paginationClone.Limit).Iter()
for iter.NextWithContext(ctx, &verificationRequest) {
@@ -92,9 +83,7 @@ func (p *provider) ListVerificationRequests(ctx context.Context, pagination *mod
lastEval = iter.LastEvaluatedKey()
iteration += paginationClone.Limit
}
paginationClone.Total = count
return &model.VerificationRequests{
VerificationRequests: verificationRequests,
Pagination: paginationClone,
@@ -104,7 +93,6 @@ func (p *provider) ListVerificationRequests(ctx context.Context, pagination *mod
// DeleteVerificationRequest to delete verification request from database
func (p *provider) DeleteVerificationRequest(ctx context.Context, verificationRequest *models.VerificationRequest) error {
collection := p.db.Table(models.Collections.VerificationRequest)
if verificationRequest.ID != "" {
err := collection.Delete("id", verificationRequest.ID).RunWithContext(ctx)

View File

@@ -13,16 +13,13 @@ import (
// AddWebhookLog to add webhook log
func (p *provider) AddWebhookLog(ctx context.Context, webhookLog *models.WebhookLog) (*model.WebhookLog, error) {
collection := p.db.Table(models.Collections.WebhookLog)
if webhookLog.ID == "" {
webhookLog.ID = uuid.New().String()
}
webhookLog.Key = webhookLog.ID
webhookLog.CreatedAt = time.Now().Unix()
webhookLog.UpdatedAt = time.Now().Unix()
err := collection.Put(webhookLog).RunWithContext(ctx)
if err != nil {
return nil, err
}
@@ -42,7 +39,6 @@ func (p *provider) ListWebhookLogs(ctx context.Context, pagination *model.Pagina
collection := p.db.Table(models.Collections.WebhookLog)
paginationClone := pagination
scanner := collection.Scan()
if webhookID != "" {
iter = scanner.Index("webhook_id").Filter("'webhook_id' = ?", webhookID).Iter()
for iter.NextWithContext(ctx, &webhookLog) {
@@ -68,7 +64,6 @@ func (p *provider) ListWebhookLogs(ctx context.Context, pagination *model.Pagina
iteration += paginationClone.Limit
}
}
paginationClone.Total = count
// paginationClone.Cursor = iter.LastEvaluatedKey()
return &model.WebhookLogs{