Clean verbose or not and optional one query param

master
Bel LaPointe 2020-07-22 23:43:21 -06:00
parent 098a2b9bbc
commit cc7ea37676
1 changed files with 47 additions and 23 deletions

View File

@ -1,6 +1,7 @@
package view
import (
"context"
"encoding/json"
"fmt"
"local/dndex/config"
@ -45,39 +46,62 @@ func who(g storage.Graph, w http.ResponseWriter, r *http.Request) error {
namespace = strings.Replace(namespace[1:], "/", ".", -1)
ids := r.URL.Query()["id"]
_, verbose := r.URL.Query()["v"]
_, one := r.URL.Query()["one"]
results := make(map[string]entity.One)
for i := 0; i < len(ids); i++ {
id := ids[i]
ones, err := g.List(r.Context(), namespace, id)
one, err := whoOne(r.Context(), g, namespace, id, verbose)
if err != nil {
return err
}
if len(ones) != 1 {
ones = append(ones, entity.One{})
results[id] = one
}
var marshalme interface{}
if one {
for k := range results {
marshalme = results[k]
break
}
one := ones[0]
if verbose {
ones, err := g.List(r.Context(), namespace, one.Peers()...)
if err != nil {
return err
}
for _, another := range ones {
another.Connections = nil
for j := range one.Connections {
if one.Connections[j].Name == another.Name {
one.Connections[j] = entity.One{
Relationship: one.Connections[j].Relationship,
Type: another.Type,
Modified: another.Modified,
}
} else {
marshalme = results
}
log.Printf("id=%+v, one=%v, verbose=%v, results:%+v", ids, one, verbose, marshalme)
enc := json.NewEncoder(w)
enc.SetIndent("", " ")
return enc.Encode(marshalme)
}
func whoOne(ctx context.Context, g storage.Graph, namespace, id string, verbose bool) (entity.One, error) {
ones, err := g.List(ctx, namespace, id)
if err != nil {
return entity.One{}, err
}
if len(ones) != 1 {
ones = append(ones, entity.One{})
}
one := ones[0]
if verbose {
ones, err := g.List(ctx, namespace, one.Peers()...)
if err != nil {
return entity.One{}, err
}
for _, another := range ones {
another.Connections = nil
for j := range one.Connections {
if one.Connections[j].Name == another.Name {
one.Connections[j] = entity.One{
Relationship: one.Connections[j].Relationship,
Type: another.Type,
Modified: another.Modified,
}
}
}
}
results[id] = one
} else {
for j, v := range one.Connections {
v.Name = ""
one.Connections[j] = v
}
}
log.Println("results:", results)
enc := json.NewEncoder(w)
enc.SetIndent("", " ")
return enc.Encode(results)
return one, nil
}