diff --git a/main.go b/main.go index c9ce341..059ab6d 100644 --- a/main.go +++ b/main.go @@ -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 diff --git a/templates/admin_dashboard.gohtml b/templates/admin_dashboard.gohtml new file mode 100644 index 0000000..3ec8a2a --- /dev/null +++ b/templates/admin_dashboard.gohtml @@ -0,0 +1,102 @@ + + +
+ + + +Welcome to admin interface for holiday api
+ +Name | +Description | +Date | +State holiday | +Religious holiday | ++ |
---|---|---|---|---|---|
{{$entry.Name}} | +{{$entry.Description}} | +{{$entry.Date.Format "2006-01-02"}} | +{{$entry.IsStateHoliday}} | +{{$entry.IsReligiousHoliday}} | ++ Edit + + | +
Create query
+ + + + \ No newline at end of file diff --git a/templates/index.gohtml b/templates/index.gohtml index 2018e96..64c2b9f 100644 --- a/templates/index.gohtml +++ b/templates/index.gohtml @@ -7,96 +7,55 @@Welcome to admin interface for holiday api
- +Welcome to holiday api - simple page for tracking holidays
Name | -Description | -Date | -State holiday | -Religious holiday | -- |
---|---|---|---|---|---|
{{$entry.Name}} | -{{$entry.Description}} | -{{$entry.Date.Format "2006-01-02"}} | -{{$entry.IsStateHoliday}} | -{{$entry.IsReligiousHoliday}} | -- Edit - - | -