diff --git a/holiday/holiday_service.go b/holiday/holiday_service.go index c9edf67..83204c1 100644 --- a/holiday/holiday_service.go +++ b/holiday/holiday_service.go @@ -102,6 +102,23 @@ func (s *HolidayService) Delete(id uuid.UUID) error { return err } +func (s *HolidayService) Copy(country string, from int, to int) error { + holidays, err := s.findByYear(from, nil, nil, country) + if err != nil { + return err + } + var diff = to - from + + for _, holiday := range holidays { + holiday.Date = holiday.Date.AddDate(diff, 0, 0) + _, err := s.Create(holiday) + if err != nil { + return err + } + } + return nil +} + func getDateOrDefault(date *time.Time, year int) time.Time { if date == nil { return time.Date(year, 1, 1, 0, 0, 0, 0, time.UTC) diff --git a/main.go b/main.go index 07433b0..907af6a 100644 --- a/main.go +++ b/main.go @@ -110,6 +110,8 @@ func loadTemplates(g *gin.Engine) { "templates/admin_dashboard.gohtml", "templates/countries.gohtml", + "templates/dialogs/copy-year.gohtml", + "templates/dialogs/add-holiday.gohtml", "templates/dialogs/edit-holiday.gohtml", "templates/dialogs/delete-holiday.gohtml", @@ -189,6 +191,24 @@ func setupAdminDashboard(adminDashboard *gin.RouterGroup, service holiday.Holida c.Redirect(http.StatusSeeOther, "/admin?country="+request.Country+"&year="+strconv.FormatInt(int64(request.Date.Year()), 10)) } }) + adminDashboard.POST("/holidays/copy", func(c *gin.Context) { + request := struct { + From int `form:"from"` + To int `form:"to"` + Country string `form:"country" binding:"len=2"` + }{} + if err := c.ShouldBind(&request); err != nil { + c.AbortWithError(http.StatusBadRequest, err) + return + } + + err := service.Copy(request.Country, request.From, request.To) + if err != nil { + c.AbortWithError(http.StatusInternalServerError, err) + } else { + c.Redirect(http.StatusSeeOther, "/admin?country="+request.Country+"&year="+strconv.FormatInt(int64(request.To), 10)) + } + }) adminDashboard.POST("/holidays/:id/delete", func(c *gin.Context) { id := uuid.MustParse(c.Param("id")) hol, err := service.FindById(id) @@ -266,6 +286,15 @@ func setupAdminDashboard(adminDashboard *gin.RouterGroup, service holiday.Holida countries, _ := countryService.Find() c.HTML(http.StatusOK, "edit-holiday.gohtml", gin.H{"Countries": countries, "Holiday": hol}) }) + adminDashboard.GET("/dialogs/copy-year", func(c *gin.Context) { + country := c.Query("country") + year, err := strconv.ParseInt(c.Query("year"), 10, 32) + if err != nil { + c.AbortWithError(http.StatusNotFound, err) + return + } + c.HTML(http.StatusOK, "copy-year.gohtml", gin.H{"Country": country, "Year": year}) + }) adminDashboard.GET("/dialogs/edit-country", func(c *gin.Context) { id := uuid.MustParse(c.Query("id")) country, err := countryService.FindById(id) diff --git a/makefile b/makefile index 28bdee8..6fb7f0f 100644 --- a/makefile +++ b/makefile @@ -1,7 +1,7 @@ # scripts for building app # requires go 1.19+ and git installed -VERSION := 0.4.0 +VERSION := 0.5.0 serve: go run ./... diff --git a/templates/admin_dashboard.gohtml b/templates/admin_dashboard.gohtml index 8e9b160..1ed3ae5 100644 --- a/templates/admin_dashboard.gohtml +++ b/templates/admin_dashboard.gohtml @@ -64,8 +64,11 @@