Handling Calendar changes
This commit is contained in:
@ -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)
|
||||
}
|
||||
|
Reference in New Issue
Block a user