search song. find

This commit is contained in:
winter 2023-01-05 03:40:52 +09:00
parent d25b006e57
commit ff578ed7a8
2 changed files with 60 additions and 0 deletions

View File

@ -0,0 +1,37 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<link rel="icon" type="image/png" href="/favicon.png" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<link rel="stylesheet" href="/style.css" />
<title>Song Search</title>
</head>
<body>
<form action="/tools/song_search" method="post">
ID/Author/Title: <input type="text" id="query" name="query" />
<input type="submit" value="Submit" />
</form>
<%- if error -%>
<div>Error while searching for song: <%= error %></div><br />
<%- elsif songs -%>
<div>
<table>
<tr>
<th>ID</th>
<th>Author</th>
<th>Title</th>
</tr>
<%- songs.each do |song| -%>
<tr>
<td><%= song[0] %></td>
<td><%= song[1] %></td>
<td><%= song[2] %></td>
</tr>
<%- end -%>
</table>
</div>
<%- end -%>
</body>
</html>

View File

@ -0,0 +1,23 @@
require "ecr"
include CrystalGauntlet
CrystalGauntlet.template_endpoints["/tools/song_search"] = ->(context : HTTP::Server::Context): String {
error = nil
songs = nil
result_limit = 10
body = context.request.body
if body
begin
params = URI::Params.parse(body.gets_to_end)
query = "%#{params["query"]}%"
songs = DATABASE.query_all("select song_data.id, song_authors.name, song_data.name from song_data join song_authors on song_authors.id = song_data.author_id where song_data.id = ? or song_authors.name like ? or song_data.name like ? limit #{result_limit}", params["query"], query, query, as: {Int32, String, String})
rescue error
ECR.render("./public/template/song_search.ecr")
else
ECR.render("./public/template/song_search.ecr")
end
end
ECR.render("./public/template/song_search.ecr")
}