2024-02-24 09:54:25 +01:00
|
|
|
package ui
|
|
|
|
|
|
|
|
import (
|
2024-02-24 13:25:32 +01:00
|
|
|
"fmt"
|
2024-02-24 09:54:25 +01:00
|
|
|
"html/template"
|
|
|
|
"log"
|
|
|
|
"net/http"
|
2024-03-01 12:25:53 +01:00
|
|
|
"strconv"
|
2024-02-24 09:54:25 +01:00
|
|
|
|
|
|
|
"streifling.com/jason/cpolis/cmd/data"
|
|
|
|
)
|
|
|
|
|
2024-02-27 09:03:21 +01:00
|
|
|
type AddUserData struct {
|
2024-03-01 12:25:53 +01:00
|
|
|
data.User
|
|
|
|
Msg string
|
2024-02-27 09:03:21 +01:00
|
|
|
}
|
2024-02-24 09:54:25 +01:00
|
|
|
|
2024-03-01 12:25:53 +01:00
|
|
|
func inputsEmpty(user data.User, pass, pass2 string) bool {
|
|
|
|
return len(user.UserName) == 0 ||
|
|
|
|
len(user.FirstName) == 0 ||
|
|
|
|
len(user.LastName) == 0 ||
|
2024-02-27 09:03:21 +01:00
|
|
|
len(pass) == 0 ||
|
2024-03-01 12:25:53 +01:00
|
|
|
len(pass2) == 0
|
2024-02-27 09:03:21 +01:00
|
|
|
}
|
2024-02-24 13:25:32 +01:00
|
|
|
|
2024-03-01 12:25:53 +01:00
|
|
|
func checkUserStrings(user data.User) (string, int, bool) {
|
2024-02-27 09:03:21 +01:00
|
|
|
userLen := 15
|
|
|
|
nameLen := 50
|
|
|
|
|
2024-03-01 12:25:53 +01:00
|
|
|
if len(user.UserName) > userLen {
|
2024-02-27 09:03:21 +01:00
|
|
|
return "Benutzername", userLen, false
|
2024-03-01 12:25:53 +01:00
|
|
|
} else if len(user.FirstName) > nameLen {
|
2024-02-27 09:03:21 +01:00
|
|
|
return "Vorname", nameLen, false
|
2024-03-01 12:25:53 +01:00
|
|
|
} else if len(user.LastName) > nameLen {
|
2024-02-27 09:03:21 +01:00
|
|
|
return "Nachname", nameLen, false
|
|
|
|
} else {
|
|
|
|
return "", 0, true
|
2024-02-24 09:54:25 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-01 12:25:53 +01:00
|
|
|
func HomePage(db *data.DB) http.HandlerFunc {
|
|
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
numRows, err := db.CountEntries()
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalln(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if numRows == 0 {
|
|
|
|
template.Must(template.ParseFiles("web/templates/index.html", "web/templates/add-user.html")).Execute(w, nil)
|
|
|
|
} else {
|
|
|
|
template.Must(template.ParseFiles("web/templates/index.html", "web/templates/login.html")).Execute(w, nil)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-02-27 09:03:21 +01:00
|
|
|
func CreateUser() http.HandlerFunc {
|
2024-02-24 09:54:25 +01:00
|
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
2024-02-27 09:03:21 +01:00
|
|
|
template.Must(template.ParseFiles("web/templates/add-user.html")).ExecuteTemplate(w, "page-content", nil)
|
2024-02-24 09:54:25 +01:00
|
|
|
}
|
|
|
|
}
|
2024-02-24 10:28:12 +01:00
|
|
|
|
2024-02-24 14:49:29 +01:00
|
|
|
func AddUser(db *data.DB) http.HandlerFunc {
|
2024-02-24 10:28:12 +01:00
|
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
2024-03-01 12:25:53 +01:00
|
|
|
role, err := strconv.Atoi(r.PostFormValue("role"))
|
|
|
|
if err != nil {
|
|
|
|
log.Println(err)
|
|
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
2024-02-24 10:28:12 +01:00
|
|
|
|
2024-03-01 11:30:31 +01:00
|
|
|
htmlData := AddUserData{
|
2024-03-01 12:25:53 +01:00
|
|
|
User: data.User{
|
|
|
|
UserName: r.PostFormValue("username"),
|
|
|
|
FirstName: r.PostFormValue("first-name"),
|
|
|
|
LastName: r.PostFormValue("last-name"),
|
|
|
|
Role: data.Role(role),
|
|
|
|
},
|
2024-02-24 13:25:32 +01:00
|
|
|
}
|
2024-02-24 10:28:12 +01:00
|
|
|
pass := r.PostFormValue("password")
|
|
|
|
pass2 := r.PostFormValue("password2")
|
|
|
|
|
2024-03-01 12:25:53 +01:00
|
|
|
if inputsEmpty(htmlData.User, pass, pass2) {
|
2024-03-01 11:30:31 +01:00
|
|
|
htmlData.Msg = "Alle Felder müssen ausgefüllt werden."
|
|
|
|
template.Must(template.ParseFiles("web/templates/add-user.html")).ExecuteTemplate(w, "page-content", htmlData)
|
2024-02-24 13:25:32 +01:00
|
|
|
return
|
2024-02-24 12:10:34 +01:00
|
|
|
}
|
2024-03-01 12:25:53 +01:00
|
|
|
userString, stringLen, ok := checkUserStrings(htmlData.User)
|
2024-02-24 10:28:12 +01:00
|
|
|
if !ok {
|
2024-03-01 11:30:31 +01:00
|
|
|
htmlData.Msg = fmt.Sprint(userString, " ist zu lang. Maximal ", stringLen, " Zeichen erlaubt.")
|
|
|
|
template.Must(template.ParseFiles("web/templates/add-user.html")).ExecuteTemplate(w, "page-content", htmlData)
|
2024-02-24 13:25:32 +01:00
|
|
|
return
|
2024-02-24 10:56:12 +01:00
|
|
|
}
|
2024-03-01 12:25:53 +01:00
|
|
|
id, _ := db.GetID(htmlData.UserName)
|
2024-02-24 10:56:12 +01:00
|
|
|
if id != 0 {
|
2024-03-01 12:25:53 +01:00
|
|
|
htmlData.Msg = fmt.Sprint(htmlData.UserName, " ist bereits vergeben. Bitte anderen Benutzernamen wählen.")
|
2024-03-01 11:30:31 +01:00
|
|
|
template.Must(template.ParseFiles("web/templates/add-user.html")).ExecuteTemplate(w, "page-content", htmlData)
|
2024-02-24 13:25:32 +01:00
|
|
|
return
|
2024-02-24 10:28:12 +01:00
|
|
|
}
|
|
|
|
if pass != pass2 {
|
2024-03-01 11:30:31 +01:00
|
|
|
htmlData.Msg = "Die Passwörter stimmen nicht überein."
|
|
|
|
template.Must(template.ParseFiles("web/templates/add-user.html")).ExecuteTemplate(w, "page-content", htmlData)
|
2024-02-24 13:25:32 +01:00
|
|
|
return
|
2024-02-24 10:28:12 +01:00
|
|
|
}
|
|
|
|
|
2024-03-01 12:25:53 +01:00
|
|
|
if err := db.AddUser(htmlData.User, pass); err != nil {
|
2024-02-24 10:56:12 +01:00
|
|
|
log.Println(err)
|
|
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
2024-02-24 11:41:01 +01:00
|
|
|
return
|
2024-02-24 10:56:12 +01:00
|
|
|
}
|
2024-02-24 15:31:33 +01:00
|
|
|
template.Must(template.ParseFiles("web/templates/hub.html")).ExecuteTemplate(w, "page-content", nil)
|
|
|
|
}
|
|
|
|
}
|