Added index and user search + docs init

This commit is contained in:
Borna Rajković 2023-06-20 18:00:12 +02:00
parent 2172b8232e
commit eb91033eb7
6 changed files with 295 additions and 80 deletions

55
main.go
View File

@ -59,6 +59,57 @@ func main() {
r.Get("/api/v1/holidays", getHolidays(holidayService))
templates, err := template.New("").ParseFiles("templates/index.gohtml", "templates/search.gohtml", "templates/search_date.gohtml", "templates/docs.gohtml")
if err != nil {
log.Fatalf("couldn't load templates: %v", err)
}
r.Get("/docs", func(w http.ResponseWriter, r *http.Request) {
templates, err := template.New("").ParseFiles("templates/docs.gohtml")
if err != nil {
log.Fatalf("couldn't load templates: %v", err)
}
renderTemplate(w, r, 200, templates, "docs.gohtml", 0)
})
r.Get("/", func(w http.ResponseWriter, r *http.Request) {
renderTemplate(w, r, 200, templates, "index.gohtml", 0)
})
r.Get("/search", func(w http.ResponseWriter, r *http.Request) {
r.ParseForm()
country := r.FormValue("country")
year := time.Now().Year()
if y := r.FormValue("year"); y != "" {
if yr, err := strconv.ParseInt(y, 10, 64); err == nil {
year = int(yr)
}
}
search := holiday.Search{Year: &year, Country: country}
holidays, _ := holidayService.Find(search, holiday.Paging{PageSize: 100})
holidayResponse := mapHolidays(holidays)
renderTemplate(w, r, 200, templates, "search.gohtml", holidayResponse)
})
r.Get("/search/date", func(w http.ResponseWriter, r *http.Request) {
r.ParseForm()
country := r.FormValue("country")
date := time.Now()
if y := r.FormValue("date"); y != "" {
date = parseDate(y)
}
search := holiday.Search{Date: &date, Country: country}
holidays, _ := holidayService.Find(search, holiday.Paging{PageSize: 100})
holidayResponse := mapHolidays(holidays)
renderTemplate(w, r, 200, templates, "search_date.gohtml", holidayResponse)
})
r.Mount("/admin", adminDashboard(holidayService))
log.Fatal(http.ListenAndServe(":5281", r))
@ -77,7 +128,7 @@ func adminDashboard(service holiday.Service) http.Handler {
}
},
"deferint": func(value *int) int { return *value },
}).ParseFiles("templates/index.gohtml", "templates/holiday.gohtml", "templates/error.gohtml")
}).ParseFiles("templates/admin_dashboard.gohtml", "templates/holiday.gohtml", "templates/error.gohtml")
if err != nil {
log.Fatalf("couldn't load templates: %v", err)
}
@ -211,7 +262,7 @@ func adminDashboard(service holiday.Service) http.Handler {
response["holidays"] = holidayResponse
response["search"] = search
renderTemplate(w, r, 200, templates, "index.gohtml", response)
renderTemplate(w, r, 200, templates, "admin_dashboard.gohtml", response)
})
return r

View File

