search results
This commit is contained in:
parent
3dbe12b562
commit
eb372c0dae
|
@ -28,32 +28,78 @@ func (h searchHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||||
|
|
||||||
term := r.URL.Query().Get("term")
|
term := r.URL.Query().Get("term")
|
||||||
|
|
||||||
var results []SearchResult
|
world := h.server.context.world
|
||||||
|
if term != "" {
|
||||||
|
var results []SearchResult
|
||||||
|
results = searchMap(term, world.HistoricalFigures, results, "/hf")
|
||||||
|
results = searchMap(term, world.Entities, results, "/entity")
|
||||||
|
results = searchMap(term, world.Sites, results, "/site")
|
||||||
|
for _, site := range world.Sites {
|
||||||
|
results = searchMap(term, site.Structures, results, fmt.Sprintf("/site/%d/structure", site.Id_))
|
||||||
|
}
|
||||||
|
results = searchMap(term, world.Regions, results, "/region")
|
||||||
|
results = searchMap(term, world.Artifacts, results, "/artifavt")
|
||||||
|
results = searchMap(term, world.WorldConstructions, results, "/worldconstruction")
|
||||||
|
results = searchMap(term, world.DanceForms, results, "/danceForm")
|
||||||
|
results = searchMap(term, world.MusicalForms, results, "/musicalForm")
|
||||||
|
results = searchMap(term, world.PoeticForms, results, "/poeticForm")
|
||||||
|
results = searchMap(term, world.WrittenContents, results, "/writtencontent")
|
||||||
|
results = searchMap(term, world.Landmasses, results, "/landmass")
|
||||||
|
results = searchMap(term, world.MountainPeaks, results, "/mountain")
|
||||||
|
|
||||||
results = seachMap(term, h.server.context.world.HistoricalFigures, results, "/hf")
|
sort.Slice(results, func(i, j int) bool { return results[i].Label < results[j].Label })
|
||||||
results = seachMap(term, h.server.context.world.Entities, results, "/entity")
|
|
||||||
results = seachMap(term, h.server.context.world.Sites, results, "/site")
|
w.Header().Set("Content-Type", "application/json")
|
||||||
for _, site := range h.server.context.world.Sites {
|
w.WriteHeader(http.StatusOK)
|
||||||
results = seachMap(term, site.Structures, results, fmt.Sprintf("/site/%d/structure", site.Id_))
|
json.NewEncoder(w).Encode(results)
|
||||||
|
} else {
|
||||||
|
term = r.URL.Query().Get("search")
|
||||||
|
|
||||||
|
var structures []*model.Structure
|
||||||
|
for _, site := range world.Sites {
|
||||||
|
structures = search(term, site.Structures, structures)
|
||||||
|
}
|
||||||
|
|
||||||
|
results := struct {
|
||||||
|
Term string
|
||||||
|
HistoricalFigures []*model.HistoricalFigure
|
||||||
|
Entities []*model.Entity
|
||||||
|
Sites []*model.Site
|
||||||
|
Structures []*model.Structure
|
||||||
|
Regions []*model.Region
|
||||||
|
Artifacts []*model.Artifact
|
||||||
|
WorldConstructions []*model.WorldConstruction
|
||||||
|
DanceForms []*model.DanceForm
|
||||||
|
MusicalForms []*model.MusicalForm
|
||||||
|
PoeticForms []*model.PoeticForm
|
||||||
|
WrittenContents []*model.WrittenContent
|
||||||
|
Landmasses []*model.Landmass
|
||||||
|
MountainPeaks []*model.MountainPeak
|
||||||
|
}{
|
||||||
|
Term: term,
|
||||||
|
HistoricalFigures: search(term, world.HistoricalFigures, nil),
|
||||||
|
Entities: search(term, world.Entities, nil),
|
||||||
|
Sites: search(term, world.Sites, nil),
|
||||||
|
Structures: structures,
|
||||||
|
Regions: search(term, world.Regions, nil),
|
||||||
|
Artifacts: search(term, world.Artifacts, nil),
|
||||||
|
WorldConstructions: search(term, world.WorldConstructions, nil),
|
||||||
|
DanceForms: search(term, world.DanceForms, nil),
|
||||||
|
MusicalForms: search(term, world.MusicalForms, nil),
|
||||||
|
PoeticForms: search(term, world.PoeticForms, nil),
|
||||||
|
WrittenContents: search(term, world.WrittenContents, nil),
|
||||||
|
Landmasses: search(term, world.Landmasses, nil),
|
||||||
|
MountainPeaks: search(term, world.MountainPeaks, nil),
|
||||||
|
}
|
||||||
|
|
||||||
|
err := h.server.templates.Render(w, "search.html", results)
|
||||||
|
if err != nil {
|
||||||
|
httpError(w, err)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
results = seachMap(term, h.server.context.world.Regions, results, "/region")
|
|
||||||
results = seachMap(term, h.server.context.world.Artifacts, results, "/artifavt")
|
|
||||||
results = seachMap(term, h.server.context.world.WorldConstructions, results, "/worldconstruction")
|
|
||||||
results = seachMap(term, h.server.context.world.DanceForms, results, "/danceForm")
|
|
||||||
results = seachMap(term, h.server.context.world.MusicalForms, results, "/musicalForm")
|
|
||||||
results = seachMap(term, h.server.context.world.PoeticForms, results, "/poeticForm")
|
|
||||||
results = seachMap(term, h.server.context.world.WrittenContents, results, "/writtencontent")
|
|
||||||
results = seachMap(term, h.server.context.world.Landmasses, results, "/landmass")
|
|
||||||
results = seachMap(term, h.server.context.world.MountainPeaks, results, "/mountain")
|
|
||||||
|
|
||||||
sort.Slice(results, func(i, j int) bool { return results[i].Label < results[j].Label })
|
|
||||||
|
|
||||||
w.Header().Set("Content-Type", "application/json")
|
|
||||||
w.WriteHeader(http.StatusOK)
|
|
||||||
json.NewEncoder(w).Encode(results)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func seachMap[T model.Named](s string, input map[int]T, output []SearchResult, baseUrl string) []SearchResult {
|
func searchMap[T model.Named](s string, input map[int]T, output []SearchResult, baseUrl string) []SearchResult {
|
||||||
s = strings.ToLower(s)
|
s = strings.ToLower(s)
|
||||||
for id, v := range input {
|
for id, v := range input {
|
||||||
if strings.Contains(strings.ToLower(v.Name()), s) {
|
if strings.Contains(strings.ToLower(v.Name()), s) {
|
||||||
|
@ -65,3 +111,14 @@ func seachMap[T model.Named](s string, input map[int]T, output []SearchResult, b
|
||||||
}
|
}
|
||||||
return output
|
return output
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func search[T model.Named](s string, input map[int]T, output []T) []T {
|
||||||
|
s = strings.ToLower(s)
|
||||||
|
for _, v := range input {
|
||||||
|
if strings.Contains(strings.ToLower(v.Name()), s) {
|
||||||
|
output = append(output, v)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
sort.Slice(output, func(i, j int) bool { return output[i].Name() < output[j].Name() })
|
||||||
|
return output
|
||||||
|
}
|
||||||
|
|
|
@ -39,7 +39,9 @@ func (srv *DfServer) LoadTemplates() {
|
||||||
"danceForm": func(id int) template.HTML { return model.LinkDanceForm(srv.context.world, id) },
|
"danceForm": func(id int) template.HTML { return model.LinkDanceForm(srv.context.world, id) },
|
||||||
"musicalForm": func(id int) template.HTML { return model.LinkMusicalForm(srv.context.world, id) },
|
"musicalForm": func(id int) template.HTML { return model.LinkMusicalForm(srv.context.world, id) },
|
||||||
"poeticForm": func(id int) template.HTML { return model.LinkPoeticForm(srv.context.world, id) },
|
"poeticForm": func(id int) template.HTML { return model.LinkPoeticForm(srv.context.world, id) },
|
||||||
"writtencontent": func(id int) template.HTML { return model.LinkWrittenContent(srv.context.world, id) },
|
"writtenContent": func(id int) template.HTML { return model.LinkWrittenContent(srv.context.world, id) },
|
||||||
|
"landmass": func(id int) template.HTML { return model.LinkWrittenContent(srv.context.world, id) }, // TODO
|
||||||
|
"mountain": func(id int) template.HTML { return model.LinkWrittenContent(srv.context.world, id) }, // TODO
|
||||||
"events": func(obj any) *model.EventList {
|
"events": func(obj any) *model.EventList {
|
||||||
return model.NewEventList(srv.context.world, obj)
|
return model.NewEventList(srv.context.world, obj)
|
||||||
},
|
},
|
||||||
|
|
|
@ -55,9 +55,10 @@
|
||||||
<a class="nav-link" href="/collections">Collections</a>
|
<a class="nav-link" href="/collections">Collections</a>
|
||||||
</li>
|
</li>
|
||||||
</ul>
|
</ul>
|
||||||
<form class="d-flex">
|
<form class="d-flex" action="/search" method="get">
|
||||||
<div class="input-group">
|
<div class="input-group">
|
||||||
<input id="search" class="form-control" type="search" placeholder="Search" aria-label="Search" autocomplete="off">
|
<input id="search" class="form-control" name="search" type="search" placeholder="Search" aria-label="Search"
|
||||||
|
autocomplete="off">
|
||||||
<button class="btn btn-outline-secondary" type="submit"><i class="fa-solid fa-magnifying-glass"></i></button>
|
<button class="btn btn-outline-secondary" type="submit"><i class="fa-solid fa-magnifying-glass"></i></button>
|
||||||
</div>
|
</div>
|
||||||
</form>
|
</form>
|
||||||
|
|
|
@ -0,0 +1,115 @@
|
||||||
|
{{template "layout.html" .}}
|
||||||
|
|
||||||
|
{{define "title"}}Search Results{{end}}
|
||||||
|
|
||||||
|
{{define "content"}}
|
||||||
|
<h3>Search Results for "{{.Term}}"</h3>
|
||||||
|
|
||||||
|
<div class="row">
|
||||||
|
{{- if ne 0 (len .Entities) }}
|
||||||
|
<div class="col-4">
|
||||||
|
<h5>Entities</h5>
|
||||||
|
<ul>
|
||||||
|
{{range .Entities}}<li>{{ entity .Id }}</li>{{end}}
|
||||||
|
</ul>
|
||||||
|
</div>
|
||||||
|
{{- end}}
|
||||||
|
{{- if ne 0 (len .Sites) }}
|
||||||
|
<div class="col-4">
|
||||||
|
<h5>Sites</h5>
|
||||||
|
<ul>
|
||||||
|
{{range .Sites}}<li>{{ site .Id }}</li>{{end}}
|
||||||
|
</ul>
|
||||||
|
</div>
|
||||||
|
{{- end}}
|
||||||
|
{{- if ne 0 (len .Structures) }}
|
||||||
|
<div class="col-4">
|
||||||
|
<h5>Structures</h5>
|
||||||
|
<ul>
|
||||||
|
{{range .Structures}}<li>{{ structure .SiteId .Id }}</li>{{end}}
|
||||||
|
</ul>
|
||||||
|
</div>
|
||||||
|
{{- end}}
|
||||||
|
{{- if ne 0 (len .Regions) }}
|
||||||
|
<div class="col-4">
|
||||||
|
<h5>Regions</h5>
|
||||||
|
<ul>
|
||||||
|
{{range .Regions}}<li>{{ region .Id }}</li>{{end}}
|
||||||
|
</ul>
|
||||||
|
</div>
|
||||||
|
{{- end}}
|
||||||
|
{{- if ne 0 (len .Artifacts) }}
|
||||||
|
<div class="col-4">
|
||||||
|
<h5>Artifacts</h5>
|
||||||
|
<ul>
|
||||||
|
{{range .Artifacts}}<li>{{ artifact .Id }}</li>{{end}}
|
||||||
|
</ul>
|
||||||
|
</div>
|
||||||
|
{{- end}}
|
||||||
|
{{- if ne 0 (len .HistoricalFigures) }}
|
||||||
|
<div class="col-4">
|
||||||
|
<h5>HistoricalFigures</h5>
|
||||||
|
<ul>
|
||||||
|
{{range .HistoricalFigures}}<li>{{ hf .Id }}</li>{{end}}
|
||||||
|
</ul>
|
||||||
|
</div>
|
||||||
|
{{- end}}
|
||||||
|
{{- if ne 0 (len .WorldConstructions) }}
|
||||||
|
<div class="col-4">
|
||||||
|
<h5>WorldConstructions</h5>
|
||||||
|
<ul>
|
||||||
|
{{range .WorldConstructions}}<li>{{ worldconstruction .Id }}</li>{{end}}
|
||||||
|
</ul>
|
||||||
|
</div>
|
||||||
|
{{- end}}
|
||||||
|
{{- if ne 0 (len .DanceForms) }}
|
||||||
|
<div class="col-4">
|
||||||
|
<h5>DanceForms</h5>
|
||||||
|
<ul>
|
||||||
|
{{range .DanceForms}}<li>{{ danceForm .Id }}</li>{{end}}
|
||||||
|
</ul>
|
||||||
|
</div>
|
||||||
|
{{- end}}
|
||||||
|
{{- if ne 0 (len .MusicalForms) }}
|
||||||
|
<div class="col-4">
|
||||||
|
<h5>MusicalForms</h5>
|
||||||
|
<ul>
|
||||||
|
{{range .MusicalForms}}<li>{{ musicalForm .Id }}</li>{{end}}
|
||||||
|
</ul>
|
||||||
|
</div>
|
||||||
|
{{- end}}
|
||||||
|
{{- if ne 0 (len .PoeticForms) }}
|
||||||
|
<div class="col-4">
|
||||||
|
<h5>PoeticForms</h5>
|
||||||
|
<ul>
|
||||||
|
{{range .PoeticForms}}<li>{{ poeticForm .Id }}</li>{{end}}
|
||||||
|
</ul>
|
||||||
|
</div>
|
||||||
|
{{- end}}
|
||||||
|
{{- if ne 0 (len .WrittenContents) }}
|
||||||
|
<div class="col-4">
|
||||||
|
<h5>Written Contents</h5>
|
||||||
|
<ul>
|
||||||
|
{{range .WrittenContents}}<li>{{ writtenContent .Id }}</li>{{end}}
|
||||||
|
</ul>
|
||||||
|
</div>
|
||||||
|
{{- end}}
|
||||||
|
{{- if ne 0 (len .Landmasses) }}
|
||||||
|
<div class="col-4">
|
||||||
|
<h5>Landmasses</h5>
|
||||||
|
<ul>
|
||||||
|
{{range .Landmasses}}<li>{{ landmass .Id }}</li>{{end}}
|
||||||
|
</ul>
|
||||||
|
</div>
|
||||||
|
{{- end}}
|
||||||
|
{{- if ne 0 (len .MountainPeaks) }}
|
||||||
|
<div class="col-4">
|
||||||
|
<h5>Mountain Peaks</h5>
|
||||||
|
<ul>
|
||||||
|
{{range .MountainPeaks}}<li>{{ mountain .Id }}</li>{{end}}
|
||||||
|
</ul>
|
||||||
|
</div>
|
||||||
|
{{- end}}
|
||||||
|
</div>
|
||||||
|
|
||||||
|
{{- end }}
|
|
@ -23,7 +23,7 @@
|
||||||
</tr>
|
</tr>
|
||||||
{{- range $v }}{{- if not (eq .Name "") }}
|
{{- range $v }}{{- if not (eq .Name "") }}
|
||||||
<tr>
|
<tr>
|
||||||
<td>{{ writtencontent .Id }}</td>
|
<td>{{ writtenContent .Id }}</td>
|
||||||
<td></td>
|
<td></td>
|
||||||
</tr>
|
</tr>
|
||||||
{{- end}}{{- end}}
|
{{- end}}{{- end}}
|
||||||
|
|
Loading…
Reference in New Issue