cpolis/cmd/ui/handlers.go

117 lines
3.5 KiB
Go

package ui
import (
"fmt"
"html/template"
"log"
"net/http"
"streifling.com/jason/cpolis/cmd/data"
"streifling.com/jason/cpolis/cmd/feed"
)
func HandleLogin(db *data.DB) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
user := r.PostFormValue("username")
pass := r.PostFormValue("password")
id, err := db.GetID(user)
if err != nil {
log.Println(err)
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
if err := db.CheckPassword(id, pass); err != nil {
log.Println(err)
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
template.Must(template.ParseFiles("web/templates/editor.html")).ExecuteTemplate(w, "page-content", nil)
}
}
func HandleFinishedEdit(f *feed.Feed) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
title := r.PostFormValue("editor-title")
desc := r.PostFormValue("editor-desc")
mdContent := r.PostFormValue("editor-text")
content, err := data.ConvertToHTML(mdContent)
if err != nil {
log.Println(err)
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
feed.AddToFeed(f, title, desc, content)
feed.SaveFeed(f, "tmp/rss.gob")
// template.Must(template.ParseFiles("web/templates/editor.html")).ExecuteTemplate(w, "html-result", rssItem)
}
}
func HandleAddUser(db *data.DB) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
var writer, editor, admin bool
data := AddUserData{
User: r.PostFormValue("username"),
First: r.PostFormValue("first-name"),
Last: r.PostFormValue("last-name"),
Role: r.PostFormValue("role"),
}
pass := r.PostFormValue("password")
pass2 := r.PostFormValue("password2")
if inputsEmpty(data.User, pass, pass2, data.First, data.Last, data.Role) {
data.Msg = "Alle Felder müssen ausgefüllt werden."
template.Must(template.ParseFiles("web/templates/add-user.html")).ExecuteTemplate(w, "page-content", data)
return
}
userString, stringLen, ok := checkUserStrings(data.User, data.First, data.Last)
if !ok {
data.Msg = fmt.Sprint(userString, " ist zu lang. Maximal ", stringLen, " Zeichen erlaubt.")
template.Must(template.ParseFiles("web/templates/add-user.html")).ExecuteTemplate(w, "page-content", data)
return
}
id, _ := db.GetID(data.User)
if id != 0 {
data.Msg = fmt.Sprint(data.User, " ist bereits vergeben. Bitte anderen Benutzernamen wählen.")
template.Must(template.ParseFiles("web/templates/add-user.html")).ExecuteTemplate(w, "page-content", data)
return
}
if pass != pass2 {
data.Msg = "Die Passwörter stimmen nicht überein."
template.Must(template.ParseFiles("web/templates/add-user.html")).ExecuteTemplate(w, "page-content", data)
return
}
switch data.Role {
case "writer":
writer = true
editor = false
admin = false
case "editor":
writer = false
editor = true
admin = false
case "admin":
writer = false
editor = false
admin = true
default:
log.Println("When setting up", data.User, "the HTML was altered.")
data.Msg = "Das HTML der Seite zu verändern ist unzulässig. Dieser Vorfall wurde gespeichert."
template.Must(template.ParseFiles("web/templates/add-user.html")).ExecuteTemplate(w, "page-content", data)
return
}
if err := db.AddUser(data.User, pass, data.First, data.Last, writer, editor, admin); err != nil {
log.Println(err)
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
template.Must(template.ParseFiles("web/templates/editor.html")).Execute(w, nil)
}
}