3
0

Make structs for page block & chart config

This commit is contained in:
Denis Arh 2019-09-18 15:19:25 +02:00
parent 71a78d53a5
commit 40ab1e22ea
4 changed files with 115 additions and 26 deletions

View File

@ -66,7 +66,10 @@ func (ctrl Chart) Create(ctx context.Context, r *request.ChartCreate) (interface
mod := &types.Chart{
NamespaceID: r.NamespaceID,
Name: r.Name,
Config: r.Config,
}
if err = r.Config.Unmarshal(&mod.Config); err != nil {
return nil, err
}
mod, err = ctrl.chart.With(ctx).Create(mod)
@ -81,15 +84,18 @@ func (ctrl Chart) Read(ctx context.Context, r *request.ChartRead) (interface{},
func (ctrl Chart) Update(ctx context.Context, r *request.ChartUpdate) (interface{}, error) {
var (
mod = &types.Chart{}
err error
mod = &types.Chart{
ID: r.ChartID,
Name: r.Name,
NamespaceID: r.NamespaceID,
UpdatedAt: r.UpdatedAt,
}
)
mod.ID = r.ChartID
mod.Name = r.Name
mod.Config = r.Config
mod.NamespaceID = r.NamespaceID
mod.UpdatedAt = r.UpdatedAt
if err = r.Config.Unmarshal(&mod.Config); err != nil {
return nil, err
}
mod, err = ctrl.chart.With(ctx).Update(mod)
return ctrl.makePayload(ctx, mod, err)

View File

@ -77,11 +77,14 @@ func (ctrl *Page) Create(ctx context.Context, r *request.PageCreate) (interface{
ModuleID: r.ModuleID,
Title: r.Title,
Description: r.Description,
Blocks: r.Blocks,
Visible: r.Visible,
}
)
if err = r.Blocks.Unmarshal(&mod.Blocks); err != nil {
return nil, err
}
mod, err = ctrl.page.With(ctx).Create(mod)
return ctrl.makePayload(ctx, mod, err)
}
@ -106,11 +109,14 @@ func (ctrl *Page) Update(ctx context.Context, r *request.PageUpdate) (interface{
ModuleID: r.ModuleID,
Title: r.Title,
Description: r.Description,
Blocks: r.Blocks,
Visible: r.Visible,
}
)
if err = r.Blocks.Unmarshal(&mod.Blocks); err != nil {
return nil, err
}
mod, err = ctrl.page.With(ctx).Update(mod)
return ctrl.makePayload(ctx, mod, err)
}

View File

@ -1,18 +1,22 @@
package types
import (
"database/sql/driver"
"encoding/json"
"time"
"github.com/davecgh/go-spew/spew"
"github.com/pkg/errors"
"github.com/cortezaproject/corteza-server/internal/permissions"
"github.com/jmoiron/sqlx/types"
)
type (
Chart struct {
ID uint64 `json:"chartID,string" db:"id"`
Handle string `json:"handle" db:"handle"`
Name string `json:"name" db:"name"`
Config types.JSONText `json:"config" db:"config"`
ID uint64 `json:"chartID,string" db:"id"`
Handle string `json:"handle" db:"handle"`
Name string `json:"name" db:"name"`
Config ChartConfig `json:"config" db:"config"`
NamespaceID uint64 `json:"namespaceID,string" db:"rel_namespace,string"`
@ -21,6 +25,20 @@ type (
DeletedAt *time.Time `db:"deleted_at" json:"deletedAt,omitempty"`
}
ChartConfig struct {
Reports []*ChartConfigReport `json:"reports,omitempty" yaml:",omitempty"`
}
ChartConfigReport struct {
Filter string `json:"filter" yaml:",omitempty"`
ModuleID uint64 `json:"moduleID,string" yaml:"moduleID"`
Metrics []map[string]interface{} `json:"metrics,omitempty" yaml:",omitempty"`
Dimensions []map[string]interface{} `json:"dimensions,omitempty" yaml:",omitempty"`
Renderer struct {
Version string `json:"version,omitempty" yaml:",omitempty"`
} `json:"renderer,omitempty" yaml:",omitempty"`
}
ChartFilter struct {
NamespaceID uint64 `json:"namespaceID,string"`
Query string `json:"query"`
@ -46,3 +64,23 @@ func (set ChartSet) FindByHandle(handle string) *Chart {
return nil
}
func (cc *ChartConfig) Scan(value interface{}) error {
//lint:ignore S1034 This typecast is intentional, we need to get []byte out of a []uint8
switch value.(type) {
case nil:
*cc = ChartConfig{}
case []uint8:
b := value.([]byte)
spew.Dump(string(b))
if err := json.Unmarshal(b, cc); err != nil {
return errors.Wrapf(err, "Can not scan '%v' into ChartConfig", string(b))
}
}
return nil
}
func (cc ChartConfig) Value() (driver.Value, error) {
return json.Marshal(cc)
}

View File

@ -1,10 +1,13 @@
package types
import (
"database/sql/driver"
"encoding/json"
"time"
"github.com/pkg/errors"
"github.com/cortezaproject/corteza-server/internal/permissions"
"github.com/jmoiron/sqlx/types"
)
type (
@ -21,7 +24,7 @@ type (
Title string `json:"title" db:"title"`
Description string `json:"description" db:"description"`
Blocks types.JSONText `json:"blocks" db:"blocks"`
Blocks PageBlocks `json:"blocks" db:"blocks"`
Children PageSet `json:"children,omitempty" db:"-"`
@ -33,16 +36,22 @@ type (
DeletedAt *time.Time `db:"deleted_at" json:"deletedAt,omitempty"`
}
// Block - value of Page.Blocks ([]Block)
Block struct {
Title string `json:"title"`
Description string `json:"description"`
Options types.JSONText `json:"options"`
Kind string `json:"kind"`
X int `json:"x"`
Y int `json:"y"`
Width int `json:"width"`
Height int `json:"height"`
PageBlocks []PageBlock
PageBlock struct {
Title string `json:"title,omitempty" yaml:",omitempty"`
Description string `json:"description,omitempty" yaml:",omitempty"`
Options map[string]interface{} `json:"options,omitempty" yaml:",omitempty"`
Style PageBlockStyle `json:"style,omitempty" yaml:",omitempty"`
Kind string `json:"kind"`
X int `json:"x"`
Y int `json:"y"`
Width int `json:"width"`
Height int `json:"height"`
}
PageBlockStyle struct {
Variants map[string]string `json:"variants,omitempty" yaml:",omitempty,flow"`
}
PageFilter struct {
@ -71,3 +80,33 @@ func (set PageSet) FindByHandle(handle string) *Page {
return nil
}
func (bb *PageBlocks) Scan(value interface{}) error {
//lint:ignore S1034 This typecast is intentional, we need to get []byte out of a []uint8
switch value.(type) {
case nil:
*bb = PageBlocks{}
case []uint8:
b := value.([]byte)
if err := json.Unmarshal(b, bb); err != nil {
return errors.Wrapf(err, "Can not scan '%v' into PageBlocks", string(b))
}
}
return nil
}
func (bb PageBlocks) Value() (driver.Value, error) {
return json.Marshal(bb)
}
func (set PageSet) FindByParent(parentID uint64) (out PageSet) {
out = PageSet{}
for i := range set {
if set[i].SelfID == parentID {
out = append(out, set[i])
}
}
return
}