working recursive filters
This commit is contained in:
parent
62bd66f771
commit
07d7de60d8
1 changed files with 49 additions and 30 deletions
79
main.go
79
main.go
|
@ -1,7 +1,6 @@
|
||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
|
||||||
"fmt"
|
"fmt"
|
||||||
"log"
|
"log"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
@ -61,10 +60,18 @@ func (web Web) deleteWatch(c *gin.Context) {
|
||||||
}
|
}
|
||||||
|
|
||||||
type FilterDepth struct {
|
type FilterDepth struct {
|
||||||
Filter Filter
|
Filter *Filter
|
||||||
Depth int
|
Depth int
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func FilterPrint(filter *Filter, depth int) {
|
||||||
|
for _, f := range filter.Filters {
|
||||||
|
log.Println("----")
|
||||||
|
log.Println(depth, f)
|
||||||
|
FilterPrint(&f, depth+1)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func (web Web) viewWatch(c *gin.Context) {
|
func (web Web) viewWatch(c *gin.Context) {
|
||||||
id := c.Param("id")
|
id := c.Param("id")
|
||||||
|
|
||||||
|
@ -72,47 +79,59 @@ func (web Web) viewWatch(c *gin.Context) {
|
||||||
web.db.Model(&Watch{}).First(&watch, id)
|
web.db.Model(&Watch{}).First(&watch, id)
|
||||||
|
|
||||||
var filters []Filter
|
var filters []Filter
|
||||||
web.db.Model(&Filter{}).Find(&filters)
|
web.db.Model(&Filter{}).Where("watch_id = ?", watch.ID).Find(&filters)
|
||||||
|
|
||||||
|
filterMap := make(map[uint]*Filter)
|
||||||
|
for i := range filters {
|
||||||
|
filterMap[filters[i].ID] = &filters[i]
|
||||||
|
}
|
||||||
|
|
||||||
queuedFilters := []*Filter{}
|
queuedFilters := []*Filter{}
|
||||||
filterMap := make(map[uint]*Filter)
|
for i := range filters {
|
||||||
for _, filter := range filters {
|
filter := &filters[i]
|
||||||
filterMap[filter.ID] = &filter
|
if filter.ParentID != nil {
|
||||||
if filter.ParentID == nil {
|
|
||||||
queuedFilters = append(queuedFilters, &filter)
|
|
||||||
}
|
|
||||||
s, _ := json.MarshalIndent(filter, "", "\t")
|
|
||||||
fmt.Println(s)
|
|
||||||
}
|
|
||||||
|
|
||||||
for _, filter := range filterMap {
|
|
||||||
if filter.Parent != nil {
|
|
||||||
parent := filterMap[*filter.ParentID]
|
parent := filterMap[*filter.ParentID]
|
||||||
parent.Filters = append(parent.Filters, *filter)
|
parent.Filters = append(parent.Filters, *filter)
|
||||||
|
} else {
|
||||||
|
queuedFilters = append(queuedFilters, filter)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
nextFilters := []*Filter{}
|
|
||||||
bftFilters := []FilterDepth{}
|
bftFilters := []FilterDepth{}
|
||||||
depth := 0
|
depth := 0
|
||||||
for len(queuedFilters) > 0 {
|
for len(queuedFilters) > 0 {
|
||||||
for _, f1 := range queuedFilters {
|
filter := queuedFilters[0]
|
||||||
bftFilters = append(bftFilters, FilterDepth{
|
bftFilters = append(bftFilters, FilterDepth{
|
||||||
Filter: *f1,
|
Filter: filter,
|
||||||
Depth: depth,
|
Depth: depth,
|
||||||
})
|
})
|
||||||
for _, f2 := range f1.Filters {
|
for _, filter := range filter.Filters {
|
||||||
nextFilters = append(nextFilters, &f2)
|
queuedFilters = append(queuedFilters, &filter)
|
||||||
}
|
|
||||||
}
|
}
|
||||||
log.Println(nextFilters)
|
queuedFilters = queuedFilters[1:]
|
||||||
queuedFilters = nextFilters
|
|
||||||
log.Println(queuedFilters)
|
|
||||||
nextFilters = []*Filter{}
|
|
||||||
log.Println(nextFilters)
|
|
||||||
depth += 1
|
|
||||||
}
|
}
|
||||||
|
/*
|
||||||
|
nextFilters := []*Filter{}
|
||||||
|
depth := 0
|
||||||
|
for len(queuedFilters) > 0 {
|
||||||
|
for _, f1 := range queuedFilters {
|
||||||
|
bftFilters = append(bftFilters, FilterDepth{
|
||||||
|
Filter: *f1,
|
||||||
|
Depth: depth,
|
||||||
|
})
|
||||||
|
for _, f2 := range f1.Filters {
|
||||||
|
nextFilters = append(nextFilters, &f2)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
queuedFilters = nextFilters
|
||||||
|
nextFilters = []*Filter{}
|
||||||
|
depth += 1
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, f := range bftFilters {
|
||||||
|
FilterPrint(&f.Filter, 0)
|
||||||
|
}
|
||||||
|
*/
|
||||||
c.HTML(http.StatusOK, "viewWatch", gin.H{
|
c.HTML(http.StatusOK, "viewWatch", gin.H{
|
||||||
"Watch": watch,
|
"Watch": watch,
|
||||||
"Filters": bftFilters,
|
"Filters": bftFilters,
|
||||||
|
|
Loading…
Add table
Reference in a new issue