From 9302b95fbf4af346fe6f20350b569e8fc47d63ff Mon Sep 17 00:00:00 2001 From: Erfan Date: Thu, 9 Dec 2021 14:49:23 +0330 Subject: [PATCH] complete getReservationsBetweenMonth --- repository/dbrepo/dbrepo.go | 39 ++++++++++++++++++++++++++++++++++++ repository/dbrepo/test-db.go | 4 ++++ repository/repository.go | 1 + 3 files changed, 44 insertions(+) diff --git a/repository/dbrepo/dbrepo.go b/repository/dbrepo/dbrepo.go index 58db55f..e6e6002 100644 --- a/repository/dbrepo/dbrepo.go +++ b/repository/dbrepo/dbrepo.go @@ -463,3 +463,42 @@ func (psdb postgresDbRepo) CompleteReservation(id int) (bool, error) { return true, nil } + +func (psdb postgresDbRepo) GetReservationsBetweemMonth(start, end time.Time) ([]models.Reservation, error) { + ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second) + defer cancel() + + query := `select * from reservations + where end_date > $1 and start_date < $2` + + sd := start.Format("2006-01-02") + ed := end.Format("2006-01-02") + + var reservations []models.Reservation + + rows, err := psdb.DB.QueryContext(ctx, query, sd, ed) + if err != nil { + return reservations, err + } + + for rows.Next() { + res := models.Reservation{} + + rows.Scan( + &res.ID, + &res.FirstName, + &res.LastName, + &res.Email, + &res.Phone, + &res.StartDate, + &res.EndDate, + &res.RoomId, + &res.CreatedAt, + &res.UpdatedAt, + ) + + reservations = append(reservations, res) + } + + return reservations, nil +} diff --git a/repository/dbrepo/test-db.go b/repository/dbrepo/test-db.go index 918b9ad..a72e1f0 100644 --- a/repository/dbrepo/test-db.go +++ b/repository/dbrepo/test-db.go @@ -81,3 +81,7 @@ func (psdb testDbRepo) DeleteReservation(id int) (bool, error) { func (psdb testDbRepo) CompleteReservation(id int) (bool, error) { return true, nil } + +func (psdb testDbRepo) GetReservationsBetweemMonth(start, end time.Time) ([]models.Reservation, error) { + return []models.Reservation{}, nil +} diff --git a/repository/repository.go b/repository/repository.go index 62c6302..36c8a3c 100644 --- a/repository/repository.go +++ b/repository/repository.go @@ -22,4 +22,5 @@ type DatabaseRepository interface { UpdateReservation(models.Reservation) (bool, error) DeleteReservation(id int) (bool, error) CompleteReservation(id int) (bool, error) + GetReservationsBetweemMonth(start, end time.Time) ([]models.Reservation, error) }