Skip to content

Commit

Permalink
feat: added db utils to account table
Browse files Browse the repository at this point in the history
  • Loading branch information
AvineshTripathi committed Apr 21, 2024
1 parent 38af933 commit 807888b
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 37 deletions.
17 changes: 9 additions & 8 deletions handlers/accounts_handler.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package handlers

import (
"context"
"database/sql"
"encoding/json"
"fmt"
Expand Down Expand Up @@ -31,14 +30,15 @@ func (handler *ApiHandler) IsOnboardedHandler(c *gin.Context) {
Status: "COMPLETE",
}

if handler.db == nil {
if handler.dbHandler.Db == nil {
output.Status = "PENDING_DATABASE"
c.JSON(http.StatusOK, output)
return
}

accounts := make([]models.Account, 0)
err := handler.db.NewRaw("SELECT * FROM accounts").Scan(handler.ctx, &accounts)

_, err := handler.dbHandler.HandleQuery(handler.ctx, "GET_ACCOUNT", &accounts, nil)
if err != nil {
log.WithError(err).Error("scan failed")
c.JSON(http.StatusInternalServerError, gin.H{"error": "scan failed"})
Expand All @@ -62,7 +62,7 @@ func (handler *ApiHandler) ListCloudAccountsHandler(c *gin.Context) {
return
}

err := handler.db.NewRaw("SELECT * FROM accounts").Scan(handler.ctx, &accounts)
_, err := handler.dbHandler.HandleQuery(handler.ctx, "GET_ACCOUNT", &accounts, nil)
if err != nil {
log.WithError(err).Error("scan failed")
c.JSON(http.StatusInternalServerError, gin.H{"error": "scan failed"})
Expand Down Expand Up @@ -104,7 +104,8 @@ func (handler *ApiHandler) NewCloudAccountHandler(c *gin.Context) {

unsavedAccounts = append(unsavedAccounts, account)
} else {
result, err := handler.db.NewInsert().Model(&account).Exec(context.Background())

result, err := handler.dbHandler.HandleQuery(handler.ctx, "ADD_ACCOUNT", &account, nil)
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
return
Expand Down Expand Up @@ -152,7 +153,7 @@ func (handler *ApiHandler) ReScanAccount(c *gin.Context) {
accountId := c.Param("id")

account := new(models.Account)
res, err := handler.db.NewUpdate().Model(account).Set("status = ? ", "SCANNING").Where("id = ?", accountId).Where("status = ?", "CONNECTED").Returning("*").Exec(handler.ctx)
res, err := handler.dbHandler.HandleQuery(handler.ctx, "RE_SCAN_ACCOUNT", &account, map[string]string{"id": accountId, "status": "CONNECTED"})
if err != nil {
log.Error("Couldn't set status", err)
return
Expand All @@ -169,7 +170,7 @@ func (handler *ApiHandler) DeleteCloudAccountHandler(c *gin.Context) {
accountId := c.Param("id")

account := new(models.Account)
_, err := handler.db.NewDelete().Model(account).Where("id = ?", accountId).Exec(handler.ctx)
_, err := handler.dbHandler.HandleQuery(handler.ctx, "DELETE_ACCOUNT", &account, map[string]string{"id": accountId})
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
return
Expand All @@ -188,7 +189,7 @@ func (handler *ApiHandler) UpdateCloudAccountHandler(c *gin.Context) {
return
}

_, err = handler.db.NewUpdate().Model(&account).Column("name", "provider", "credentials").Where("id = ?", accountId).Exec(handler.ctx)
_, err = handler.dbHandler.HandleQuery(handler.ctx, "UPDATE_ACCOOUNT", &account, map[string]string{"id": accountId})
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
return
Expand Down
28 changes: 15 additions & 13 deletions handlers/resources_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,24 +18,26 @@ import (
)

type ApiHandler struct {
db *bun.DB
ctx context.Context
telemetry bool
cfg models.Config
db *bun.DB
dbHandler DbHandler
ctx context.Context
telemetry bool
cfg models.Config
configPath string
analytics utils.Analytics
accounts []models.Account
analytics utils.Analytics
accounts []models.Account
}

func NewApiHandler(ctx context.Context, telemetry bool, analytics utils.Analytics, db *bun.DB, cfg models.Config, configPath string, accounts []models.Account) *ApiHandler {
handler := ApiHandler{
db: db,
ctx: ctx,
telemetry: telemetry,
cfg: cfg,
db: db,
dbHandler: NewDbHandler(db),
ctx: ctx,
telemetry: telemetry,
cfg: cfg,
configPath: configPath,
analytics: analytics,
accounts: accounts,
analytics: analytics,
accounts: accounts,
}
return &handler
}
Expand Down Expand Up @@ -428,7 +430,7 @@ func (handler *ApiHandler) RelationStatsHandler(c *gin.Context) {
Provider: ele.Provider,
})
}

c.JSON(http.StatusOK, out)

}
Expand Down
49 changes: 34 additions & 15 deletions models/execution.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package models
import (
"context"
"database/sql"

"fmt"

"github.com/uptrace/bun"
)
Expand All @@ -18,12 +18,12 @@ const (
)

type DbHandler struct {
db *bun.DB
Db *bun.DB
}

func NewDbHandler(db *bun.DB) DbHandler {
return DbHandler{
db: db,
Db: db,
}
}

Expand All @@ -35,60 +35,79 @@ type Object struct {

type Data map[string]Object

func (dbHandler DbHandler) HandleQuery(ctx context.Context, queryTitle string, schema interface{}) (sql.Result, error) {
func (dbHandler DbHandler) HandleQuery(ctx context.Context, queryTitle string, schema interface{}, additionals map[string]string) (sql.Result, error) {
var resp sql.Result
var err error
switch Queries[queryTitle].Type {
case SELECT:
err = executeRaw(dbHandler.db, ctx, Queries[queryTitle].Query, schema)
err = executeRaw(dbHandler.Db, ctx, Queries[queryTitle].Query, schema, additionals)
if err != nil {
return resp, err
}
case INSERT:
resp, err = executeInsert(dbHandler.db, ctx, schema)
resp, err = executeInsert(dbHandler.Db, ctx, schema, additionals)
if err != nil {
return resp, err
}
case DELETE:
resp, err = executeDelete(dbHandler.db, ctx, schema, Queries[queryTitle].Query)
resp, err = executeDelete(dbHandler.Db, ctx, schema, Queries[queryTitle].Query, additionals)
if err != nil {
return resp, err
}
case UPDATE:
resp, err := executeUpdate(dbHandler.db, ctx, schema, Queries[queryTitle].Query, Queries[queryTitle].Params)
resp, err := executeUpdate(dbHandler.Db, ctx, schema, Queries[queryTitle].Query, Queries[queryTitle].Params, additionals)
if err != nil {
return resp, err
}
}
return resp, nil
}

func executeRaw(db *bun.DB, ctx context.Context, query string, schema interface{}) error {
func executeRaw(db *bun.DB, ctx context.Context, query string, schema interface{}, additionals map[string]string) error {
if len(additionals) > 0 {
query = fmt.Sprintf("%s where", query)
}

for key, value := range additionals {
query = fmt.Sprintf("%s %s = %s", query, key, value)
}

err := db.NewRaw(query).Scan(ctx, schema)
if err != nil {
return err
}
return nil
return nil
}

func executeInsert(db *bun.DB, ctx context.Context, schema interface{}) (sql.Result, error) {
func executeInsert(db *bun.DB, ctx context.Context, schema interface{}, additionals map[string]string) (sql.Result, error) {
resp, err := db.NewInsert().Model(schema).Exec(ctx)
if err != nil {
return resp, err
}
return resp, nil
}

func executeDelete(db *bun.DB, ctx context.Context, schema interface{}, query string) (sql.Result, error) {
resp, err := db.NewDelete().Model(schema).Where("id = ?", query).Exec(ctx)
func executeDelete(db *bun.DB, ctx context.Context, schema interface{}, query string, additionals map[string]string) (sql.Result, error) {
updatedQuery := db.NewUpdate().Model(schema)
for key, value := range additionals {
updatedQuery = updatedQuery.Where(key, value)
}

resp, err := updatedQuery.Exec(ctx)
if err != nil {
return resp, err
}
return resp, nil
}

func executeUpdate(db *bun.DB, ctx context.Context, schema interface{}, query string, params []string) (sql.Result, error) {
resp, err := db.NewUpdate().Model(schema).Column(params...).Where("id = ?", query).Exec(ctx)
func executeUpdate(db *bun.DB, ctx context.Context, schema interface{}, query string, columns []string, additionals map[string]string) (sql.Result, error) {
updatedQuery := db.NewUpdate().Model(schema).Column(columns...)

for key, value := range additionals {
updatedQuery = updatedQuery.Where(key, value)
}

resp, err := updatedQuery.Exec(ctx)
if err != nil {
return resp, err
}
Expand Down
7 changes: 6 additions & 1 deletion models/queries.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@ var Queries = Data{
"UPDATE_ACCOUNT": Object{
Query: "",
Type: UPDATE,
Params: []string{"provider", "name"},
Params: []string{"name", "provider", "credentials"},
},
"RE_SCAN_ACCOUNT": Object{
Query: "",
Type: UPDATE,
Params: []string{"status"},
},
}

0 comments on commit 807888b

Please sign in to comment.