Added search
Added a search page and fixed som error with migration
This commit is contained in:
@@ -8,13 +8,10 @@ import (
|
||||
//go:embed swagger
|
||||
var swagger embed.FS
|
||||
|
||||
//go:embed search
|
||||
var search embed.FS
|
||||
|
||||
func main() {
|
||||
conf.SetupDb()
|
||||
|
||||
conf.SetupRestServer(swagger, search)
|
||||
conf.SetupRestServer(swagger)
|
||||
|
||||
conf.CloseDb()
|
||||
}
|
||||
|
||||
20
cmd/web/assets/css/input.css
Normal file
20
cmd/web/assets/css/input.css
Normal file
@@ -0,0 +1,20 @@
|
||||
@tailwind base;
|
||||
@tailwind components;
|
||||
@tailwind utilities;
|
||||
|
||||
#search-container {
|
||||
text-align: center;
|
||||
}
|
||||
|
||||
#search_term {
|
||||
width: 60vw;
|
||||
font-size: 2vh;
|
||||
}
|
||||
|
||||
#clear {
|
||||
font-size: 2vh;
|
||||
}
|
||||
|
||||
#games-container{
|
||||
font-size: 2vh;
|
||||
}
|
||||
3476
cmd/web/assets/js/htmx.min.js
vendored
Normal file
3476
cmd/web/assets/js/htmx.min.js
vendored
Normal file
File diff suppressed because it is too large
Load Diff
18
cmd/web/base.templ
Normal file
18
cmd/web/base.templ
Normal file
@@ -0,0 +1,18 @@
|
||||
package web
|
||||
|
||||
templ Base() {
|
||||
<!DOCTYPE html>
|
||||
<html lang="en" class="h-screen">
|
||||
<head>
|
||||
<meta charset="utf-8"/>
|
||||
<title>Music Search</title>
|
||||
<link href="assets/css/output.css" rel="stylesheet"/>
|
||||
<script src="assets/js/htmx.min.js"></script>
|
||||
</head>
|
||||
<body class="bg-gray-100">
|
||||
<main class="mx-auto p-4">
|
||||
{ children... }
|
||||
</main>
|
||||
</body>
|
||||
</html>
|
||||
}
|
||||
6
cmd/web/efs.go
Normal file
6
cmd/web/efs.go
Normal file
@@ -0,0 +1,6 @@
|
||||
package web
|
||||
|
||||
import "embed"
|
||||
|
||||
//go:embed "assets"
|
||||
var Files embed.FS
|
||||
117
cmd/web/hello.go
Normal file
117
cmd/web/hello.go
Normal file
@@ -0,0 +1,117 @@
|
||||
package web
|
||||
|
||||
import (
|
||||
"log"
|
||||
"music-server/pkg/server"
|
||||
"net/http"
|
||||
"regexp"
|
||||
"strings"
|
||||
)
|
||||
|
||||
var games_added []string
|
||||
|
||||
func FindGameWebHandler(w http.ResponseWriter, r *http.Request) {
|
||||
err := r.ParseForm()
|
||||
if err != nil {
|
||||
http.Error(w, "Bad Request", http.StatusBadRequest)
|
||||
}
|
||||
|
||||
search_term := r.FormValue("search_term")
|
||||
|
||||
search(search_term)
|
||||
|
||||
component := FoundGames(games_added)
|
||||
err = component.Render(r.Context(), w)
|
||||
if err != nil {
|
||||
http.Error(w, err.Error(), http.StatusBadRequest)
|
||||
log.Fatalf("Error rendering in FindGameWebHandler: %e", err)
|
||||
}
|
||||
}
|
||||
|
||||
func search(searchText string) {
|
||||
games_added = nil
|
||||
games := server.GetAllGames()
|
||||
for _, game := range games {
|
||||
if is_match_exact(searchText, game) {
|
||||
add_game(game)
|
||||
}
|
||||
}
|
||||
for _, game := range games {
|
||||
if is_match_contains(clean_term(searchText), clean_term(game)) {
|
||||
add_game(game)
|
||||
}
|
||||
}
|
||||
for _, game := range games {
|
||||
if is_match_regex(clean_term(searchText), clean_term(game)) {
|
||||
add_game(game)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func is_match_exact(search_term string, game_name string) bool {
|
||||
search_term = strings.ToLower(search_term)
|
||||
game_name = strings.ToLower(game_name)
|
||||
|
||||
if search_term == "" {
|
||||
return true
|
||||
} else if strings.Contains(game_name, search_term) {
|
||||
return true
|
||||
} else {
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
func is_match_contains(search_term string, game_name string) bool {
|
||||
if search_term == "" {
|
||||
return true
|
||||
} else if strings.Contains(game_name, search_term) {
|
||||
return true
|
||||
} else {
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
func is_match_regex(search_term string, game_name string) bool {
|
||||
if search_term == "" {
|
||||
return true
|
||||
} else if compile_regex(search_term).MatchString(game_name) {
|
||||
return true
|
||||
} else {
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
func add_game(game string) {
|
||||
if !check_if_game_exists(game) {
|
||||
games_added = append(games_added, game)
|
||||
}
|
||||
}
|
||||
|
||||
func check_if_game_exists(gameName string) bool {
|
||||
game_exists := false
|
||||
for _, child := range games_added {
|
||||
if child == gameName {
|
||||
game_exists = true
|
||||
}
|
||||
}
|
||||
return game_exists
|
||||
}
|
||||
|
||||
func compile_regex(search_term string) *regexp.Regexp {
|
||||
regText := ".*"
|
||||
for _, letter := range search_term {
|
||||
regText += string(letter) + ".*"
|
||||
}
|
||||
r, _ := regexp.Compile(regText)
|
||||
|
||||
return r
|
||||
}
|
||||
|
||||
func clean_term(term string) string {
|
||||
term = strings.ReplaceAll(term, " ", "")
|
||||
term = strings.ReplaceAll(term, "é", "e")
|
||||
term = strings.ReplaceAll(term, "+", "plus")
|
||||
term = strings.ReplaceAll(term, "&", "and")
|
||||
term = strings.ReplaceAll(term, "'n", "and")
|
||||
return strings.ToLower(term)
|
||||
}
|
||||
32
cmd/web/hello.templ
Normal file
32
cmd/web/hello.templ
Normal file
@@ -0,0 +1,32 @@
|
||||
package web
|
||||
|
||||
templ HelloForm() {
|
||||
@Base() {
|
||||
<div id="search-container">
|
||||
<input class="bg-gray-200 text-black p-2 border border-gray-400 rounded-lg" id="search_term" name="search_term" type="text" hx-post="/find" hx-trigger="keyup changed delay:0.25s" hx-target="#games-container"/>
|
||||
<button type="button" class="bg-orange-500 hover:bg-orange-700 text-white py-2 px-4 rounded" id="clear" name="clear">Clear</button>
|
||||
</div>
|
||||
<div id="games-container"></div>
|
||||
<script>
|
||||
document.addEventListener('readystatechange', () => {
|
||||
if (document.readyState == 'complete') {
|
||||
htmx.ajax('POST', '/find', '#games-container');
|
||||
document.getElementById("search_term").focus();
|
||||
}
|
||||
});
|
||||
document.getElementById("clear").addEventListener("click", function (event) {
|
||||
document.getElementById("name").value = "";
|
||||
htmx.ajax('POST', '/find', '#games-container');
|
||||
document.getElementById("search_term").focus();
|
||||
});
|
||||
</script>
|
||||
}
|
||||
}
|
||||
|
||||
templ FoundGames(games []string) {
|
||||
for _, game := range games {
|
||||
<div class="bg-green-100 p-4 shadow-md rounded-lg mt-6">
|
||||
<p>{ game }</p>
|
||||
</div>
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user