2024-07-06 20:55:25 +00:00
|
|
|
package dbrepo
|
|
|
|
|
2024-07-08 21:04:17 +00:00
|
|
|
import (
|
|
|
|
"context"
|
2024-07-19 07:44:27 +00:00
|
|
|
"errors"
|
2024-07-08 21:04:17 +00:00
|
|
|
"go-udemy-web-1/internal/models"
|
|
|
|
"time"
|
2024-07-19 07:44:27 +00:00
|
|
|
|
|
|
|
"golang.org/x/crypto/bcrypt"
|
2024-07-08 21:04:17 +00:00
|
|
|
)
|
|
|
|
|
2024-07-06 20:55:25 +00:00
|
|
|
func (m *postgresDBRepo) AllUsers() bool {
|
|
|
|
return true
|
|
|
|
}
|
2024-07-08 21:04:17 +00:00
|
|
|
|
|
|
|
// InsertReservation inserts a reservation into the database
|
2024-07-09 20:57:07 +00:00
|
|
|
func (m *postgresDBRepo) InsertReservation(res models.Reservation) (int, error) {
|
2024-07-08 21:04:17 +00:00
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second)
|
|
|
|
defer cancel()
|
2024-07-09 20:57:07 +00:00
|
|
|
|
|
|
|
var newId int
|
2024-07-08 21:04:17 +00:00
|
|
|
// statement
|
|
|
|
stmt := `insert into reservations (first_name, last_name, email, phone,
|
|
|
|
start_date, end_date, room_id, created_at, updated_at)
|
2024-07-09 20:57:07 +00:00
|
|
|
values ($1, $2, $3, $4, $5, $6, $7, $8, $9) returning id`
|
|
|
|
|
|
|
|
row := m.DB.QueryRowContext(ctx, stmt,
|
|
|
|
res.FirstName,
|
|
|
|
res.LastName,
|
|
|
|
res.Email,
|
|
|
|
res.Phone,
|
|
|
|
res.StartDate,
|
|
|
|
res.EndDate,
|
|
|
|
res.RoomID,
|
|
|
|
time.Now(),
|
|
|
|
time.Now())
|
|
|
|
|
|
|
|
err := row.Scan(&newId)
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return newId, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// InsertRoomRestriction inserts a room restriction into the database
|
|
|
|
func (m *postgresDBRepo) InsertRoomRestriction(r models.RoomRestriction) error {
|
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second)
|
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
stmt := `insert into room_restrictions (
|
|
|
|
start_date, end_date, room_id, reservation_id, restriction_id,
|
|
|
|
created_at, updated_at)
|
|
|
|
values ($1, $2, $3, $4, $5, $6, $7)`
|
2024-07-08 21:04:17 +00:00
|
|
|
|
2024-07-09 20:57:07 +00:00
|
|
|
_, err := m.DB.ExecContext(ctx, stmt,
|
|
|
|
r.StartDate,
|
|
|
|
r.EndDate,
|
|
|
|
r.RoomID,
|
|
|
|
r.ReservationID,
|
|
|
|
r.RestrictionID,
|
|
|
|
time.Now(),
|
|
|
|
time.Now())
|
2024-07-08 21:04:17 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
2024-07-09 21:08:31 +00:00
|
|
|
|
2024-07-09 21:50:54 +00:00
|
|
|
// SearchAvailabilityByDatesByRoomID returns true if availability exists for roomID, and false if no availability
|
|
|
|
func (m *postgresDBRepo) SearchAvailabilityByDatesByRoomID(start, end time.Time, roomID int) (bool, error) {
|
2024-07-09 21:08:31 +00:00
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second)
|
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
var numRows int
|
|
|
|
stmt := `select count(id) from room_restrictions
|
|
|
|
where room_id = $1 and
|
|
|
|
$2 < end_date and $3> start_date`
|
|
|
|
|
|
|
|
row := m.DB.QueryRowContext(ctx, stmt, roomID, start, end)
|
|
|
|
err := row.Scan(&numRows)
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if numRows == 0 {
|
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return false, nil
|
|
|
|
}
|
2024-07-09 21:50:54 +00:00
|
|
|
|
|
|
|
// SearchAvailabilityForAllRooms returns a slice of rooms, if any, for given date range
|
|
|
|
func (m *postgresDBRepo) SearchAvailabilityForAllRooms(start, end time.Time) ([]models.Room, error) {
|
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second)
|
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
var rooms []models.Room
|
|
|
|
stmt := `select
|
|
|
|
r.id, r.room_name
|
|
|
|
from
|
|
|
|
rooms r
|
|
|
|
where r.id not in
|
|
|
|
(select
|
2024-07-10 20:08:23 +00:00
|
|
|
room_id
|
2024-07-09 21:50:54 +00:00
|
|
|
from
|
|
|
|
room_restrictions rr
|
|
|
|
where
|
|
|
|
$1 < rr.end_date and $2> rr.start_date)`
|
|
|
|
|
|
|
|
rows, err := m.DB.QueryContext(ctx, stmt, start, end)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
for rows.Next() {
|
|
|
|
var room models.Room
|
|
|
|
|
|
|
|
err := rows.Scan(&room.ID, &room.RoomName)
|
|
|
|
if err != nil {
|
|
|
|
return rooms, err
|
|
|
|
}
|
|
|
|
rooms = append(rooms, room)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err = rows.Err(); err != nil {
|
|
|
|
return rooms, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return rooms, nil
|
|
|
|
}
|
2024-07-10 20:34:27 +00:00
|
|
|
|
|
|
|
// GetRoomById gets a room by id
|
|
|
|
func (m *postgresDBRepo) GetRoomById(id int) (models.Room, error) {
|
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second)
|
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
var room models.Room
|
|
|
|
stmt := `select
|
|
|
|
id, room_name, created_at, updated_at
|
|
|
|
from
|
|
|
|
rooms
|
|
|
|
where id = $1`
|
|
|
|
|
|
|
|
row := m.DB.QueryRowContext(ctx, stmt, id)
|
|
|
|
err := row.Scan(&room.ID, &room.RoomName, &room.CreatedAt, &room.UpdatedAt)
|
|
|
|
if err != nil {
|
|
|
|
return room, err
|
|
|
|
}
|
|
|
|
return room, nil
|
|
|
|
}
|
2024-07-19 07:44:27 +00:00
|
|
|
|
|
|
|
// GetUserByID gets a user by id
|
|
|
|
func (m *postgresDBRepo) GetUserByID(id int) (models.User, error) {
|
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second)
|
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
query := `select id, first_name, last_name, email, password, access_level, created_at, updated_at
|
|
|
|
from users where id = $1`
|
|
|
|
|
|
|
|
row := m.DB.QueryRowContext(ctx, query, id)
|
|
|
|
var u models.User
|
|
|
|
err := row.Scan(
|
|
|
|
&u.ID,
|
|
|
|
&u.FirstName,
|
|
|
|
&u.LastName,
|
|
|
|
&u.Email,
|
|
|
|
&u.Password,
|
|
|
|
&u.AccessLevel,
|
|
|
|
&u.CreatedAt,
|
|
|
|
&u.UpdatedAt,
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return models.User{}, err
|
|
|
|
}
|
|
|
|
return u, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// UpdateUser updates a user in the database
|
|
|
|
func (m *postgresDBRepo) UpdateUser(u models.User) error {
|
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second)
|
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
query := `update users set first_name = $1, last_name = $2, email = $3, access_level = $4, updated_at = $5`
|
|
|
|
|
|
|
|
_, err := m.DB.ExecContext(ctx, query, u.FirstName, u.LastName, u.Email, u.AccessLevel, time.Now())
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Authenticate authenticates a user
|
|
|
|
func (m *postgresDBRepo) Authenticate(email, testPassword string) (int, string, error) {
|
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second)
|
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
var id int
|
|
|
|
var hashedPassword string
|
|
|
|
row := m.DB.QueryRowContext(ctx, "select id, password from users where email = $1", email)
|
|
|
|
err := row.Scan(&id, &hashedPassword)
|
|
|
|
if err != nil {
|
|
|
|
return id, "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
err = bcrypt.CompareHashAndPassword([]byte(hashedPassword), []byte(testPassword))
|
|
|
|
if err == bcrypt.ErrMismatchedHashAndPassword {
|
|
|
|
return 0, "", errors.New("incorrect password")
|
|
|
|
} else if err != nil {
|
|
|
|
return 0, "", err
|
|
|
|
}
|
|
|
|
return id, hashedPassword, nil
|
|
|
|
}
|