cpolis/cmd/ui/ui.go

88 lines
2.3 KiB
Go
Raw Normal View History

2024-02-24 09:54:25 +01:00
package ui
import (
"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 {
http.Error(w, err.Error(), http.StatusInternalServerError)
// TODO: und nun?
}
if err := db.CheckPassword(id, pass); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
} else {
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 {
http.Error(w, err.Error(), http.StatusInternalServerError)
log.Panicln(err)
}
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)
}
}
2024-02-24 10:28:12 +01:00
func HandleAddUser(db *data.DB) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
var writer, editor, admin bool
user := r.PostFormValue("username")
pass := r.PostFormValue("password")
pass2 := r.PostFormValue("password2")
first := r.PostFormValue("first-name")
last := r.PostFormValue("last-name")
role := r.PostFormValue("role")
_, _, ok := checkUserStrings(user, first, last)
if !ok {
template.Must(template.ParseFiles("web/templates/add-user.html")).Execute(w, nil)
}
if pass != pass2 {
template.Must(template.ParseFiles("web/templates/add-user.html")).Execute(w, nil)
}
switch 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:
template.Must(template.ParseFiles("web/templates/add-user.html")).Execute(w, nil)
}
db.AddUser(user, pass, first, last, writer, editor, admin)
template.Must(template.ParseFiles("web/templates/editor.html")).Execute(w, nil)
}
}