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)
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
@ -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
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
@ -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
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
@ -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
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
		Reference in New Issue
	
	Block a user