refactor: remove db package and move database logic to service layer

This commit is contained in:
2026-03-29 21:24:09 +02:00
parent 6ff013dd2a
commit 4ebcb88628
16 changed files with 163 additions and 217 deletions

View File

@@ -1,7 +1,6 @@
package output
import (
"axolotl/db"
"axolotl/models"
"axolotl/service"
"encoding/json"
@@ -59,21 +58,7 @@ const (
iconNamespace = "\uf07b"
)
func render(rm RenderMap, key string, short bool) string {
v, ok := rm[key]
if !ok {
v, ok = rm[""]
if !ok {
return ""
}
}
if short {
return v.c.Sprint(v.s)
}
return v.c.Sprint(v.l)
}
func PrintNodes(w io.Writer, nodes []*models.Node, jsonOut bool) error {
func PrintNodes(w io.Writer, svc service.NodeService, nodes []*models.Node, jsonOut bool) error {
if jsonOut {
return json.NewEncoder(w).Encode(nodes)
}
@@ -82,11 +67,6 @@ func PrintNodes(w io.Writer, nodes []*models.Node, jsonOut bool) error {
return nil
}
d, err := db.GetDB()
if err != nil {
return err
}
fmt.Fprintln(w)
sort.Slice(nodes, func(i, j int) bool {
si, sj := nodes[i].GetProperty("status"), nodes[j].GetProperty("status")
@@ -97,9 +77,14 @@ func PrintNodes(w io.Writer, nodes []*models.Node, jsonOut bool) error {
})
for _, n := range nodes {
ns_rels, err := d.GetRelNames(n, models.RelInNamespace)
if err != nil {
return err
ns_rel_node_ids := n.Relations[string(models.RelInNamespace)]
ns_rel_node_titles := make([]string, 0, len(ns_rel_node_ids))
for _, id := range ns_rel_node_ids {
ns_rel_node, err := svc.GetByID(id)
if err != nil {
fmt.Fprintf(w, "err: %v", err)
}
ns_rel_node_titles = append(ns_rel_node_titles, ns_rel_node.Title)
}
fmt.Fprintf(w, " %s %s %s %s %s %s",
cDim.Sprint(n.ID),
@@ -107,7 +92,7 @@ func PrintNodes(w io.Writer, nodes []*models.Node, jsonOut bool) error {
render(statusRM, n.GetProperty("status"), true),
render(typeRM, n.GetProperty("type"), true),
cTitle.Sprint(truncate(n.Title, 80)),
cDim.Sprint("["+strings.Join(ns_rels, ",")+"]"),
cDim.Sprint("["+strings.Join(ns_rel_node_titles, ",")+"]"),
)
tags := n.GetDisplayTags()
if len(tags) > 0 {
@@ -119,7 +104,7 @@ func PrintNodes(w io.Writer, nodes []*models.Node, jsonOut bool) error {
return nil
}
func PrintNode(w io.Writer, n *models.Node, jsonOut bool) error {
func PrintNode(w io.Writer, svc service.NodeService, n *models.Node, jsonOut bool) error {
if jsonOut {
return json.NewEncoder(w).Encode(n)
}
@@ -138,20 +123,17 @@ func PrintNode(w io.Writer, n *models.Node, jsonOut bool) error {
fmt.Fprintf(w, "\n tags: %s\n", cPrimary.Sprint(strings.Join(tags, " • ")))
}
if db, err := db.GetDB(); err != nil {
fmt.Fprintf(w, "failed to attach to db: %v", err)
} else {
for relType := range n.Relations {
names, err := db.GetRelNames(n, models.RelType(relType))
for relType := range n.Relations {
rel_node_ids := n.Relations[string(relType)]
if len(rel_node_ids) > 0 {
fmt.Fprintf(w, "\n %s\n", string(relType))
}
for _, id := range rel_node_ids {
rel_node, err := svc.GetByID(id)
if err != nil {
fmt.Fprintf(w, "err: %v", err)
}
if len(names) > 0 {
fmt.Fprintf(w, "\n %s\n", string(relType))
}
for _, name := range names {
fmt.Fprintf(w, " %s %s\n", relIcons[relType], name)
}
fmt.Fprintf(w, " %s %s\n", relIcons[relType], rel_node.Title)
}
}
@@ -201,6 +183,20 @@ func PrintAction(w io.Writer, action, detail string, isError bool) {
fmt.Fprintln(w, cGood.Sprint(icon+" "+action+" ")+cDim.Sprint(detail))
}
func render(rm RenderMap, key string, short bool) string {
v, ok := rm[key]
if !ok {
v, ok = rm[""]
if !ok {
return ""
}
}
if short {
return v.c.Sprint(v.s)
}
return v.c.Sprint(v.l)
}
func truncate(s string, max int) string {
if len(s) <= max {
return s