Instanziierung von structs an passenden Stellen auf struct{}-Schreibweise umgestellt
This commit is contained in:
parent
6404792946
commit
3b641a90a9
@ -127,7 +127,7 @@ func (db *DB) GetAllOverviewTableData() ([]OverviewTableData, error) {
|
|||||||
|
|
||||||
data := make([]OverviewTableData, 0)
|
data := make([]OverviewTableData, 0)
|
||||||
for rows.Next() {
|
for rows.Next() {
|
||||||
otd := new(OverviewTableData)
|
otd := OverviewTableData{}
|
||||||
|
|
||||||
err := rows.Scan(
|
err := rows.Scan(
|
||||||
&otd.InstructorFirstName,
|
&otd.InstructorFirstName,
|
||||||
@ -145,7 +145,7 @@ func (db *DB) GetAllOverviewTableData() ([]OverviewTableData, error) {
|
|||||||
return nil, fmt.Errorf("error: *DB.ReadAllBriefings: rows.Scan(): %v", err)
|
return nil, fmt.Errorf("error: *DB.ReadAllBriefings: rows.Scan(): %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
data = append(data, *otd)
|
data = append(data, otd)
|
||||||
}
|
}
|
||||||
|
|
||||||
return data, nil
|
return data, nil
|
||||||
@ -194,7 +194,7 @@ func (db *DB) GetOverviewTableDataByName(n string) ([]OverviewTableData, error)
|
|||||||
|
|
||||||
data := make([]OverviewTableData, 0)
|
data := make([]OverviewTableData, 0)
|
||||||
for rows.Next() {
|
for rows.Next() {
|
||||||
otd := new(OverviewTableData)
|
otd := OverviewTableData{}
|
||||||
|
|
||||||
err := rows.Scan(
|
err := rows.Scan(
|
||||||
&otd.InstructorFirstName,
|
&otd.InstructorFirstName,
|
||||||
@ -212,7 +212,7 @@ func (db *DB) GetOverviewTableDataByName(n string) ([]OverviewTableData, error)
|
|||||||
return nil, fmt.Errorf("error: *DB.ReadAllBriefings: rows.Scan(): %v", err)
|
return nil, fmt.Errorf("error: *DB.ReadAllBriefings: rows.Scan(): %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
data = append(data, *otd)
|
data = append(data, otd)
|
||||||
}
|
}
|
||||||
|
|
||||||
return data, nil
|
return data, nil
|
||||||
@ -253,9 +253,11 @@ func (db *DB) GetInstructors() ([]*Instructor, error) {
|
|||||||
instructors := make([]*Instructor, 0)
|
instructors := make([]*Instructor, 0)
|
||||||
for rows.Next() {
|
for rows.Next() {
|
||||||
instructor := new(Instructor)
|
instructor := new(Instructor)
|
||||||
|
|
||||||
if err = rows.Scan(&instructor.ID, &instructor.FirstName, &instructor.LastName); err != nil {
|
if err = rows.Scan(&instructor.ID, &instructor.FirstName, &instructor.LastName); err != nil {
|
||||||
return nil, fmt.Errorf("error: *DB.GetInstructors: rows.Scan(): %v", err)
|
return nil, fmt.Errorf("error: *DB.GetInstructors: rows.Scan(): %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
instructors = append(instructors, instructor)
|
instructors = append(instructors, instructor)
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -278,27 +280,21 @@ func (db *DB) GetQuestions(nums []string) ([]Question, error) {
|
|||||||
// TODO: not scalable
|
// TODO: not scalable
|
||||||
questions := make([]Question, 0)
|
questions := make([]Question, 0)
|
||||||
for rows.Next() {
|
for rows.Next() {
|
||||||
q := new(Question)
|
q := Question{}
|
||||||
a1 := new(Answer)
|
a1 := Answer{ID: 1}
|
||||||
a2 := new(Answer)
|
a2 := Answer{ID: 2}
|
||||||
a3 := new(Answer)
|
a3 := Answer{ID: 3}
|
||||||
a4 := new(Answer)
|
a4 := Answer{ID: 4}
|
||||||
|
|
||||||
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 {
|
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)
|
return nil, fmt.Errorf("error: *DB.GetQuestions: rows.Scan(): %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
q.Answers = append(q.Answers, *a1)
|
q.Answers = append(q.Answers, a1)
|
||||||
q.Answers = append(q.Answers, *a2)
|
q.Answers = append(q.Answers, a2)
|
||||||
q.Answers = append(q.Answers, *a3)
|
q.Answers = append(q.Answers, a3)
|
||||||
q.Answers = append(q.Answers, *a4)
|
q.Answers = append(q.Answers, a4)
|
||||||
|
questions = append(questions, q)
|
||||||
questions = append(questions, *q)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return questions, nil
|
return questions, nil
|
||||||
|
@ -21,15 +21,14 @@ func HandleInternalLogin(db *data.DB, ss *[]*Session, cs chan<- *Session) http.H
|
|||||||
|
|
||||||
for _, i := range instructors {
|
for _, i := range instructors {
|
||||||
if r.PostFormValue("login") == fmt.Sprint(i.ID) {
|
if r.PostFormValue("login") == fmt.Sprint(i.ID) {
|
||||||
session := new(Session)
|
session := Session{
|
||||||
session.ID = uuid.New()
|
ID: uuid.New(),
|
||||||
session.Briefing = new(data.Briefing)
|
Briefing: &data.Briefing{InstructorID: i.ID},
|
||||||
session.InstructorID = i.ID
|
}
|
||||||
(*ss) = append((*ss), session)
|
(*ss) = append((*ss), &session)
|
||||||
cs <- session
|
cs <- &session
|
||||||
|
|
||||||
data := new(tableHTMLData)
|
data := tableHTMLData{SessionID: session.ID}
|
||||||
data.SessionID = session.ID
|
|
||||||
data.OTD, err = db.GetAllOverviewTableData()
|
data.OTD, err = db.GetAllOverviewTableData()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||||
@ -61,34 +60,30 @@ func (s *Session) HandleSearch(db *data.DB) http.HandlerFunc {
|
|||||||
|
|
||||||
func (s *Session) HandleNewBriefing() http.HandlerFunc {
|
func (s *Session) HandleNewBriefing() http.HandlerFunc {
|
||||||
return func(w http.ResponseWriter, r *http.Request) {
|
return func(w http.ResponseWriter, r *http.Request) {
|
||||||
data := new(participantHTMLData)
|
template.Must(template.ParseFiles("templates/briefing.html")).ExecuteTemplate(w, "content", participantHTMLData{SessionID: s.ID})
|
||||||
data.SessionID = s.ID
|
|
||||||
template.Must(template.ParseFiles("templates/briefing.html")).ExecuteTemplate(w, "content", data)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *Session) HandleNewParticipant(cp chan<- *BriefingParticipant) http.HandlerFunc {
|
func (s *Session) HandleNewParticipant(cp chan<- *BriefingParticipant) http.HandlerFunc {
|
||||||
return func(w http.ResponseWriter, r *http.Request) {
|
return func(w http.ResponseWriter, r *http.Request) {
|
||||||
var err error
|
var err error
|
||||||
p := new(BriefingParticipant)
|
participant := BriefingParticipant{
|
||||||
p.Participant = new(data.Participant)
|
Participant: new(data.Participant),
|
||||||
p.NoIncorrect = -1
|
NoIncorrect: -1,
|
||||||
p.AllowRetry = false
|
AllowRetry: false,
|
||||||
|
}
|
||||||
|
|
||||||
p.Login, err = generateLogin()
|
participant.Login, err = generateLogin()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||||
log.Panicln(err)
|
log.Panicln(err)
|
||||||
}
|
}
|
||||||
s.Participants = append(s.Participants, p)
|
s.Participants = append(s.Participants, &participant)
|
||||||
cp <- p
|
cp <- &participant
|
||||||
|
|
||||||
data := new(participantHTMLData)
|
data := participantHTMLData{
|
||||||
data.SessionID = s.ID
|
SessionID: s.ID,
|
||||||
data.Login = p.Login
|
BriefingParticipant: BriefingParticipant{Login: participant.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)
|
template.Must(template.ParseFiles("templates/briefing.html")).ExecuteTemplate(w, "new", data)
|
||||||
@ -111,9 +106,10 @@ func (s *Session) HandleBriefingForm(db *data.DB) http.HandlerFunc {
|
|||||||
log.Panicln(err)
|
log.Panicln(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
data := new(summaryHTMLData)
|
data := summaryHTMLData{
|
||||||
data.SessionID = s.ID
|
SessionID: s.ID,
|
||||||
data.ParticipantsData = make([]participantHTMLData, len(s.Participants))
|
ParticipantsData: make([]participantHTMLData, len(s.Participants)),
|
||||||
|
}
|
||||||
for i, p := range s.Participants {
|
for i, p := range s.Participants {
|
||||||
data.ParticipantsData[i].SessionID = s.ID
|
data.ParticipantsData[i].SessionID = s.ID
|
||||||
data.ParticipantsData[i].BriefingParticipant = *p
|
data.ParticipantsData[i].BriefingParticipant = *p
|
||||||
@ -127,9 +123,10 @@ func HandleExternalLogin(ss *[]*Session) http.HandlerFunc {
|
|||||||
return func(w http.ResponseWriter, r *http.Request) {
|
return func(w http.ResponseWriter, r *http.Request) {
|
||||||
session, participant, loginCorrect := findCorrectLogin(r.PostFormValue("login"), ss)
|
session, participant, loginCorrect := findCorrectLogin(r.PostFormValue("login"), ss)
|
||||||
if loginCorrect {
|
if loginCorrect {
|
||||||
data := new(participantHTMLData)
|
data := participantHTMLData{
|
||||||
data.SessionID = session.ID
|
SessionID: session.ID,
|
||||||
data.Login = participant.Login
|
BriefingParticipant: BriefingParticipant{Login: participant.Login},
|
||||||
|
}
|
||||||
|
|
||||||
template.Must(template.ParseFiles("templates/participant.html")).ExecuteTemplate(w, "content", data)
|
template.Must(template.ParseFiles("templates/participant.html")).ExecuteTemplate(w, "content", data)
|
||||||
} else {
|
} else {
|
||||||
@ -150,11 +147,14 @@ func (s *Session) HandleParticipant(db *data.DB, p *BriefingParticipant) http.Ha
|
|||||||
log.Panicln(err)
|
log.Panicln(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
data := new(questionHTMLData)
|
data := questionHTMLData{
|
||||||
data.SessionID = s.ID
|
participantHTMLData: participantHTMLData{
|
||||||
data.Login = p.Login
|
SessionID: s.ID,
|
||||||
data.Question = s.Questions[0]
|
BriefingParticipant: BriefingParticipant{Login: p.Login},
|
||||||
data.QuestionID = 1
|
},
|
||||||
|
QuestionID: 1,
|
||||||
|
Question: s.Questions[0],
|
||||||
|
}
|
||||||
|
|
||||||
template.Must(template.ParseFiles("templates/question.html")).ExecuteTemplate(w, "content", data)
|
template.Must(template.ParseFiles("templates/question.html")).ExecuteTemplate(w, "content", data)
|
||||||
}
|
}
|
||||||
@ -168,11 +168,14 @@ func (s *Session) HandleAnswer(db *data.DB, p *BriefingParticipant, i int64) htt
|
|||||||
log.Panicln(err)
|
log.Panicln(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
data := new(questionHTMLData)
|
data := questionHTMLData{
|
||||||
data.SessionID = s.ID
|
participantHTMLData: participantHTMLData{
|
||||||
data.Login = p.Login
|
SessionID: s.ID,
|
||||||
data.Question = s.Questions[i]
|
BriefingParticipant: BriefingParticipant{Login: p.Login},
|
||||||
data.QuestionID = i + 1
|
},
|
||||||
|
QuestionID: i + 1,
|
||||||
|
Question: s.Questions[i],
|
||||||
|
}
|
||||||
|
|
||||||
template.Must(template.ParseFiles("templates/question.html")).ExecuteTemplate(w, "content", data)
|
template.Must(template.ParseFiles("templates/question.html")).ExecuteTemplate(w, "content", data)
|
||||||
} else {
|
} else {
|
||||||
@ -188,10 +191,13 @@ func (s *Session) HandleAnswer(db *data.DB, p *BriefingParticipant, i int64) htt
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
data := new(resultHTMLData)
|
data := resultHTMLData{
|
||||||
data.SessionID = s.ID
|
participantHTMLData: participantHTMLData{
|
||||||
data.BriefingParticipant = *p
|
SessionID: s.ID,
|
||||||
data.Questions = makeHTMLQuestions(s.Questions, p.GivenAnswers)
|
BriefingParticipant: *p,
|
||||||
|
},
|
||||||
|
Questions: makeHTMLQuestions(s.Questions, p.GivenAnswers),
|
||||||
|
}
|
||||||
|
|
||||||
if data.NoIncorrect == 0 {
|
if data.NoIncorrect == 0 {
|
||||||
if err := db.WriteGivenAnswers(*s.Briefing, *p.Participant, s.Questions, p.GivenAnswers); err != nil {
|
if err := db.WriteGivenAnswers(*s.Briefing, *p.Participant, s.Questions, p.GivenAnswers); err != nil {
|
||||||
@ -218,18 +224,24 @@ func (s *Session) HandleRetry(p *BriefingParticipant, i *int) http.HandlerFunc {
|
|||||||
p.AllowRetry = false
|
p.AllowRetry = false
|
||||||
(*i) = 0
|
(*i) = 0
|
||||||
|
|
||||||
data := new(questionHTMLData)
|
data := questionHTMLData{
|
||||||
data.SessionID = s.ID
|
participantHTMLData: participantHTMLData{
|
||||||
data.Login = p.Login
|
SessionID: s.ID,
|
||||||
data.Question = s.Questions[*i]
|
BriefingParticipant: BriefingParticipant{Login: p.Login},
|
||||||
data.QuestionID = int64(*i + 1)
|
},
|
||||||
|
QuestionID: int64(*i + 1),
|
||||||
|
Question: s.Questions[*i],
|
||||||
|
}
|
||||||
|
|
||||||
template.Must(template.ParseFiles("templates/question.html")).ExecuteTemplate(w, "content", data)
|
template.Must(template.ParseFiles("templates/question.html")).ExecuteTemplate(w, "content", data)
|
||||||
} else {
|
} else {
|
||||||
data := new(resultHTMLData)
|
data := resultHTMLData{
|
||||||
data.SessionID = s.ID
|
participantHTMLData: participantHTMLData{
|
||||||
data.BriefingParticipant = *p
|
SessionID: s.ID,
|
||||||
data.Questions = makeHTMLQuestions(s.Questions, p.GivenAnswers)
|
BriefingParticipant: *p,
|
||||||
|
},
|
||||||
|
Questions: makeHTMLQuestions(s.Questions, p.GivenAnswers),
|
||||||
|
}
|
||||||
|
|
||||||
template.Must(template.ParseFiles("templates/result.html")).ExecuteTemplate(w, "content", data)
|
template.Must(template.ParseFiles("templates/result.html")).ExecuteTemplate(w, "content", data)
|
||||||
}
|
}
|
||||||
@ -238,9 +250,10 @@ func (s *Session) HandleRetry(p *BriefingParticipant, i *int) http.HandlerFunc {
|
|||||||
|
|
||||||
func (s *Session) HandleRefresh(p *BriefingParticipant) http.HandlerFunc {
|
func (s *Session) HandleRefresh(p *BriefingParticipant) http.HandlerFunc {
|
||||||
return func(w http.ResponseWriter, r *http.Request) {
|
return func(w http.ResponseWriter, r *http.Request) {
|
||||||
data := new(participantHTMLData)
|
data := participantHTMLData{
|
||||||
data.SessionID = s.ID
|
SessionID: s.ID,
|
||||||
data.BriefingParticipant = *p
|
BriefingParticipant: *p,
|
||||||
|
}
|
||||||
|
|
||||||
template.Must(template.ParseFiles("templates/summary.html")).ExecuteTemplate(w, "participant", data)
|
template.Must(template.ParseFiles("templates/summary.html")).ExecuteTemplate(w, "participant", data)
|
||||||
}
|
}
|
||||||
|
@ -54,15 +54,15 @@ func handleGivenAnswer(p *BriefingParticipant, i int64, r *http.Request) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func makeHTMLQuestions(sq []data.Question, givenAnswers []int) []resultQuestion {
|
func makeHTMLQuestions(sq []data.Question, givenAnswers []int) []resultQuestion {
|
||||||
questions := make([]resultQuestion, 0)
|
questions := make([]resultQuestion, len(sq))
|
||||||
for i, q := range sq {
|
for i, q := range sq {
|
||||||
question := new(resultQuestion)
|
question := resultQuestion{
|
||||||
question.Text = q.Text
|
Text: q.Text,
|
||||||
|
Answers: make([]resultAnswer, len(q.Answers)),
|
||||||
|
}
|
||||||
|
|
||||||
question.Answers = make([]resultAnswer, 0)
|
|
||||||
for j, a := range q.Answers {
|
for j, a := range q.Answers {
|
||||||
answer := new(resultAnswer)
|
answer := resultAnswer{Text: a.Text}
|
||||||
answer.Text = a.Text
|
|
||||||
|
|
||||||
if j+1 == q.Correct {
|
if j+1 == q.Correct {
|
||||||
answer.Correct = true
|
answer.Correct = true
|
||||||
@ -75,9 +75,9 @@ func makeHTMLQuestions(sq []data.Question, givenAnswers []int) []resultQuestion
|
|||||||
} else {
|
} else {
|
||||||
answer.Chosen = false
|
answer.Chosen = false
|
||||||
}
|
}
|
||||||
question.Answers = append(question.Answers, *answer)
|
question.Answers[j] = answer
|
||||||
}
|
}
|
||||||
questions = append(questions, *question)
|
questions[i] = question
|
||||||
}
|
}
|
||||||
|
|
||||||
return questions
|
return questions
|
||||||
|
@ -17,8 +17,8 @@ type participantHTMLData struct {
|
|||||||
|
|
||||||
type questionHTMLData struct {
|
type questionHTMLData struct {
|
||||||
participantHTMLData
|
participantHTMLData
|
||||||
Question data.Question
|
|
||||||
QuestionID int64
|
QuestionID int64
|
||||||
|
Question data.Question
|
||||||
}
|
}
|
||||||
|
|
||||||
type resultAnswer struct {
|
type resultAnswer struct {
|
||||||
|
@ -24,9 +24,7 @@ func (mux *Mux) handleParticipants(db *data.DB, cp <-chan *BriefingParticipant,
|
|||||||
}
|
}
|
||||||
|
|
||||||
func NewMux() *Mux {
|
func NewMux() *Mux {
|
||||||
mux := new(Mux)
|
return &Mux{ServeMux: http.NewServeMux()}
|
||||||
mux.ServeMux = http.NewServeMux()
|
|
||||||
return mux
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (mux *Mux) HandleSessions(db *data.DB, cs <-chan *Session, ss *[]*Session) {
|
func (mux *Mux) HandleSessions(db *data.DB, cs <-chan *Session, ss *[]*Session) {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user