fix: refs for db provider and few utils
This commit is contained in:
@@ -10,7 +10,7 @@ import (
|
||||
)
|
||||
|
||||
// AddEmailTemplate to add EmailTemplate
|
||||
func (p *provider) AddEmailTemplate(ctx context.Context, emailTemplate models.EmailTemplate) (*model.EmailTemplate, error) {
|
||||
func (p *provider) AddEmailTemplate(ctx context.Context, emailTemplate *models.EmailTemplate) (*model.EmailTemplate, error) {
|
||||
if emailTemplate.ID == "" {
|
||||
emailTemplate.ID = uuid.New().String()
|
||||
}
|
||||
@@ -27,7 +27,7 @@ func (p *provider) AddEmailTemplate(ctx context.Context, emailTemplate models.Em
|
||||
}
|
||||
|
||||
// UpdateEmailTemplate to update EmailTemplate
|
||||
func (p *provider) UpdateEmailTemplate(ctx context.Context, emailTemplate models.EmailTemplate) (*model.EmailTemplate, error) {
|
||||
func (p *provider) UpdateEmailTemplate(ctx context.Context, emailTemplate *models.EmailTemplate) (*model.EmailTemplate, error) {
|
||||
emailTemplate.UpdatedAt = time.Now().Unix()
|
||||
|
||||
res := p.db.Save(&emailTemplate)
|
||||
@@ -38,9 +38,8 @@ func (p *provider) UpdateEmailTemplate(ctx context.Context, emailTemplate models
|
||||
}
|
||||
|
||||
// ListEmailTemplates to list EmailTemplate
|
||||
func (p *provider) ListEmailTemplate(ctx context.Context, pagination model.Pagination) (*model.EmailTemplates, error) {
|
||||
var emailTemplates []models.EmailTemplate
|
||||
|
||||
func (p *provider) ListEmailTemplate(ctx context.Context, pagination *model.Pagination) (*model.EmailTemplates, error) {
|
||||
var emailTemplates []*models.EmailTemplate
|
||||
result := p.db.Limit(int(pagination.Limit)).Offset(int(pagination.Offset)).Order("created_at DESC").Find(&emailTemplates)
|
||||
if result.Error != nil {
|
||||
return nil, result.Error
|
||||
@@ -60,14 +59,14 @@ func (p *provider) ListEmailTemplate(ctx context.Context, pagination model.Pagin
|
||||
responseEmailTemplates = append(responseEmailTemplates, w.AsAPIEmailTemplate())
|
||||
}
|
||||
return &model.EmailTemplates{
|
||||
Pagination: &paginationClone,
|
||||
Pagination: paginationClone,
|
||||
EmailTemplates: responseEmailTemplates,
|
||||
}, nil
|
||||
}
|
||||
|
||||
// GetEmailTemplateByID to get EmailTemplate by id
|
||||
func (p *provider) GetEmailTemplateByID(ctx context.Context, emailTemplateID string) (*model.EmailTemplate, error) {
|
||||
var emailTemplate models.EmailTemplate
|
||||
var emailTemplate *models.EmailTemplate
|
||||
|
||||
result := p.db.Where("id = ?", emailTemplateID).First(&emailTemplate)
|
||||
if result.Error != nil {
|
||||
@@ -78,7 +77,7 @@ func (p *provider) GetEmailTemplateByID(ctx context.Context, emailTemplateID str
|
||||
|
||||
// GetEmailTemplateByEventName to get EmailTemplate by event_name
|
||||
func (p *provider) GetEmailTemplateByEventName(ctx context.Context, eventName string) (*model.EmailTemplate, error) {
|
||||
var emailTemplate models.EmailTemplate
|
||||
var emailTemplate *models.EmailTemplate
|
||||
|
||||
result := p.db.Where("event_name = ?", eventName).First(&emailTemplate)
|
||||
if result.Error != nil {
|
||||
@@ -95,6 +94,5 @@ func (p *provider) DeleteEmailTemplate(ctx context.Context, emailTemplate *model
|
||||
if result.Error != nil {
|
||||
return result.Error
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
@@ -9,7 +9,7 @@ import (
|
||||
)
|
||||
|
||||
// AddEnv to save environment information in database
|
||||
func (p *provider) AddEnv(ctx context.Context, env models.Env) (models.Env, error) {
|
||||
func (p *provider) AddEnv(ctx context.Context, env *models.Env) (*models.Env, error) {
|
||||
if env.ID == "" {
|
||||
env.ID = uuid.New().String()
|
||||
}
|
||||
@@ -26,10 +26,9 @@ func (p *provider) AddEnv(ctx context.Context, env models.Env) (models.Env, erro
|
||||
}
|
||||
|
||||
// UpdateEnv to update environment information in database
|
||||
func (p *provider) UpdateEnv(ctx context.Context, env models.Env) (models.Env, error) {
|
||||
func (p *provider) UpdateEnv(ctx context.Context, env *models.Env) (*models.Env, error) {
|
||||
env.UpdatedAt = time.Now().Unix()
|
||||
result := p.db.Save(&env)
|
||||
|
||||
if result.Error != nil {
|
||||
return env, result.Error
|
||||
}
|
||||
@@ -37,13 +36,11 @@ func (p *provider) UpdateEnv(ctx context.Context, env models.Env) (models.Env, e
|
||||
}
|
||||
|
||||
// GetEnv to get environment information from database
|
||||
func (p *provider) GetEnv(ctx context.Context) (models.Env, error) {
|
||||
var env models.Env
|
||||
func (p *provider) GetEnv(ctx context.Context) (*models.Env, error) {
|
||||
var env *models.Env
|
||||
result := p.db.First(&env)
|
||||
|
||||
if result.Error != nil {
|
||||
return env, result.Error
|
||||
}
|
||||
|
||||
return env, nil
|
||||
}
|
||||
|
@@ -77,7 +77,7 @@ func NewProvider() (*provider, error) {
|
||||
logrus.Debug("Failed to drop phone number constraint:", err)
|
||||
}
|
||||
|
||||
err = sqlDB.AutoMigrate(&models.User{}, &models.VerificationRequest{}, &models.Session{}, &models.Env{}, &models.Webhook{}, models.WebhookLog{}, models.EmailTemplate{}, &models.OTP{})
|
||||
err = sqlDB.AutoMigrate(&models.User{}, &models.VerificationRequest{}, &models.Session{}, &models.Env{}, &models.Webhook{}, &models.WebhookLog{}, &models.EmailTemplate{}, &models.OTP{})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
@@ -10,7 +10,7 @@ import (
|
||||
)
|
||||
|
||||
// AddSession to save session information in database
|
||||
func (p *provider) AddSession(ctx context.Context, session models.Session) error {
|
||||
func (p *provider) AddSession(ctx context.Context, session *models.Session) error {
|
||||
if session.ID == "" {
|
||||
session.ID = uuid.New().String()
|
||||
}
|
||||
|
@@ -17,7 +17,7 @@ import (
|
||||
)
|
||||
|
||||
// AddUser to save user information in database
|
||||
func (p *provider) AddUser(ctx context.Context, user models.User) (models.User, error) {
|
||||
func (p *provider) AddUser(ctx context.Context, user *models.User) (*models.User, error) {
|
||||
if user.ID == "" {
|
||||
user.ID = uuid.New().String()
|
||||
}
|
||||
@@ -53,7 +53,7 @@ func (p *provider) AddUser(ctx context.Context, user models.User) (models.User,
|
||||
}
|
||||
|
||||
// UpdateUser to update user information in database
|
||||
func (p *provider) UpdateUser(ctx context.Context, user models.User) (models.User, error) {
|
||||
func (p *provider) UpdateUser(ctx context.Context, user *models.User) (*models.User, error) {
|
||||
user.UpdatedAt = time.Now().Unix()
|
||||
|
||||
result := p.db.Save(&user)
|
||||
@@ -66,7 +66,7 @@ func (p *provider) UpdateUser(ctx context.Context, user models.User) (models.Use
|
||||
}
|
||||
|
||||
// DeleteUser to delete user information from database
|
||||
func (p *provider) DeleteUser(ctx context.Context, user models.User) error {
|
||||
func (p *provider) DeleteUser(ctx context.Context, user *models.User) error {
|
||||
result := p.db.Where("user_id = ?", user.ID).Delete(&models.Session{})
|
||||
if result.Error != nil {
|
||||
return result.Error
|
||||
@@ -81,7 +81,7 @@ func (p *provider) DeleteUser(ctx context.Context, user models.User) error {
|
||||
}
|
||||
|
||||
// ListUsers to get list of users from database
|
||||
func (p *provider) ListUsers(ctx context.Context, pagination model.Pagination) (*model.Users, error) {
|
||||
func (p *provider) ListUsers(ctx context.Context, pagination *model.Pagination) (*model.Users, error) {
|
||||
var users []models.User
|
||||
result := p.db.Limit(int(pagination.Limit)).Offset(int(pagination.Offset)).Order("created_at DESC").Find(&users)
|
||||
if result.Error != nil {
|
||||
@@ -103,31 +103,28 @@ func (p *provider) ListUsers(ctx context.Context, pagination model.Pagination) (
|
||||
paginationClone.Total = total
|
||||
|
||||
return &model.Users{
|
||||
Pagination: &paginationClone,
|
||||
Pagination: paginationClone,
|
||||
Users: responseUsers,
|
||||
}, nil
|
||||
}
|
||||
|
||||
// GetUserByEmail to get user information from database using email address
|
||||
func (p *provider) GetUserByEmail(ctx context.Context, email string) (models.User, error) {
|
||||
var user models.User
|
||||
func (p *provider) GetUserByEmail(ctx context.Context, email string) (*models.User, error) {
|
||||
var user *models.User
|
||||
result := p.db.Where("email = ?", email).First(&user)
|
||||
if result.Error != nil {
|
||||
return user, result.Error
|
||||
}
|
||||
|
||||
return user, nil
|
||||
}
|
||||
|
||||
// GetUserByID to get user information from database using user ID
|
||||
func (p *provider) GetUserByID(ctx context.Context, id string) (models.User, error) {
|
||||
var user models.User
|
||||
|
||||
func (p *provider) GetUserByID(ctx context.Context, id string) (*models.User, error) {
|
||||
var user *models.User
|
||||
result := p.db.Where("id = ?", id).First(&user)
|
||||
if result.Error != nil {
|
||||
return user, result.Error
|
||||
}
|
||||
|
||||
return user, nil
|
||||
}
|
||||
|
||||
@@ -136,14 +133,12 @@ func (p *provider) GetUserByID(ctx context.Context, id string) (models.User, err
|
||||
func (p *provider) UpdateUsers(ctx context.Context, data map[string]interface{}, ids []string) error {
|
||||
// set updated_at time for all users
|
||||
data["updated_at"] = time.Now().Unix()
|
||||
|
||||
var res *gorm.DB
|
||||
if ids != nil && len(ids) > 0 {
|
||||
if len(ids) > 0 {
|
||||
res = p.db.Model(&models.User{}).Where("id in ?", ids).Updates(data)
|
||||
} else {
|
||||
res = p.db.Model(&models.User{}).Updates(data)
|
||||
}
|
||||
|
||||
if res.Error != nil {
|
||||
return res.Error
|
||||
}
|
||||
@@ -154,10 +149,8 @@ func (p *provider) UpdateUsers(ctx context.Context, data map[string]interface{},
|
||||
func (p *provider) GetUserByPhoneNumber(ctx context.Context, phoneNumber string) (*models.User, error) {
|
||||
var user *models.User
|
||||
result := p.db.Where("phone_number = ?", phoneNumber).First(&user)
|
||||
|
||||
if result.Error != nil {
|
||||
return nil, result.Error
|
||||
}
|
||||
|
||||
return user, nil
|
||||
}
|
||||
|
@@ -11,11 +11,10 @@ import (
|
||||
)
|
||||
|
||||
// AddVerification to save verification request in database
|
||||
func (p *provider) AddVerificationRequest(ctx context.Context, verificationRequest models.VerificationRequest) (models.VerificationRequest, error) {
|
||||
func (p *provider) AddVerificationRequest(ctx context.Context, verificationRequest *models.VerificationRequest) (*models.VerificationRequest, error) {
|
||||
if verificationRequest.ID == "" {
|
||||
verificationRequest.ID = uuid.New().String()
|
||||
}
|
||||
|
||||
verificationRequest.Key = verificationRequest.ID
|
||||
verificationRequest.CreatedAt = time.Now().Unix()
|
||||
verificationRequest.UpdatedAt = time.Now().Unix()
|
||||
@@ -23,75 +22,61 @@ func (p *provider) AddVerificationRequest(ctx context.Context, verificationReque
|
||||
Columns: []clause.Column{{Name: "email"}, {Name: "identifier"}},
|
||||
DoUpdates: clause.AssignmentColumns([]string{"token", "expires_at", "nonce", "redirect_uri"}),
|
||||
}).Create(&verificationRequest)
|
||||
|
||||
if result.Error != nil {
|
||||
return verificationRequest, result.Error
|
||||
}
|
||||
|
||||
return verificationRequest, nil
|
||||
}
|
||||
|
||||
// GetVerificationRequestByToken to get verification request from database using token
|
||||
func (p *provider) GetVerificationRequestByToken(ctx context.Context, token string) (models.VerificationRequest, error) {
|
||||
var verificationRequest models.VerificationRequest
|
||||
func (p *provider) GetVerificationRequestByToken(ctx context.Context, token string) (*models.VerificationRequest, error) {
|
||||
var verificationRequest *models.VerificationRequest
|
||||
result := p.db.Where("token = ?", token).First(&verificationRequest)
|
||||
|
||||
if result.Error != nil {
|
||||
return verificationRequest, result.Error
|
||||
}
|
||||
|
||||
return verificationRequest, nil
|
||||
}
|
||||
|
||||
// GetVerificationRequestByEmail to get verification request by email from database
|
||||
func (p *provider) GetVerificationRequestByEmail(ctx context.Context, email string, identifier string) (models.VerificationRequest, error) {
|
||||
var verificationRequest models.VerificationRequest
|
||||
|
||||
func (p *provider) GetVerificationRequestByEmail(ctx context.Context, email string, identifier string) (*models.VerificationRequest, error) {
|
||||
var verificationRequest *models.VerificationRequest
|
||||
result := p.db.Where("email = ? AND identifier = ?", email, identifier).First(&verificationRequest)
|
||||
|
||||
if result.Error != nil {
|
||||
return verificationRequest, result.Error
|
||||
}
|
||||
|
||||
return verificationRequest, nil
|
||||
}
|
||||
|
||||
// ListVerificationRequests to get list of verification requests from database
|
||||
func (p *provider) ListVerificationRequests(ctx context.Context, pagination model.Pagination) (*model.VerificationRequests, error) {
|
||||
func (p *provider) ListVerificationRequests(ctx context.Context, pagination *model.Pagination) (*model.VerificationRequests, error) {
|
||||
var verificationRequests []models.VerificationRequest
|
||||
|
||||
result := p.db.Limit(int(pagination.Limit)).Offset(int(pagination.Offset)).Order("created_at DESC").Find(&verificationRequests)
|
||||
if result.Error != nil {
|
||||
return nil, result.Error
|
||||
}
|
||||
|
||||
responseVerificationRequests := []*model.VerificationRequest{}
|
||||
for _, v := range verificationRequests {
|
||||
responseVerificationRequests = append(responseVerificationRequests, v.AsAPIVerificationRequest())
|
||||
}
|
||||
|
||||
var total int64
|
||||
totalRes := p.db.Model(&models.VerificationRequest{}).Count(&total)
|
||||
if totalRes.Error != nil {
|
||||
return nil, totalRes.Error
|
||||
}
|
||||
|
||||
paginationClone := pagination
|
||||
paginationClone.Total = total
|
||||
|
||||
return &model.VerificationRequests{
|
||||
VerificationRequests: responseVerificationRequests,
|
||||
Pagination: &paginationClone,
|
||||
Pagination: paginationClone,
|
||||
}, nil
|
||||
}
|
||||
|
||||
// DeleteVerificationRequest to delete verification request from database
|
||||
func (p *provider) DeleteVerificationRequest(ctx context.Context, verificationRequest models.VerificationRequest) error {
|
||||
func (p *provider) DeleteVerificationRequest(ctx context.Context, verificationRequest *models.VerificationRequest) error {
|
||||
result := p.db.Delete(&verificationRequest)
|
||||
|
||||
if result.Error != nil {
|
||||
return result.Error
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
@@ -12,7 +12,7 @@ import (
|
||||
)
|
||||
|
||||
// AddWebhook to add webhook
|
||||
func (p *provider) AddWebhook(ctx context.Context, webhook models.Webhook) (*model.Webhook, error) {
|
||||
func (p *provider) AddWebhook(ctx context.Context, webhook *models.Webhook) (*model.Webhook, error) {
|
||||
if webhook.ID == "" {
|
||||
webhook.ID = uuid.New().String()
|
||||
}
|
||||
@@ -29,7 +29,7 @@ func (p *provider) AddWebhook(ctx context.Context, webhook models.Webhook) (*mod
|
||||
}
|
||||
|
||||
// UpdateWebhook to update webhook
|
||||
func (p *provider) UpdateWebhook(ctx context.Context, webhook models.Webhook) (*model.Webhook, error) {
|
||||
func (p *provider) UpdateWebhook(ctx context.Context, webhook *models.Webhook) (*model.Webhook, error) {
|
||||
webhook.UpdatedAt = time.Now().Unix()
|
||||
// Event is changed
|
||||
if !strings.Contains(webhook.EventName, "-") {
|
||||
@@ -43,7 +43,7 @@ func (p *provider) UpdateWebhook(ctx context.Context, webhook models.Webhook) (*
|
||||
}
|
||||
|
||||
// ListWebhooks to list webhook
|
||||
func (p *provider) ListWebhook(ctx context.Context, pagination model.Pagination) (*model.Webhooks, error) {
|
||||
func (p *provider) ListWebhook(ctx context.Context, pagination *model.Pagination) (*model.Webhooks, error) {
|
||||
var webhooks []models.Webhook
|
||||
result := p.db.Limit(int(pagination.Limit)).Offset(int(pagination.Offset)).Order("created_at DESC").Find(&webhooks)
|
||||
if result.Error != nil {
|
||||
@@ -61,14 +61,14 @@ func (p *provider) ListWebhook(ctx context.Context, pagination model.Pagination)
|
||||
responseWebhooks = append(responseWebhooks, w.AsAPIWebhook())
|
||||
}
|
||||
return &model.Webhooks{
|
||||
Pagination: &paginationClone,
|
||||
Pagination: paginationClone,
|
||||
Webhooks: responseWebhooks,
|
||||
}, nil
|
||||
}
|
||||
|
||||
// GetWebhookByID to get webhook by id
|
||||
func (p *provider) GetWebhookByID(ctx context.Context, webhookID string) (*model.Webhook, error) {
|
||||
var webhook models.Webhook
|
||||
var webhook *models.Webhook
|
||||
|
||||
result := p.db.Where("id = ?", webhookID).First(&webhook)
|
||||
if result.Error != nil {
|
||||
|
@@ -12,7 +12,7 @@ import (
|
||||
)
|
||||
|
||||
// AddWebhookLog to add webhook log
|
||||
func (p *provider) AddWebhookLog(ctx context.Context, webhookLog models.WebhookLog) (*model.WebhookLog, error) {
|
||||
func (p *provider) AddWebhookLog(ctx context.Context, webhookLog *models.WebhookLog) (*model.WebhookLog, error) {
|
||||
if webhookLog.ID == "" {
|
||||
webhookLog.ID = uuid.New().String()
|
||||
}
|
||||
@@ -32,7 +32,7 @@ func (p *provider) AddWebhookLog(ctx context.Context, webhookLog models.WebhookL
|
||||
}
|
||||
|
||||
// ListWebhookLogs to list webhook logs
|
||||
func (p *provider) ListWebhookLogs(ctx context.Context, pagination model.Pagination, webhookID string) (*model.WebhookLogs, error) {
|
||||
func (p *provider) ListWebhookLogs(ctx context.Context, pagination *model.Pagination, webhookID string) (*model.WebhookLogs, error) {
|
||||
var webhookLogs []models.WebhookLog
|
||||
var result *gorm.DB
|
||||
var totalRes *gorm.DB
|
||||
@@ -63,6 +63,6 @@ func (p *provider) ListWebhookLogs(ctx context.Context, pagination model.Paginat
|
||||
}
|
||||
return &model.WebhookLogs{
|
||||
WebhookLogs: responseWebhookLogs,
|
||||
Pagination: &paginationClone,
|
||||
Pagination: paginationClone,
|
||||
}, nil
|
||||
}
|
||||
|
Reference in New Issue
Block a user