Compare commits
	
		
			58 Commits
		
	
	
		
			datenbank_
			...
			sessions
		
	
	| Author | SHA1 | Date | |
|---|---|---|---|
| ba53bba0f6 | |||
| 33cbf4b215 | |||
| 34ef95ad76 | |||
| e3fb756bb1 | |||
| 387f662355 | |||
| 3496fe5f86 | |||
| 10247722c8 | |||
| c7bb630043 | |||
| 5049db064c | |||
| aded71394d | |||
| d054b3644b | |||
| b78e30d109 | |||
| 52cd5756d8 | |||
| 1e6d83de1d | |||
| 4de7d36385 | |||
| 35d565ec7d | |||
| 6359caf3e9 | |||
| 8622f81f89 | |||
| 4e0c8ec1ac | |||
| 5019432b24 | |||
| 39d8108521 | |||
| 82ced65513 | |||
| b605217625 | |||
| db070776b1 | |||
| 3e9cfb49eb | |||
| b42f739581 | |||
| c22647edd9 | |||
| 9bdc6e9f43 | |||
| b17fa1edc7 | |||
| c69bfdfab2 | |||
| c38d3131c6 | |||
| e4d2f9ae3e | |||
| 523fee3ff2 | |||
| 230d79c675 | |||
| 664c24974b | |||
| 7144489afb | |||
| 76f1fe9588 | |||
| 726c8b6dcb | |||
| 608879d008 | |||
| 616df72041 | |||
| 1597d38d34 | |||
| 8dbb5f946d | |||
| 7c7cb5959d | |||
| 2beb90a345 | |||
| f80dca4b10 | |||
| bf05bc0be7 | |||
| f6a073fc39 | |||
| 82870e100f | |||
| 2b119f6752 | |||
| c04932383e | |||
| 8ea0c2964a | |||
| 519dc82023 | |||
| 324a1c54d6 | |||
| 15675d5e6c | |||
| 8ae3019b9c | |||
| b13eba8008 | |||
| da77201a93 | |||
| 71bf8978ac | 
							
								
								
									
										44
									
								
								.air.toml
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										44
									
								
								.air.toml
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,44 @@
 | 
			
		||||
root = "."
 | 
			
		||||
testdata_dir = "testdata"
 | 
			
		||||
tmp_dir = "tmp"
 | 
			
		||||
 | 
			
		||||
[build]
 | 
			
		||||
  args_bin = []
 | 
			
		||||
  bin = "./tmp/main"
 | 
			
		||||
  cmd = "go build -o ./tmp/main ."
 | 
			
		||||
  delay = 0
 | 
			
		||||
  exclude_dir = ["assets", "tmp", "vendor", "testdata"]
 | 
			
		||||
  exclude_file = []
 | 
			
		||||
  exclude_regex = ["_test.go"]
 | 
			
		||||
  exclude_unchanged = false
 | 
			
		||||
  follow_symlink = false
 | 
			
		||||
  full_bin = ""
 | 
			
		||||
  include_dir = []
 | 
			
		||||
  include_ext = ["go", "tpl", "tmpl", "html"]
 | 
			
		||||
  include_file = []
 | 
			
		||||
  kill_delay = "0s"
 | 
			
		||||
  log = "build-errors.log"
 | 
			
		||||
  poll = false
 | 
			
		||||
  poll_interval = 0
 | 
			
		||||
  rerun = false
 | 
			
		||||
  rerun_delay = 500
 | 
			
		||||
  send_interrupt = false
 | 
			
		||||
  stop_on_error = false
 | 
			
		||||
 | 
			
		||||
[color]
 | 
			
		||||
  app = ""
 | 
			
		||||
  build = "yellow"
 | 
			
		||||
  main = "magenta"
 | 
			
		||||
  runner = "green"
 | 
			
		||||
  watcher = "cyan"
 | 
			
		||||
 | 
			
		||||
[log]
 | 
			
		||||
  main_only = false
 | 
			
		||||
  time = false
 | 
			
		||||
 | 
			
		||||
[misc]
 | 
			
		||||
  clean_on_exit = false
 | 
			
		||||
 | 
			
		||||
[screen]
 | 
			
		||||
  clear_on_rebuild = false
 | 
			
		||||
  keep_scroll = true
 | 
			
		||||
							
								
								
									
										3
									
								
								.gitignore
									
									
									
									
										vendored
									
									
								
							
							
						
						
									
										3
									
								
								.gitignore
									
									
									
									
										vendored
									
									
								
							@@ -1 +1,2 @@
 | 
			
		||||
create_tables.sql
 | 
			
		||||
tmp
 | 
			
		||||
test.sql
 | 
			
		||||
 
 | 
			
		||||
							
								
								
									
										102
									
								
								create_tables.sql
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										102
									
								
								create_tables.sql
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,102 @@
 | 
			
		||||
USE sicherheitsunterweisung;
 | 
			
		||||
 | 
			
		||||
SET FOREIGN_KEY_CHECKS = 0;
 | 
			
		||||
DROP TABLE IF EXISTS instructors;
 | 
			
		||||
DROP TABLE IF EXISTS briefings;
 | 
			
		||||
DROP TABLE IF EXISTS participants;
 | 
			
		||||
DROP TABLE IF EXISTS questions;
 | 
			
		||||
DROP TABLE IF EXISTS given_answers;
 | 
			
		||||
SET FOREIGN_KEY_CHECKS = 1;
 | 
			
		||||
 | 
			
		||||
CREATE TABLE instructors (
 | 
			
		||||
  id         INT         NOT NULL,
 | 
			
		||||
  first_name VARCHAR(32) NOT NULL,
 | 
			
		||||
  last_name  VARCHAR(32) NOT NULL,
 | 
			
		||||
 | 
			
		||||
  PRIMARY KEY(id)
 | 
			
		||||
);
 | 
			
		||||
 | 
			
		||||
CREATE TABLE briefings (
 | 
			
		||||
  id            INT         AUTO_INCREMENT,
 | 
			
		||||
  date          DATE        NOT NULL,
 | 
			
		||||
  time          TIME        NOT NULL,
 | 
			
		||||
  location      VARCHAR(32) NOT NULL,
 | 
			
		||||
  document_name VARCHAR(16) NOT NULL,
 | 
			
		||||
  as_of         DATE        NOT NULL,
 | 
			
		||||
  instructor_id INT         NOT NULL,
 | 
			
		||||
 | 
			
		||||
  PRIMARY KEY(id),
 | 
			
		||||
  FOREIGN KEY(instructor_id) REFERENCES instructors(id)
 | 
			
		||||
);
 | 
			
		||||
 | 
			
		||||
CREATE TABLE participants (
 | 
			
		||||
  id         INT         AUTO_INCREMENT,
 | 
			
		||||
  first_name VARCHAR(32) NOT NULL,
 | 
			
		||||
  last_name  VARCHAR(32) NOT NULL,
 | 
			
		||||
  company    VARCHAR(32) NOT NULL,
 | 
			
		||||
 | 
			
		||||
  PRIMARY KEY(id)
 | 
			
		||||
);
 | 
			
		||||
 | 
			
		||||
CREATE TABLE questions (
 | 
			
		||||
  id             INT          AUTO_INCREMENT,
 | 
			
		||||
  question       VARCHAR(256) NOT NULL,
 | 
			
		||||
  answer_1       VARCHAR(64)  NOT NULL,
 | 
			
		||||
  answer_2       VARCHAR(64)  NOT NULL,
 | 
			
		||||
  answer_3       VARCHAR(64)  NOT NULL,
 | 
			
		||||
  answer_4       VARCHAR(64)  NOT NULL,
 | 
			
		||||
  correct_answer INT          NOT NULL,
 | 
			
		||||
 | 
			
		||||
  PRIMARY KEY(id)
 | 
			
		||||
);
 | 
			
		||||
 | 
			
		||||
CREATE TABLE given_answers (
 | 
			
		||||
  briefing_id    INT NOT NULL,
 | 
			
		||||
  participant_id INT NOT NULL,
 | 
			
		||||
  question_id    INT NOT NULL,
 | 
			
		||||
  given_answer   INT NOT NULL,
 | 
			
		||||
 | 
			
		||||
  PRIMARY KEY(briefing_id, participant_id, question_id),
 | 
			
		||||
  FOREIGN KEY(briefing_id)    REFERENCES briefings(id),
 | 
			
		||||
  FOREIGN KEY(participant_id) REFERENCES participants(id),
 | 
			
		||||
  FOREIGN KEY(question_id)    REFERENCES questions(id)
 | 
			
		||||
);
 | 
			
		||||
 | 
			
		||||
INSERT INTO instructors
 | 
			
		||||
  (id, first_name, last_name)
 | 
			
		||||
VALUES
 | 
			
		||||
  ( '123456', 'Jason', 'Streifling'     ),
 | 
			
		||||
  ( '123457', 'Tim',   'Taler'          ),
 | 
			
		||||
  ( '123458', 'Georg', 'aus dem Jungel' );
 | 
			
		||||
 | 
			
		||||
INSERT INTO briefings (
 | 
			
		||||
  date, time, location, document_name, as_of, instructor_id
 | 
			
		||||
) VALUES
 | 
			
		||||
  ( '2023-10-16', '17:00:00', 'Werk Langenhagen', 'ICS-2021-LGH', '2021-02-01', '123456' ),
 | 
			
		||||
  ( '2023-10-16', '17:05:00', 'Werk Langenhagen', 'ICS-2021-LGH', '2021-02-01', '123457' );
 | 
			
		||||
 | 
			
		||||
INSERT INTO participants (
 | 
			
		||||
  first_name, last_name, company
 | 
			
		||||
) VALUES
 | 
			
		||||
  ( 'Peter', 'Enis',  'Körber'     ),
 | 
			
		||||
  ( 'Dürüm', 'Döner', 'MP Technic' );
 | 
			
		||||
 | 
			
		||||