@ -0,0 +1,102 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Holiday-api</title>
</head>
<body>
<h1><a href="/admin">Holiday-api</a></h1>
<p>Welcome to admin interface for holiday api</p>
<div>
<a href="/admin/holiday">Add a holiday</a>
</div>
<div>
<form id="filter-holidays" action="/admin" method="get">
<label for="country">Country</label>
<select id="country" name="country">
<option value="HR" {{if eq .search.Country "HR"}}selected{{end}}>Croatia</option>
<option value="GB" {{if eq .search.Country "GB"}}selected{{end}}>Great Britain</option>
<option value="US" {{if eq .search.Country "US"}}selected{{end}}>USA</option>
<option value="FR" {{if eq .search.Country "FR"}}selected{{end}}>France</option>
</select>
<label for="year">Year</label>
<select id="year" name="year">
<option value="2020" {{if eq (deferint .search.Year) 2020}}selected{{end}}>2020</option>
<option value="2021" {{if eq (deferint .search.Year) 2021}}selected{{end}}>2021</option>
<option value="2022" {{if eq (deferint .search.Year) 2022}}selected{{end}}>2022</option>
<option value="2023" {{if eq (deferint .search.Year) 2023}}selected{{end}}>2023</option>
<option value="2024" {{if eq (deferint .search.Year) 2024}}selected{{end}}>2024</option>
<option value="2025" {{if eq (deferint .search.Year) 2025}}selected{{end}}>2025</option>
<option value="2026" {{if eq (deferint .search.Year) 2026}}selected{{end}}>2026</option>
<option value="2027" {{if eq (deferint .search.Year) 2027}}selected{{end}}>2027</option>
</select>
<div>
<label>State holiday</label>
<label for="state-holiday-true">True</label>
<input type="radio" value="true" {{if boolcmp .search.StateHoliday "true"}}checked{{end}} id="state-holiday-true" name="stateHoliday">
<label for="state-holiday-false">False</label>
<input type="radio" value="false" {{if boolcmp .search.StateHoliday "false"}}checked{{end}} id="state-holiday-false" name="stateHoliday">
<label for="state-holiday-any">Any</label>
<input type="radio" value="" {{if boolcmp .search.StateHoliday "nil"}}checked{{end}} id="state-holiday-any" name="stateHoliday">
</div>
<div>
<label>Religious holiday</label>
<label for="religious-holiday-true">True</label>
<input type="radio" value="true" {{if boolcmp .search.ReligiousHoliday "true"}}checked{{end}} id="religious-holiday-true" name="religiousHoliday">
<label for="religious-holiday-false">False</label>
<input type="radio" value="false" {{if boolcmp .search.ReligiousHoliday "false"}}checked{{end}} id="religious-holiday-false" name="religiousHoliday">
<label for="religious-holiday-any">Any</label>
<input type="radio" value="" {{if boolcmp .search.ReligiousHoliday "nil"}}checked{{end}} id="religious-holiday-any" name="religiousHoliday">
</div>
<button type="submit">Search</button>
</form>
</div>
<div>
<table>
<thead>
<tr>
<th>Name</th>
<th>Description</th>
<th>Date</th>
<th>State holiday</th>
<th>Religious holiday</th>
<th></th>
</tr>
</thead>
<tbody>
{{range $entry := .holidays.Holidays}}
<tr>
<td>{{$entry.Name}}</td>
<td>{{$entry.Description}}</td>
<td>{{$entry.Date.Format "2006-01-02"}}</td>
<td>{{$entry.IsStateHoliday}}</td>
<td>{{$entry.IsReligiousHoliday}}</td>
<td>
<a href="/admin/holiday?id={{$entry.Id}}">Edit</a>
<form action="/admin/holiday/delete" method="post">
<input id="id" type="hidden" name="id" value="{{$entry.Id}}">
<button type="submit">Delete</button>
</form>
</td>
</tr>
{{end}}
</tbody>
</table>
</div>
</body>
</html>

22
templates/docs.gohtml Normal file
View File

@ -0,0 +1,22 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Holiday-api</title>
</head>
<body>
<h1><a href="/">Holiday-api</a></h1>
<p>Create query</p>
<form>
<select id="country" name="country">
<option value="HR">Croatia</option>
<option value="GB">Great Britain</option>
<option value="US">USA</option>
<option value="FR">France</option>
</select>
</form>
</body>
</html>

View File

