Skip to content

Commit c454628

Browse files
MartinRepohsluoyz
authored andcommitted
feat: add graph list and edit pages (casibase#1539)
1 parent 5545f5c commit c454628

File tree

9 files changed

+793
-38
lines changed

9 files changed

+793
-38
lines changed

controllers/graph.go

Lines changed: 175 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,175 @@
1+
// Copyright 2025 The Casibase Authors. All Rights Reserved.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package controllers
16+
17+
import (
18+
"encoding/json"
19+
20+
"github.com/beego/beego/utils/pagination"
21+
"github.com/casibase/casibase/object"
22+
"github.com/casibase/casibase/util"
23+
)
24+
25+
// GetGlobalGraphs
26+
// @Title GetGlobalGraphs
27+
// @Tag Graph API
28+
// @Description get global graphs
29+
// @Success 200 {array} object.Graph The Response object
30+
// @router /get-global-graphs [get]
31+
func (c *ApiController) GetGlobalGraphs() {
32+
graphs, err := object.GetGlobalGraphs()
33+
if err != nil {
34+
c.ResponseError(err.Error())
35+
return
36+
}
37+
38+
c.ResponseOk(object.GetMaskedGraphs(graphs, true))
39+
}
40+
41+
// GetGraphs
42+
// @Title GetGraphs
43+
// @Tag Graph API
44+
// @Description get graphs
45+
// @Param owner query string true "The owner of Graph"
46+
// @Success 200 {array} object.Graph The Response object
47+
// @router /get-graphs [get]
48+
func (c *ApiController) GetGraphs() {
49+
owner := c.Input().Get("owner")
50+
limit := c.Input().Get("pageSize")
51+
page := c.Input().Get("p")
52+
field := c.Input().Get("field")
53+
value := c.Input().Get("value")
54+
sortField := c.Input().Get("sortField")
55+
sortOrder := c.Input().Get("sortOrder")
56+
57+
if limit == "" || page == "" {
58+
graphs, err := object.GetGraphs(owner)
59+
if err != nil {
60+
c.ResponseError(err.Error())
61+
return
62+
}
63+
64+
c.ResponseOk(object.GetMaskedGraphs(graphs, true))
65+
} else {
66+
limit := util.ParseInt(limit)
67+
count, err := object.GetGraphCount(owner, field, value)
68+
if err != nil {
69+
c.ResponseError(err.Error())
70+
return
71+
}
72+
73+
paginator := pagination.SetPaginator(c.Ctx, limit, count)
74+
graphs, err := object.GetPaginationGraphs(owner, paginator.Offset(), limit, field, value, sortField, sortOrder)
75+
if err != nil {
76+
c.ResponseError(err.Error())
77+
return
78+
}
79+
c.ResponseOk(graphs, paginator.Nums())
80+
}
81+
}
82+
83+
// GetGraph
84+
// @Title GetGraph
85+
// @Tag Graph API
86+
// @Description get Graph
87+
// @Param id query string true "The id (owner/name) of Graph"
88+
// @Success 200 {object} object.Graph The Response object
89+
// @router /get-Graph [get]
90+
func (c *ApiController) GetGraph() {
91+
id := c.Input().Get("id")
92+
93+
Graph, err := object.GetGraph(id)
94+
if err != nil {
95+
c.ResponseError(err.Error())
96+
return
97+
}
98+
99+
c.ResponseOk(object.GetMaskedGraph(Graph, true))
100+
}
101+
102+
// UpdateGraph
103+
// @Title UpdateGraph
104+
// @Tag Graph API
105+
// @Description update Graph
106+
// @Param id query string true "The id (owner/name) of the Graph"
107+
// @Param body body object.Graph true "The details of the Graph"
108+
// @Success 200 {object} controllers.Response The Response object
109+
// @router /update-Graph [post]
110+
func (c *ApiController) UpdateGraph() {
111+
id := c.Input().Get("id")
112+
113+
var Graph object.Graph
114+
err := json.Unmarshal(c.Ctx.Input.RequestBody, &Graph)
115+
if err != nil {
116+
c.ResponseError(err.Error())
117+
return
118+
}
119+
120+
success, err := object.UpdateGraph(id, &Graph)
121+
if err != nil {
122+
c.ResponseError(err.Error())
123+
return
124+
}
125+
126+
c.ResponseOk(success)
127+
}
128+
129+
// AddGraph
130+
// @Title AddGraph
131+
// @Tag Graph API
132+
// @Description add Graph
133+
// @Param body body object.Graph true "The details of the Graph"
134+
// @Success 200 {object} controllers.Response The Response object
135+
// @router /add-Graph [post]
136+
func (c *ApiController) AddGraph() {
137+
var Graph object.Graph
138+
err := json.Unmarshal(c.Ctx.Input.RequestBody, &Graph)
139+
if err != nil {
140+
c.ResponseError(err.Error())
141+
return
142+
}
143+
144+
success, err := object.AddGraph(&Graph)
145+
if err != nil {
146+
c.ResponseError(err.Error())
147+
return
148+
}
149+
150+
c.ResponseOk(success)
151+
}
152+
153+
// DeleteGraph
154+
// @Title DeleteGraph
155+
// @Tag Graph API
156+
// @Description delete Graph
157+
// @Param body body object.Graph true "The details of the Graph"
158+
// @Success 200 {object} controllers.Response The Response object
159+
// @router /delete-Graph [post]
160+
func (c *ApiController) DeleteGraph() {
161+
var Graph object.Graph
162+
err := json.Unmarshal(c.Ctx.Input.RequestBody, &Graph)
163+
if err != nil {
164+
c.ResponseError(err.Error())
165+
return
166+
}
167+
168+
success, err := object.DeleteGraph(&Graph)
169+
if err != nil {
170+
c.ResponseError(err.Error())
171+
return
172+
}
173+
174+
c.ResponseOk(success)
175+
}

object/adapter.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -274,4 +274,9 @@ func (a *Adapter) createTable() {
274274
if err != nil {
275275
panic(err)
276276
}
277+
278+
err = a.engine.Sync2(new(Graph))
279+
if err != nil {
280+
panic(err)
281+
}
277282
}

object/graph.go

Lines changed: 122 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright 2023 The Casibase Authors. All Rights Reserved.
1+
// Copyright 2025 The Casibase Authors. All Rights Reserved.
22
//
33
// Licensed under the Apache License, Version 2.0 (the "License");
44
// you may not use this file except in compliance with the License.
@@ -14,6 +14,13 @@
1414

1515
package object
1616

17+
import (
18+
"fmt"
19+
20+
"github.com/casibase/casibase/util"
21+
"xorm.io/core"
22+
)
23+
1724
type GraphNode struct {
1825
Id string `json:"id"`
1926
Name string `json:"name"`
@@ -23,53 +30,130 @@ type GraphNode struct {
2330
Weight int `json:"weight"`
2431
}
2532

26-
func newNode(id string, name string, value int, color string, tag string, weight int) *GraphNode {
27-
n := GraphNode{}
28-
n.Id = id
29-
n.Name = name
30-
n.Value = value
31-
n.Color = color
32-
n.Tag = tag
33-
n.Weight = weight
34-
return &n
33+
type Graph struct {
34+
Owner string `xorm:"varchar(100) notnull pk" json:"owner"`
35+
Name string `xorm:"varchar(100) notnull pk" json:"name"`
36+
CreatedTime string `xorm:"varchar(100)" json:"createdTime"`
37+
38+
DisplayName string `xorm:"varchar(100)" json:"displayName"`
39+
Text string `xorm:"mediumtext" json:"text"`
3540
}
3641

37-
type Link struct {
38-
Name string `json:"name"`
39-
Source string `json:"source"`
40-
Target string `json:"target"`
41-
Value int `json:"value"`
42-
Color string `json:"color"`
43-
Tag string `json:"tag"`
42+
func GetMaskedGraph(graph *Graph, isMaskEnabled bool) *Graph {
43+
if !isMaskEnabled {
44+
return graph
45+
}
46+
47+
if graph == nil {
48+
return nil
49+
}
50+
51+
return graph
4452
}
4553

46-
func newLink(name string, source string, target string, value int, color string, tag string) *Link {
47-
l := Link{}
48-
l.Name = name
49-
l.Source = source
50-
l.Target = target
51-
l.Value = value
52-
l.Color = color
53-
l.Tag = tag
54-
return &l
54+
func GetMaskedGraphs(graphs []*Graph, isMaskEnabled bool) []*Graph {
55+
if !isMaskEnabled {
56+
return graphs
57+
}
58+
59+
for _, graph := range graphs {
60+
graph = GetMaskedGraph(graph, isMaskEnabled)
61+
}
62+
return graphs
5563
}
5664

57-
type Graph struct {
58-
Nodes []*GraphNode `json:"nodes"`
59-
Links []*Link `json:"links"`
65+
func GetGlobalGraphs() ([]*Graph, error) {
66+
graphs := []*Graph{}
67+
err := adapter.engine.Asc("owner").Desc("created_time").Find(&graphs)
68+
if err != nil {
69+
return graphs, err
70+
}
71+
72+
return graphs, nil
6073
}
6174

62-
func newGraph() *Graph {
63-
g := Graph{}
64-
return &g
75+
func GetGraphs(owner string) ([]*Graph, error) {
76+
graphs := []*Graph{}
77+
err := adapter.engine.Desc("created_time").Find(&graphs, &Graph{Owner: owner})
78+
if err != nil {
79+
return graphs, err
80+
}
81+
82+
return graphs, nil
83+
}
84+
85+
func getGraph(owner string, name string) (*Graph, error) {
86+
graph := Graph{Owner: owner, Name: name}
87+
existed, err := adapter.engine.Get(&graph)
88+
if err != nil {
89+
return &graph, err
90+
}
91+
92+
if existed {
93+
return &graph, nil
94+
} else {
95+
return nil, nil
96+
}
6597
}
6698

67-
func (g *Graph) addNode(id string, name string, value int, color string, tag string, weight int) {
68-
n := newNode(id, name, value, color, tag, weight)
69-
g.Nodes = append(g.Nodes, n)
99+
func GetGraph(id string) (*Graph, error) {
100+
owner, name := util.GetOwnerAndNameFromId(id)
101+
return getGraph(owner, name)
70102
}
71103

72-
func (g *Graph) addLink(name string, source string, target string, value int, color string, tag string) {
73-
l := newLink(name, source, target, value, color, tag)
74-
g.Links = append(g.Links, l)
104+
func UpdateGraph(id string, graph *Graph) (bool, error) {
105+
owner, name := util.GetOwnerAndNameFromId(id)
106+
_, err := getGraph(owner, name)
107+
if err != nil {
108+
return false, err
109+
}
110+
if graph == nil {
111+
return false, nil
112+
}
113+
114+
_, err = adapter.engine.ID(core.PK{owner, name}).AllCols().Update(graph)
115+
if err != nil {
116+
return false, err
117+
}
118+
119+
// return affected != 0
120+
return true, nil
121+
}
122+
123+
func AddGraph(graph *Graph) (bool, error) {
124+
affected, err := adapter.engine.Insert(graph)
125+
if err != nil {
126+
return false, err
127+
}
128+
129+
return affected != 0, nil
130+
}
131+
132+
func DeleteGraph(graph *Graph) (bool, error) {
133+
affected, err := adapter.engine.ID(core.PK{graph.Owner, graph.Name}).Delete(&Graph{})
134+
if err != nil {
135+
return false, err
136+
}
137+
138+
return affected != 0, nil
139+
}
140+
141+
func (graph *Graph) GetId() string {
142+
return fmt.Sprintf("%s/%s", graph.Owner, graph.Name)
143+
}
144+
145+
func GetGraphCount(owner string, field, value string) (int64, error) {
146+
session := GetDbSession(owner, -1, -1, field, value, "", "")
147+
return session.Count(&Graph{})
148+
}
149+
150+
func GetPaginationGraphs(owner string, offset, limit int, field, value, sortField, sortOrder string) ([]*Graph, error) {
151+
graphs := []*Graph{}
152+
session := GetDbSession(owner, offset, limit, field, value, sortField, sortOrder)
153+
err := session.Find(&graphs)
154+
if err != nil {
155+
return graphs, err
156+
}
157+
158+
return graphs, nil
75159
}

routers/router.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,13 @@ func initAPI() {
100100
beego.Router("/api/delete-message", &controllers.ApiController{}, "POST:DeleteMessage")
101101
beego.Router("/api/delete-welcome-message", &controllers.ApiController{}, "POST:DeleteWelcomeMessage")
102102

103+
beego.Router("/api/get-global-graphs", &controllers.ApiController{}, "GET:GetGlobalGraphs")
104+
beego.Router("/api/get-graphs", &controllers.ApiController{}, "GET:GetGraphs")
105+
beego.Router("/api/get-graph", &controllers.ApiController{}, "GET:GetGraph")
106+
beego.Router("/api/update-graph", &controllers.ApiController{}, "POST:UpdateGraph")
107+
beego.Router("/api/add-graph", &controllers.ApiController{}, "POST:AddGraph")
108+
beego.Router("/api/delete-graph", &controllers.ApiController{}, "POST:DeleteGraph")
109+
103110
beego.Router("/api/get-templates", &controllers.ApiController{}, "GET:GetTemplates")
104111
beego.Router("/api/get-template", &controllers.ApiController{}, "GET:GetTemplate")
105112
beego.Router("/api/update-template", &controllers.ApiController{}, "POST:UpdateTemplate")

0 commit comments

Comments
 (0)