INSERT INTO questions (
 | 
			
		||||
  question, answer_1, answer_2, answer_3, answer_4, correct_answer
 | 
			
		||||
) VALUES
 | 
			
		||||
  ( 'Was ist 1+1?', '1', '2', '3', '4', '2' ),
 | 
			
		||||
  ( 'Was ist 1+2?', '1', '2', '3', '4', '3' ),
 | 
			
		||||
  ( 'Was ist 2+2?', '1', '2', '3', '4', '4' ),
 | 
			
		||||
  ( 'Was ist 0+1?', '1', '2', '3', '4', '1' );
 | 
			
		||||
 | 
			
		||||
INSERT INTO given_answers (
 | 
			
		||||
  briefing_id, participant_id, question_id, given_answer
 | 
			
		||||
) VALUES
 | 
			
		||||
  ( '1', '1', '1', '2' ),
 | 
			
		||||
  ( '1', '1', '2', '3' ),
 | 
			
		||||
  ( '1', '1', '3', '3' ),
 | 
			
		||||
  ( '1', '1', '4', '1' ),
 | 
			
		||||
  ( '2', '2', '1', '2' ),
 | 
			
		||||
  ( '2', '2', '2', '3' ),
 | 
			
		||||
  ( '2', '2', '3', '4' ),
 | 
			
		||||
  ( '2', '2', '4', '1' );
 | 
			
		||||
							
								
								
									
										1
									
								
								go.mod
									
									
									
									
									
								
							
							
						
						
									
										1
									
								
								go.mod
									
									
									
									
									
								
							@@ -4,6 +4,7 @@ go 1.21.1
 | 
			
		||||
 | 
			
		||||
