[server] common util for couchbase syntax
This commit is contained in:
parent
642581eefd
commit
8212e81023
|
@ -70,9 +70,11 @@ func (p *provider) UpdateEmailTemplate(ctx context.Context, emailTemplate models
|
|||
func (p *provider) ListEmailTemplate(ctx context.Context, pagination model.Pagination) (*model.EmailTemplates, error) {
|
||||
emailTemplates := []*model.EmailTemplate{}
|
||||
paginationClone := pagination
|
||||
|
||||
_, paginationClone.Total = p.GetTotalDocs(ctx, models.Collections.EmailTemplate)
|
||||
|
||||
total, err := p.GetTotalDocs(ctx, models.Collections.EmailTemplate)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
paginationClone.Total = total
|
||||
userQuery := fmt.Sprintf("SELECT _id, event_name, subject, design, template, created_at, updated_at FROM %s.%s ORDER BY _id OFFSET $1 LIMIT $2", p.scopeName, models.Collections.EmailTemplate)
|
||||
|
||||
queryResult, err := p.db.Query(userQuery, &gocb.QueryOptions{
|
||||
|
|
|
@ -44,7 +44,7 @@ func GetSetFields(webhookMap map[string]interface{}) (string, map[string]interfa
|
|||
return updateFields, params
|
||||
}
|
||||
|
||||
func (p *provider) GetTotalDocs(ctx context.Context, collection string) (error, int64) {
|
||||
func (p *provider) GetTotalDocs(ctx context.Context, collection string) (int64, error) {
|
||||
totalDocs := TotalDocs{}
|
||||
|
||||
countQuery := fmt.Sprintf("SELECT COUNT(*) as Total FROM %s.%s", p.scopeName, collection)
|
||||
|
@ -55,9 +55,9 @@ func (p *provider) GetTotalDocs(ctx context.Context, collection string) (error,
|
|||
queryRes.One(&totalDocs)
|
||||
|
||||
if err != nil {
|
||||
return err, totalDocs.Total
|
||||
return totalDocs.Total, err
|
||||
}
|
||||
return nil, totalDocs.Total
|
||||
return totalDocs.Total, nil
|
||||
}
|
||||
|
||||
type TotalDocs struct {
|
||||
|
|
|
@ -77,13 +77,14 @@ func (p *provider) ListUsers(ctx context.Context, pagination model.Pagination) (
|
|||
Context: ctx,
|
||||
PositionalParameters: []interface{}{paginationClone.Offset, paginationClone.Limit},
|
||||
})
|
||||
|
||||
_, paginationClone.Total = p.GetTotalDocs(ctx, models.Collections.User)
|
||||
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
total, err := p.GetTotalDocs(ctx, models.Collections.User)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
paginationClone.Total = total
|
||||
for queryResult.Next() {
|
||||
var user models.User
|
||||
err := queryResult.Row(&user)
|
||||
|
@ -92,12 +93,9 @@ func (p *provider) ListUsers(ctx context.Context, pagination model.Pagination) (
|
|||
}
|
||||
users = append(users, user.AsAPIUser())
|
||||
}
|
||||
|
||||
if err := queryResult.Err(); err != nil {
|
||||
return nil, err
|
||||
|
||||
}
|
||||
|
||||
return &model.Users{
|
||||
Pagination: &paginationClone,
|
||||
Users: users,
|
||||
|
@ -150,10 +148,8 @@ 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()
|
||||
|
||||
updateFields, params := GetSetFields(data)
|
||||
|
||||
if ids != nil && len(ids) > 0 {
|
||||
if len(ids) > 0 {
|
||||
for _, id := range ids {
|
||||
params["id"] = id
|
||||
userQuery := fmt.Sprintf("UPDATE %s.%s SET %s WHERE _id = $id", p.scopeName, models.Collections.User, updateFields)
|
||||
|
|
|
@ -83,16 +83,17 @@ func (p *provider) GetVerificationRequestByEmail(ctx context.Context, email stri
|
|||
func (p *provider) ListVerificationRequests(ctx context.Context, pagination model.Pagination) (*model.VerificationRequests, error) {
|
||||
var verificationRequests []*model.VerificationRequest
|
||||
paginationClone := pagination
|
||||
|
||||
_, paginationClone.Total = p.GetTotalDocs(ctx, models.Collections.VerificationRequest)
|
||||
|
||||
total, err := p.GetTotalDocs(ctx, models.Collections.VerificationRequest)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
paginationClone.Total = total
|
||||
query := fmt.Sprintf("SELECT _id, env, created_at, updated_at FROM %s.%s OFFSET $1 LIMIT $2", p.scopeName, models.Collections.VerificationRequest)
|
||||
queryResult, err := p.db.Query(query, &gocb.QueryOptions{
|
||||
Context: ctx,
|
||||
ScanConsistency: gocb.QueryScanConsistencyRequestPlus,
|
||||
PositionalParameters: []interface{}{paginationClone.Offset, paginationClone.Limit},
|
||||
})
|
||||
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
@ -104,7 +105,6 @@ func (p *provider) ListVerificationRequests(ctx context.Context, pagination mode
|
|||
}
|
||||
verificationRequests = append(verificationRequests, verificationRequest.AsAPIVerificationRequest())
|
||||
}
|
||||
|
||||
if err := queryResult.Err(); err != nil {
|
||||
return nil, err
|
||||
|
||||
|
|
|
@ -76,17 +76,17 @@ func (p *provider) ListWebhook(ctx context.Context, pagination model.Pagination)
|
|||
params := make(map[string]interface{}, 1)
|
||||
params["offset"] = paginationClone.Offset
|
||||
params["limit"] = paginationClone.Limit
|
||||
|
||||
total, err := p.GetTotalDocs(ctx, models.Collections.Webhook)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
paginationClone.Total = total
|
||||
query := fmt.Sprintf("SELECT _id, env, created_at, updated_at FROM %s.%s OFFSET $offset LIMIT $limit", p.scopeName, models.Collections.Webhook)
|
||||
|
||||
_, paginationClone.Total = p.GetTotalDocs(ctx, models.Collections.Webhook)
|
||||
|
||||
queryResult, err := p.db.Query(query, &gocb.QueryOptions{
|
||||
Context: ctx,
|
||||
ScanConsistency: gocb.QueryScanConsistencyRequestPlus,
|
||||
NamedParameters: params,
|
||||
})
|
||||
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
@ -98,10 +98,8 @@ func (p *provider) ListWebhook(ctx context.Context, pagination model.Pagination)
|
|||
}
|
||||
webhooks = append(webhooks, webhook.AsAPIWebhook())
|
||||
}
|
||||
|
||||
if err := queryResult.Err(); err != nil {
|
||||
return nil, err
|
||||
|
||||
}
|
||||
return &model.Webhooks{
|
||||
Pagination: &paginationClone,
|
||||
|
|
|
@ -45,9 +45,11 @@ func (p *provider) ListWebhookLogs(ctx context.Context, pagination model.Paginat
|
|||
params["webhookID"] = webhookID
|
||||
params["offset"] = paginationClone.Offset
|
||||
params["limit"] = paginationClone.Limit
|
||||
|
||||
_, paginationClone.Total = p.GetTotalDocs(ctx, models.Collections.WebhookLog)
|
||||
|
||||
total, err := p.GetTotalDocs(ctx, models.Collections.WebhookLog)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
paginationClone.Total = total
|
||||
if webhookID != "" {
|
||||
query = fmt.Sprintf(`SELECT _id, http_status, response, request, webhook_id, created_at, updated_at FROM %s.%s WHERE webhook_id=$webhookID`, p.scopeName, models.Collections.WebhookLog)
|
||||
} else {
|
||||
|
|
Loading…
Reference in New Issue
Block a user