Skip to content

Commit

Permalink
better allReservation func
Browse files Browse the repository at this point in the history
  • Loading branch information
ErfiDev committed Dec 4, 2021
1 parent 0bec186 commit bedce18
Showing 1 changed file with 92 additions and 83 deletions.
175 changes: 92 additions & 83 deletions repository/dbrepo/dbrepo.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,35 +3,36 @@ package dbrepo
import (
"context"
"database/sql"
"time"

"github.com/erfidev/hotel-web-app/config"
"github.com/erfidev/hotel-web-app/models"
"github.com/erfidev/hotel-web-app/repository"
"golang.org/x/crypto/bcrypt"
"time"
)

type postgresDbRepo struct {
App *config.AppConfig
DB *sql.DB
}

func NewPostgresRepo(connection *sql.DB ,app *config.AppConfig) repository.DatabaseRepository {
func NewPostgresRepo(connection *sql.DB, app *config.AppConfig) repository.DatabaseRepository {
return &postgresDbRepo{
App : app,
DB: connection,
App: app,
DB: connection,
}
}

func (psdb postgresDbRepo) InsertReservation(reservation models.Reservation) (int , error) {
ctx , cancel := context.WithTimeout(context.Background() , 3 * time.Second)
func (psdb postgresDbRepo) InsertReservation(reservation models.Reservation) (int, error) {
ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second)
defer cancel()

statement := `insert into reservations (first_name, last_name, email, phone, start_date, end_date,
room_id, created_at, updated_at) values ($1,$2,$3,$4,$5,$6,$7,$8,$9) returning id
`
var newId int

err :=psdb.DB.QueryRowContext(
err := psdb.DB.QueryRowContext(
ctx,
statement,
reservation.FirstName,
Expand All @@ -42,26 +43,26 @@ func (psdb postgresDbRepo) InsertReservation(reservation models.Reservation) (in
reservation.EndDate,
reservation.RoomId,
time.Now(),
time.Now(),
time.Now(),
).Scan(&newId)

if err != nil {
return 0 , err
return 0, err
}
return newId , nil

return newId, nil
}

func (psdb postgresDbRepo) InsertRoomRestriction(roomRestriction models.RoomRestriction) error {
ctx , cancel := context.WithTimeout(context.Background() , 3 * time.Second)
ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second)
defer cancel()

statement := `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)`

_ , err := psdb.DB.ExecContext(
ctx ,
_, err := psdb.DB.ExecContext(
ctx,
statement,
roomRestriction.StartDate,
roomRestriction.EndDate,
Expand All @@ -79,8 +80,8 @@ func (psdb postgresDbRepo) InsertRoomRestriction(roomRestriction models.RoomRest
return nil
}

func (psdb postgresDbRepo) SearchAvailability(roomId int ,start , end time.Time) (bool , error) {
ctx , cancel := context.WithTimeout(context.Background() , 3 * time.Second)
func (psdb postgresDbRepo) SearchAvailability(roomId int, start, end time.Time) (bool, error) {
ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second)
defer cancel()

var numRows int
Expand All @@ -97,28 +98,28 @@ func (psdb postgresDbRepo) SearchAvailability(roomId int ,start , end time.Time)
)
err := row.Scan(&numRows)
if err != nil {
return false , err
return false, err
}

if numRows == 0 {
return true , nil
return true, nil
}

return false , nil
return false, nil
}

func (psdb postgresDbRepo) SearchAvailabilityForAllRooms(start , end time.Time) ([]models.Room , error) {
ctx , cancel := context.WithTimeout(context.Background() , 3 * time.Second)
func (psdb postgresDbRepo) SearchAvailabilityForAllRooms(start, end time.Time) ([]models.Room, error) {
ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second)
defer cancel()

statement := `select id , room_name from rooms
where id not in (select room_id from room_restrictions where $1 < end_date and $2 > start_date)`

var rooms []models.Room

rows , err := psdb.DB.QueryContext(ctx , statement , start,end)
rows, err := psdb.DB.QueryContext(ctx, statement, start, end)
if err != nil {
return rooms , err
return rooms, err
}

for rows.Next() {
Expand All @@ -129,31 +130,30 @@ func (psdb postgresDbRepo) SearchAvailabilityForAllRooms(start , end time.Time)
)

if err != nil {
return rooms , err
return rooms, err
}

rooms = append(rooms , room)
rooms = append(rooms, room)
}

if err = rows.Err(); err != nil {
return rooms , err
return rooms, err
}


return rooms , nil
return rooms, nil
}

func (psdb postgresDbRepo) FindRoomById(id int) (models.Room,error){
ctx , cancel := context.WithTimeout(context.Background() , 3 * time.Second)
func (psdb postgresDbRepo) FindRoomById(id int) (models.Room, error) {
ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second)
defer cancel()

statement := `select id, room_name, updated_at, created_at from rooms where id = $1`

var room models.Room

row := psdb.DB.QueryRowContext(ctx, statement , id)
row := psdb.DB.QueryRowContext(ctx, statement, id)
if err := row.Err(); err != nil {
return room , err
return room, err
}

err := row.Scan(
Expand All @@ -163,44 +163,43 @@ func (psdb postgresDbRepo) FindRoomById(id int) (models.Room,error){
&room.CreatedAt,
)
if err != nil {
return room , err
return room, err
}

return room , nil
return room, nil
}

func (psdb postgresDbRepo) InsertUser(user models.User) (bool , error) {
ctx , cancel := context.WithTimeout(context.Background() , 3 * time.Second)
func (psdb postgresDbRepo) InsertUser(user models.User) (bool, error) {
ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second)
defer cancel()


query := `
insert into users (first_name , last_name , email , password , access_level)
values ($1 , $2 , $3 , $4 , $5);
`

hashPass , err := bcrypt.GenerateFromPassword([]byte(user.Password) , 10)
hashPass, err := bcrypt.GenerateFromPassword([]byte(user.Password), 10)
if err != nil {
return false , err
return false, err
}

_ , err = psdb.DB.ExecContext(ctx , query ,
user.FirstName ,
user.LastName ,
user.Email ,
hashPass ,
_, err = psdb.DB.ExecContext(ctx, query,
user.FirstName,
user.LastName,
user.Email,
hashPass,
user.AccessLevel,
)

if err != nil {
return false , err
return false, err
}

return true , nil
return true, nil
}

func (psdb postgresDbRepo) GetUserById(id int) (models.User , error) {
ctx , cancel := context.WithTimeout(context.Background() , 3 * time.Second)
func (psdb postgresDbRepo) GetUserById(id int) (models.User, error) {
ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second)
defer cancel()

query := `
Expand All @@ -210,7 +209,7 @@ func (psdb postgresDbRepo) GetUserById(id int) (models.User , error) {

var user models.User

row := psdb.DB.QueryRowContext(ctx , query , id)
row := psdb.DB.QueryRowContext(ctx, query, id)
err := row.Scan(
&user.ID,
&user.FirstName,
Expand All @@ -221,14 +220,14 @@ func (psdb postgresDbRepo) GetUserById(id int) (models.User , error) {
)

if err != nil {
return models.User{} , err
return models.User{}, err
}

return user , nil
return user, nil
}

func (psdb postgresDbRepo) Authenticate(email , password string) (int, bool , string) {
ctx , cancel := context.WithTimeout(context.Background() , 3 * time.Second)
func (psdb postgresDbRepo) Authenticate(email, password string) (int, bool, string) {
ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second)
defer cancel()

query := `
Expand All @@ -239,38 +238,38 @@ func (psdb postgresDbRepo) Authenticate(email , password string) (int, bool , st
var pass string
var id int

row := psdb.DB.QueryRowContext(ctx , query , email)
row := psdb.DB.QueryRowContext(ctx, query, email)

err := row.Scan(&id , &pass)
err := row.Scan(&id, &pass)
if err != nil {
return 0 ,false , "can't find user with this email!"
return 0, false, "can't find user with this email!"
}

// check passwords
hashError := bcrypt.CompareHashAndPassword([]byte(pass) , []byte(password))
hashError := bcrypt.CompareHashAndPassword([]byte(pass), []byte(password))
if hashError == bcrypt.ErrMismatchedHashAndPassword {
return 0 ,false , "mismatched hash and password"
return 0, false, "mismatched hash and password"
} else if hashError != nil {
return 0 ,false , "unexpected error"
return 0, false, "unexpected error"
}

return id , true , ""
return id, true, ""
}

func (psdb postgresDbRepo) UpdateUser(user models.User) (bool , error) {
ctx , cancel := context.WithTimeout(context.Background() , 3 * time.Second)
func (psdb postgresDbRepo) UpdateUser(user models.User) (bool, error) {
ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second)
defer cancel()

query := `
update users set first_name = $1, last_name = $2, email = $3, password = $4 , access_level = $5
`

hashPass , err := bcrypt.GenerateFromPassword([]byte(user.Password) , 10)
if err != nil {
return false , err
hashPass, err := bcrypt.GenerateFromPassword([]byte(user.Password), 10)
if err != nil {
return false, err
}

_ , errOnExec := psdb.DB.ExecContext(ctx , query ,
_, errOnExec := psdb.DB.ExecContext(ctx, query,
user.FirstName,
user.LastName,
user.Email,
Expand All @@ -279,42 +278,52 @@ func (psdb postgresDbRepo) UpdateUser(user models.User) (bool , error) {
)

if errOnExec != nil {
return false , errOnExec
return false, errOnExec
}

return true , nil
return true, nil
}

func (psdb postgresDbRepo) AllReservations() ([]models.Reservation , error) {
ctx , cancel := context.WithTimeout(context.Background() , 3 * time.Second)
func (psdb postgresDbRepo) AllReservations() ([]models.Reservation, error) {
ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second)
defer cancel()

query := `
select rr.start_date, rr.end_date , re.email
from room_restrictions rr
left join rooms r on (rr.room_id = r.id)
left join reservations re on (rr.reservation_id = re.id);
select r.start_date, r.end_date , r.email ,
r.first_name , r.last_name , r.phone , r.created_at,
r.updated_at , rm.room_name , rm.id
from reservations r
left join rooms rm on (r.room_id = rm.id)
order by r.start_date asc;
`

var reservations []models.Reservation

rows , err := psdb.DB.QueryContext(ctx , query)
rows, err := psdb.DB.QueryContext(ctx, query)
defer rows.Close()
if err != nil {
return reservations , err
return reservations, err
}

for rows.Next() {
res := models.Reservation{}
_ = rows.Scan(
err := rows.Scan(
&res.StartDate,
&res.EndDate,
&res.Email,
&res.FirstName,
&res.LastName,
&res.Phone,
&res.CreatedAt,
&res.UpdatedAt,
&res.Room.RoomName,
&res.RoomId,
)

reservations = append(reservations , res)
if err != nil {
return reservations, err
}
reservations = append(reservations, res)
}

return reservations , nil
return reservations, nil
}


0 comments on commit bedce18

Please sign in to comment.