Skip to content

Commit

Permalink
Handling Calendar changes IV
Browse files Browse the repository at this point in the history
  • Loading branch information
Pomog committed Jan 7, 2024
1 parent b257c19 commit c39db53
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 7 deletions.
25 changes: 19 additions & 6 deletions internal/handlers/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -744,13 +744,20 @@ func (m *Repository) AdminPostCalendarReservations(w http.ResponseWriter, r *htt
// that does not exist in the posted data and restrictionID > 1, then it is a blok need to remove
curMap := m.App.Session.Get(r.Context(), fmt.Sprintf("blockMap_map_%d", x.ID)).(map[string]int)
for name, value := range curMap {
fmt.Println("name: ", name, " value: ", value)
// ok will be false if value is not th the map
if val, ok := curMap[name]; ok {
// only pay attention to val > 0, and than are not in the form post
fmt.Println("val: ", val)
if val > 0 {
if !form.Has(fmt.Sprintf("remove_block_%d_%s", x.ID, name)) {
// delete restriction by id
log.Println("would delete block", value)
err := m.DB.DeleteBlockForRoom(value)
if err != nil {
helpers.ServerError(w, err)
return
}
log.Println("deleted restriction :", value, " name: ", name)
}
}
}
Expand All @@ -759,12 +766,18 @@ func (m *Repository) AdminPostCalendarReservations(w http.ResponseWriter, r *htt
}

// handle new blocks
for name, _:=range r.PostForm{
if strings.HasPrefix(name, "add_block"){
expoded := strings.Split(name, "_")
roomID, _ := strconv.Atoi(expoded[2])
for name := range r.PostForm {
if strings.HasPrefix(name, "add_block") {
exploded := strings.Split(name, "_")
roomID, _ := strconv.Atoi(exploded[2])
t, _ := time.Parse("2006-01-2", exploded[3])
// insert a new block
log.Println("would block for roomID: ", roomID, " for date: ", expoded[3])
err := m.DB.InsertBlockForRoom(roomID, t)
if err != nil {
helpers.ServerError(w, err)
return
}
log.Println("inserted restriction for roomId:", roomID, " at: ", exploded[3])
}
}

Expand Down
37 changes: 36 additions & 1 deletion internal/repository/dbrepo/postgres.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package dbrepo
import (
"context"
"errors"
"log"
"time"
"udemyCourse1/internal/models"

Expand Down Expand Up @@ -512,4 +513,38 @@ func (m *postgresDBRepo) GetRestictionsForRoomByDate(roomID int, start, end time
}

return restrictions, err
}
}

// insert 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, 1, time.Now(), time.Now())
if err != nil {
log.Println(err)
return err
}

return nil
}

// insert Room restriction
func (m *postgresDBRepo) DeleteBlockForRoom(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
}
10 changes: 10 additions & 0 deletions internal/repository/dbrepo/testRepo.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,3 +103,13 @@ func (m *testDBRepo) GetRestictionsForRoomByDate(roomID int, start, end time.Tim
var restrictions []models.RoomRestriction
return restrictions, nil
}

// insert Room restriction
func (m *testDBRepo) InsertBlockForRoom(id int, startDate time.Time) error {
return nil
}

// insert Room restriction
func (m *testDBRepo) DeleteBlockForRoom(id int) error {
return nil
}
2 changes: 2 additions & 0 deletions internal/repository/repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,6 @@ type DatabaseRepo interface {
DeleteReservation(id int) error
UpdateProcessedForReservation(id, processed int) error
GetRestictionsForRoomByDate(roomID int, start, end time.Time) ([]models.RoomRestriction, error)
DeleteBlockForRoom(id int) error
InsertBlockForRoom(id int, startDate time.Time) error
}

0 comments on commit c39db53

Please sign in to comment.