@ -7,96 +7,55 @@
<title>Holiday-api</title>
</head>
<body>
<h1>Holiday-api</h1>
<p>Welcome to admin interface for holiday api</p>
<h1><a href="/">Holiday-api</a></h1>
<p>Welcome to holiday api - simple page for tracking holidays</p>
<div>
<a href="/admin/holiday">Add a holiday</a>
</div>
<div>
<form id="filter-holidays" action="/admin" method="get">
<h2>Is a holiday?</h2>
<form method="get" action="/search/date">
<label for="country">Country</label>
<select id="country" name="country">
<option value="HR" {{if eq .search.Country "HR"}}selected{{end}}>Croatia</option>
<option value="GB" {{if eq .search.Country "GB"}}selected{{end}}>Great Britain</option>
<option value="US" {{if eq .search.Country "US"}}selected{{end}}>USA</option>
<option value="FR" {{if eq .search.Country "FR"}}selected{{end}}>France</option>
<option value="HR">Croatia</option>
<option value="GB">Great Britain</option>
<option value="US">USA</option>
<option value="FR">France</option>
</select>
<label for="date">Date</label>
<input id="date" name="date" type="date">
<button type="submit">Check is a holiday</button>
</form>
</div>
<div>
<h2>Find holidays for</h2>
<form method="get" action="/search">
<label for="country">Country</label>
<select id="country" name="country">
<option value="HR">Croatia</option>
<option value="GB">Great Britain</option>
<option value="US">USA</option>
<option value="FR">France</option>
</select>
<label for="year">Year</label>
<select id="year" name="year">
<option value="2020" {{if eq (deferint .search.Year) 2020}}selected{{end}}>2020</option>
<option value="2021" {{if eq (deferint .search.Year) 2021}}selected{{end}}>2021</option>
<option value="2022" {{if eq (deferint .search.Year) 2022}}selected{{end}}>2022</option>
<option value="2023" {{if eq (deferint .search.Year) 2023}}selected{{end}}>2023</option>
<option value="2024" {{if eq (deferint .search.Year) 2024}}selected{{end}}>2024</option>
<option value="2025" {{if eq (deferint .search.Year) 2025}}selected{{end}}>2025</option>
<option value="2026" {{if eq (deferint .search.Year) 2026}}selected{{end}}>2026</option>
<option value="2027" {{if eq (deferint .search.Year) 2027}}selected{{end}}>2027</option>
<option value="2020">2020</option>
<option value="2021">2021</option>
<option value="2022">2022</option>
<option value="2023">2023</option>
<option value="2024">2024</option>
<option value="2025">2025</option>
<option value="2026">2026</option>
<option value="2027">2027</option>
</select>
<div>
<label>State holiday</label>
<label for="state-holiday-true">True</label>
<input type="radio" value="true" {{if boolcmp .search.StateHoliday "true"}}checked{{end}} id="state-holiday-true" name="stateHoliday">
<label for="state-holiday-false">False</label>
<input type="radio" value="false" {{if boolcmp .search.StateHoliday "false"}}checked{{end}} id="state-holiday-false" name="stateHoliday">
<label for="state-holiday-any">Any</label>
<input type="radio" value="" {{if boolcmp .search.StateHoliday "nil"}}checked{{end}} id="state-holiday-any" name="stateHoliday">
</div>
<div>
<label>Religious holiday</label>
<label for="religious-holiday-true">True</label>
<input type="radio" value="true" {{if boolcmp .search.ReligiousHoliday "true"}}checked{{end}} id="religious-holiday-true" name="religiousHoliday">
<label for="religious-holiday-false">False</label>
<input type="radio" value="false" {{if boolcmp .search.ReligiousHoliday "false"}}checked{{end}} id="religious-holiday-false" name="religiousHoliday">
<label for="religious-holiday-any">Any</label>
<input type="radio" value="" {{if boolcmp .search.ReligiousHoliday "nil"}}checked{{end}} id="religious-holiday-any" name="religiousHoliday">
</div>
<button type="submit">Search</button>
<button type="submit">Find holidays</button>
</form>
</div>
<div>
<table>
<thead>
<tr>
<th>Name</th>
<th>Description</th>
<th>Date</th>
<th>State holiday</th>
<th>Religious holiday</th>
<th></th>
</tr>
</thead>
<tbody>
{{range $entry := .holidays.Holidays}}
<tr>
<td>{{$entry.Name}}</td>
<td>{{$entry.Description}}</td>
<td>{{$entry.Date.Format "2006-01-02"}}</td>
<td>{{$entry.IsStateHoliday}}</td>
<td>{{$entry.IsReligiousHoliday}}</td>
<td>
<a href="/admin/holiday?id={{$entry.Id}}">Edit</a>
<form action="/admin/holiday/delete" method="post">
<input id="id" type="hidden" name="id" value="{{$entry.Id}}">
<button type="submit">Delete</button>
</form>
</td>
</tr>
{{end}}
</tbody>
</table>
<h2>Create api query</h2>
<a href="/docs">Goto query editor</a>
</div>
</body>
</html>

38
templates/search.gohtml Normal file
View File

@ -0,0 +1,38 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Holiday-api</title>
</head>
<body>
<h1><a href="/">Holiday-api</a></h1>
<p>Search results</p>
<div>
<table>
<thead>
<tr>
<th>Name</th>
<th>Description</th>
<th>Date</th>
<th>State holiday</th>
<th>Religious holiday</th>
</tr>
</thead>
<tbody>
{{range $entry := .Holidays}}
<tr>
<td>{{$entry.Name}}</td>
<td>{{$entry.Description}}</td>
<td>{{$entry.Date.Format "2006-01-02"}}</td>
<td>{{$entry.IsStateHoliday}}</td>
<td>{{$entry.IsReligiousHoliday}}</td>
</tr>
{{end}}
</tbody>
</table>
</div>
</body>
</html>

View File

@ -0,0 +1,43 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Holiday-api</title>
</head>
<body>
<h1><a href="/">Holiday-api</a></h1>
<p>{{if .Holidays}}Yes it is a holday{{else}}No it isn't a holiday{{end}}</p>
<p>Search results</p>
<div>
{{if .Holidays}}
<table>
<thead>
<tr>
<th>Name</th>
<th>Description</th>
<th>Date</th>
<th>State holiday</th>
<th>Religious holiday</th>
</tr>
</thead>
<tbody>
{{range $entry := .Holidays}}
<tr>
<td>{{$entry.Name}}</td>
<td>{{$entry.Description}}</td>
<td>{{$entry.Date.Format "2006-01-02"}}</td>
<td>{{$entry.IsStateHoliday}}</td>
<td>{{$entry.IsReligiousHoliday}}</td>
</tr>
{{end}}
</tbody>
</table>
{{end}}
</div>
</body>
</html>