Skip to content

Commit

Permalink
fix: update storage
Browse files Browse the repository at this point in the history
  • Loading branch information
martynvdijke committed Sep 12, 2024
1 parent 617056b commit bce6f9d
Showing 1 changed file with 8 additions and 27 deletions.
35 changes: 8 additions & 27 deletions backend/plugins/webhook/api/generic.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ limitations under the License.
package api

import (
"encoding/json"
"fmt"
"net/http"
"time"
Expand All @@ -36,17 +35,16 @@ import (
type WebhookGenericReq struct {
Title string `mapstructure:"title" validate:"required"`
Description string `mapstructure:"description"`
Json map[string]interface{} `mapstructure:"json"`
Data map[string]interface{} `mapstructure:"json"`

CreatedDate *time.Time `mapstructure:"createdDate"`
}

type Generic struct {
Id int
Title string
Description string
CreatedDate *time.Time
data string
Data string
}

func PostGeneric(input *plugin.ApiResourceInput) (*plugin.ApiResourceOutput, errors.Error) {
Expand Down Expand Up @@ -74,20 +72,20 @@ func PostGeneric(input *plugin.ApiResourceInput) (*plugin.ApiResourceOutput, err
txHelper := dbhelper.NewTxHelper(basicRes, &err)
defer txHelper.End()
tx := txHelper.Begin()
if err := Create(connection, request, tx, logger); err != nil {
if err := SaveGeneric(connection, request, tx, logger); err != nil {
logger.Error(err, "create generic")
return nil, err
}

return &plugin.ApiResourceOutput{Body: nil, Status: http.StatusOK}, nil
}

func Create(connection *models.WebhookConnection, request *WebhookGenericReq, db dal.Transaction, logger log.Logger) errors.Error {
func SaveGeneric(connection *models.WebhookConnection, request *WebhookGenericReq, db dal.Transaction, logger log.Logger) errors.Error {
// validation
if request == nil {
return errors.BadInput.New("request body is nil")
}
if len(request.Json) == 0 {
if len(request.Data) == 0 {
return errors.BadInput.New("json payload is empty")
}
createdDate := time.Now()
Expand All @@ -98,34 +96,17 @@ func Create(connection *models.WebhookConnection, request *WebhookGenericReq, db
request.CreatedDate = &createdDate
}

fmt.Println(request.Json)

jsonString, err := json.Marshal(request.Json)
fmt.Println(jsonString)
if err != nil {
logger.Error(err, "Error marshaling JSON:")
}

generic := new(Generic)
generic.Title = request.Title
generic.Description = request.Description
generic.CreatedDate = request.CreatedDate
generic.data = string(jsonString)
generic.Data = fmt.Sprintf("%s", request.Data)

if !db.HasTable(generic) {
db.AutoMigrate(generic)
}
db.AutoMigrate(generic)
err = db.Create(generic)
err := db.CreateOrUpdate(generic)
if err != nil {
return nil
return err
}
db.All(generic)

// if err := tx.CreateOrUpdate(generic); err != nil {
// logger.Error(err, "failed to generic json data to disk")
// return err
// }

return nil
}

0 comments on commit bce6f9d

Please sign in to comment.