108 lines
2.9 KiB
Go
108 lines
2.9 KiB
Go
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 {
|
|
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
|
|
} 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 {
|
|
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
|
|
|
|
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")
|
|
|
|
if inputsEmpty(user, pass, pass2, first, last, role) {
|
|
log.Println("inputsEmpty")
|
|
template.Must(template.ParseFiles("web/templates/add-user.html")).Execute(w, nil)
|
|
}
|
|
_, _, ok := checkUserStrings(user, first, last)
|
|
if !ok {
|
|
log.Println("checkUserStrings")
|
|
template.Must(template.ParseFiles("web/templates/add-user.html")).Execute(w, nil)
|
|
}
|
|
id, _ := db.GetID(user)
|
|
if id != 0 {
|
|
log.Println("GetID")
|
|
template.Must(template.ParseFiles("web/templates/add-user.html")).Execute(w, nil)
|
|
}
|
|
if pass != pass2 {
|
|
log.Println("pass")
|
|
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:
|
|
log.Println("switch")
|
|
template.Must(template.ParseFiles("web/templates/add-user.html")).Execute(w, nil)
|
|
}
|
|
|
|
if err := db.AddUser(user, pass, first, 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)
|
|
}
|
|
}
|