Cleaning up our code

This commit is contained in:
vinchent 2024-07-07 22:37:48 +02:00
parent 44385fbbce
commit 18c82ba19f
6 changed files with 22 additions and 19 deletions

View File

@ -48,6 +48,9 @@ func main() {
func run() (*driver.DB, error) { func run() (*driver.DB, error) {
// what am I going to put in the session // what am I going to put in the session
gob.Register(models.Reservation{}) gob.Register(models.Reservation{})
gob.Register(models.User{})
gob.Register(models.Room{})
gob.Register(models.Restriction{})
// change this to true when in production // change this to true when in production
app.InProduction = false app.InProduction = false
@ -87,6 +90,6 @@ func run() (*driver.DB, error) {
handlers.NewHandlers(repo) handlers.NewHandlers(repo)
helpers.NewHelpers(&app) helpers.NewHelpers(&app)
render.NewTemplates(&app) render.NewRenderer(&app)
return db, nil return db, nil
} }

View File

@ -3,7 +3,7 @@ package main
import "testing" import "testing"
func TestRun(t *testing.T) { func TestRun(t *testing.T) {
err := run() _, err := run()
if err != nil { if err != nil {
t.Error("failed run()") t.Error("failed run()")
} }

View File

@ -38,28 +38,28 @@ func NewHandlers(r *Repository) {
// Home is the home page handler // Home is the home page handler
func (m *Repository) Home(w http.ResponseWriter, r *http.Request) { func (m *Repository) Home(w http.ResponseWriter, r *http.Request) {
render.RenderTemplate(w, r, "home.page.tmpl", &models.TemplateData{}) render.Template(w, r, "home.page.tmpl", &models.TemplateData{})
} }
// About is the about page handler // About is the about page handler
func (m *Repository) About(w http.ResponseWriter, r *http.Request) { func (m *Repository) About(w http.ResponseWriter, r *http.Request) {
// send the data to the template // send the data to the template
render.RenderTemplate(w, r, "about.page.tmpl", &models.TemplateData{}) render.Template(w, r, "about.page.tmpl", &models.TemplateData{})
} }
// Contact is the contact page handler // Contact is the contact page handler
func (m *Repository) Contact(w http.ResponseWriter, r *http.Request) { func (m *Repository) Contact(w http.ResponseWriter, r *http.Request) {
render.RenderTemplate(w, r, "contact.page.tmpl", &models.TemplateData{}) render.Template(w, r, "contact.page.tmpl", &models.TemplateData{})
} }
// Generals is the General's Quarters page handler // Generals is the General's Quarters page handler
func (m *Repository) Generals(w http.ResponseWriter, r *http.Request) { func (m *Repository) Generals(w http.ResponseWriter, r *http.Request) {
render.RenderTemplate(w, r, "generals.page.tmpl", &models.TemplateData{}) render.Template(w, r, "generals.page.tmpl", &models.TemplateData{})
} }
// Majors is the Major's Suite page handler // Majors is the Major's Suite page handler
func (m *Repository) Majors(w http.ResponseWriter, r *http.Request) { func (m *Repository) Majors(w http.ResponseWriter, r *http.Request) {
render.RenderTemplate(w, r, "majors.page.tmpl", &models.TemplateData{}) render.Template(w, r, "majors.page.tmpl", &models.TemplateData{})
} }
// MakeReservation is the make reservation page handler // MakeReservation is the make reservation page handler
@ -71,7 +71,7 @@ func (m *Repository) MakeReservation(w http.ResponseWriter, r *http.Request) {
data["reservation"] = emptyReservation data["reservation"] = emptyReservation
render.RenderTemplate(w, r, "make-reservation.page.tmpl", &models.TemplateData{ render.Template(w, r, "make-reservation.page.tmpl", &models.TemplateData{
Form: forms.New(nil), Form: forms.New(nil),
Data: data, Data: data,
}) })
@ -102,7 +102,7 @@ func (m *Repository) PostMakeReservation(w http.ResponseWriter, r *http.Request)
data := make(map[string]interface{}) data := make(map[string]interface{})
data["reservation"] = reservation data["reservation"] = reservation
render.RenderTemplate(w, r, "make-reservation.page.tmpl", &models.TemplateData{ render.Template(w, r, "make-reservation.page.tmpl", &models.TemplateData{
Data: data, Data: data,
Form: form, Form: form,
}) })
@ -128,14 +128,14 @@ func (m *Repository) ReservationSummary(w http.ResponseWriter, r *http.Request)
data := make(map[string]interface{}) data := make(map[string]interface{})
data["reservation"] = reservation data["reservation"] = reservation
render.RenderTemplate(w, r, "reservation-summary.page.tmpl", &models.TemplateData{ render.Template(w, r, "reservation-summary.page.tmpl", &models.TemplateData{
Data: data, Data: data,
}) })
} }
// Availability is the search for availability page handler // Availability is the search for availability page handler
func (m *Repository) Availability(w http.ResponseWriter, r *http.Request) { func (m *Repository) Availability(w http.ResponseWriter, r *http.Request) {
render.RenderTemplate(w, r, "availability.page.tmpl", &models.TemplateData{}) render.Template(w, r, "availability.page.tmpl", &models.TemplateData{})
} }
// PostAvailability is the search for availability page handler // PostAvailability is the search for availability page handler

View File

@ -55,7 +55,7 @@ func getRoutes() http.Handler {
repo := NewRepo(&app) repo := NewRepo(&app)
NewHandlers(repo) NewHandlers(repo)
render.NewTemplates(&app) render.NewRenderer(&app)
mux := chi.NewMux() mux := chi.NewMux()

View File

@ -21,8 +21,8 @@ var (
pathToTemplates = "./templates" pathToTemplates = "./templates"
) )
// NewTemplates sets the config for the template package // NewRenderer sets the config for the template package
func NewTemplates(a *config.AppConfig) { func NewRenderer(a *config.AppConfig) {
app = a app = a
} }
@ -35,8 +35,8 @@ func AddDefaultData(td *models.TemplateData, r *http.Request) *models.TemplateDa
return td return td
} }
// RenderTemplate renders a HTML template file // Template renders a HTML template file
func RenderTemplate(w http.ResponseWriter, r *http.Request, tmpl string, td *models.TemplateData) error { func Template(w http.ResponseWriter, r *http.Request, tmpl string, td *models.TemplateData) error {
var tc map[string]*template.Template var tc map[string]*template.Template
if app.UseCache { if app.UseCache {
// get the template cache from the app config // get the template cache from the app config

View File

@ -39,12 +39,12 @@ func TestRenderTemplate(t *testing.T) {
var ww myWriter var ww myWriter
err = RenderTemplate(&ww, r, "home.page.tmpl", &models.TemplateData{}) err = Template(&ww, r, "home.page.tmpl", &models.TemplateData{})
if err != nil { if err != nil {
t.Error("error writiing template to browser") t.Error("error writiing template to browser")
} }
err = RenderTemplate(&ww, r, "non-existent.page.html", &models.TemplateData{}) err = Template(&ww, r, "non-existent.page.html", &models.TemplateData{})
if err == nil { if err == nil {
t.Error("rendered template that doesn't exist") t.Error("rendered template that doesn't exist")
} }
@ -64,7 +64,7 @@ func getSession() (*http.Request, error) {
} }
func TestNewTemplate(t *testing.T) { func TestNewTemplate(t *testing.T) {
NewTemplates(app) NewRenderer(app)
} }
func TestCreateTemplateCache(t *testing.T) { func TestCreateTemplateCache(t *testing.T) {