Handling Calendar changes

This commit is contained in:
vinchent 2024-07-28 14:43:39 +02:00
parent 14828fb901
commit d7f7a2d8d9
6 changed files with 154 additions and 40 deletions

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -26,7 +26,12 @@ Reservations Calendar
</div>
<div class="clearfix"></div>
{{range $rooms}}
<form method="post" action="/admin/reservations-calendar">
<input type="hidden" name="csrf_token" value="{{.CSRFToken}}">
<input type="hidden" name="m" value="{{index .StringMap "this_month"}}">
<input type="hidden" name="y" value="{{index .StringMap "this_month_year"}}">
{{range $rooms}}
{{$roomID := .ID}}
{{$blocks := index $.Data (printf "block_map_%d" .ID)}}
{{$reservations := index $.Data (printf "reservation_map_%d" .ID)}}
@ -55,6 +60,7 @@ Reservations Calendar
value="{{index $blocks (printf "%s-%s-%d" $curYear $curMonth (add $index 1))}}"
{{else}}
name="add_block_{{$roomID}}_{{printf "%s-%s-%d" $curYear $curMonth (add $index 1)}}"
value="1"
{{end}}
type="checkbox">
{{end}}
@ -63,6 +69,9 @@ Reservations Calendar
</tr>
</table>
</div>
{{end}}
{{end}}
<hr>
<input type="submit" class="btn btn-primary" value="Save Changes">
</form>
</div>
{{end}}