From 770fbd5b88cb1f9583ad4a84df32a90ca4955d7b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Borna=20Rajkovi=C4=87?= Date: Sat, 5 Aug 2023 21:49:33 +0200 Subject: [PATCH] Added dynamic support for years --- holiday/year_searvice.go | 14 ++++++++++++++ main.go | 13 +++++++++---- templates/admin_dashboard.gohtml | 11 +++-------- templates/documentation.gohtml | 11 +++-------- templates/index.gohtml | 7 +++---- 5 files changed, 32 insertions(+), 24 deletions(-) create mode 100644 holiday/year_searvice.go diff --git a/holiday/year_searvice.go b/holiday/year_searvice.go new file mode 100644 index 0000000..88882b8 --- /dev/null +++ b/holiday/year_searvice.go @@ -0,0 +1,14 @@ +package holiday + +import ( + "github.com/jmoiron/sqlx" +) + +type YearService struct { + DB *sqlx.DB +} + +func (s *YearService) Find() ([]int, error) { + var years []int + return years, s.DB.Select(&years, "SELECT distinct extract(year from date) as date FROM holiday order by date asc") +} diff --git a/main.go b/main.go index ccdd344..1f64b86 100644 --- a/main.go +++ b/main.go @@ -55,10 +55,11 @@ func main() { holidayService := holiday.HolidayService{DB: client} countryService := holiday.CountryService{DB: client} + yearService := holiday.YearService{DB: client} g.GET("/api/v1/holidays", getHolidays(holidayService)) - setupAdminDashboard(g.Group("/admin"), holidayService, countryService) + setupAdminDashboard(g.Group("/admin"), holidayService, countryService, yearService) g.GET("/", func(c *gin.Context) { year := time.Now().Year() @@ -69,11 +70,13 @@ func main() { } holidays, _ := holidayService.Find(search, holiday.Paging{PageSize: 100}) countries, _ := countryService.Find() - c.HTML(http.StatusOK, "index.gohtml", gin.H{"Countries": countries, "Search": search, "Holidays": mapHolidays(holidays).Holidays}) + years, _ := yearService.Find() + c.HTML(http.StatusOK, "index.gohtml", gin.H{"Years": years, "Countries": countries, "Search": search, "Holidays": mapHolidays(holidays).Holidays}) }) g.GET("/documentation", func(c *gin.Context) { countries, _ := countryService.Find() - c.HTML(http.StatusOK, "documentation.gohtml", gin.H{"Countries": countries}) + years, _ := yearService.Find() + c.HTML(http.StatusOK, "documentation.gohtml", gin.H{"Years": years, "Countries": countries}) }) g.GET("/search", func(c *gin.Context) { request := holiday.Search{} @@ -129,7 +132,7 @@ func loadTemplates(g *gin.Engine) { ) } -func setupAdminDashboard(adminDashboard *gin.RouterGroup, service holiday.HolidayService, countryService holiday.CountryService) { +func setupAdminDashboard(adminDashboard *gin.RouterGroup, service holiday.HolidayService, countryService holiday.CountryService, yearService holiday.YearService) { adminDashboard.Use(gin.BasicAuth(loadAuth())) adminDashboard.GET("/", func(c *gin.Context) { @@ -142,11 +145,13 @@ func setupAdminDashboard(adminDashboard *gin.RouterGroup, service holiday.Holida holidays, _ := service.Find(search, holiday.Paging{PageSize: 100}) holidayResponse := mapHolidays(holidays) countries, _ := countryService.Find() + years, _ := yearService.Find() response := map[string]any{} response["Holidays"] = holidayResponse response["Search"] = search response["Countries"] = countries + response["Years"] = years c.HTML(http.StatusOK, "admin_dashboard.gohtml", response) }) diff --git a/templates/admin_dashboard.gohtml b/templates/admin_dashboard.gohtml index 40dcac9..06f16cb 100644 --- a/templates/admin_dashboard.gohtml +++ b/templates/admin_dashboard.gohtml @@ -36,14 +36,9 @@
diff --git a/templates/documentation.gohtml b/templates/documentation.gohtml index e907ae2..0846200 100644 --- a/templates/documentation.gohtml +++ b/templates/documentation.gohtml @@ -61,14 +61,9 @@
diff --git a/templates/index.gohtml b/templates/index.gohtml index dc7c300..7055a89 100644 --- a/templates/index.gohtml +++ b/templates/index.gohtml @@ -36,10 +36,9 @@