added year duplication
This commit is contained in:
Borna Rajković 2024-02-08 21:30:20 +01:00
parent 8fbdffc965
commit 45b220c69f
6 changed files with 74 additions and 5 deletions

View File

@ -102,6 +102,23 @@ func (s *HolidayService) Delete(id uuid.UUID) error {
return err 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 { func getDateOrDefault(date *time.Time, year int) time.Time {
if date == nil { if date == nil {
return time.Date(year, 1, 1, 0, 0, 0, 0, time.UTC) return time.Date(year, 1, 1, 0, 0, 0, 0, time.UTC)

29
main.go
View File

@ -110,6 +110,8 @@ func loadTemplates(g *gin.Engine) {
"templates/admin_dashboard.gohtml", "templates/admin_dashboard.gohtml",
"templates/countries.gohtml", "templates/countries.gohtml",
"templates/dialogs/copy-year.gohtml",
"templates/dialogs/add-holiday.gohtml", "templates/dialogs/add-holiday.gohtml",
"templates/dialogs/edit-holiday.gohtml", "templates/dialogs/edit-holiday.gohtml",
"templates/dialogs/delete-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)) 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) { adminDashboard.POST("/holidays/:id/delete", func(c *gin.Context) {
id := uuid.MustParse(c.Param("id")) id := uuid.MustParse(c.Param("id"))
hol, err := service.FindById(id) hol, err := service.FindById(id)
@ -266,6 +286,15 @@ func setupAdminDashboard(adminDashboard *gin.RouterGroup, service holiday.Holida
countries, _ := countryService.Find() countries, _ := countryService.Find()
c.HTML(http.StatusOK, "edit-holiday.gohtml", gin.H{"Countries": countries, "Holiday": hol}) 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) { adminDashboard.GET("/dialogs/edit-country", func(c *gin.Context) {
id := uuid.MustParse(c.Query("id")) id := uuid.MustParse(c.Query("id"))
country, err := countryService.FindById(id) country, err := countryService.FindById(id)

View File

@ -1,7 +1,7 @@
# scripts for building app # scripts for building app
# requires go 1.19+ and git installed # requires go 1.19+ and git installed
VERSION := 0.4.0 VERSION := 0.5.0
serve: serve:
go run ./... go run ./...

View File

@ -64,8 +64,11 @@
</div> </div>
<div style="flex-grow: 1; background: transparent"> <div style="flex-grow: 1; background: transparent">
<button style="float: right; margin-top: 0" data-type="dialog" data-trigger="#create-card" data-url="/admin/dialogs/add-holiday" class="primary">Dodaj novi praznik</button> <div style="display: flex; flex-direction: row; flex-wrap: wrap; justify-content: end; background: transparent; gap: 0.5em">
<section class="dialog table-results" style="margin: 0; margin-top: 3.5em" id="results"> <button data-type="dialog" data-trigger="#copy-year" data-url="/admin/dialogs/copy-year?year={{$.Search.Year}}&country={{$.Search.Country}}" class="secondary">Kopiraj godinu</button>
<button data-type="dialog" data-trigger="#create-card" data-url="/admin/dialogs/add-holiday" class="primary">Dodaj novi praznik</button>
</div>
<section class="dialog table-results" style="margin: 0; margin-top: 0.5em" id="results">
<table> <table>
<thead> <thead>
<tr> <tr>

View File

@ -17,8 +17,10 @@
</header> </header>
<main class="admin-dashboard"> <main class="admin-dashboard">
<div style="width: 640px; max-width: 100%; margin: auto; background: transparent"> <div style="width: 640px; max-width: 100%; margin: auto; background: transparent">
<div style="display: flex; flex-direction: row; flex-wrap: wrap; justify-content: end; background: transparent">
<button style="float: right; margin-top: 0" data-type="dialog" data-trigger="#create-card" data-url="/admin/dialogs/add-country" class="primary">Dodaj državu</button> <button style="float: right; margin-top: 0" data-type="dialog" data-trigger="#create-card" data-url="/admin/dialogs/add-country" class="primary">Dodaj državu</button>
<section class="dialog table-results" style="margin: 0; margin-top: 3.5em" id="results"> </div>
<section class="dialog table-results" style="margin: 0; margin-top: 0.5em" id="results">
<table style="width: 100%"> <table style="width: 100%">
<thead> <thead>
<tr> <tr>

View File

@ -0,0 +1,18 @@
<dialog class="card" id="copy-year">
<h3 class="card-title">Kopiraj godinu {{.Year}}</h3>
<p>Na koji godinu želite kopirati praznike iz godine {{.Year}}?</p>
<form method="post" action="/admin/holidays/copy">
<input type="hidden" name="from" value="{{.Year}}">
<input type="hidden" name="country" value="{{.Country}}">
<section class="form-field">
<label for="to">Godina:</label>
<input required id="to" name="to" type="number" step="1">
</section>
<section class="actions">
<button class="primary" type="submit">Kopiraj</button>
<button class="secondary" type="button" onclick="closeDialog('#copy-year')">Odustani</button>
</section>
</form>
</dialog>