Compare commits
No commits in common. "74e9fee9420c126b0c88086bf7b9e83627d8d888" and "2ae5d9a2c286a703f1978a5bbf116444a71e0e56" have entirely different histories.
74e9fee942
...
2ae5d9a2c2
@ -65,7 +65,7 @@ func run() (*driver.DB, error) {
|
|||||||
|
|
||||||
// connect to database
|
// connect to database
|
||||||
log.Println("Connecting to database...")
|
log.Println("Connecting to database...")
|
||||||
dsn := fmt.Sprintf("host=localhost port=5432 dbname=bookings user=%s password=%s", os.Getenv("PGUSER"), os.Getenv("PGPWD"))
|
dsn := fmt.Sprintf("host=localhost port=5432 dbname=test_connect user=%s password=%s", os.Getenv("PGUSER"), os.Getenv("PGPWD"))
|
||||||
db, err := driver.ConnectSQL(dsn)
|
db, err := driver.ConnectSQL(dsn)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal("Cannot connect to database! Dying...")
|
log.Fatal("Cannot connect to database! Dying...")
|
||||||
|
@ -95,18 +95,15 @@ func (m *Repository) PostMakeReservation(w http.ResponseWriter, r *http.Request)
|
|||||||
startDate, err := time.Parse(layout, sd)
|
startDate, err := time.Parse(layout, sd)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
helpers.ServerError(w, err)
|
helpers.ServerError(w, err)
|
||||||
return
|
|
||||||
}
|
}
|
||||||
endDate, err := time.Parse(layout, ed)
|
endDate, err := time.Parse(layout, ed)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
helpers.ServerError(w, err)
|
helpers.ServerError(w, err)
|
||||||
return
|
|
||||||
}
|
}
|
||||||
|
|
||||||
roomId, err := strconv.Atoi(r.Form.Get("room_id"))
|
roomId, err := strconv.Atoi(r.Form.Get("room_id"))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
helpers.ServerError(w, err)
|
helpers.ServerError(w, err)
|
||||||
return
|
|
||||||
}
|
}
|
||||||
|
|
||||||
reservation := models.Reservation{
|
reservation := models.Reservation{
|
||||||
@ -136,25 +133,9 @@ func (m *Repository) PostMakeReservation(w http.ResponseWriter, r *http.Request)
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
newReservationID, err := m.DB.InsertReservation(reservation)
|
err = m.DB.InsertReservation(reservation)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
helpers.ServerError(w, err)
|
helpers.ServerError(w, err)
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
restriction := models.RoomRestriction{
|
|
||||||
StartDate: startDate,
|
|
||||||
EndDate: endDate,
|
|
||||||
ID: 0,
|
|
||||||
RoomID: roomId,
|
|
||||||
ReservationID: newReservationID,
|
|
||||||
RestrictionID: 1,
|
|
||||||
}
|
|
||||||
|
|
||||||
err = m.DB.InsertRoomRestriction(restriction)
|
|
||||||
if err != nil {
|
|
||||||
helpers.ServerError(w, err)
|
|
||||||
return
|
|
||||||
}
|
}
|
||||||
|
|
||||||
m.App.Session.Put(r.Context(), "reservation", reservation)
|
m.App.Session.Put(r.Context(), "reservation", reservation)
|
||||||
|
@ -55,6 +55,6 @@ type RoomRestriction struct {
|
|||||||
Restriction Restriction
|
Restriction Restriction
|
||||||
ID int
|
ID int
|
||||||
RoomID int
|
RoomID int
|
||||||
ReservationID int
|
ReservationId int
|
||||||
RestrictionID int
|
RestrictionID int
|
||||||
}
|
}
|
||||||
|
@ -11,53 +11,16 @@ func (m *postgresDBRepo) AllUsers() bool {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// InsertReservation inserts a reservation into the database
|
// InsertReservation inserts a reservation into the database
|
||||||
func (m *postgresDBRepo) InsertReservation(res models.Reservation) (int, error) {
|
func (m *postgresDBRepo) InsertReservation(res models.Reservation) error {
|
||||||
ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second)
|
ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second)
|
||||||
defer cancel()
|
defer cancel()
|
||||||
|
|
||||||
var newId int
|
|
||||||
// statement
|
// statement
|
||||||
stmt := `insert into reservations (first_name, last_name, email, phone,
|
stmt := `insert into reservations (first_name, last_name, email, phone,
|
||||||
start_date, end_date, room_id, created_at, updated_at)
|
start_date, end_date, room_id, created_at, updated_at)
|
||||||
values ($1, $2, $3, $4, $5, $6, $7, $8, $9) returning id`
|
values ($1, $2, $3, $4, $5, $6, $7, $8, $9)`
|
||||||
|
|
||||||
row := m.DB.QueryRowContext(ctx, stmt,
|
_, err := m.DB.ExecContext(ctx, stmt, res.FirstName, res.LastName, res.Email, res.Phone,
|
||||||
res.FirstName,
|
res.StartDate, res.EndDate, res.RoomID, res.CreatedAt, res.UpdatedAt)
|
||||||
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)`
|
|
||||||
|
|
||||||
_, err := m.DB.ExecContext(ctx, stmt,
|
|
||||||
r.StartDate,
|
|
||||||
r.EndDate,
|
|
||||||
r.RoomID,
|
|
||||||
r.ReservationID,
|
|
||||||
r.RestrictionID,
|
|
||||||
time.Now(),
|
|
||||||
time.Now())
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
@ -5,6 +5,5 @@ import "go-udemy-web-1/internal/models"
|
|||||||
type DatabaseRepo interface {
|
type DatabaseRepo interface {
|
||||||
AllUsers() bool
|
AllUsers() bool
|
||||||
|
|
||||||
InsertReservation(res models.Reservation) (int, error)
|
InsertReservation(res models.Reservation) error
|
||||||
InsertRoomRestriction(res models.RoomRestriction) error
|
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user