Skip to content

Commit

Permalink
Reservation Calendar II
Browse files Browse the repository at this point in the history
  • Loading branch information
Pomog committed Dec 31, 2023
1 parent bd76ad2 commit 0a78294
Show file tree
Hide file tree
Showing 6 changed files with 118 additions and 9 deletions.
22 changes: 22 additions & 0 deletions internal/handlers/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -545,6 +545,9 @@ func (m *Repository) AdminCalendarReservations(w http.ResponseWriter, r *http.Re
now = time.Date(year, time.Month(month), 1, 0, 0, 0, 0, time.UTC)
}

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

next := now.AddDate(0, 1, 0)
last := now.AddDate(0, -1, 0)

Expand All @@ -563,8 +566,27 @@ func (m *Repository) AdminCalendarReservations(w http.ResponseWriter, r *http.Re
stringMap["this_month"] = now.Format("01")
stringMap["this_month_year"] = now.Format("2006")

//get the first and last days of the month
currentYear, cirrentMonth, _ := now.Date()
currentLocation := now.Location()
firstOfMonth := time.Date(currentYear, cirrentMonth, 1, 0, 0, 0, 0, currentLocation)
lastOfMonth := firstOfMonth.AddDate(0, 1, -1)

intMap :=make(map[string]int)
intMap["days_in_month"] = lastOfMonth.Day()

rooms, err := m.DB.AllRooms()
if err != nil {
helpers.ServerError(w, err)
return
}

data["rooms"] = rooms

render.Template(w, r, "admin-reservations-calendar.page.tmpl", &models.TemplateData{
StringMap: stringMap,
Data: data,
IntMap: intMap,
})
}

Expand Down
19 changes: 18 additions & 1 deletion internal/render/render.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,32 @@ import (

var app *config.AppConfig
var pathToTemplates = "./templates"

var functions = template.FuncMap{
"dateformate": HTMLFormateDate,
"formatDate": FormatDate,
"iterate": Iterate,
}

// returtns time as string YYYY-MM-DD
func HTMLFormateDate (t time.Time) string{
func HTMLFormateDate(t time.Time) string {
return t.Format("2006-01-02")
}

func FormatDate(t time.Time, f string) string {
return t.Format(f)
}

// Iterate returns a slice of ints starting at 1, going to count
func Iterate(count int) []int {
var i int
var items []int
for i = 1; i <= count; i++ {
items = append(items, i)
}
return items
}

// NewRenderer creates a new template cache
func NewRenderer(a *config.AppConfig) {
app = a
Expand Down
46 changes: 41 additions & 5 deletions internal/repository/dbrepo/postgres.go
Original file line number Diff line number Diff line change
Expand Up @@ -379,7 +379,7 @@ func (m *postgresDBRepo) GetReservationByID(id int) (models.Reservation, error)
}

// UpdateReservation updates the reservation by reservation model in the database
func (m *postgresDBRepo) UpdateReservation (r models.Reservation) error {
func (m *postgresDBRepo) UpdateReservation(r models.Reservation) error {
ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second)
defer cancel()

Expand All @@ -402,9 +402,8 @@ func (m *postgresDBRepo) UpdateReservation (r models.Reservation) error {
return nil
}


// DeleteReservation delete the reservation by ID
func (m *postgresDBRepo) DeleteReservation (id int) error {
func (m *postgresDBRepo) DeleteReservation(id int) error {
ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second)
defer cancel()

Expand All @@ -421,7 +420,7 @@ func (m *postgresDBRepo) DeleteReservation (id int) error {
}

// UpdateProcessedForReservation updates processed for a reservation by ID
func (m *postgresDBRepo) UpdateProcessedForReservation (id, processed int) error {
func (m *postgresDBRepo) UpdateProcessedForReservation(id, processed int) error {
ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second)
defer cancel()

Expand All @@ -435,4 +434,41 @@ func (m *postgresDBRepo) UpdateProcessedForReservation (id, processed int) error
return err
}
return nil
}
}

func (m *postgresDBRepo) AllRooms() ([]models.Room, error) {
ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second)
defer cancel()

var rooms []models.Room

query := `
select id, room_name, created_at, updated_at from rooms order by room_name
`

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

for rows.Next() {
var rm models.Room
err := rows.Scan(
&rm.ID,
&rm.RoomName,
&rm.CreatedAt,
&rm.UpdatedAt,
)
if err != nil {
return rooms, err
}
rooms = append(rooms, rm)
}

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

return rooms, nil
}
9 changes: 7 additions & 2 deletions internal/repository/dbrepo/testRepo.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,11 +84,16 @@ func (m *testDBRepo) UpdateReservation(r models.Reservation) error {
}

// DeleteReservation delete the reservation by ID
func (m *testDBRepo) DeleteReservation (id int) error {
func (m *testDBRepo) DeleteReservation(id int) error {
return nil
}

// UpdateProcessedForReservation updates processed for a reservation by ID
func (m *testDBRepo) UpdateProcessedForReservation (id, processed int) error {
func (m *testDBRepo) UpdateProcessedForReservation(id, processed int) error {
return nil
}

func (m *testDBRepo) AllRooms() ([]models.Room, error) {
var rooms []models.Room
return rooms, nil
}
1 change: 1 addition & 0 deletions internal/repository/repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (

type DatabaseRepo interface {
AllUsers() bool
AllRooms() ([]models.Room, error)

InserReservation(res models.Reservation) (int, error)
InsertRoomRestriction(r models.RoomRestriction) error
Expand Down
30 changes: 29 additions & 1 deletion templates/admin-reservations-calendar.page.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,14 @@
{{end}}

{{define "content"}}
{{$now := index .Data "now"}}
{{$rooms := index .Data "rooms"}}
{{$dim:= index .IntMap "days_in_month"}}
<div class="col-md-12">
Reservations Calendar content

<div class="text-center">
<h3>{{index .StringMap "this_month"}} {{index .StringMap "this_month_year"}}</h3>
<h3>{{formatDate $now "January"}} {{formatDate $now "2006"}}</h3>
</div>

<div class="float-left">
Expand All @@ -26,5 +29,30 @@
</a>
</div>
<div class="clearfix"></div>

{{range $rooms}}
{{$roomID := .ID}}
<h4 class="mt-4">{{.RoomName}}</h4>
<div class="table-response">
<table class="table table-bordered table-sm">
<tr class="table-dark">
{{range $index := iterate $dim}}
<td class="text-center">
{{$index}}
</td>
{{end}}
</tr>
<tr>
{{range $index := iterate $dim}}
<td class="text-center">
<input type="checkbox">
</td>
{{end}}
<tr>
</table>
</div>

{{end}}

</div>
{{end}}

0 comments on commit 0a78294

Please sign in to comment.