Skip to content

Commit

Permalink
new reservations
Browse files Browse the repository at this point in the history
  • Loading branch information
Pomog committed Dec 28, 2023
1 parent 2bd2089 commit 75651bb
Show file tree
Hide file tree
Showing 9 changed files with 159 additions and 21 deletions.
26 changes: 15 additions & 11 deletions internal/handlers/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -418,18 +418,10 @@ func (m *Repository) BookRoom(w http.ResponseWriter, r *http.Request) {
sd := r.URL.Query().Get("s")
ed := r.URL.Query().Get("e")

fmt.Println("BookRoom sd", sd)
fmt.Println("BookRoom ed", ed)

layout := "2006-01-02"
startDate, _ := time.Parse(layout, sd)
endDate, _ := time.Parse(layout, ed)

fmt.Println("BookRoom startDate", startDate)
fmt.Println("BookRoom endDate", endDate)

fmt.Println(endDate)

var res models.Reservation

room, err := m.DB.GetRoomById(roomID)
Expand All @@ -448,6 +440,7 @@ func (m *Repository) BookRoom(w http.ResponseWriter, r *http.Request) {
http.Redirect(w, r, "/make-reservation", http.StatusSeeOther)
}

// ShowLogin shows the login screen
func (m *Repository) ShowLogin(w http.ResponseWriter, r *http.Request) {
render.Template(w, r, "login.page.tmpl", &models.TemplateData{
Form: forms.New(nil),
Expand Down Expand Up @@ -505,15 +498,26 @@ func (m *Repository) PostShowLogin(w http.ResponseWriter, r *http.Request) {
}

func (m *Repository) AdminDashBoard(w http.ResponseWriter, r *http.Request) {
fmt.Println("inside AdminDashBoard")
render.Template(w, r, "admin-dashboard.page.tmpl", &models.TemplateData{})
}

// AdminNewReservations shows all new reservations in admin tool
func (m *Repository) AdminNewReservations(w http.ResponseWriter, r *http.Request) {
fmt.Println("inside AdminNewReservations")
render.Template(w, r, "admin-new-reservations.page.tmpl", &models.TemplateData{})
reservations, err := m.DB.AllNewReservations()
if err != nil{
helpers.ServerError(w, err)
return
}

data := make(map[string]interface{})
data["reservations"] = reservations

render.Template(w, r, "admin-new-reservations.page.tmpl", &models.TemplateData{
Data: data,
})
}

// AdminAllReservations shows all reservations in admin tool
func (m *Repository) AdminAllReservations(w http.ResponseWriter, r *http.Request) {
reservations, err := m.DB.AllReservations()
if err != nil{
Expand Down
9 changes: 5 additions & 4 deletions internal/models/models.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ type Reservation struct {
CreatedAt time.Time
UpdatedAt time.Time
Room Room
Processed int
}

// RoomRestriction is the model for the room_restrictions table in the database
Expand All @@ -64,9 +65,9 @@ type RoomRestriction struct {

// MailData holds an email message
type MailData struct {
To string
From string
Subject string
Content string
To string
From string
Subject string
Content string
Template string
}
58 changes: 57 additions & 1 deletion internal/repository/dbrepo/postgres.go
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,7 @@ func (m *postgresDBRepo) AllReservations() ([]models.Reservation, error){

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,
r.start_date, r.end_date, r.room_id, r.created_at, r.updated_at, r.processed,
rm.id, rm.room_name
from reservations r
left join rooms rm
Expand Down Expand Up @@ -267,6 +267,7 @@ func (m *postgresDBRepo) AllReservations() ([]models.Reservation, error){
&i.RoomID,
&i.CreatedAt,
&i.UpdatedAt,
&i.Processed,
&i.Room.ID,
&i.Room.RoomName,
)
Expand All @@ -285,3 +286,58 @@ func (m *postgresDBRepo) AllReservations() ([]models.Reservation, error){
return reservations, nil
}

// AllNewReservations returns a slice of all NON processed reservations
func (m *postgresDBRepo) AllNewReservations() ([]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)
where processed = 0
order by r.start_date asc
`

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

defer rows.Close()

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)
}

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

return reservations, nil
}
8 changes: 8 additions & 0 deletions internal/repository/dbrepo/testRepo.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,16 @@ func (m *testDBRepo) Autenticate(email, testPassword string) (int, string, error
return 0, "", nil
}

// AllReservations returns a slice of all reservations
func (m *testDBRepo) AllReservations() ([]models.Reservation, error) {
var reservations []models.Reservation

return reservations, nil
}

// AllNewReservations returns a slice of all NON processed reservations
func (m *testDBRepo) AllNewReservations() ([]models.Reservation, error){
var reservations []models.Reservation

return reservations, nil
}
4 changes: 4 additions & 0 deletions internal/repository/repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,17 @@ import (

type DatabaseRepo interface {
AllUsers() bool

InserReservation(res models.Reservation) (int, error)
InsertRoomRestriction(r models.RoomRestriction) error
SearchAvailabilityByDatesByRoomID(start, end time.Time, roomID int) (bool, error)
SearchAvailabilityForAllRooms(start, end time.Time) ([]models.Room, error)
GetRoomById(id int) (models.Room, error)

GetUserByID(id int) (models.User, error)
UpdateUser(u models.User) (error)
Autenticate(email, testPassword string) (int, string, error)

AllReservations() ([]models.Reservation, error)
AllNewReservations() ([]models.Reservation, error)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
drop_column("reservations", "processed")
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
add_column("reservations", "processed", "integer", {"default": 0})
7 changes: 4 additions & 3 deletions migrations/schema.sql
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
-- PostgreSQL database dump
--

-- Dumped from database version 14.9 (Ubuntu 14.9-0ubuntu0.22.04.1)
-- Dumped by pg_dump version 14.9 (Ubuntu 14.9-0ubuntu0.22.04.1)
-- Dumped from database version 14.10 (Ubuntu 14.10-0ubuntu0.22.04.1)
-- Dumped by pg_dump version 14.10 (Ubuntu 14.10-0ubuntu0.22.04.1)

SET statement_timeout = 0;
SET lock_timeout = 0;
Expand Down Expand Up @@ -34,7 +34,8 @@ CREATE TABLE public.reservations (
end_date date NOT NULL,
room_id integer NOT NULL,
created_at timestamp without time zone NOT NULL,
updated_at timestamp without time zone NOT NULL
updated_at timestamp without time zone NOT NULL,
processed integer DEFAULT 0 NOT NULL
);


Expand Down
66 changes: 64 additions & 2 deletions templates/admin-new-reservations.page.tmpl
Original file line number Diff line number Diff line change
@@ -1,11 +1,73 @@
{{template "admin" .}}

{{define "css"}}
<link href="https://cdn.jsdelivr.net/npm/simple-datatables@latest/dist/style.css" rel="stylesheet" type="text/css">
{{end}}

{{define "page-title"}}
New Reservations
All Reservations
{{end}}

{{define "content"}}
<div class="col-md-12">
New Reservations content
<h2>{{template "page-title" .}}</h2>
{{ $res := index .Data "reservations"}}
<div class="table-responsive">

<table class="table table-striped table-hover" id="new-res">
<thead>
<tr>
<th>ID</th>
<th>New</th>
<th>First Name</th>
<th>Last Name</th>
<th>Email</th>
<th>Phone</th>
<th>Start Date</th>
<th>End Date</th>
<th>Room ID</th>
<th>Created At</th>
<th>Updated At</th>
<th>Room ID</th>
<th>Room Name</th>
</tr>
</thead>
<tbody>
{{range $i, $reservation := $res}}
<tr>
<td>{{$reservation.ID}}</td>
<td>{{$reservation.Processed}}</td>
<td>{{$reservation.FirstName}}</td>
<td>
<a href="/admin/reservations/new/{{$reservation.ID}}">
{{$reservation.LastName}}
</a>
</td>
<td>{{$reservation.Email}}</td>
<td>{{$reservation.Phone}}</td>
<td>{{dateformate $reservation.StartDate}}</td>
<td>{{dateformate $reservation.EndDate}}</td>
<td>{{$reservation.RoomID}}</td>
<td>{{dateformate $reservation.CreatedAt}}</td>
<td>{{dateformate $reservation.UpdatedAt}}</td>
<td>{{$reservation.Room.ID}}</td>
<td>{{$reservation.Room.RoomName}}</td>
</tr>
{{end}}
</tbody>
</table>
</div>
</div>
{{end}}

{{define "js"}}
<script src="https://cdn.jsdelivr.net/npm/simple-datatables@latest" type="text/javascript"></script>

<script>
document.addEventListener("DOMContentLoaded", function(){
const dataTable = new simpleDatatables.DataTable("#new-res", {
select: 5, sort: "desc",
})
})
</script>
{{end}}

0 comments on commit 75651bb

Please sign in to comment.