From d7f7a2d8d95ee5499a4bd33e29a9682e5d55e526 Mon Sep 17 00:00:00 2001 From: vinchent Date: Sun, 28 Jul 2024 14:43:39 +0200 Subject: [PATCH] Handling Calendar changes --- cmd/web/routes.go | 1 + internal/handlers/handlers.go | 67 ++++++++++++++- internal/repository/dbrepo/postgres.go | 33 ++++++++ internal/repository/dbrepo/test-repo.go | 10 +++ internal/repository/repository.go | 2 + .../admin-reservations-calendar.page.tmpl | 81 ++++++++++--------- 6 files changed, 154 insertions(+), 40 deletions(-) diff --git a/cmd/web/routes.go b/cmd/web/routes.go index 24aeca4..3cbcf99 100644 --- a/cmd/web/routes.go +++ b/cmd/web/routes.go @@ -44,6 +44,7 @@ func routes(app *config.AppConfig) http.Handler { mux.Get("/reservations-new", handlers.Repo.AdminNewReservations) mux.Get("/reservations-all", handlers.Repo.AdminAllReservations) mux.Get("/reservations-calendar", handlers.Repo.AdminReservationsCalendar) + mux.Post("/reservations-calendar", handlers.Repo.AdminPostReservationsCalendar) mux.Get("/process-reservation/{src}/{id}", handlers.Repo.AdminProcessReservation) mux.Get("/delete-reservation/{src}/{id}", handlers.Repo.AdminDeleteReservation) diff --git a/internal/handlers/handlers.go b/internal/handlers/handlers.go index 8709a6a..caddd91 100644 --- a/internal/handlers/handlers.go +++ b/internal/handlers/handlers.go @@ -685,10 +685,9 @@ func (m *Repository) AdminReservationsCalendar(w http.ResponseWriter, r *http.Re reservationMap[d.Format("2006-01-2")] = y.ReservationID } } else { - // it's a block - for d := y.StartDate; !d.After(y.EndDate); d = d.AddDate(0, 0, 1) { - blockMap[d.Format("2006-01-2")] = y.ID - } + // it's a block. + // NOTE:A block can only be set day by day + blockMap[y.StartDate.Format("2006-01-2")] = y.ID } } data[fmt.Sprintf("reservation_map_%d", x.ID)] = reservationMap @@ -728,3 +727,63 @@ func (m *Repository) AdminDeleteReservation(w http.ResponseWriter, r *http.Reque http.Redirect(w, r, fmt.Sprintf("/admin/reservations-%s", src), http.StatusSeeOther) } + +// AdminPostReservationsCalendar handles post of reservation calendar +func (m *Repository) AdminPostReservationsCalendar(w http.ResponseWriter, r *http.Request) { + if err := r.ParseForm(); err != nil { + helpers.ServerError(w, err) + return + } + year, _ := strconv.Atoi(r.Form.Get("y")) + month, _ := strconv.Atoi(r.Form.Get("m")) + + // process blocks + rooms, err := m.DB.AllRooms() + if err != nil { + helpers.ServerError(w, err) + return + } + + form := forms.New(r.PostForm) + for _, x := range rooms { + // Get the block map from the session. Loop through entire map, if we + // have an entry in the map taht does not exist in our posted data, + // and if the restriction id > 0, then it is a block we need to remove. + curMap, _ := m.App.Session.Get(r.Context(), fmt.Sprintf("block_map_%d", x.ID)).(map[string]int) + // TODO check session get ok + for name, value := range curMap { + // ok will be false if the value is not in the map + if val, ok := curMap[name]; ok { + // only pay attention to values > 0, and that are not in the form post + // the rest are just placeholders for days without blocks + if val > 0 { + if !form.Has(fmt.Sprintf("remove_block_%d_%s", x.ID, name)) { + err := m.DB.DeleteBlockByID(value) + if err != nil { + helpers.ServerError(w, err) + return + } + } + } + } + } + } + + // handle new blocks + for name := range r.PostForm { + if strings.HasPrefix(name, "add_block") { + exploded := strings.Split(name, "_") + roomID, _ := strconv.Atoi(exploded[2]) + startDate, _ := time.Parse("2006-01-2", exploded[3]) + // insert a new block + err := m.DB.InsertBlockForRoom(roomID, startDate) + if err != nil { + helpers.ServerError(w, err) + return + } + } + } + + m.App.Session.Put(r.Context(), "flash", "Changes saved") + http.Redirect(w, r, fmt.Sprintf("/admin/reservations-calendar?y=%d&m=%d", year, month), http.StatusSeeOther) +} diff --git a/internal/repository/dbrepo/postgres.go b/internal/repository/dbrepo/postgres.go index cdbd682..e58b875 100644 --- a/internal/repository/dbrepo/postgres.go +++ b/internal/repository/dbrepo/postgres.go @@ -4,6 +4,7 @@ import ( "context" "errors" "go-udemy-web-1/internal/models" + "log" "time" "golang.org/x/crypto/bcrypt" @@ -459,3 +460,35 @@ func (m *postgresDBRepo) GetRestrictionsForRoomByDate(roomId int, start, end tim } return restrictions, nil } + +// InsertBlockForRoom inserts a room restriction +func (m *postgresDBRepo) InsertBlockForRoom(id int, startDate time.Time) error { + ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second) + defer cancel() + + query := `insert into room_restrictions (start_date, end_date, room_id, restriction_id, + created_at, updated_at) + values ($1, $2, $3, $4, $5, $6)` + + _, err := m.DB.ExecContext(ctx, query, startDate, startDate.AddDate(0, 0, 1), id, 2, time.Now(), time.Now()) + if err != nil { + log.Println(err) + return err + } + return nil +} + +// DeleteBlockByID deletes a block by ID +func (m *postgresDBRepo) DeleteBlockByID(id int) error { + ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second) + defer cancel() + + query := `delete from room_restrictions where id = $1` + + _, err := m.DB.ExecContext(ctx, query, id) + if err != nil { + log.Println(err) + return err + } + return nil +} diff --git a/internal/repository/dbrepo/test-repo.go b/internal/repository/dbrepo/test-repo.go index d03bba1..0cda1ea 100644 --- a/internal/repository/dbrepo/test-repo.go +++ b/internal/repository/dbrepo/test-repo.go @@ -128,3 +128,13 @@ func (m *testDBRepo) GetRestrictionsForRoomByDate(roomId int, start, end time.Ti var restrictions []models.RoomRestriction return restrictions, nil } + +// InsertBlockForRoom inserts a room restriction +func (m *testDBRepo) InsertBlockForRoom(id int, startDate time.Time) error { + return nil +} + +// DeleteBlockByID deletes a block by ID +func (m *testDBRepo) DeleteBlockByID(id int) error { + return nil +} diff --git a/internal/repository/repository.go b/internal/repository/repository.go index a7a4928..f191a89 100644 --- a/internal/repository/repository.go +++ b/internal/repository/repository.go @@ -24,4 +24,6 @@ type DatabaseRepo interface { UpdateProcessedForReservation(id, processed int) error AllRooms() ([]models.Room, error) GetRestrictionsForRoomByDate(roomId int, start, end time.Time) ([]models.RoomRestriction, error) + InsertBlockForRoom(id int, startDate time.Time) error + DeleteBlockByID(id int) error } diff --git a/templates/admin-reservations-calendar.page.tmpl b/templates/admin-reservations-calendar.page.tmpl index 2567e7a..769cacb 100644 --- a/templates/admin-reservations-calendar.page.tmpl +++ b/templates/admin-reservations-calendar.page.tmpl @@ -26,43 +26,52 @@ Reservations Calendar
-{{range $rooms}} - {{$roomID := .ID}} - {{$blocks := index $.Data (printf "block_map_%d" .ID)}} - {{$reservations := index $.Data (printf "reservation_map_%d" .ID)}} -

{{.RoomName}}

-
- - - {{range $index := iterate $dim}} - - {{end}} - - - {{range $index := iterate $dim}} - + {{end}} + +
- {{add $index 1}} -
- {{if gt (index $reservations (printf "%s-%s-%d" $curYear $curMonth (add $index 1))) 0}} - - R - - {{else}} - + + + + + {{range $rooms}} + {{$roomID := .ID}} + {{$blocks := index $.Data (printf "block_map_%d" .ID)}} + {{$reservations := index $.Data (printf "reservation_map_%d" .ID)}} +

{{.RoomName}}

+
+ + + {{range $index := iterate $dim}} + + {{end}} + + + {{range $index := iterate $dim}} + - {{end}} - -
+ {{add $index 1}} +
+ {{if gt (index $reservations (printf "%s-%s-%d" $curYear $curMonth (add $index 1))) 0}} + + R + {{else}} - name="add_block_{{$roomID}}_{{printf "%s-%s-%d" $curYear $curMonth (add $index 1)}}" + {{end}} - type="checkbox"> - {{end}} -
-
-{{end}} +
+
+ {{end}} +
+ + {{end}}