Add result page
This commit is contained in:
@@ -17,8 +17,9 @@ const (
|
|||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
db *sql.DB
|
db *sql.DB
|
||||||
indexHtmlTemplate *template.Template
|
indexHtmlTemplate *template.Template
|
||||||
|
resultHtmlTemplate *template.Template
|
||||||
)
|
)
|
||||||
|
|
||||||
type ChoicesPost struct {
|
type ChoicesPost struct {
|
||||||
@@ -64,10 +65,16 @@ func initDatabase() {
|
|||||||
|
|
||||||
func prepareStaticContent() {
|
func prepareStaticContent() {
|
||||||
var err error
|
var err error
|
||||||
|
|
||||||
indexHtmlTemplate, err = template.ParseFiles("./static/index.html.tmpl")
|
indexHtmlTemplate, err = template.ParseFiles("./static/index.html.tmpl")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatalf("[!] Could not parse index.html template, error: %s\n", err)
|
log.Fatalf("[!] Could not parse index.html template, error: %s\n", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
resultHtmlTemplate, err = template.ParseFiles("./static/result.html.tmpl")
|
||||||
|
if err != nil {
|
||||||
|
log.Fatalf("[!] Could not parse result.html template, error: %s\n", err)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func handleGetRoot(w http.ResponseWriter, r *http.Request) {
|
func handleGetRoot(w http.ResponseWriter, r *http.Request) {
|
||||||
@@ -106,6 +113,42 @@ func handleGetRoot(w http.ResponseWriter, r *http.Request) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func handleGetResult(w http.ResponseWriter, r *http.Request) {
|
func handleGetResult(w http.ResponseWriter, r *http.Request) {
|
||||||
|
var pollName string
|
||||||
|
pollId, err := strconv.Atoi(r.PathValue("pollId"))
|
||||||
|
if err != nil {
|
||||||
|
log.Printf("[!] Invallid poll id url segment, error: %s\n", err)
|
||||||
|
http.Error(w, "Poll not found", http.StatusBadRequest)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
const query = `
|
||||||
|
SELECT name
|
||||||
|
FROM polls
|
||||||
|
WHERE id = ?;
|
||||||
|
`
|
||||||
|
|
||||||
|
err = db.QueryRow(query, pollId).Scan(&pollName)
|
||||||
|
if err != nil {
|
||||||
|
log.Printf("[!] No poll with id %d was found, error: %s\n", pollId, err)
|
||||||
|
http.Error(w, "Poll not found", http.StatusBadRequest)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
err = resultHtmlTemplate.Execute(w, struct {
|
||||||
|
PollId int
|
||||||
|
PollName string
|
||||||
|
}{
|
||||||
|
PollId: pollId,
|
||||||
|
PollName: pollName,
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
log.Printf("[!] Could not execute result.html template, error: %s\n", err)
|
||||||
|
http.Error(w, "Error rendering template", http.StatusInternalServerError)
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
func handleGetResultApi(w http.ResponseWriter, r *http.Request) {
|
||||||
pollId, err := strconv.Atoi(r.PathValue("pollId"))
|
pollId, err := strconv.Atoi(r.PathValue("pollId"))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Printf("[!] Invallid poll id url segment, error: %s\n", err)
|
log.Printf("[!] Invallid poll id url segment, error: %s\n", err)
|
||||||
@@ -290,6 +333,11 @@ func main() {
|
|||||||
handleOptionsSubmit,
|
handleOptionsSubmit,
|
||||||
)
|
)
|
||||||
|
|
||||||
|
http.HandleFunc(
|
||||||
|
"GET /api/result/{pollId}",
|
||||||
|
handleGetResultApi,
|
||||||
|
)
|
||||||
|
|
||||||
log.Printf("[*] Datefinder server listening on :%s\n", PORT)
|
log.Printf("[*] Datefinder server listening on :%s\n", PORT)
|
||||||
if err := http.ListenAndServe(":8080", nil); err != nil {
|
if err := http.ListenAndServe(":8080", nil); err != nil {
|
||||||
log.Fatalf("[!] Could not start server on %s, error: %s\n", PORT, err)
|
log.Fatalf("[!] Could not start server on %s, error: %s\n", PORT, err)
|
||||||
|
|||||||
@@ -0,0 +1,105 @@
|
|||||||
|
<!DOCTYPE html>
|
||||||
|
<html lang="en">
|
||||||
|
|
||||||
|
<head>
|
||||||
|
<meta charset="UTF-8">
|
||||||
|
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||||
|
<title>Datefinder</title>
|
||||||
|
<style>
|
||||||
|
body {
|
||||||
|
font-family: Arial, sans-serif;
|
||||||
|
text-align: center;
|
||||||
|
}
|
||||||
|
|
||||||
|
.calendar {
|
||||||
|
display: grid;
|
||||||
|
grid-template-columns: repeat(7, 1fr);
|
||||||
|
/* 7 columns for 7 days */
|
||||||
|
gap: 10px;
|
||||||
|
margin: 20px auto;
|
||||||
|
width: 90%;
|
||||||
|
}
|
||||||
|
|
||||||
|
.day-column {
|
||||||
|
border: 1px solid #ccc;
|
||||||
|
background-color: #f9f9f9;
|
||||||
|
padding: 5px;
|
||||||
|
}
|
||||||
|
|
||||||
|
.day-header {
|
||||||
|
font-weight: bold;
|
||||||
|
margin-bottom: 5px;
|
||||||
|
}
|
||||||
|
|
||||||
|
.hour-block {
|
||||||
|
border: 1px solid #ddd;
|
||||||
|
padding: 10px;
|
||||||
|
margin: 2px 0;
|
||||||
|
cursor: pointer;
|
||||||
|
background-color: #fff;
|
||||||
|
text-align: center;
|
||||||
|
}
|
||||||
|
|
||||||
|
.hour-block.clicked {
|
||||||
|
background-color: #87CEEB;
|
||||||
|
/* Change color when clicked */
|
||||||
|
}
|
||||||
|
|
||||||
|
// TODO(jona) This should be specific for the sumbit-name-lable id!
|
||||||
|
label {
|
||||||
|
color: red;
|
||||||
|
visibility: hidden;
|
||||||
|
}
|
||||||
|
</style>
|
||||||
|
</head>
|
||||||
|
|
||||||
|
<body>
|
||||||
|
<h1>Datefinder</h1>
|
||||||
|
<h2>{{ .PollName }}</h2>
|
||||||
|
<div class="calendar" id="calendar"></div>
|
||||||
|
<script>
|
||||||
|
|
||||||
|
function createResultView(result) {
|
||||||
|
const daysOfWeek = ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"];
|
||||||
|
const calendar = document.getElementById("calendar");
|
||||||
|
|
||||||
|
for (let day = 0; day < 7; day++) {
|
||||||
|
const dayColumn = document.createElement("div");
|
||||||
|
dayColumn.classList.add("day-column");
|
||||||
|
|
||||||
|
const dayHeader = document.createElement("div");
|
||||||
|
dayHeader.classList.add("day-header");
|
||||||
|
dayHeader.textContent = daysOfWeek[day];
|
||||||
|
dayColumn.appendChild(dayHeader);
|
||||||
|
|
||||||
|
for (let hour = 0; hour < 24; hour++) {
|
||||||
|
const hourBlock = document.createElement("div");
|
||||||
|
hourBlock.classList.add("hour-block");
|
||||||
|
hourBlock.textContent = `${hour}:00\n${result.votes[day][hour].votes}\n${result.votes[day][hour].usernames}`;
|
||||||
|
|
||||||
|
dayColumn.appendChild(hourBlock);
|
||||||
|
}
|
||||||
|
|
||||||
|
calendar.appendChild(dayColumn);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const url = "http://localhost:8080/api/result/{{ .PollId }}"
|
||||||
|
|
||||||
|
fetch(url, {method: "GET"})
|
||||||
|
.then(response => {
|
||||||
|
if (!response.ok) {
|
||||||
|
console.log(response)
|
||||||
|
}
|
||||||
|
return response.json()
|
||||||
|
})
|
||||||
|
.then(data => {
|
||||||
|
createResultView(data)
|
||||||
|
})
|
||||||
|
.catch(error => {
|
||||||
|
console.error("Fetch error:", error);
|
||||||
|
})
|
||||||
|
</script>
|
||||||
|
</body>
|
||||||
|
|
||||||
|
</html>
|
||||||
Reference in New Issue
Block a user