Skip to content

Commit

Permalink
create /admin/reservations route and handler
Browse files Browse the repository at this point in the history
  • Loading branch information
ErfiDev committed Dec 4, 2021
1 parent a670f75 commit d571101
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 36 deletions.
19 changes: 18 additions & 1 deletion controllers/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -468,7 +468,7 @@ func (r Repository) AdminDashboard(res http.ResponseWriter, req *http.Request) {
utils.RenderTemplate(res, req, "admin-dashboard.page.gohtml", &models.TmpData{
Data: map[string]interface{}{
"title": "Admin dashboard",
// "path": "/admin/dashboard",
"path": "/dashboard",
},
})
}
Expand Down Expand Up @@ -500,3 +500,20 @@ func (r Repository) AllReservationsApi(res http.ResponseWriter, req *http.Reques
res.Header().Add("Content-Type", "application/json; charset=utf8")
res.Write(toJson)
}

func (r Repository) AdminReservations(res http.ResponseWriter, req *http.Request) {
reservations, err := r.DB.AllReservations()
if err != nil {
utils.ServerError(res, err)
return
}

data := make(map[string]interface{})
data["reservations"] = reservations
data["title"] = "all reservations"
data["path"] = "/reservations"

utils.RenderTemplate(res, req, "admin-reservations.page.gohtml", &models.TmpData{
Data: data,
})
}
50 changes: 26 additions & 24 deletions routes/routes.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
package routes

import (
"net/http"

"github.com/erfidev/hotel-web-app/config"
"github.com/erfidev/hotel-web-app/controllers"
"github.com/erfidev/hotel-web-app/models"
"github.com/erfidev/hotel-web-app/utils"
"github.com/go-chi/chi"
"github.com/go-chi/chi/middleware"
"net/http"
)

var appConfig *config.AppConfig
Expand All @@ -24,45 +25,46 @@ func Routes() http.Handler {
router.Use(NoSurf)
router.Use(ServeSession)

router.Get("/" , controllers.Repo.Home)
router.Get("/about" , controllers.Repo.About)
for _ , route := range []string{"/rooms" , "/rooms/generals" , "/rooms/majors"} {
router.Get(route , controllers.Repo.Rooms)
router.Get("/", controllers.Repo.Home)
router.Get("/about", controllers.Repo.About)
for _, route := range []string{"/rooms", "/rooms/generals", "/rooms/majors"} {
router.Get(route, controllers.Repo.Rooms)
}
router.Get("/book-now" , controllers.Repo.BookNow)
router.Get("/contact" , controllers.Repo.Contact)
router.Get("/make-reservation" , controllers.Repo.MakeReservation)
router.Get("/book-now", controllers.Repo.BookNow)
router.Get("/contact", controllers.Repo.Contact)
router.Get("/make-reservation", controllers.Repo.MakeReservation)
fileServer := http.FileServer(http.Dir("./static"))
router.Handle("/static/*" , http.StripPrefix("/static" , fileServer))
router.Get("/reservation-summary" , controllers.Repo.ReservationSummary)
router.Get("/choose-room/{id}" , controllers.Repo.ChooseRoom)
router.Get("/login" , controllers.Repo.Login)
router.Get("/logout" , controllers.Repo.Logout)
router.Route("/admin" , func (mux chi.Router) {
router.Handle("/static/*", http.StripPrefix("/static", fileServer))
router.Get("/reservation-summary", controllers.Repo.ReservationSummary)
router.Get("/choose-room/{id}", controllers.Repo.ChooseRoom)
router.Get("/login", controllers.Repo.Login)
router.Get("/logout", controllers.Repo.Logout)
router.Route("/admin", func(mux chi.Router) {
mux.Use(Authenticate)
mux.Get("/dashboard" , controllers.Repo.AdminDashboard)
mux.Get("/dashboard", controllers.Repo.AdminDashboard)
mux.Get("/reservations", controllers.Repo.AdminReservations)
})

// POST routes
router.Post("/book-now" , controllers.Repo.BookNowPost)
router.Post("/make-reservation" , controllers.Repo.MakeReservationPost)
router.Post("/search-availability" , controllers.Repo.SearchAvailability)
router.Post("/user/login" , controllers.Repo.LoginPost)
router.Post("/book-now", controllers.Repo.BookNowPost)
router.Post("/make-reservation", controllers.Repo.MakeReservationPost)
router.Post("/search-availability", controllers.Repo.SearchAvailability)
router.Post("/user/login", controllers.Repo.LoginPost)

// Api GET routes
router.Get("/api/allReservations" , controllers.Repo.AllReservationsApi)
router.Get("/api/allReservations", controllers.Repo.AllReservationsApi)

// Custom 404 page
router.NotFound(NotFound)

return router
}

func NotFound(res http.ResponseWriter , req *http.Request) {
utils.RenderTemplate(res , req , "404.page.gohtml" , &models.TmpData{
func NotFound(res http.ResponseWriter, req *http.Request) {
utils.RenderTemplate(res, req, "404.page.gohtml", &models.TmpData{
Data: map[string]interface{}{
"title": "page not found",
"path": "/404",
"path": "/404",
},
})
}
}
2 changes: 1 addition & 1 deletion static/css/admin.css
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ body {

.admin-content-section {
width: 100%;
min-height: 70vh;
min-height: 80vh;
height: auto;
display: grid;
grid-template-columns: 1fr 4fr;
Expand Down
2 changes: 1 addition & 1 deletion views/admin-reservations.page.gohtml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{{template "admin" .}}

{{define "admin-subpage"}}
{{define "adminsubpage"}}
<div class="admin-content-reservations">

</div>
Expand Down
39 changes: 30 additions & 9 deletions views/admin.layout.gohtml
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,37 @@
</div>
<div class="admin-content-section">
<div class="admin-sidebar">
{{$path := .Data.path}}
<ul class="admin-sidebar-list">
<li class="admin-sidebar-item">
<a href="/admin/reservations">Reservations</a>
</li>
<li class="admin-sidebar-item">
<a href="/admin/admins">Admins</a>
</li>
<li class="admin-sidebar-item">
<a href="/admin/setting">Setting</a>
</li>
{{if eq $path "reservations"}}
<li class="admin-sidebar-item used">
<a href="/admin/reservations">Reservations</a>
</li>
{{else}}
<li class="admin-sidebar-item">
<a href="/admin/reservations">Reservations</a>
</li>
{{end}}
{{if eq $path "/admins"}}
<li class="admin-sidebar-item used">
<a href="/admin/admins">Admins</a>
</li>
{{else}}
<li class="admin-sidebar-item">
<a href="/admin/admins">Admins</a>
</li>
{{end}}
{{if eq $path "/setting"}}
<li class="admin-sidebar-item used">
<a href="/admin/setting">Setting</a>
</li>
{{else}}
<li class="admin-sidebar-item">
<a href="/admin/setting">Setting</a>
</li>
{{end}}
{{if eq $path "/dashboard"}}
{{end}}
</ul>
</div>
<div class="admin-content">
Expand Down

0 comments on commit d571101

Please sign in to comment.