Added dynamic support for years

This commit is contained in:
Borna Rajković 2023-08-05 21:49:33 +02:00
parent 47b9939ead
commit 770fbd5b88
5 changed files with 32 additions and 24 deletions

14
holiday/year_searvice.go Normal file
View File

@ -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")
}

13
main.go
View File

@ -55,10 +55,11 @@ func main() {
holidayService := holiday.HolidayService{DB: client} holidayService := holiday.HolidayService{DB: client}
countryService := holiday.CountryService{DB: client} countryService := holiday.CountryService{DB: client}
yearService := holiday.YearService{DB: client}
g.GET("/api/v1/holidays", getHolidays(holidayService)) 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) { g.GET("/", func(c *gin.Context) {
year := time.Now().Year() year := time.Now().Year()
@ -69,11 +70,13 @@ func main() {
} }
holidays, _ := holidayService.Find(search, holiday.Paging{PageSize: 100}) holidays, _ := holidayService.Find(search, holiday.Paging{PageSize: 100})
countries, _ := countryService.Find() 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) { g.GET("/documentation", func(c *gin.Context) {
countries, _ := countryService.Find() 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) { g.GET("/search", func(c *gin.Context) {
request := holiday.Search{} 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.Use(gin.BasicAuth(loadAuth()))
adminDashboard.GET("/", func(c *gin.Context) { 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}) holidays, _ := service.Find(search, holiday.Paging{PageSize: 100})
holidayResponse := mapHolidays(holidays) holidayResponse := mapHolidays(holidays)
countries, _ := countryService.Find() countries, _ := countryService.Find()
years, _ := yearService.Find()
response := map[string]any{} response := map[string]any{}
response["Holidays"] = holidayResponse response["Holidays"] = holidayResponse
response["Search"] = search response["Search"] = search
response["Countries"] = countries response["Countries"] = countries
response["Years"] = years
c.HTML(http.StatusOK, "admin_dashboard.gohtml", response) c.HTML(http.StatusOK, "admin_dashboard.gohtml", response)
}) })

View File

@ -36,14 +36,9 @@
<section> <section>
<label for="year">Year:</label> <label for="year">Year:</label>
<select id="year" name="year"> <select id="year" name="year">
<option value="2020" {{if eq (deferint .Search.Year) 2020}}selected{{end}}>2020</option> {{range $entry := .Years}}
<option value="2021" {{if eq (deferint .Search.Year) 2021}}selected{{end}}>2021</option> <option {{if intpeq $.Search.Year $entry}}selected{{end}} value="{{$entry}}">{{$entry}}</option>
<option value="2022" {{if eq (deferint .Search.Year) 2022}}selected{{end}}>2022</option> {{end}}
<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> </select>
</section> </section>
<section class="radio-group"> <section class="radio-group">

View File

@ -61,14 +61,9 @@
<section> <section>
<label for="dsy-year">Year</label> <label for="dsy-year">Year</label>
<select id="dsy-year" name="year"> <select id="dsy-year" name="year">
<option value="2020">2020</option> {{range $entry := .Years}}
<option value="2021">2021</option> <option value="{{$entry}}">{{$entry}}</option>
<option value="2022">2022</option> {{end}}
<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> </select>
</section> </section>
</div> </div>

View File

@ -36,10 +36,9 @@
<section> <section>
<label for="year">Year:</label> <label for="year">Year:</label>
<select id="year" name="year"> <select id="year" name="year">
<option {{if intpeq .Search.Year 2020}}selected{{end}} value="2020">2020</option> {{range $entry := .Years}}
<option {{if intpeq .Search.Year 2021}}selected{{end}} value="2021">2021</option> <option {{if intpeq $.Search.Year $entry}}selected{{end}} value="{{$entry}}">{{$entry}}</option>
<option {{if intpeq .Search.Year 2022}}selected{{end}} value="2022">2022</option> {{end}}
<option {{if intpeq .Search.Year 2023}}selected{{end}} value="2023">2023</option>
</select> </select>
</section> </section>
<section class="radio-group"> <section class="radio-group">