require (
 | 
			
		||||
	github.com/go-sql-driver/mysql v1.7.1
 | 
			
		||||
	github.com/google/uuid v1.3.1
 | 
			
		||||
	golang.org/x/term v0.13.0
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
							
								
								
									
										2
									
								
								go.sum
									
									
									
									
									
								
							
							
						
						
									
										2
									
								
								go.sum
									
									
									
									
									
								
							@@ -1,5 +1,7 @@
 | 
			
		||||
github.com/go-sql-driver/mysql v1.7.1 h1:lUIinVbN1DY0xBg0eMOzmmtGoHwWBbvnWubQUrtU8EI=
 | 
			
		||||
github.com/go-sql-driver/mysql v1.7.1/go.mod h1:OXbVy3sEdcQ2Doequ6Z5BW6fXNQTmx+9S1MCJN5yJMI=
 | 
			
		||||
github.com/google/uuid v1.3.1 h1:KjJaJ9iWZ3jOFZIf1Lqf4laDRCasjl0BCmnEGxkdLb4=
 | 
			
		||||
github.com/google/uuid v1.3.1/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
 | 
			
		||||
golang.org/x/sys v0.13.0 h1:Af8nKPmuFypiUBjVoU9V20FiaFXOcuZI21p0ycVYYGE=
 | 
			
		||||
golang.org/x/sys v0.13.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
 | 
			
		||||
golang.org/x/term v0.13.0 h1:bb+I9cTfFazGW51MZqBVmZy7+JEJMouUHTUSKVQLBek=
 | 
			
		||||
 
 | 
			
		||||
							
								
								
									
										36
									
								
								main.go
									
									
									
									
									
								
							
							
						
						
									
										36
									
								
								main.go
									
									
									
									
									
								
							@@ -1,38 +1,32 @@
 | 
			
		||||
package main
 | 
			
		||||
 | 
			
		||||
import (
 | 
			
		||||
	"html/template"
 | 
			
		||||
	"log"
 | 
			
		||||
	"net/http"
 | 
			
		||||
 | 
			
		||||
	"streifling.com/jason/sicherheitsunterweisung/packages/db"
 | 
			
		||||
	"streifling.com/jason/sicherheitsunterweisung/packages/server"
 | 
			
		||||
	"streifling.com/jason/sicherheitsunterweisung/packages/types"
 | 
			
		||||
	"streifling.com/jason/sicherheitsunterweisung/packages/data"
 | 
			
		||||
	"streifling.com/jason/sicherheitsunterweisung/packages/session"
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
func writeBriefing(ch chan *types.Briefing, db *db.DB) {
 | 
			
		||||
	for b := range ch {
 | 
			
		||||
		db.WriteBriefing(b)
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func main() {
 | 
			
		||||
	var i, j int64
 | 
			
		||||
 | 
			
		||||
	i, j = 1, 1
 | 
			
		||||
	mux := http.NewServeMux()
 | 
			
		||||
	cb := make(chan *types.Briefing)
 | 
			
		||||
 | 
			
		||||
	db, err := db.Open("sicherheitsunterweisung")
 | 
			
		||||
	db, err := data.OpenDB("sicherheitsunterweisung")
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		log.Fatalln(err)
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	mux.Handle("/static/", http.StripPrefix("/static/", http.FileServer(http.Dir("static/"))))
 | 
			
		||||
	mux.HandleFunc("/", server.DisplayForm(&i))
 | 
			
		||||
	mux.HandleFunc("/add-participant/", server.AddParticipant(&i))
 | 
			
		||||
	mux.HandleFunc("/submit/", server.SubmitForm(cb, &i, &j))
 | 
			
		||||
	mux := session.NewMux()
 | 
			
		||||
	sessions := make([]*session.Session, 0)
 | 
			
		||||
	sessionChan := make(chan *session.Session)
 | 
			
		||||
 | 
			
		||||
	go writeBriefing(cb, db)
 | 
			
		||||
	mux.Handle("/static/", http.StripPrefix("/static/", http.FileServer(http.Dir("static/"))))
 | 
			
		||||
	mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
 | 
			
		||||
		template.Must(template.ParseFiles("templates/index.html", "templates/login.html")).Execute(w, nil)
 | 
			
		||||
	})
 | 
			
		||||
	mux.HandleFunc("/internal-login/", session.HandleInternalLogin(db, &sessions, sessionChan))
 | 
			
		||||
	mux.HandleFunc("/external-login/", session.HandleExternalLogin(&sessions))
 | 
			
		||||
 | 
			
		||||
	go mux.HandleSessions(db, sessionChan, &sessions)
 | 
			
		||||
 | 
			
		||||
	log.Fatalln(http.ListenAndServe(":8080", mux))
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
							
								
								
									
										62
									
								
								packages/data/dataStructs.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										62
									
								
								packages/data/dataStructs.go
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,62 @@
 | 
			
		||||
package data
 | 
			
		||||
 | 
			
		||||
import "database/sql"
 | 
			
		||||
 | 
			
		||||
type DB struct {
 | 
			
		||||
	*sql.DB
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
type Person struct {
 | 
			
		||||
	ID        int64
 | 
			
		||||
	FirstName string
 | 
			
		||||
	LastName  string
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
type Instructor Person
 | 
			
		||||
 | 
			
		||||
type Participant struct {
 | 
			
		||||
	Person
 | 
			
		||||
	Company string
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
type Briefing struct {
 | 
			
		||||
	ID           int64
 | 
			
		||||
	Date         string
 | 
			
		||||
	Time         string
 | 
			
		||||
	Location     string
 | 
			
		||||
	DocumentName string
 | 
			
		||||
	AsOf         string
 | 
			
		||||
	InstructorID int64
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
type Answer struct {
 | 
			
		||||
	ID   int64
 | 
			
		||||
	Text string
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
type Question struct {
 | 
			
		||||
	ID      int64
 | 
			
		||||
	Text    string
 | 
			
		||||
	Answers []Answer
 | 
			
		||||
	Correct int
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
type GivenAnswer struct {
 | 
			
		||||
	BriefingID    int64
 | 
			
		||||
	ParticipantID int64
 | 
			
		||||
	QuestionID    int64
 | 
			
		||||
	GivenAnswer   int
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
type OverviewTableData struct {
 | 
			
		||||
	InstructorFirstName  string
 | 
			
		||||
	InstructorLastName   string
 | 
			
		||||
	BriefingDate         string
 | 
			
		||||
	BriefingTime         string
 | 
			
		||||
	BriefingLocation     string
 | 
			
		||||
	BriefingDocumentName string
 | 
			
		||||
	BriefingAsOf         string
 | 
			
		||||
	ParticipantFirstName string
 | 
			
		||||
	ParticipantLastName  string
 | 
			
		||||
	ParticipantCompany   string
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										326
									
								
								packages/data/dbFuncs.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										326
									
								
								packages/data/dbFuncs.go
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,326 @@
 | 
			
		||||
package data
 | 
			
		||||
 | 
			
		||||
import (
 | 
			
		||||
	"database/sql"
 | 
			
		||||
	"fmt"
 | 
			
		||||
	"strings"
 | 
			
		||||
 | 
			
		||||
	"github.com/go-sql-driver/mysql"
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
func OpenDB(dbName string) (*DB, error) {
 | 
			
		||||
	var err error
 | 
			
		||||
	db := new(DB)
 | 
			
		||||
 | 
			
		||||
	cfg := mysql.NewConfig()
 | 
			
		||||
	cfg.DBName = dbName
 | 
			
		||||
	cfg.User, cfg.Passwd, err = getCredentials()
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return nil, fmt.Errorf("error: OpenDB: getCredentials(): %v", err)
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	db.DB, err = sql.Open("mysql", cfg.FormatDSN())
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return nil, fmt.Errorf("error: OpenDB: sql.Open(\"mysql\", cfg.FormatDSN()): %v", err)
 | 
			
		||||
	}
 | 
			
		||||
	if err := db.Ping(); err != nil {
 | 
			
		||||
		return nil, fmt.Errorf("error: OpenDB: db.Ping(): %v", err)
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	return db, nil
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func (db *DB) WriteBriefing(b *Briefing) error {
 | 
			
		||||
	query := `
 | 
			
		||||
  INSERT INTO briefings
 | 
			
		||||
  	(date, time, location, document_name, as_of, instructor_id)
 | 
			
		||||
  VALUES
 | 
			
		||||
  	(?, ?, ?, ?, ?, ?)
 | 
			
		||||
  `
 | 
			
		||||
 | 
			
		||||
	result, err := db.Exec(query, b.Date, b.Time, b.Location, b.DocumentName, b.AsOf, b.InstructorID)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return fmt.Errorf("error: *DB.writeBriefing: db.Exec(): %v", err)
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	b.ID, err = result.LastInsertId()
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return fmt.Errorf("error: *DB.writeBriefing: result.LastInsertId(): %v", err)
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	return nil
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func (db *DB) WriteParticipant(p *Participant) error {
 | 
			
		||||
	query := `
 | 
			
		||||
  INSERT INTO participants
 | 
			
		||||
  	(first_name, last_name, company)
 | 
			
		||||
  VALUES
 | 
			
		||||
  	(?, ?, ?)
 | 
			
		||||
  `
 | 
			
		||||
 | 
			
		||||
	result, err := db.Exec(query, p.FirstName, p.LastName, p.Company)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return fmt.Errorf("error: *DB.writeParticipants: db.Exec(): %v", err)
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	p.ID, err = result.LastInsertId()
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return fmt.Errorf("error: *DB.writeParticipants: result.LastInsertId(): %v", err)
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	return nil
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func (db *DB) WriteGivenAnswers(b Briefing, p Participant, sq []Question, givenAnswers []int) error {
 | 
			
		||||
	query := `
 | 
			
		||||
    INSERT INTO given_answers
 | 
			
		||||
    	(briefing_id, participant_id, question_id, given_answer)
 | 
			
		||||
    VALUES
 | 
			
		||||
    	(?, ?, ?, ?)
 | 
			
		||||
    `
 | 
			
		||||
 | 
			
		||||
	for i, q := range sq {
 | 
			
		||||
		_, err := db.Exec(query, b.ID, p.ID, q.ID, givenAnswers[i])
 | 
			
		||||
		if err != nil {
 | 
			
		||||
			return fmt.Errorf("error: *DB.WriteGivenAnswers: db.Exec(): %v", err)
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	return nil
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func (db *DB) GetAllOverviewTableData() ([]OverviewTableData, error) {
 | 
			
		||||
	query := `
 | 
			
		||||
  SELECT
 | 
			
		||||
    i.first_name,
 | 
			
		||||
    i.last_name,
 | 
			
		||||
    b.date,
 | 
			
		||||
    b.time,
 | 
			
		||||
    b.location,
 | 
			
		||||
    b.document_name,
 | 
			
		||||
    b.as_of,
 | 
			
		||||
    p.first_name,
 | 
			
		||||
    p.last_name,
 | 
			
		||||
    p.company
 | 
			
		||||
  FROM given_answers AS g
 | 
			
		||||
    INNER JOIN briefings AS b
 | 
			
		||||
      ON b.id = g.briefing_id
 | 
			
		||||
    INNER JOIN participants AS p
 | 
			
		||||
      ON p.id = g.participant_id
 | 
			
		||||
    INNER JOIN questions AS q
 | 
			
		||||
      ON q.id = g.question_id
 | 
			
		||||
    INNER JOIN instructors AS i
 | 
			
		||||
      ON i.id = b.instructor_id
 | 
			
		||||
  WHERE
 | 
			
		||||
    q.id = 1
 | 
			
		||||
  ORDER BY
 | 
			
		||||
    b.id DESC,
 | 
			
		||||
    p.id
 | 
			
		||||
  `
 | 
			
		||||
 | 
			
		||||
	rows, err := db.Query(query)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return nil, fmt.Errorf("error: *DB.ReadAllBriefings: db.Query(): %v", err)
 | 
			
		||||
	}
 | 
			
		||||
	defer rows.Close()
 | 
			
		||||
 | 
			
		||||
	data := make([]OverviewTableData, 0)
 | 
			
		||||
	for rows.Next() {
 | 
			
		||||
		otd := new(OverviewTableData)
 | 
			
		||||
 | 
			
		||||
		err := rows.Scan(
 | 
			
		||||
			&otd.InstructorFirstName,
 | 
			
		||||
			&otd.InstructorLastName,
 | 
			
		||||
			&otd.BriefingDate,
 | 
			
		||||
			&otd.BriefingTime,
 | 
			
		||||
			&otd.BriefingLocation,
 | 
			
		||||
			&otd.BriefingDocumentName,
 | 
			
		||||
			&otd.BriefingAsOf,
 | 
			
		||||
			&otd.ParticipantFirstName,
 | 
			
		||||
			&otd.ParticipantLastName,
 | 
			
		||||
			&otd.ParticipantCompany,
 | 
			
		||||
		)
 | 
			
		||||
		if err != nil {
 | 
			
		||||
			return nil, fmt.Errorf("error: *DB.ReadAllBriefings: rows.Scan(): %v", err)
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		data = append(data, *otd)
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	return data, nil
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func (db *DB) GetOverviewTableDataByName(n string) ([]OverviewTableData, error) {
 | 
			
		||||
	query := `
 | 
			
		||||
  SELECT
 | 
			
		||||
    i.first_name,
 | 
			
		||||
    i.last_name,
 | 
			
		||||
    b.date,
 | 
			
		||||
    b.time,
 | 
			
		||||
    b.location,
 | 
			
		||||
    b.document_name,
 | 
			
		||||
    b.as_of,
 | 
			
		||||
    p.first_name,
 | 
			
		||||
    p.last_name,
 | 
			
		||||
    p.company
 | 
			
		||||
  FROM given_answers AS g
 | 
			
		||||
    INNER JOIN briefings AS b
 | 
			
		||||
      ON b.id = g.briefing_id
 | 
			
		||||
    INNER JOIN participants AS p
 | 
			
		||||
      ON p.id = g.participant_id
 | 
			
		||||
    INNER JOIN instructors AS i
 | 
			
		||||
      ON i.id = b.instructor_id
 | 
			
		||||
  WHERE
 | 
			
		||||
    q.id = 1 AND
 | 
			
		||||
    i.first_name LIKE ? OR
 | 
			
		||||
    i.last_name LIKE ? OR
 | 
			
		||||
    p.first_name LIKE ? OR
 | 
			
		||||
    p.last_name LIKE ?
 | 
			
		||||
  ORDER BY
 | 
			
		||||
    b.id DESC,
 | 
			
		||||
    p.id
 | 
			
		||||
  `
 | 
			
		||||
 | 
			
		||||
	rows, err := db.Query(query, "%"+n+"%", "%"+n+"%", "%"+n+"%", "%"+n+"%")
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return nil, fmt.Errorf("error: *DB.GetOverviewTableDataByName: db.Query(): %v", err)
 | 
			
		||||
	}
 | 
			
		||||
	defer rows.Close()
 | 
			
		||||
 | 
			
		||||
	data := make([]OverviewTableData, 0)
 | 
			
		||||
	for rows.Next() {
 | 
			
		||||
		otd := new(OverviewTableData)
 | 
			
		||||
 | 
			
		||||
		err := rows.Scan(
 | 
			
		||||
			&otd.InstructorFirstName,
 | 
			
		||||
			&otd.InstructorLastName,
 | 
			
		||||
			&otd.BriefingDate,
 | 
			
		||||
			&otd.BriefingTime,
 | 
			
		||||
			&otd.BriefingLocation,
 | 
			
		||||
			&otd.BriefingDocumentName,
 | 
			
		||||
			&otd.BriefingAsOf,
 | 
			
		||||
			&otd.ParticipantFirstName,
 | 
			
		||||
			&otd.ParticipantLastName,
 | 
			
		||||
			&otd.ParticipantCompany,
 | 
			
		||||
		)
 | 
			
		||||
		if err != nil {
 | 
			
		||||
			return nil, fmt.Errorf("error: *DB.ReadAllBriefings: rows.Scan(): %v", err)
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		data = append(data, *otd)
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	return data, nil
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func (db *DB) GetLastID(table string) (int, error) {
 | 
			
		||||
	var id int
 | 
			
		||||
	query := `
 | 
			
		||||
  SELECT id
 | 
			
		||||
  FROM ?
 | 
			
		||||
  ORDER BY id DESC
 | 
			
		||||
    LIMIT 0, 1
 | 
			
		||||
  `
 | 
			
		||||
 | 
			
		||||
	row := db.QueryRow(query, table)
 | 
			
		||||
	if err := row.Scan(&id); err != nil {
 | 
			
		||||
		return -1, fmt.Errorf("error: *DB.GetLastID: row.Scan(): %v", err)
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	return id, nil
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func (db *DB) GetInstructors() ([]*Instructor, error) {
 | 
			
		||||
	query := `
 | 
			
		||||
  SELECT *
 | 
			
		||||
  FROM instructors
 | 
			
		||||
  ORDER BY
 | 
			
		||||
    last_name,
 | 
			
		||||
    first_name
 | 
			
		||||
  `
 | 
			
		||||
 | 
			
		||||
	rows, err := db.Query(query)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return nil, fmt.Errorf("error: *DB.GetInstructors: db.Query(): %v", err)
 | 
			
		||||
	}
 | 
			
		||||
	defer rows.Close()
 | 
			
		||||
 | 
			
		||||
	instructors := make([]*Instructor, 0)
 | 
			
		||||
	for rows.Next() {
 | 
			
		||||
		instructor := new(Instructor)
 | 
			
		||||
		if err = rows.Scan(&instructor.ID, &instructor.FirstName, &instructor.LastName); err != nil {
 | 
			
		||||
			return nil, fmt.Errorf("error: *DB.GetInstructors: rows.Scan(): %v", err)
 | 
			
		||||
		}
 | 
			
		||||
		instructors = append(instructors, instructor)
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	return instructors, nil
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func (db *DB) GetQuestions(nums []string) ([]Question, error) {
 | 
			
		||||
	query := `
 | 
			
		||||
  SELECT *
 | 
			
		||||
  FROM questions
 | 
			
		||||
  WHERE id IN (` + strings.Join(nums, ", ") + `)
 | 
			
		||||
  `
 | 
			
		||||
 | 
			
		||||
	rows, err := db.Query(query)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return nil, fmt.Errorf("error: *DB.GetQuestions: db.Query(): %v", err)
 | 
			
		||||
	}
 | 
			
		||||
	defer rows.Close()
 | 
			
		||||
 | 
			
		||||
	// TODO: not scalable
 | 
			
		||||
	questions := make([]Question, 0)
 | 
			
		||||
	for rows.Next() {
 | 
			
		||||
		q := new(Question)
 | 
			
		||||
		a1 := new(Answer)
 | 
			
		||||
		a2 := new(Answer)
 | 
			
		||||
		a3 := new(Answer)
 | 
			
		||||
		a4 := new(Answer)
 | 
			
		||||
 | 
			
		||||
		a1.ID = 1
 | 
			
		||||
		a2.ID = 2
 | 
			
		||||
		a3.ID = 3
 | 
			
		||||
		a4.ID = 4
 | 
			
		||||
 | 
			
		||||
		if err := rows.Scan(&q.ID, &q.Text, &a1.Text, &a2.Text, &a3.Text, &a4.Text, &q.Correct); err != nil {
 | 
			
		||||
			return nil, fmt.Errorf("error: *DB.GetQuestions: rows.Scan(): %v", err)
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		q.Answers = append(q.Answers, *a1)
 | 
			
		||||
		q.Answers = append(q.Answers, *a2)
 | 
			
		||||
		q.Answers = append(q.Answers, *a3)
 | 
			
		||||
		q.Answers = append(q.Answers, *a4)
 | 
			
		||||
 | 
			
		||||
		questions = append(questions, *q)
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	return questions, nil
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func (db *DB) GetGivenAnswers(bid, pid int64, sq []Question) ([]int, error) {
 | 
			
		||||
	answers := make([]int, 0)
 | 
			
		||||
	query := `
 | 
			
		||||
  SELECT given_answer
 | 
			
		||||
  FROM given_answers
 | 
			
		||||
  WHERE
 | 
			
		||||
    briefing_id = ? AND
 | 
			
		||||
    participant_id = ? AND
 | 
			
		||||
    question_id = ?
 | 
			
		||||
  `
 | 
			
		||||
 | 
			
		||||
	for _, q := range sq {
 | 
			
		||||
		var answer int
 | 
			
		||||
 | 
			
		||||
		row := db.QueryRow(query, bid, pid, q.ID)
 | 
			
		||||
		if err := row.Scan(&answer); err != nil {
 | 
			
		||||
			return nil, fmt.Errorf("error: *DB.GetGivenAnswers: row.Scan(): %v", err)
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		answers = append(answers, answer)
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	return answers, nil
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										52
									
								
								packages/data/helperFuncs.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										52
									
								
								packages/data/helperFuncs.go
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,52 @@
 | 
			
		||||
package data
 | 
			
		||||
 | 
			
		||||
import (
 | 
			
		||||
	"bufio"
 | 
			
		||||
	"fmt"
 | 
			
		||||
	"os"
 | 
			
		||||
	"strings"
 | 
			
		||||
	"syscall"
 | 
			
		||||
 | 
			
		||||
	"golang.org/x/term"
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
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("error: getUsername: bufio.NewReader().ReadString(): %v", err)
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
	return strings.TrimSpace(user), nil
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
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("error: getPassword: term.ReadPassword(): %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("error: getCredentials: getUsername(): %v", err)
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	pass, err := getPassword()
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return "", "", fmt.Errorf("error: getCredentials: getPassword(): %v", err)
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	return user, pass, nil
 | 
			
		||||
}
 | 
			
		||||
@@ -1,124 +0,0 @@
 | 
			
		||||
package db
 | 
			
		||||
 | 
			
		||||
import (
 | 
			
		||||
	"bufio"
 | 
			
		||||
	"database/sql"
 | 
			
		||||
	"fmt"
 | 
			
		||||
	"os"
 | 
			
		||||
	"strings"
 | 
			
		||||
	"syscall"
 | 
			
		||||
 | 
			
		||||
	"streifling.com/jason/sicherheitsunterweisung/packages/types"
 | 
			
		||||
 | 
			
		||||
	"github.com/go-sql-driver/mysql"
 | 
			
		||||
	"golang.org/x/term"
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
type DB struct {
 | 
			
		||||
	*sql.DB
 | 
			
		||||
	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)
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	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
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func Open(dbName string) (*DB, error) {
 | 
			
		||||
	var err error
 | 
			
		||||
	db := new(DB)
 | 
			
		||||
 | 
			
		||||
	cfg := mysql.NewConfig()
 | 
			
		||||
	cfg.DBName = dbName
 | 
			
		||||
	cfg.User, cfg.Passwd, err = getCredentials()
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return nil, fmt.Errorf("Open: getCredentials(): %v\n", err)
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	db.Name = dbName
 | 
			
		||||
	db.DB, err = sql.Open("mysql", cfg.FormatDSN())
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return nil, fmt.Errorf("Open: sql.Open(\"mysql\", cfg.FormatDSN()): %v\n", err)
 | 
			
		||||
	}
 | 
			
		||||
	if err := db.Ping(); err != nil {
 | 
			
		||||
		return nil, fmt.Errorf("Open: db.Ping(): %v\n", err)
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	return db, nil
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func (db *DB) WriteBriefing(b *types.Briefing) error {
 | 
			
		||||
	for i := 0; i < len(b.Participants); i++ {
 | 
			
		||||
		result, err := db.Exec("INSERT INTO "+db.Name+" (instructor_first,"+
 | 
			
		||||
			"instructor_last, date, time, state, location, participant_first,"+
 | 
			
		||||
			"participant_last, company) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)",
 | 
			
		||||
			b.FirstName, b.LastName, b.Date, b.Time, b.State, b.Location,
 | 
			
		||||
			b.Participants[i].FirstName, b.Participants[i].LastName,
 | 
			
		||||
			b.Participants[i].Company)
 | 
			
		||||
		if err != nil {
 | 
			
		||||
			return fmt.Errorf("*DB.WriteBriefing: db.Exec(\"INSERT INTO"+
 | 
			
		||||
				"\"+db.Name+\" (instructor_first, instructor_last, date, time, state,"+
 | 
			
		||||
				"location, participant_first, participant_last, company) VALUES (?, ?,"+
 | 
			
		||||
				"?, ?, ?, ?, ?, ?, ?)\", b.FirstName, b.LastName, b.Date, b.Time,"+
 | 
			
		||||
				"b.State, b.Location, b.Participants[i].FirstName,"+
 | 
			
		||||
				"b.Participants[i].LastName, b.Participants[i].Company): %v\n", err)
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		_, err = result.LastInsertId()
 | 
			
		||||
		if err != nil {
 | 
			
		||||
			return fmt.Errorf("*DB.WriteBriefing: result.LastInsertId(): %v\n", err)
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	return nil
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func (db *DB) ReadByName(name string) (*[]types.Briefing, error) {
 | 
			
		||||
	bs := make([]types.Briefing, 0)
 | 
			
		||||
 | 
			
		||||
	rows, err := db.Query("SELECT *"+
 | 
			
		||||
		" FROM "+db.Name+
 | 
			
		||||
		" WHERE instructor_first LIKE ?"+
 | 
			
		||||
		" OR instructor_last LIKE ?"+
 | 
			
		||||
		" OR participant_first LIKE ?"+
 | 
			
		||||
		" OR participant_last LIKE ?",
 | 
			
		||||
		"%"+name+"%", "%"+name+"%", "%"+name+"%", "%"+name+"%")
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return nil, fmt.Errorf("*DB.ReadByName: db.Query(\"SELECT *"+
 | 
			
		||||
			" FROM \"+db.Name+"+
 | 
			
		||||
			" WHERE instructor_first LIKE ?"+
 | 
			
		||||
			" OR instructor_last LIKE ?"+
 | 
			
		||||
			" OR participant_first LIKE ?"+
 | 
			
		||||
			" OR participant_last LIKE ?\"): %v\n", err)
 | 
			
		||||
	}
 | 
			
		||||
	defer rows.Close()
 | 
			
		||||
 | 
			
		||||
	for rows.Next() {
 | 
			
		||||
		b := new(types.Briefing)
 | 
			
		||||
		p := new(types.Participant)
 | 
			
		||||
 | 
			
		||||
		if err := rows.Scan(&p.ID, &b.FirstName, &b.LastName, &b.Date, &b.Time, &b.State,
 | 
			
		||||
			&b.Location, &p.FirstName, &p.LastName, &p.Company); err != nil {
 | 
			
		||||
			return nil, fmt.Errorf("*DB.ReadByName: rows.Scan(&p.ID, &b.FirstName,"+
 | 
			
		||||
				" &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)
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	return &bs, nil
 | 
			
		||||
}
 | 
			
		||||
@@ -1,49 +0,0 @@
 | 
			
		||||
package server
 | 
			
		||||
 | 
			
		||||
import (
 | 
			
		||||
	"fmt"
 | 
			
		||||
	"html/template"
 | 
			
		||||
	"log"
 | 
			
		||||
	"net/http"
 | 
			
		||||
	"streifling.com/jason/sicherheitsunterweisung/packages/types"
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
func DisplayForm(i *int64) http.HandlerFunc {
 | 
			
		||||
	return func(w http.ResponseWriter, r *http.Request) {
 | 
			
		||||
		template.Must(template.ParseFiles("templates/index.html")).Execute(w, i)
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func AddParticipant(i *int64) http.HandlerFunc {
 | 
			
		||||
	return func(w http.ResponseWriter, r *http.Request) {
 | 
			
		||||
		*i++
 | 
			
		||||
		template.Must(template.ParseFiles("templates/index.html", "templates/participant.html")).ExecuteTemplate(w, "participant", i)
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func SubmitForm(ch chan<- *types.Briefing, i, j *int64) http.HandlerFunc {
 | 
			
		||||
	return func(w http.ResponseWriter, r *http.Request) {
 | 
			
		||||
		b := new(types.Briefing)
 | 
			
		||||
 | 
			
		||||
		b.FirstName = r.PostFormValue("instructor-first")
 | 
			
		||||
		b.LastName = r.PostFormValue("instructor-last")
 | 
			
		||||
		b.Date = r.PostFormValue("date")
 | 
			
		||||
		b.Time = r.PostFormValue("time")
 | 
			
		||||
		b.State = r.PostFormValue("state")
 | 
			
		||||
		b.Location = r.PostFormValue("location")
 | 
			
		||||
 | 
			
		||||
		for ; *j <= *i; *j++ {
 | 
			
		||||
			b.Participants = append(b.Participants, types.Participant{
 | 
			
		||||
				ID: *j,
 | 
			
		||||
				Person: types.Person{
 | 
			
		||||
					FirstName: r.PostFormValue("participant-first-" + fmt.Sprint(*j)),
 | 
			
		||||
					LastName:  r.PostFormValue(("participant-last-" + fmt.Sprint(*j))),
 | 
			
		||||
				},
 | 
			
		||||
				Company: r.PostFormValue(("participant-company-" + fmt.Sprint(*j))),
 | 
			
		||||
			})
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		log.Println(b)
 | 
			
		||||
		ch <- b
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										254
									
								
								packages/session/handlerFuncs.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										254
									
								
								packages/session/handlerFuncs.go
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,254 @@
 | 
			
		||||
package session
 | 
			
		||||
 | 
			
		||||
import (
 | 
			
		||||
	"fmt"
 | 
			
		||||
	"html/template"
 | 
			
		||||
	"log"
 | 
			
		||||
	"net/http"
 | 
			
		||||
	"time"
 | 
			
		||||
 | 
			
		||||
	"github.com/google/uuid"
 | 
			
		||||
	"streifling.com/jason/sicherheitsunterweisung/packages/data"
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
func HandleInternalLogin(db *data.DB, ss *[]*Session, cs chan<- *Session) http.HandlerFunc {
 | 
			
		||||
	return func(w http.ResponseWriter, r *http.Request) {
 | 
			
		||||
		instructors, err := db.GetInstructors()
 | 
			
		||||
		if err != nil {
 | 
			
		||||
			http.Error(w, err.Error(), http.StatusInternalServerError)
 | 
			
		||||
			log.Panicln(err)
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		for _, i := range instructors {
 | 
			
		||||
			if r.PostFormValue("login") == fmt.Sprint(i.ID) {
 | 
			
		||||
				session := new(Session)
 | 
			
		||||
				session.ID = uuid.New()
 | 
			
		||||
				session.Briefing = new(data.Briefing)
 | 
			
		||||
				session.InstructorID = i.ID
 | 
			
		||||
				(*ss) = append((*ss), session)
 | 
			
		||||
				cs <- session
 | 
			
		||||
 | 
			
		||||
				data := new(tableHTMLData)
 | 
			
		||||
				data.SessionID = session.ID
 | 
			
		||||
				data.OTD, err = db.GetAllOverviewTableData()
 | 
			
		||||
				if err != nil {
 | 
			
		||||
					http.Error(w, err.Error(), http.StatusInternalServerError)
 | 
			
		||||
					log.Panicln(err)
 | 
			
		||||
				}
 | 
			
		||||
 | 
			
		||||
				template.Must(template.ParseFiles("templates/table.html")).ExecuteTemplate(w, "content", data)
 | 
			
		||||
				return
 | 
			
		||||
			}
 | 
			
		||||
		}
 | 
			
		||||
		template.Must(template.ParseFiles("templates/login.html")).ExecuteTemplate(w, "content", nil)
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func (s *Session) HandleSearch(db *data.DB) http.HandlerFunc {
 | 
			
		||||
	return func(w http.ResponseWriter, r *http.Request) {
 | 
			
		||||
		log.Println("hier")
 | 
			
		||||
		var err error
 | 
			
		||||
		data := tableHTMLData{}
 | 
			
		||||
 | 
			
		||||
		data.SessionID = s.ID
 | 
			
		||||
		data.OTD, err = db.GetOverviewTableDataByName(r.PostFormValue("search"))
 | 
			
		||||
		if err != nil {
 | 
			
		||||
			http.Error(w, err.Error(), http.StatusInternalServerError)
 | 
			
		||||
			log.Panicln(err)
 | 
			
		||||
		}
 | 
			
		||||
		template.Must(template.ParseFiles("templates/table.html")).ExecuteTemplate(w, "rows", data)
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func (s *Session) HandleNewBriefing() http.HandlerFunc {
 | 
			
		||||
	return func(w http.ResponseWriter, r *http.Request) {
 | 
			
		||||
		data := new(participantHTMLData)
 | 
			
		||||
		data.SessionID = s.ID
 | 
			
		||||
		template.Must(template.ParseFiles("templates/briefing.html")).ExecuteTemplate(w, "content", data)
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func (s *Session) HandleNewParticipant(cp chan<- *BriefingParticipant) http.HandlerFunc {
 | 
			
		||||
	return func(w http.ResponseWriter, r *http.Request) {
 | 
			
		||||
		var err error
 | 
			
		||||
		p := new(BriefingParticipant)
 | 
			
		||||
		p.Participant = new(data.Participant)
 | 
			
		||||
		p.NoIncorrect = -1
 | 
			
		||||
		p.AllowRetry = false
 | 
			
		||||
 | 
			
		||||
		p.Login, err = generateLogin()
 | 
			
		||||
		if err != nil {
 | 
			
		||||
			http.Error(w, err.Error(), http.StatusInternalServerError)
 | 
			
		||||
			log.Panicln(err)
 | 
			
		||||
		}
 | 
			
		||||
		s.Participants = append(s.Participants, p)
 | 
			
		||||
		cp <- p
 | 
			
		||||
 | 
			
		||||
		data := new(participantHTMLData)
 | 
			
		||||
		data.SessionID = s.ID
 | 
			
		||||
		data.Login = p.Login
 | 
			
		||||
		if err != nil {
 | 
			
		||||
			http.Error(w, err.Error(), http.StatusInternalServerError)
 | 
			
		||||
			log.Panicln(err)
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		template.Must(template.ParseFiles("templates/briefing.html")).ExecuteTemplate(w, "new", data)
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func (s *Session) HandleBriefingForm(db *data.DB) http.HandlerFunc {
 | 
			
		||||
	return func(w http.ResponseWriter, r *http.Request) {
 | 
			
		||||
		now := time.Now()
 | 
			
		||||
 | 
			
		||||
		s.Date = now.Format("2006-01-02")
 | 
			
		||||
		s.Time = now.Format("15:04:05")
 | 
			
		||||
		s.Location = r.PostFormValue("location")
 | 
			
		||||
		s.DocumentName = r.PostFormValue("document")
 | 
			
		||||
		s.AsOf = r.PostFormValue("as-of")
 | 
			
		||||
 | 
			
		||||
		err := db.WriteBriefing(s.Briefing)
 | 
			
		||||
		if err != nil {
 | 
			
		||||
			http.Error(w, err.Error(), http.StatusInternalServerError)
 | 
			
		||||
			log.Panicln(err)
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		data := new(summaryHTMLData)
 | 
			
		||||
		data.SessionID = s.ID
 | 
			
		||||
		data.ParticipantsData = make([]participantHTMLData, len(s.Participants))
 | 
			
		||||
		for i, p := range s.Participants {
 | 
			
		||||
			data.ParticipantsData[i].SessionID = s.ID
 | 
			
		||||
			data.ParticipantsData[i].BriefingParticipant = *p
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		template.Must(template.ParseFiles("templates/summary.html")).ExecuteTemplate(w, "content", data)
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func HandleExternalLogin(ss *[]*Session) http.HandlerFunc {
 | 
			
		||||
	return func(w http.ResponseWriter, r *http.Request) {
 | 
			
		||||
		session, participant, loginCorrect := findCorrectLogin(r.PostFormValue("login"), ss)
 | 
			
		||||
		if loginCorrect {
 | 
			
		||||
			data := new(participantHTMLData)
 | 
			
		||||
			data.SessionID = session.ID
 | 
			
		||||
			data.Login = participant.Login
 | 
			
		||||
 | 
			
		||||
			template.Must(template.ParseFiles("templates/participant.html")).ExecuteTemplate(w, "content", data)
 | 
			
		||||
		} else {
 | 
			
		||||
			template.Must(template.ParseFiles("templates/login.html")).ExecuteTemplate(w, "content", nil)
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func (s *Session) HandleParticipant(db *data.DB, p *BriefingParticipant) http.HandlerFunc {
 | 
			
		||||
	return func(w http.ResponseWriter, r *http.Request) {
 | 
			
		||||
		p.FirstName = r.PostFormValue("first-" + fmt.Sprint(p.Login))
 | 
			
		||||
		p.LastName = r.PostFormValue("last-" + fmt.Sprint(p.Login))
 | 
			
		||||
		p.Company = r.PostFormValue("company-" + fmt.Sprint(p.Login))
 | 
			
		||||
 | 
			
		||||
		err := db.WriteParticipant(p.Participant)
 | 
			
		||||
		if err != nil {
 | 
			
		||||
			http.Error(w, err.Error(), http.StatusInternalServerError)
 | 
			
		||||
			log.Panicln(err)
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		data := new(questionHTMLData)
 | 
			
		||||
		data.SessionID = s.ID
 | 
			
		||||
		data.Login = p.Login
 | 
			
		||||
		data.Question = s.Questions[0]
 | 
			
		||||
		data.QuestionID = 1
 | 
			
		||||
 | 
			
		||||
		template.Must(template.ParseFiles("templates/question.html")).ExecuteTemplate(w, "content", data)
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func (s *Session) HandleAnswer(db *data.DB, p *BriefingParticipant, i int64) http.HandlerFunc {
 | 
			
		||||
	return func(w http.ResponseWriter, r *http.Request) {
 | 
			
		||||
		if i < int64(len(s.Questions)) {
 | 
			
		||||
			if err := handleGivenAnswer(p, i-1, r); err != nil {
 | 
			
		||||
				http.Error(w, err.Error(), http.StatusInternalServerError)
 | 
			
		||||
				log.Panicln(err)
 | 
			
		||||
			}
 | 
			
		||||
 | 
			
		||||
			data := new(questionHTMLData)
 | 
			
		||||
			data.SessionID = s.ID
 | 
			
		||||
			data.Login = p.Login
 | 
			
		||||
			data.Question = s.Questions[i]
 | 
			
		||||
			data.QuestionID = i + 1
 | 
			
		||||
 | 
			
		||||
			template.Must(template.ParseFiles("templates/question.html")).ExecuteTemplate(w, "content", data)
 | 
			
		||||
		} else {
 | 
			
		||||
			if err := handleGivenAnswer(p, i-1, r); err != nil {
 | 
			
		||||
				http.Error(w, err.Error(), http.StatusInternalServerError)
 | 
			
		||||
				log.Panicln(err)
 | 
			
		||||
			}
 | 
			
		||||
 | 
			
		||||
			p.NoIncorrect = 0
 | 
			
		||||
			for i, q := range s.Questions {
 | 
			
		||||
				if p.GivenAnswers[i] != q.Correct {
 | 
			
		||||
					p.NoIncorrect++
 | 
			
		||||
				}
 | 
			
		||||
			}
 | 
			
		||||
 | 
			
		||||
			data := new(resultHTMLData)
 | 
			
		||||
			data.SessionID = s.ID
 | 
			
		||||
			data.BriefingParticipant = *p
 | 
			
		||||
			data.Questions = makeHTMLQuestions(s.Questions, p.GivenAnswers)
 | 
			
		||||
 | 
			
		||||
			if data.NoIncorrect == 0 {
 | 
			
		||||
				if err := db.WriteGivenAnswers(*s.Briefing, *p.Participant, s.Questions, p.GivenAnswers); err != nil {
 | 
			
		||||
					http.Error(w, err.Error(), http.StatusInternalServerError)
 | 
			
		||||
					log.Panicln(err)
 | 
			
		||||
				}
 | 
			
		||||
			}
 | 
			
		||||
 | 
			
		||||
			template.Must(template.ParseFiles("templates/result.html")).ExecuteTemplate(w, "content", data)
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func (s *Session) HandleAllowRetry(p *BriefingParticipant) http.HandlerFunc {
 | 
			
		||||
	return func(w http.ResponseWriter, r *http.Request) {
 | 
			
		||||
		p.NoIncorrect = -1
 | 
			
		||||
		p.AllowRetry = true
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func (s *Session) HandleRetry(p *BriefingParticipant, i *int) http.HandlerFunc {
 | 
			
		||||
	return func(w http.ResponseWriter, r *http.Request) {
 | 
			
		||||
		if p.AllowRetry {
 | 
			
		||||
			p.AllowRetry = false
 | 
			
		||||
			(*i) = 0
 | 
			
		||||
 | 
			
		||||
			data := new(questionHTMLData)
 | 
			
		||||
			data.SessionID = s.ID
 | 
			
		||||
			data.Login = p.Login
 | 
			
		||||
			data.Question = s.Questions[*i]
 | 
			
		||||
			data.QuestionID = int64(*i + 1)
 | 
			
		||||
 | 
			
		||||
			template.Must(template.ParseFiles("templates/question.html")).ExecuteTemplate(w, "content", data)
 | 
			
		||||
		} else {
 | 
			
		||||
			data := new(resultHTMLData)
 | 
			
		||||
			data.SessionID = s.ID
 | 
			
		||||
			data.BriefingParticipant = *p
 | 
			
		||||
			data.Questions = makeHTMLQuestions(s.Questions, p.GivenAnswers)
 | 
			
		||||
 | 
			
		||||
			template.Must(template.ParseFiles("templates/result.html")).ExecuteTemplate(w, "content", data)
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func (s *Session) HandleRefresh(p *BriefingParticipant) http.HandlerFunc {
 | 
			
		||||
	return func(w http.ResponseWriter, r *http.Request) {
 | 
			
		||||
		data := new(participantHTMLData)
 | 
			
		||||
		data.SessionID = s.ID
 | 
			
		||||
		data.BriefingParticipant = *p
 | 
			
		||||
 | 
			
		||||
		template.Must(template.ParseFiles("templates/summary.html")).ExecuteTemplate(w, "participant", data)
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func (s *Session) HandleBriefingDone() http.HandlerFunc {
 | 
			
		||||
	return func(w http.ResponseWriter, r *http.Request) {
 | 
			
		||||
		template.Must(template.ParseFiles("templates/login.html")).ExecuteTemplate(w, "content", nil)
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										84
									
								
								packages/session/helperFuncs.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										84
									
								
								packages/session/helperFuncs.go
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,84 @@
 | 
			
		||||
package session
 | 
			
		||||
 | 
			
		||||
import (
 | 
			
		||||
	"crypto/rand"
 | 
			
		||||
	"encoding/hex"
 | 
			
		||||
	"fmt"
 | 
			
		||||
	"net/http"
 | 
			
		||||
	"strconv"
 | 
			
		||||
 | 
			
		||||
	"streifling.com/jason/sicherheitsunterweisung/packages/data"
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
func generateLogin() (string, error) {
 | 
			
		||||
	bs := make([]byte, 4)
 | 
			
		||||
 | 
			
		||||
	if _, err := rand.Read(bs); err != nil {
 | 
			
		||||
		return "", fmt.Errorf("error: generateLogin: rand.Read(bs): %v", err)
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	return hex.EncodeToString(bs), nil
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func findCorrectLogin(l string, ss *[]*Session) (*Session, *BriefingParticipant, bool) {
 | 
			
		||||
	for _, session := range *ss {
 | 
			
		||||
		for _, p := range session.Participants {
 | 
			
		||||
			if l == p.Login {
 | 
			
		||||
				return session, p, true
 | 
			
		||||
			}
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
	return nil, nil, false
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func newParticipant(l string) (*data.Participant, error) {
 | 
			
		||||
	var err error
 | 
			
		||||
	p := new(data.Participant)
 | 
			
		||||
 | 
			
		||||
	p.ID, err = strconv.ParseInt(l, 10, 64)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return nil, fmt.Errorf("error: newParticipant: strconv.Atoi(): %v", err)
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	return p, nil
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func handleGivenAnswer(p *BriefingParticipant, i int64, r *http.Request) error {
 | 
			
		||||
	answer, err := strconv.Atoi(r.PostFormValue("answer"))
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return fmt.Errorf("error: handleGivenAnswer: strconv.Atoi(): %v", err)
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	p.GivenAnswers[i] = answer
 | 
			
		||||
	return nil
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func makeHTMLQuestions(sq []data.Question, givenAnswers []int) []resultQuestion {
 | 
			
		||||
	questions := make([]resultQuestion, 0)
 | 
			
		||||
	for i, q := range sq {
 | 
			
		||||
		question := new(resultQuestion)
 | 
			
		||||
		question.Text = q.Text
 | 
			
		||||
 | 
			
		||||
		question.Answers = make([]resultAnswer, 0)
 | 
			
		||||
		for j, a := range q.Answers {
 | 
			
		||||
			answer := new(resultAnswer)
 | 
			
		||||
			answer.Text = a.Text
 | 
			
		||||
 | 
			
		||||
			if j+1 == q.Correct {
 | 
			
		||||
				answer.Correct = true
 | 
			
		||||
			} else {
 | 
			
		||||
				answer.Correct = false
 | 
			
		||||
			}
 | 
			
		||||
 | 
			
		||||
			if j+1 == givenAnswers[i] {
 | 
			
		||||
				answer.Chosen = true
 | 
			
		||||
			} else {
 | 
			
		||||
				answer.Chosen = false
 | 
			
		||||
			}
 | 
			
		||||
			question.Answers = append(question.Answers, *answer)
 | 
			
		||||
		}
 | 
			
		||||
		questions = append(questions, *question)
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	return questions
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										43
									
								
								packages/session/htmlStructs.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										43
									
								
								packages/session/htmlStructs.go
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,43 @@
 | 
			
		||||
package session
 | 
			
		||||
 | 
			
		||||
import (
 | 
			
		||||
	"github.com/google/uuid"
 | 
			
		||||
	"streifling.com/jason/sicherheitsunterweisung/packages/data"
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
type tableHTMLData struct {
 | 
			
		||||
	SessionID uuid.UUID
 | 
			
		||||
	OTD       []data.OverviewTableData
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
type participantHTMLData struct {
 | 
			
		||||
	SessionID uuid.UUID
 | 
			
		||||
	BriefingParticipant
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
type questionHTMLData struct {
 | 
			
		||||
	participantHTMLData
 | 
			
		||||
	Question   data.Question
 | 
			
		||||
	QuestionID int64
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
type resultAnswer struct {
 | 
			
		||||
	Text    string
 | 
			
		||||
	Correct bool
 | 
			
		||||
	Chosen  bool
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
type resultQuestion struct {
 | 
			
		||||
	Text    string
 | 
			
		||||
	Answers []resultAnswer
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
type resultHTMLData struct {
 | 
			
		||||
	participantHTMLData
 | 
			
		||||
	Questions []resultQuestion
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
type summaryHTMLData struct {
 | 
			
		||||
	SessionID        uuid.UUID
 | 
			
		||||
	ParticipantsData []participantHTMLData
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										56
									
								
								packages/session/muxFuncs.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										56
									
								
								packages/session/muxFuncs.go
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,56 @@
 | 
			
		||||
package session
 | 
			
		||||
 | 
			
		||||
import (
 | 
			
		||||
	"fmt"
 | 
			
		||||
	"log"
 | 
			
		||||
	"net/http"
 | 
			
		||||
 | 
			
		||||
	"streifling.com/jason/sicherheitsunterweisung/packages/data"
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
func (mux *Mux) handleParticipants(db *data.DB, cp <-chan *BriefingParticipant, s *Session) {
 | 
			
		||||
	for p := range cp {
 | 
			
		||||
		p.GivenAnswers = make([]int, len(s.Questions))
 | 
			
		||||
 | 
			
		||||
		mux.HandleFunc("/submit-participant/"+fmt.Sprint(s.ID)+"/"+fmt.Sprint(p.Login)+"/", s.HandleParticipant(db, p))
 | 
			
		||||
		var i int
 | 
			
		||||
		for i = range s.Questions {
 | 
			
		||||
			mux.HandleFunc("/submit-answer/"+fmt.Sprint(s.ID)+"/"+fmt.Sprint(p.Login)+"/"+fmt.Sprint(i+1)+"/", s.HandleAnswer(db, p, int64(i+1)))
 | 
			
		||||
		}
 | 
			
		||||
		mux.HandleFunc("/allow-retry/"+fmt.Sprint(s.ID)+"/"+fmt.Sprint(p.Login)+"/", s.HandleAllowRetry(p))
 | 
			
		||||
		mux.HandleFunc("/retry/"+fmt.Sprint(s.ID)+"/"+fmt.Sprint(p.Login)+"/", s.HandleRetry(p, &i))
 | 
			
		||||
		mux.HandleFunc("/refresh-summary/"+fmt.Sprint(s.ID)+"/"+fmt.Sprint(p.Login)+"/", s.HandleRefresh(p))
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func NewMux() *Mux {
 | 
			
		||||
	mux := new(Mux)
 | 
			
		||||
	mux.ServeMux = http.NewServeMux()
 | 
			
		||||
	return mux
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func (mux *Mux) HandleSessions(db *data.DB, cs <-chan *Session, ss *[]*Session) {
 | 
			
		||||
	for s := range cs {
 | 
			
		||||
		(*ss) = append(*ss, s)
 | 
			
		||||
		participantChan := make(chan *BriefingParticipant)
 | 
			
		||||
		questionIDs := make([]string, 4)
 | 
			
		||||
 | 
			
		||||
		for i := 0; i < len(questionIDs); i++ {
 | 
			
		||||
			questionIDs[i] = fmt.Sprint(i + 1)
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		var err error
 | 
			
		||||
		s.Questions, err = db.GetQuestions(questionIDs)
 | 
			
		||||
		if err != nil {
 | 
			
		||||
			log.Fatalln(err)
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		mux.HandleFunc("/search/"+fmt.Sprint(s.ID)+"/", s.HandleSearch(db))
 | 
			
		||||
		mux.HandleFunc("/new-briefing/"+fmt.Sprint(s.ID)+"/", s.HandleNewBriefing())
 | 
			
		||||
		mux.HandleFunc("/new-participant/"+fmt.Sprint(s.ID)+"/", s.HandleNewParticipant(participantChan))
 | 
			
		||||
		mux.HandleFunc("/submit-form/"+fmt.Sprint(s.ID)+"/", s.HandleBriefingForm(db))
 | 
			
		||||
		mux.HandleFunc("/briefing-done/"+fmt.Sprint(s.ID)+"/", s.HandleBriefingDone())
 | 
			
		||||
 | 
			
		||||
		go mux.handleParticipants(db, participantChan, s)
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										27
									
								
								packages/session/sessionStructs.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										27
									
								
								packages/session/sessionStructs.go
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,27 @@
 | 
			
		||||
package session
 | 
			
		||||
 | 
			
		||||
import (
 | 
			
		||||
	"net/http"
 | 
			
		||||
 | 
			
		||||
	"github.com/google/uuid"
 | 
			
		||||
	"streifling.com/jason/sicherheitsunterweisung/packages/data"
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
type Mux struct {
 | 
			
		||||
	*http.ServeMux
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
type BriefingParticipant struct {
 | 
			
		||||
	*data.Participant
 | 
			
		||||
	Login        string
 | 
			
		||||
	GivenAnswers []int
 | 
			
		||||
	NoIncorrect  int
 | 
			
		||||
	AllowRetry   bool
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
type Session struct {
 | 
			
		||||
	ID uuid.UUID
 | 
			
		||||
	*data.Briefing
 | 
			
		||||
	Participants []*BriefingParticipant
 | 
			
		||||
	Questions    []data.Question
 | 
			
		||||
}
 | 
			
		||||
@@ -1,23 +0,0 @@
 | 
			
		||||
package types
 | 
			
		||||
 | 
			
		||||
type Person struct {
 | 
			
		||||
	FirstName string
 | 
			
		||||
	LastName  string
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
type Instructor Person
 | 
			
		||||
 | 
			
		||||
type Participant struct {
 | 
			
		||||
	ID int64
 | 
			
		||||
	Person
 | 
			
		||||
	Company string
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
type Briefing struct {
 | 
			
		||||
	Instructor
 | 
			
		||||
	Date         string
 | 
			
		||||
	Time         string
 | 
			
		||||
	State        string
 | 
			
		||||
	Location     string
 | 
			
		||||
	Participants []Participant
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										7
									
								
								static/css/style.css
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										7
									
								
								static/css/style.css
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,7 @@
 | 
			
		||||
.correct {
 | 
			
		||||
  color: #00ff00;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
.incorrect {
 | 
			
		||||
  color: #ff0000;
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										37
									
								
								templates/briefing.html
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										37
									
								
								templates/briefing.html
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,37 @@
 | 
			
		||||
{{define "add-buttons"}}
 | 
			
		||||
<div id="briefing-buttons">
 | 
			
		||||
  <button type="button" hx-post="/new-participant/{{.SessionID}}/" hx-target="#briefing-buttons" hx-swap="outerHTML">
 | 
			
		||||
    Neuer Teilnehmer
 | 
			
		||||
  </button>
 | 
			
		||||
 | 
			
		||||
  <button type="submit" hx-post="/submit-form/{{.SessionID}}/" hx-target="#content">
 | 
			
		||||
    Weiter
 | 
			
		||||
  </button>
 | 
			
		||||
</div>
 | 
			
		||||
{{end}}
 | 
			
		||||
 | 
			
		||||
{{define "new"}}
 | 
			
		||||
{{template "add-buttons" .}}
 | 
			
		||||
<p>{{.Login}}</p>
 | 
			
		||||
{{end}}
 | 
			
		||||
 | 
			
		||||
{{define "content"}}
 | 
			
		||||
<form>
 | 
			
		||||
  <div>
 | 
			
		||||
    <label for="location">Ort</label>
 | 
			
		||||
    <input id="location" name="location" required type="text" value="Werk Langenhagen" />
 | 
			
		||||
  </div>
 | 
			
		||||
 | 
			
		||||
  <div>
 | 
			
		||||
    <label for="document">Dokument</label>
 | 
			
		||||
    <input id="document" name="document" required type="text" value="ICL-1901-LGH" />
 | 
			
		||||
  </div>
 | 
			
		||||
 | 
			
		||||
  <div>
 | 
			
		||||
    <label for="as-of">Stand vom</label>
 | 
			
		||||
    <input id="as-of" name="as-of" required type="date" value="2021-02-01" />
 | 
			
		||||
  </div>
 | 
			
		||||
 | 
			
		||||
  {{template "add-buttons" .}}
 | 
			
		||||
</form>
 | 
			
		||||
{{end}}
 | 
			
		||||
@@ -11,57 +11,9 @@
 | 
			
		||||
<body>
 | 
			
		||||
  <h1>Sicherheitsunterweisung</h1>
 | 
			
		||||
 | 
			
		||||
  <form>
 | 
			
		||||
    <div id="instructor">
 | 
			
		||||
      <label for="instructor-first-input">Unterweiser Vorname</label>
 | 
			
		||||
      <input type="text" name="instructor-first" id="instructor-first-input" />
 | 
			
		||||
 | 
			
		||||
      <label for="instructor-last-input">Unterweiser Nachname</label>
 | 
			
		||||
      <input type="text" name="instructor-last" id="instructor-last-input" />
 | 
			
		||||
    </div>
 | 
			
		||||
 | 
			
		||||
    <div id="date">
 | 
			
		||||
      <label for="date-input">Datum</label>
 | 
			
		||||
      <input type="date" name="date" id="date-input" />
 | 
			
		||||
    </div>
 | 
			
		||||
 | 
			
		||||
    <div id="time">
 | 
			
		||||
      <label for="time-input">Uhrzeit</label>
 | 
			
		||||
      <input type="time" name="time" id="time-input" />
 | 
			
		||||
    </div>
 | 
			
		||||
 | 
			
		||||
    <div id="state">
 | 
			
		||||
      <label for="state-input">Stand vom</label>
 | 
			
		||||
      <input type="date" name="state" id="state-input" />
 | 
			
		||||
    </div>
 | 
			
		||||
 | 
			
		||||
    <div id="location">
 | 
			
		||||
      <label for="location-input">Ort</label>
 | 
			
		||||
      <input type="text" name="location" id="location-input" />
 | 
			
		||||
    </div>
 | 
			
		||||
 | 
			
		||||
    <div id="participants">
 | 
			
		||||
      <button type="button" hx-post="/add-participant/" hx-target="#participants" hx-swap="beforeend"
 | 
			
		||||
        hx-trigger="click">
 | 
			
		||||
        +
 | 
			
		||||
      </button>
 | 
			
		||||
 | 
			
		||||
      <div id="participant-{{ . }}">
 | 
			
		||||
        <label for="participant-first-input-{{ . }}">Vorname</label>
 | 
			
		||||
        <input type="text" name="participant-first-{{ . }}" id="participant-first-input-{{ . }}" />
 | 
			
		||||
 | 
			
		||||
        <label for="participant-last-input-{{ . }}">Nachname</label>
 | 
			
		||||
        <input type="text" name="participant-last-{{ . }}" id="participant-last-input-{{ . }}" />
 | 
			
		||||
 | 
			
		||||
        <label for="participant-company-input-{{ . }}">Firma</label>
 | 
			
		||||
        <input type="text" name="participant-company-{{ . }}" id="participant-company-input-{{ . }}" />
 | 
			
		||||
      </div>
 | 
			
		||||
    </div>
 | 
			
		||||
 | 
			
		||||
    <button type="submit" hx-post="/submit/">
 | 
			
		||||
      Senden
 | 
			
		||||
    </button>
 | 
			
		||||
  </form>
 | 
			
		||||
  <div id="content">
 | 
			
		||||
    {{template "content" .}}
 | 
			
		||||
  </div>
 | 
			
		||||
 | 
			
		||||
  <script src="/static/js/htmx.min.js" type="text/javascript"></script>
 | 
			
		||||
</body>
 | 
			
		||||
 
 | 
			
		||||
							
								
								
									
										17
									
								
								templates/login.html
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										17
									
								
								templates/login.html
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,17 @@
 | 
			
		||||
{{define "content"}}
 | 
			
		||||
<h2>Anmeldung</h2>
 | 
			
		||||
 | 
			
		||||
<form>
 | 
			
		||||
  <input autocomplete="off" id="login-input" name="login" placeholder="Code" required type="text" />
 | 
			
		||||
 | 
			
		||||
  <div>
 | 
			
		||||
    <button type="submit" hx-post="/internal-login/" hx-target="#content">
 | 
			
		||||
      Intern
 | 
			
		||||
    </button>
 | 
			
		||||
 | 
			
		||||
    <button type="submit" hx-post="/external-login/" hx-target="#content">
 | 
			
		||||
      Gast
 | 
			
		||||
    </button>
 | 
			
		||||
  </div>
 | 
			
		||||
</form>
 | 
			
		||||
{{end}}
 | 
			
		||||
@@ -1,12 +1,16 @@
 | 
			
		||||
{{ define "participant" }}
 | 
			
		||||
<div id="participant-{{ . }}">
 | 
			
		||||
  <label for="participant-first-input-{{ . }}">Vorname</label>
 | 
			
		||||
  <input type="text" name="participant-first-{{ . }}" id="participant-first-input-{{ . }}" />
 | 
			
		||||
{{define "content"}}
 | 
			
		||||
<form>
 | 
			
		||||
  <label for="first-{{.Login}}">Vorname</label>
 | 
			
		||||
  <input type="text" name="first-{{.Login}}" id="first-{{.Login}}" />
 | 
			
		||||
 | 
			
		||||
  <label for="participant-last-input-{{ . }}">Nachname</label>
 | 
			
		||||
  <input type="text" name="participant-last-{{ . }}" id="participant-last-input-{{ . }}" />
 | 
			
		||||
  <label for="last-{{.Login}}">Nachname</label>
 | 
			
		||||
  <input type="text" name="last-{{.Login}}" id="last-{{.Login}}" />
 | 
			
		||||
 | 
			
		||||
  <label for="participant-company-input-{{ . }}">Firma</label>
 | 
			
		||||
  <input type="text" name="participant-company-{{ . }}" id="participant-company-input-{{ . }}" />
 | 
			
		||||
</div>
 | 
			
		||||
{{ end }}
 | 
			
		||||
  <label for="company-{{.Login}}">Firma</label>
 | 
			
		||||
  <input type="text" name="company-{{.Login}}" id="company-{{.Login}}" />
 | 
			
		||||
 | 
			
		||||
  <button type="button" hx-post="/submit-participant/{{.SessionID}}/{{.Login}}/" hx-target="#content">
 | 
			
		||||
    Fertig
 | 
			
		||||
  </button>
 | 
			
		||||
</form>
 | 
			
		||||
{{end}}
 | 
			
		||||
 
 | 
			
		||||
							
								
								
									
										21
									
								
								templates/question.html
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										21
									
								
								templates/question.html
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,21 @@
 | 
			
		||||
{{define "answers"}}
 | 
			
		||||
{{range .Question.Answers}}
 | 
			
		||||
<div>
 | 
			
		||||
  <input type="radio" name="answer" id="answer-{{.ID}}" value="{{.ID}}" />
 | 
			
		||||
  <label for="answer-{{.ID}}">{{.Text}}</label>
 | 
			
		||||
</div>
 | 
			
		||||
{{end}}
 | 
			
		||||
{{end}}
 | 
			
		||||
 | 
			
		||||
{{define "content"}}
 | 
			
		||||
<h2>Frage {{.QuestionID}}</h2>
 | 
			
		||||
<p>{{.Question.Text}}</p>
 | 
			
		||||
 | 
			
		||||
<form>
 | 
			
		||||
  {{template "answers" .}}
 | 
			
		||||
 | 
			
		||||
  <button hx-post="/submit-answer/{{.SessionID}}/{{.Login}}/{{.QuestionID}}/" hx-target="#content" type="submit">
 | 
			
		||||
    Weiter
 | 
			
		||||
  </button>
 | 
			
		||||
</form>
 | 
			
		||||
{{end}}
 | 
			
		||||
							
								
								
									
										19
									
								
								templates/result.html
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										19
									
								
								templates/result.html
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,19 @@
 | 
			
		||||
{{define "answers"}}
 | 
			
		||||
{{range .Answers}}
 | 
			
		||||
<p class="{{if and .Chosen .Correct}} correct {{else if and .Chosen (not .Correct)}} incorrect {{end}}">
 | 
			
		||||
  {{.Text}}
 | 
			
		||||
</p>
 | 
			
		||||
{{end}}
 | 
			
		||||
{{end}}
 | 
			
		||||
 | 
			
		||||
{{define "content"}}
 | 
			
		||||
<p>{{.BriefingParticipant.NoIncorrect}} Fehler</p>
 | 
			
		||||
{{if gt .BriefingParticipant.NoIncorrect 0}}
 | 
			
		||||
<p>Bitte nachschulen lassen und anschließend wiederholen.</p>
 | 
			
		||||
<button hx-post="/retry/{{.SessionID}}/{{.Login}}/" hx-target="#content" type="submit">Wiederholen</button>
 | 
			
		||||
{{end}}
 | 
			
		||||
{{range .Questions}}
 | 
			
		||||
<p>{{.Text}}</p>
 | 
			
		||||
{{template "answers" .}}
 | 
			
		||||
{{end}}
 | 
			
		||||
{{end}}
 | 
			
		||||
							
								
								
									
										35
									
								
								templates/summary.html
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										35
									
								
								templates/summary.html
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,35 @@
 | 
			
		||||
{{define "refresh"}}
 | 
			
		||||
<button hx-post="/refresh-summary/{{.SessionID}}/{{.Login}}/" hx-target="#id-{{.Login}}"
 | 
			
		||||
  type="button">Aktualisieren</button>
 | 
			
		||||
{{end}}
 | 
			
		||||
 | 
			
		||||
{{define "retry"}}
 | 
			
		||||
<button hx-post="/allow-retry/{{.SessionID}}/{{.Login}}/" type="button">Wiederholen erlauben</button>
 | 
			
		||||
{{end}}
 | 
			
		||||
 | 
			
		||||
{{define "participant"}}
 | 
			
		||||
<div id="id-{{.Login}}">
 | 
			
		||||
  <h2>{{.Login}}</h2>
 | 
			
		||||
  {{if not .LastName}}
 | 
			
		||||
  {{template "refresh" .}}
 | 
			
		||||
  {{else}}
 | 
			
		||||
  <p>{{.FirstName}} {{.LastName}}</p>
 | 
			
		||||
  <p>{{.Company}}</p>
 | 
			
		||||
  {{if lt .NoIncorrect 0}}
 | 
			
		||||
  {{template "refresh" .}}
 | 
			
		||||
  {{else if gt .NoIncorrect 0}}
 | 
			
		||||
  {{template "retry" .}}
 | 
			
		||||
  {{template "refresh" .}}
 | 
			
		||||
  {{else}}
 | 
			
		||||
  <p>{{.NoIncorrect}} Fehler</p>
 | 
			
		||||
  {{end}}
 | 
			
		||||
  {{end}}
 | 
			
		||||
</div>
 | 
			
		||||
{{end}}
 | 
			
		||||
 | 
			
		||||
{{define "content"}}
 | 
			
		||||
<button hx-post="/briefing-done/{{.SessionID}}/" hx-target="#content" type="button">Beenden</button>
 | 
			
		||||
{{range .ParticipantsData}}
 | 
			
		||||
{{template "participant" .}}
 | 
			
		||||
{{end}}
 | 
			
		||||
{{end}}
 | 
			
		||||
							
								
								
									
										48
									
								
								templates/table.html
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										48
									
								
								templates/table.html
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,48 @@
 | 
			
		||||
{{define "rows"}}
 | 
			
		||||
{{range .OTD}}
 | 
			
		||||
<tr>
 | 
			
		||||
  <td>{{.InstructorFirstName}}</td>
 | 
			
		||||
  <td>{{.InstructorLastName}}</td>
 | 
			
		||||
  <td>{{.BriefingDate}}</td>
 | 
			
		||||
  <td>{{.BriefingTime}}</td>
 | 
			
		||||
  <td>{{.BriefingLocation}}</td>
 | 
			
		||||
  <td>{{.BriefingDocumentName}}</td>
 | 
			
		||||
  <td>{{.BriefingAsOf}}</td>
 | 
			
		||||
  <td>{{.ParticipantFirstName}}</td>
 | 
			
		||||
  <td>{{.ParticipantLastName}}</td>
 | 
			
		||||
  <td>{{.ParticipantCompany}}</td>
 | 
			
		||||
</tr>
 | 
			
		||||
{{end}}
 | 
			
		||||
{{end}}
 | 
			
		||||
 | 
			
		||||
{{define "content"}}
 | 
			
		||||
<form>
 | 
			
		||||
  <label for="search-input">Suche</label>
 | 
			
		||||
  <input type="text" name="search" id="search-input" hx-post="/search/{{.SessionID}}/" hx-target="#results">
 | 
			
		||||
</form>
 | 
			
		||||
 | 
			
		||||
<form>
 | 
			
		||||
  <button type="submit" hx-post="/new-briefing/{{.SessionID}}/" hx-target="#content">
 | 
			
		||||
    Neue Unterweisung
 | 
			
		||||
  </button>
 | 
			
		||||
</form>
 | 
			
		||||
 | 
			
		||||
<table>
 | 
			
		||||
  <thead>
 | 
			
		||||
    <tr>
 | 
			
		||||
      <th colspan="2">Unterweiser</th>
 | 
			
		||||
      <th>Datum</th>
 | 
			
		||||
      <th>Uhrzeit</th>
 | 
			
		||||
      <th>Ort</th>
 | 
			
		||||
      <th>Dokument</th>
 | 
			
		||||
      <th>Stand</th>
 | 
			
		||||
      <th colspan="2">Teilnehmer</th>
 | 
			
		||||
      <th>Firma</th>
 | 
			
		||||
    </tr>
 | 
			
		||||
  </thead>
 | 
			
		||||
 | 
			
		||||
  <tbody id="results">
 | 
			
		||||
    {{template "rows" .}}
 | 
			
		||||
  </tbody>
 | 
			
		||||
</table>
 | 
			
		||||
{{end}}
 | 
			
		||||
		Reference in New Issue
	
	Block a user