Großer Umbruch der Architektur, halb fertig, zu viele Details zum Aufzählen, in Zukunft wieder kleine Commits!
This commit is contained in:
@ -19,27 +19,50 @@ type DB struct {
|
||||
Name string
|
||||
}
|
||||
|
||||
func getCredentials() (string, string, error) {
|
||||
fmt.Printf("DB Benutzer: ")
|
||||
user, err := bufio.NewReader(os.Stdin).ReadString('\n')
|
||||
if err != nil {
|
||||
return "", "", fmt.Errorf("getCredentials: bufio.NewReader(os.Stdin).ReadString('\n'): %v", err)
|
||||
func getUsername() (string, error) {
|
||||
user := os.Getenv("DB_USER")
|
||||
if user == "" {
|
||||
var err error
|
||||
fmt.Printf("DB Benutzer: ")
|
||||
user, err = bufio.NewReader(os.Stdin).ReadString('\n')
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("getUsername: bufio.NewReader(os.Stdin).ReadString('\n'): %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
fmt.Printf("DB Passwort: ")
|
||||
bytePass, err := term.ReadPassword(int(syscall.Stdin))
|
||||
if err != nil {
|
||||
return "", "", fmt.Errorf("getCredentials: term.ReadPassword(int(syscall.Stdin)): %v", err)
|
||||
}
|
||||
fmt.Println()
|
||||
pass := string(bytePass)
|
||||
|
||||
return strings.TrimSpace(user), strings.TrimSpace(pass), nil
|
||||
return strings.TrimSpace(user), nil
|
||||
}
|
||||
|
||||
func reverseOrder(bs *[]types.Briefing) {
|
||||
for i, j := 0, len(*bs)-1; i < j; i, j = i+1, j-1 {
|
||||
(*bs)[i], (*bs)[j] = (*bs)[j], (*bs)[i]
|
||||
func getPassword() (string, error) {
|
||||
pass := os.Getenv("DB_PASS")
|
||||
if pass == "" {
|
||||
fmt.Printf("DB Passwort: ")
|
||||
bytePass, err := term.ReadPassword(int(syscall.Stdin))
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("getCredentials: term.ReadPassword(int(syscall.Stdin)): %v", err)
|
||||
}
|
||||
fmt.Println()
|
||||
pass = strings.TrimSpace(string(bytePass))
|
||||
}
|
||||
return pass, nil
|
||||
}
|
||||
|
||||
func getCredentials() (string, string, error) {
|
||||
user, err := getUsername()
|
||||
if err != nil {
|
||||
return "", "", fmt.Errorf("getCredentials: getUsername(): %v", err)
|
||||
}
|
||||
|
||||
pass, err := getPassword()
|
||||
if err != nil {
|
||||
return "", "", fmt.Errorf("getCredentials: getPassword(): %v", err)
|
||||
}
|
||||
|
||||
return user, pass, nil
|
||||
}
|
||||
|
||||
func reverseOrder(bs []*types.Briefing) {
|
||||
for i, j := 0, len(bs)-1; i < j; i, j = i+1, j-1 {
|
||||
bs[i], bs[j] = bs[j], bs[i]
|
||||
}
|
||||
}
|
||||
|
||||
@ -92,8 +115,8 @@ func (db *DB) WriteBriefing(b *types.Briefing) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (db *DB) ReadAll() (*[]types.Briefing, error) {
|
||||
bs := make([]types.Briefing, 0)
|
||||
func (db *DB) ReadAll() ([]*types.Briefing, error) {
|
||||
bs := make([]*types.Briefing, 0)
|
||||
|
||||
rows, err := db.Query("SELECT *" + " FROM " + db.Name)
|
||||
if err != nil {
|
||||
@ -110,16 +133,16 @@ func (db *DB) ReadAll() (*[]types.Briefing, error) {
|
||||
return nil, fmt.Errorf("*DB.ReadAll: db.Query(): %v\n", err)
|
||||
}
|
||||
|
||||
b.Participants = append(b.Participants, *p)
|
||||
bs = append(bs, *b)
|
||||
b.Participants = append(b.Participants, p)
|
||||
bs = append(bs, b)
|
||||
}
|
||||
|
||||
reverseOrder(&bs)
|
||||
return &bs, nil
|
||||
reverseOrder(bs)
|
||||
return bs, nil
|
||||
}
|
||||
|
||||
func (db *DB) ReadByName(name string) (*[]types.Briefing, error) {
|
||||
bs := make([]types.Briefing, 0)
|
||||
func (db *DB) ReadByName(name string) ([]*types.Briefing, error) {
|
||||
bs := make([]*types.Briefing, 0)
|
||||
|
||||
rows, err := db.Query("SELECT *"+
|
||||
" FROM "+db.Name+
|
||||
@ -148,10 +171,22 @@ func (db *DB) ReadByName(name string) (*[]types.Briefing, error) {
|
||||
" &b.LastName, &b.Date, &b.Time, &b.State, &b.Location, &p.FirstName,"+
|
||||
" &p.LastName, &p.Company): %v\n", err)
|
||||
}
|
||||
b.Participants = append(b.Participants, *p)
|
||||
bs = append(bs, *b)
|
||||
b.Participants = append(b.Participants, p)
|
||||
bs = append(bs, b)
|
||||
}
|
||||
|
||||
reverseOrder(&bs)
|
||||
return &bs, nil
|
||||
reverseOrder(bs)
|
||||
return bs, nil
|
||||
}
|
||||
|
||||
func (db *DB) GetLastID(i *int64) error {
|
||||
row := db.QueryRow("SELECT id" +
|
||||
" FROM " + db.Name +
|
||||
" ORDER BY id DESC LIMIT 0, 1")
|
||||
|
||||
if err := row.Scan(i); err != nil {
|
||||
return fmt.Errorf("*DB.GetLastID: row.Scan(&i): %v\n", err)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
@ -1 +1,42 @@
|
||||
package data
|
||||
|
||||
import "streifling.com/jason/sicherheitsunterweisung/packages/types"
|
||||
|
||||
func InitQuestions() []types.Question {
|
||||
Q := make([]types.Question, 0)
|
||||
|
||||
Q = append(Q, types.Question{
|
||||
Text: "Wie viel ist 1+1?",
|
||||
Answers: []types.Answer{
|
||||
{ID: 0, Text: "1"},
|
||||
{ID: 1, Text: "2"},
|
||||
{ID: 2, Text: "3"},
|
||||
{ID: 3, Text: "4"},
|
||||
},
|
||||
Correct: 1,
|
||||
})
|
||||
|
||||
Q = append(Q, types.Question{
|
||||
Text: "Wie viel ist 2+2?",
|
||||
Answers: []types.Answer{
|
||||
{ID: 0, Text: "1"},
|
||||
{ID: 1, Text: "2"},
|
||||
{ID: 2, Text: "3"},
|
||||
{ID: 3, Text: "4"},
|
||||
},
|
||||
Correct: 3,
|
||||
})
|
||||
|
||||
Q = append(Q, types.Question{
|
||||
Text: "Wie viel ist 1+2?",
|
||||
Answers: []types.Answer{
|
||||
{ID: 0, Text: "1"},
|
||||
{ID: 1, Text: "2"},
|
||||
{ID: 2, Text: "3"},
|
||||
{ID: 3, Text: "4"},
|
||||
},
|
||||
Correct: 2,
|
||||
})
|
||||
|
||||
return Q
|
||||
}
|
||||
|
@ -7,21 +7,31 @@ import (
|
||||
"html/template"
|
||||
"log"
|
||||
"net/http"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"streifling.com/jason/sicherheitsunterweisung/packages/data"
|
||||
"streifling.com/jason/sicherheitsunterweisung/packages/types"
|
||||
)
|
||||
|
||||
type questionData struct {
|
||||
ID int64
|
||||
Q types.Question
|
||||
I int
|
||||
J int
|
||||
}
|
||||
|
||||
func DisplayTable(db *data.DB) http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
bs, err := db.ReadAll()
|
||||
if err != nil {
|
||||
_ = fmt.Errorf("DisplayTable: %v\n", err)
|
||||
}
|
||||
template.Must(template.ParseFiles("templates/index.html", "templates/table.html")).Execute(w, bs)
|
||||
template.Must(template.ParseFiles("templates/table.html")).ExecuteTemplate(w, "content", bs)
|
||||
}
|
||||
}
|
||||
|
||||
func DisplayResults(db *data.DB) http.HandlerFunc {
|
||||
func DisplaySearchResults(db *data.DB) http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
bs, err := db.ReadByName(r.PostFormValue("search"))
|
||||
if err != nil {
|
||||
@ -33,14 +43,27 @@ func DisplayResults(db *data.DB) http.HandlerFunc {
|
||||
|
||||
func DisplayForm(i *int64) http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
template.Must(template.ParseFiles("templates/form.html")).ExecuteTemplate(w, "content", i)
|
||||
template.Must(template.ParseFiles("templates/briefing.html")).ExecuteTemplate(w, "content", i)
|
||||
}
|
||||
}
|
||||
|
||||
func AddParticipant(i *int64) http.HandlerFunc {
|
||||
func generateUUID() string {
|
||||
bs := make([]byte, 2)
|
||||
|
||||
if _, err := rand.Read(bs); err != nil {
|
||||
_ = fmt.Errorf("GenerateUUID: rand.Read(bs): %v\n", err)
|
||||
return ""
|
||||
}
|
||||
|
||||
return hex.EncodeToString(bs)
|
||||
}
|
||||
|
||||
func AddParticipant(i *int64, ls *[]string) http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
*i++
|
||||
template.Must(template.ParseFiles("templates/form.html")).ExecuteTemplate(w, "participant", i)
|
||||
login := fmt.Sprintf("%d", *i) + "-" + generateUUID()
|
||||
(*ls) = append(*ls, login)
|
||||
template.Must(template.ParseFiles("templates/briefing.html")).ExecuteTemplate(w, "new", login)
|
||||
}
|
||||
}
|
||||
|
||||
@ -56,7 +79,7 @@ func SubmitForm(db *data.DB, i, j *int64) http.HandlerFunc {
|
||||
b.Location = r.PostFormValue("location")
|
||||
|
||||
for ; *j <= *i; *j++ {
|
||||
b.Participants = append(b.Participants, types.Participant{
|
||||
b.Participants = append(b.Participants, &types.Participant{
|
||||
ID: *j,
|
||||
Person: types.Person{
|
||||
FirstName: r.PostFormValue("participant-first-" + fmt.Sprint(*j)),
|
||||
@ -77,38 +100,107 @@ func SubmitForm(db *data.DB, i, j *int64) http.HandlerFunc {
|
||||
}
|
||||
}
|
||||
|
||||
func GenerateUUID(ch chan<- string) http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
bs := make([]byte, 4)
|
||||
|
||||
_, err := rand.Read(bs)
|
||||
if err != nil {
|
||||
_ = fmt.Errorf("GenerateUUID: rand.Read(bs): %v\n", err)
|
||||
// TODO: Make it only serve one purpose
|
||||
func loginIsCorrect(l string, logins *[]string) bool {
|
||||
for i, v := range *logins {
|
||||
if l == v {
|
||||
(*logins) = append((*logins)[:i], (*logins)[i+1:]...)
|
||||
return true
|
||||
}
|
||||
|
||||
uuid := hex.EncodeToString(bs)
|
||||
ch <- uuid
|
||||
|
||||
template.Must(template.ParseFiles("templates/form.html")).ExecuteTemplate(w, "uuid", uuid)
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func DisplayQuestionsIfOK(uuids *[]string) http.HandlerFunc {
|
||||
func newParticipant(l string) (*types.Participant, error) {
|
||||
p := new(types.Participant)
|
||||
|
||||
idInt, err := strconv.Atoi(strings.Split(l, "-")[0])
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("newParticipant: strconv.Atoi(idString): %v\n", err)
|
||||
}
|
||||
|
||||
p.ID = int64(idInt)
|
||||
return p, nil
|
||||
}
|
||||
|
||||
func DisplayParticipantForm(ls *[]string, cp chan<- *types.Participant) http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
if uuidIsOK(r.PostFormValue("login"), uuids) {
|
||||
template.Must(template.ParseFiles("templates/question.html")).ExecuteTemplate(w, "content", nil)
|
||||
l := r.PostFormValue("login")
|
||||
|
||||
if loginIsCorrect(l, ls) {
|
||||
p, err := newParticipant(l)
|
||||
if err != nil {
|
||||
http.Error(w, "GetParticipantData: newParticipant(l): "+fmt.Sprint(err), http.StatusInternalServerError)
|
||||
}
|
||||
|
||||
cp <- p
|
||||
template.Must(template.ParseFiles("templates/participant.html")).ExecuteTemplate(w, "content", p.ID)
|
||||
} else {
|
||||
template.Must(template.ParseFiles("templates/login.html")).ExecuteTemplate(w, "content", nil)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// TODO: Delete uuid from uuids
|
||||
func uuidIsOK(uuid string, uuids *[]string) bool {
|
||||
for _, u := range *uuids {
|
||||
if uuid == u {
|
||||
return true
|
||||
}
|
||||
func readAnswer(r *http.Request, p *types.Participant, i int) error {
|
||||
v, err := strconv.Atoi(r.PostFormValue("answer"))
|
||||
if err != nil {
|
||||
return fmt.Errorf("readAnswer: strconv.Atoi(r.PostFormValue(\"answer\")): %v\n", err)
|
||||
}
|
||||
|
||||
p.Questions[i].Chosen = v
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func DisplayQuestion(i int, p *types.Participant) http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
if i == 0 {
|
||||
p.FirstName = r.PostFormValue("participant-first-" + fmt.Sprintf("%d", p.ID))
|
||||
p.LastName = r.PostFormValue("participant-last-" + fmt.Sprintf("%d", p.ID))
|
||||
p.Company = r.PostFormValue("participant-company-" + fmt.Sprintf("%d", p.ID))
|
||||
} else {
|
||||
if err := readAnswer(r, p, i-1); err != nil {
|
||||
http.Error(w, "DisplayQuestion: readAnswer(r, p, i): "+fmt.Sprint(err), http.StatusInternalServerError)
|
||||
}
|
||||
}
|
||||
|
||||
data := new(questionData)
|
||||
data.ID = p.ID
|
||||
data.Q = p.Questions[i]
|
||||
data.I = i
|
||||
data.J = i + 1
|
||||
|
||||
template.Must(template.ParseFiles("templates/question.html")).ExecuteTemplate(w, "content", data)
|
||||
}
|
||||
}
|
||||
|
||||
func DisplayTestResults(b *types.Briefing, p *types.Participant) http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
numQuestions := len(p.Questions)
|
||||
wrongAnswers := make([]int, 0)
|
||||
fmt.Println(wrongAnswers)
|
||||
|
||||
if err := readAnswer(r, p, numQuestions-1); err != nil {
|
||||
http.Error(w, "DisplayTestResults: readAnswer(r, p, i): "+fmt.Sprint(err), http.StatusInternalServerError)
|
||||
}
|
||||
|
||||
for i, q := range p.Questions {
|
||||
if q.Chosen != q.Correct {
|
||||
wrongAnswers = append(wrongAnswers, i)
|
||||
}
|
||||
}
|
||||
|
||||
if wrongAnswers == nil {
|
||||
b.Participants = append(b.Participants, p)
|
||||
} else {
|
||||
data := new(questionData)
|
||||
data.ID = p.ID
|
||||
data.Q = p.Questions[0]
|
||||
data.I = 0
|
||||
data.J = data.I + 1
|
||||
template.Must(template.ParseFiles("templates/question.html")).ExecuteTemplate(w, "content", data)
|
||||
}
|
||||
|
||||
template.Must(template.ParseFiles("templates/results.html")).ExecuteTemplate(w, "content", nil)
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
@ -7,28 +7,23 @@ type Person struct {
|
||||
|
||||
type Instructor Person
|
||||
|
||||
type QAElement struct {
|
||||
type Answer struct {
|
||||
ID int
|
||||
Text string
|
||||
}
|
||||
|
||||
type Answer QAElement
|
||||
|
||||
type Question struct {
|
||||
QAElement
|
||||
Text string
|
||||
Answers []Answer
|
||||
}
|
||||
|
||||
type Questionaire struct {
|
||||
UUID string
|
||||
Questions []Question
|
||||
Chosen int
|
||||
Correct int
|
||||
}
|
||||
|
||||
type Participant struct {
|
||||
ID int64
|
||||
Person
|
||||
Company string
|
||||
QuestionaireUUID string
|
||||
Company string
|
||||
Questions []Question
|
||||
}
|
||||
|
||||
type Briefing struct {
|
||||
@ -37,5 +32,5 @@ type Briefing struct {
|
||||
Time string
|
||||
State string
|
||||
Location string
|
||||
Participants []Participant
|
||||
Participants []*Participant
|
||||
}
|
||||
|
Reference in New Issue
Block a user