Listing all reservations

This commit is contained in:
2024-07-22 22:14:48 +02:00
parent b34c217d98
commit f93388a8e9
8 changed files with 124 additions and 4 deletions

View File

@ -113,6 +113,8 @@ func (m *postgresDBRepo) SearchAvailabilityForAllRooms(start, end time.Time) ([]
if err != nil {
return nil, err
}
defer rows.Close()
for rows.Next() {
var room models.Room
@ -211,3 +213,48 @@ func (m *postgresDBRepo) Authenticate(email, testPassword string) (int, string,
}
return id, hashedPassword, nil
}
// AllReservations returns a slice of all reservations
func (m *postgresDBRepo) AllReservations() ([]models.Reservation, error) {
ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second)
defer cancel()
var reservations []models.Reservation
query := `select r.id, r.first_name, r.last_name, r.email, r.phone,
r.start_date, r.end_date, r.room_id, r.created_at,
r.updated_at, rm.id, rm.room_name
from reservations r
left join rooms rm on (r.room_id = rm.id)
order by r.start_date asc`
rows, err := m.DB.QueryContext(ctx, query)
if err != nil {
return reservations, err
}
defer rows.Close() // To avoid memory leak
for rows.Next() {
var i models.Reservation
err := rows.Scan(
&i.ID,
&i.FirstName,
&i.LastName,
&i.Email,
&i.Phone,
&i.StartDate,
&i.EndDate,
&i.RoomID,
&i.CreatedAt,
&i.UpdatedAt,
&i.Room.ID,
&i.Room.RoomName,
)
if err != nil {
return reservations, err
}
reservations = append(reservations, i)
}
return reservations, nil
}

View File

@ -81,3 +81,10 @@ func (m *testDBRepo) UpdateUser(u models.User) error {
func (m *testDBRepo) Authenticate(email, testPassword string) (int, string, error) {
return 1, "", nil
}
// AllReservations returns a slice of all reservations
func (m *testDBRepo) AllReservations() ([]models.Reservation, error) {
var reservations []models.Reservation
return reservations, nil
}