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