Skip to content

Commit

Permalink
added the database function to add an delete blocks to and from days
Browse files Browse the repository at this point in the history
  • Loading branch information
jagottsicher committed Sep 19, 2023
1 parent c365f1f commit b9539e7
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 2 deletions.
10 changes: 8 additions & 2 deletions internal/handlers/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -800,7 +800,10 @@ func (m *Repository) AdminPostReservationsCalendar(w http.ResponseWriter, r *htt
if val > 0 {
if !form.Has(fmt.Sprintf("remove_block_%d_%s", x.ID, name)) {
// delete the bungalow_restriction by id
log.Println("Deleting block:", value)
err := m.DB.DeleteBlockByID(value)
if err != nil {
log.Println(err)
}
}
}
}
Expand All @@ -815,7 +818,10 @@ func (m *Repository) AdminPostReservationsCalendar(w http.ResponseWriter, r *htt
t, _ := time.Parse("2006-01-2", exploded[3])

// insert the bungalow_restriction by id
log.Println("Block bungalow with id", bungalowID, "for day:", t)
err := m.DB.InsertBlockForBungalow(bungalowID, t)
if err != nil {
log.Println(err)
}
}
}

Expand Down
32 changes: 32 additions & 0 deletions 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"

"github.com/jagottsicher/myGoWebApplication/internal/models"
Expand Down Expand Up @@ -554,3 +555,34 @@ func (m *postgresDBRepo) GetRestrictionsForBungalowByDate(bungalowID int, start,
return restrictions, nil

}

// InsertBlockForBungalow inserts a bungalow restriction by bungalow id for a specific day
func (m *postgresDBRepo) InsertBlockForBungalow(id int, startDate time.Time) error {
ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second)
defer cancel()

query := `insert into bungalow_restrictions (start_date, end_date, bungalow_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 bungalow restriction by id
func (m *postgresDBRepo) DeleteBlockByID(id int) error {
ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second)
defer cancel()

query := `delete from bungalow_restrictions where id = $1`

_, err := m.DB.ExecContext(ctx, query, id)
if err != nil {
log.Println(err)
return err
}
return nil
}
9 changes: 9 additions & 0 deletions internal/repository/dbrepo/testrepo.go
Original file line number Diff line number Diff line change
Expand Up @@ -169,3 +169,12 @@ func (m *testDBRepo) GetRestrictionsForBungalowByDate(bungalowID int, start, end

return restrictions, nil
}

func (m *testDBRepo) InsertBlockForBungalow(id int, startDate time.Time) error {

return nil
}

func (m *testDBRepo) DeleteBlockByID(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 @@ -25,4 +25,6 @@ type DatabaseRepo interface {
UpdateStatusOfReservation(id, status int) error
AllBungalows() ([]models.Bungalow, error)
GetRestrictionsForBungalowByDate(bungalowID int, start, end time.Time) ([]models.BungalowRestriction, error)
InsertBlockForBungalow(id int, startDate time.Time) error
DeleteBlockByID(id int) error
}

0 comments on commit b9539e7

Please sign in to comment.