Erzeuge Templates zentral und nicht pro Aufruf
This commit is contained in:
parent
886ccc0dcb
commit
43b3631da2
7 changed files with 32 additions and 45 deletions
|
@ -2,7 +2,6 @@ package controllers
|
|||
|
||||
import (
|
||||
"fmt"
|
||||
"html/template"
|
||||
"net/http"
|
||||
"net/mail"
|
||||
"sprechstundentool/models"
|
||||
|
@ -146,35 +145,23 @@ func (b *BaseHandler) AddOfficeHourHandler(w http.ResponseWriter, req *http.Requ
|
|||
id, err := b.officeHourRepo.Add(officeHour)
|
||||
if err != nil {
|
||||
w.WriteHeader(http.StatusInternalServerError)
|
||||
failureTemplate, parseErr := template.ParseFiles("templates/addFailure.html")
|
||||
if parseErr != nil {
|
||||
w.Write([]byte(fmt.Sprintf("Template konnte nicht geparst werden : %s", err.Error())))
|
||||
return
|
||||
}
|
||||
failureTemplate.Execute(w, err)
|
||||
Templates.ExecuteTemplate(w, "addFailure.html", err)
|
||||
return
|
||||
}
|
||||
officeHour, _ = b.officeHourRepo.FindById(id)
|
||||
b.requestRepo.Add(officeHour, models.RequestActivate)
|
||||
tmpl, err := template.ParseFiles("templates/addSuccess.html")
|
||||
tmpl.Execute(w, struct{}{})
|
||||
Templates.ExecuteTemplate(w, "addSuccess.html", struct{}{})
|
||||
}
|
||||
}
|
||||
|
||||
func (b *BaseHandler) writeAddOfficeHourMask(w http.ResponseWriter, req *http.Request, data maskData) {
|
||||
tmpl, err := template.ParseFiles("templates/addMask.html")
|
||||
if err != nil {
|
||||
w.WriteHeader(http.StatusInternalServerError)
|
||||
w.Write([]byte(fmt.Sprintf("Template konnte nicht geparst werden : %s", err.Error())))
|
||||
return
|
||||
}
|
||||
if len(data.Errors) != 0 {
|
||||
w.WriteHeader(http.StatusBadRequest)
|
||||
}
|
||||
if req.Method == http.MethodGet {
|
||||
data.Errors = []string{}
|
||||
}
|
||||
err = tmpl.Execute(w, data)
|
||||
err := Templates.ExecuteTemplate(w, "addMask.html", data)
|
||||
if err != nil {
|
||||
w.Write([]byte(fmt.Sprintf("Template konnte nicht geparst werden : %s", err.Error())))
|
||||
return
|
||||
|
|
|
@ -1,7 +1,6 @@
|
|||
package controllers
|
||||
|
||||
import (
|
||||
"html/template"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
|
@ -11,21 +10,16 @@ func (b *BaseHandler) ConfirmRequestHandler(w http.ResponseWriter, req *http.Req
|
|||
|
||||
if err != nil {
|
||||
w.WriteHeader(http.StatusNotFound)
|
||||
tmpl, _ := template.ParseFiles("templates/requestNotFound.html")
|
||||
tmpl.Execute(w, struct{}{})
|
||||
Templates.ExecuteTemplate(w, "requestNotFound.html", struct{}{})
|
||||
return
|
||||
}
|
||||
|
||||
err = b.requestRepo.Execute(request)
|
||||
if err != nil {
|
||||
w.WriteHeader(http.StatusInternalServerError)
|
||||
tmpl, _ := template.ParseFiles("templates/executeFailure.html")
|
||||
tmpl.Execute(w, err.Error())
|
||||
Templates.ExecuteTemplate(w, "executeFailure.html", err.Error())
|
||||
return
|
||||
}
|
||||
tmpl, err := template.ParseFiles("templates/executeSuccess.html")
|
||||
if err != nil {
|
||||
w.WriteHeader(http.StatusInternalServerError)
|
||||
}
|
||||
tmpl.Execute(w, struct{}{})
|
||||
Templates.ExecuteTemplate(w, "executeSuccess.html", struct{}{})
|
||||
|
||||
}
|
||||
|
|
|
@ -2,7 +2,6 @@
|
|||
package controllers
|
||||
|
||||
import (
|
||||
"html/template"
|
||||
"net/http"
|
||||
"sprechstundentool/models"
|
||||
"strconv"
|
||||
|
@ -19,8 +18,7 @@ func (b *BaseHandler) DeleteOfficeHourHandler(w http.ResponseWriter, req *http.R
|
|||
w.WriteHeader(http.StatusBadRequest)
|
||||
}
|
||||
_, err = b.requestRepo.Add(officeHour, models.RequestDelete)
|
||||
tmpl, _ := template.New("deleteSuccess.html").ParseFiles("templates/deleteSuccess.html")
|
||||
tmpl.Execute(w, struct{}{})
|
||||
Templates.ExecuteTemplate(w, "deleteSuccess.html", struct{}{})
|
||||
} else {
|
||||
officeHours, _ := b.officeHourRepo.GetAll(true)
|
||||
timetable, slots := GetTimetable(officeHours)
|
||||
|
|
|
@ -51,13 +51,7 @@ func (b *BaseHandler) writeTimetablePage(w http.ResponseWriter, req *http.Reques
|
|||
SelectedRoom int
|
||||
SelectedCourse int
|
||||
}{courses, rooms, timetable, selectedRoom, selectedCourse}
|
||||
tmpl, err := template.ParseFiles("templates/index.html")
|
||||
if err != nil {
|
||||
w.WriteHeader(http.StatusInternalServerError)
|
||||
w.Write([]byte(fmt.Sprintf("Template konnte nicht geparst werden : %s", err.Error())))
|
||||
return
|
||||
}
|
||||
err = tmpl.Execute(w, data)
|
||||
err := Templates.ExecuteTemplate(w, "index.html", data)
|
||||
if err != nil {
|
||||
w.Write([]byte(fmt.Sprintf("Template konnte nicht geparst werden : %s", err.Error())))
|
||||
return
|
||||
|
|
19
controllers/templates.go
Normal file
19
controllers/templates.go
Normal file
|
@ -0,0 +1,19 @@
|
|||
package controllers
|
||||
|
||||
import (
|
||||
"html/template"
|
||||
"sprechstundentool/models"
|
||||
)
|
||||
|
||||
var Templates, _ = template.Must(template.ParseFiles(
|
||||
"templates/addFailure.html",
|
||||
"templates/addMask.html",
|
||||
"templates/addSuccess.html",
|
||||
"templates/deleteSuccess.html",
|
||||
"templates/executeFailure.html",
|
||||
"templates/executeSuccess.html",
|
||||
"templates/index.html",
|
||||
"templates/officeHourTable.html",
|
||||
"templates/requestNotFound.html")).
|
||||
New("").Funcs(template.FuncMap{"DayName": models.DayName,
|
||||
"divide": func(i int, j int) int { return i / j }}).ParseFiles("templates/confirmationMail", "templates/td.html")
|
|
@ -49,8 +49,6 @@ func GetTimetable(officeHours []models.OfficeHour) (timetable map[models.Date]ma
|
|||
|
||||
func printTimetable(timetable map[models.Date]map[int]models.OfficeHour, slots []int, deleteIcons bool) template.HTML {
|
||||
var tableBody string
|
||||
|
||||
tableCell, _ := template.New("td.html").Funcs(template.FuncMap{"divide": func(i int, j int) int { return i / j }}).ParseFiles("templates/td.html")
|
||||
for hour := 8; hour < 19; hour += 1 {
|
||||
for minute := 0; minute < 60; minute += models.MinuteGranularity {
|
||||
tableBody += "<tr>"
|
||||
|
@ -86,7 +84,7 @@ func printTimetable(timetable map[models.Date]map[int]models.OfficeHour, slots [
|
|||
MinuteGranularity int
|
||||
DeleteIcons bool
|
||||
}{current, models.MinuteGranularity, deleteIcons}
|
||||
tableCell.Execute(&celldata, data)
|
||||
Templates.ExecuteTemplate(&celldata, "td.html", data)
|
||||
tableBody += celldata.String()
|
||||
}
|
||||
} else {
|
||||
|
@ -102,7 +100,6 @@ func printTimetable(timetable map[models.Date]map[int]models.OfficeHour, slots [
|
|||
}
|
||||
}
|
||||
var table bytes.Buffer
|
||||
tableTemplate, _ := template.ParseFiles("templates/officeHourTable.html")
|
||||
|
||||
tableData := struct {
|
||||
ColspanMon int
|
||||
|
@ -119,6 +116,6 @@ func printTimetable(timetable map[models.Date]map[int]models.OfficeHour, slots [
|
|||
slots[4],
|
||||
template.HTML(tableBody),
|
||||
}
|
||||
tableTemplate.Execute(&table, tableData)
|
||||
Templates.ExecuteTemplate(&table, "officeHourTable.html", tableData)
|
||||
return template.HTML(table.String())
|
||||
}
|
||||
|
|
|
@ -5,9 +5,9 @@ import (
|
|||
"bytes"
|
||||
"crypto/rand"
|
||||
"database/sql"
|
||||
"html/template"
|
||||
"math/big"
|
||||
"net/smtp"
|
||||
"sprechstundentool/controllers"
|
||||
"sprechstundentool/models"
|
||||
)
|
||||
|
||||
|
@ -120,10 +120,8 @@ func (r *RequestRepo) newSecret() (string, error) {
|
|||
|
||||
func sendConfirmationMail(request models.Request) error {
|
||||
to := []string{request.OfficeHour.Tutor.Email}
|
||||
tmpl, err := template.New("confirmationMail").Funcs(template.FuncMap{"DayName": models.DayName}).ParseFiles("templates/confirmationMail")
|
||||
|
||||
var message bytes.Buffer
|
||||
err = tmpl.Execute(&message, request)
|
||||
err := controllers.Templates.ExecuteTemplate(&message, "confirmationMail", request)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue