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

View File

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

View File

@ -38,28 +38,28 @@ func NewHandlers(r *Repository) {
// Home is the home page handler
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
func (m *Repository) About(w http.ResponseWriter, r *http.Request) {
// 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
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
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
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
@ -71,7 +71,7 @@ func (m *Repository) MakeReservation(w http.ResponseWriter, r *http.Request) {
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),
Data: data,
})
@ -102,7 +102,7 @@ func (m *Repository) PostMakeReservation(w http.ResponseWriter, r *http.Request)
data := make(map[string]interface{})
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,
Form: form,
})
@ -128,14 +128,14 @@ func (m *Repository) ReservationSummary(w http.ResponseWriter, r *http.Request)
data := make(map[string]interface{})
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,
})
}
// Availability is the search for availability page handler
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

View File

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

View File

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

View File

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