From eb91033eb72dda86a6a1b5c39f3e0feee3d8d045 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Borna=20Rajkovi=C4=87?= Date: Tue, 20 Jun 2023 18:00:12 +0200 Subject: [PATCH] Added index and user search + docs init --- main.go | 55 ++++++++++++++- templates/admin_dashboard.gohtml | 102 +++++++++++++++++++++++++++ templates/docs.gohtml | 22 ++++++ templates/index.gohtml | 115 ++++++++++--------------------- templates/search.gohtml | 38 ++++++++++ templates/search_date.gohtml | 43 ++++++++++++ 6 files changed, 295 insertions(+), 80 deletions(-) create mode 100644 templates/admin_dashboard.gohtml create mode 100644 templates/docs.gohtml create mode 100644 templates/search.gohtml create mode 100644 templates/search_date.gohtml 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 @@ + + + + + + + Holiday-api + + +

Holiday-api

+

Welcome to admin interface for holiday api

+ +
+ Add a holiday +
+
+
+ + + + + + + +
+ + + + + + + + + + +
+ +
+ + + + + + + + + + + +
+ + +
+
+
+ + + + + + + + + + + + + {{range $entry := .holidays.Holidays}} + + + + + + + + + {{end}} + +
NameDescriptionDateState holidayReligious holiday
{{$entry.Name}}{{$entry.Description}}{{$entry.Date.Format "2006-01-02"}}{{$entry.IsStateHoliday}}{{$entry.IsReligiousHoliday}} + Edit +
+ + +
+
+
+ + \ No newline at end of file diff --git a/templates/docs.gohtml b/templates/docs.gohtml new file mode 100644 index 0000000..d9119d0 --- /dev/null +++ b/templates/docs.gohtml @@ -0,0 +1,22 @@ + + + + + + + Holiday-api + + +

Holiday-api

+

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 @@ Holiday-api -

Holiday-api

-

Welcome to admin interface for holiday api

- +

Holiday-api

+

Welcome to holiday api - simple page for tracking holidays

- Add a holiday -
-
-
+

Is a holiday?

+ + + + + +
+
+ +
+

Find holidays for

+
+ + - -
- - - - - - - - - - -
- -
- - - - - - - - - - - -
- - +
+
- - - - - - - - - - - - - {{range $entry := .holidays.Holidays}} - - - - - - - - - {{end}} - -
NameDescriptionDateState holidayReligious holiday
{{$entry.Name}}{{$entry.Description}}{{$entry.Date.Format "2006-01-02"}}{{$entry.IsStateHoliday}}{{$entry.IsReligiousHoliday}} - Edit -
- - -
-
+

Create api query

+ Goto query editor
\ No newline at end of file diff --git a/templates/search.gohtml b/templates/search.gohtml new file mode 100644 index 0000000..13f71ea --- /dev/null +++ b/templates/search.gohtml @@ -0,0 +1,38 @@ + + + + + + + Holiday-api + + +

Holiday-api

+

Search results

+ +
+ + + + + + + + + + + + {{range $entry := .Holidays}} + + + + + + + + {{end}} + +
NameDescriptionDateState holidayReligious holiday
{{$entry.Name}}{{$entry.Description}}{{$entry.Date.Format "2006-01-02"}}{{$entry.IsStateHoliday}}{{$entry.IsReligiousHoliday}}
+
+ + \ No newline at end of file diff --git a/templates/search_date.gohtml b/templates/search_date.gohtml new file mode 100644 index 0000000..d8365f7 --- /dev/null +++ b/templates/search_date.gohtml @@ -0,0 +1,43 @@ + + + + + + + Holiday-api + + +

Holiday-api

+ +

{{if .Holidays}}Yes it is a holday{{else}}No it isn't a holiday{{end}}

+ +

Search results

+ +
+ {{if .Holidays}} + + + + + + + + + + + + {{range $entry := .Holidays}} + + + + + + + + {{end}} + +
NameDescriptionDateState holidayReligious holiday
{{$entry.Name}}{{$entry.Description}}{{$entry.Date.Format "2006-01-02"}}{{$entry.IsStateHoliday}}{{$entry.IsReligiousHoliday}}
+ {{end}} +
+ + \ No newline at end of file