Instanziierung von structs an passenden Stellen auf struct{}-Schreibweise umgestellt

This commit is contained in:
Jason Streifling 2023-11-01 09:18:48 +01:00
parent 6404792946
commit 3b641a90a9
5 changed files with 95 additions and 88 deletions

View File

@ -127,7 +127,7 @@ func (db *DB) GetAllOverviewTableData() ([]OverviewTableData, error) {
data := make([]OverviewTableData, 0)
for rows.Next() {
otd := new(OverviewTableData)
otd := OverviewTableData{}
err := rows.Scan(
&otd.InstructorFirstName,
@ -145,7 +145,7 @@ func (db *DB) GetAllOverviewTableData() ([]OverviewTableData, error) {
return nil, fmt.Errorf("error: *DB.ReadAllBriefings: rows.Scan(): %v", err)
}
data = append(data, *otd)
data = append(data, otd)
}
return data, nil
@ -194,7 +194,7 @@ func (db *DB) GetOverviewTableDataByName(n string) ([]OverviewTableData, error)
data := make([]OverviewTableData, 0)
for rows.Next() {
otd := new(OverviewTableData)
otd := OverviewTableData{}
err := rows.Scan(
&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)
}
data = append(data, *otd)
data = append(data, otd)
}
return data, nil
@ -253,9 +253,11 @@ func (db *DB) GetInstructors() ([]*Instructor, error) {
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)
}
@ -278,27 +280,21 @@ func (db *DB) GetQuestions(nums []string) ([]Question, error) {
// 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
q := Question{}
a1 := Answer{ID: 1}
a2 := Answer{ID: 2}
a3 := Answer{ID: 3}
a4 := Answer{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)
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

View File

@ -21,15 +21,14 @@ func HandleInternalLogin(db *data.DB, ss *[]*Session, cs chan<- *Session) http.H
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
session := Session{
ID: uuid.New(),
Briefing: &data.Briefing{InstructorID: i.ID},
}
(*ss) = append((*ss), &session)
cs <- &session
data := new(tableHTMLData)
data.SessionID = session.ID
data := tableHTMLData{SessionID: session.ID}
data.OTD, err = db.GetAllOverviewTableData()
if err != nil {
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 {
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)
template.Must(template.ParseFiles("templates/briefing.html")).ExecuteTemplate(w, "content", participantHTMLData{SessionID: s.ID})
}
}
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
participant := BriefingParticipant{
Participant: new(data.Participant),
NoIncorrect: -1,
AllowRetry: false,
}
p.Login, err = generateLogin()
participant.Login, err = generateLogin()
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
log.Panicln(err)
}
s.Participants = append(s.Participants, p)
cp <- p
s.Participants = append(s.Participants, &participant)
cp <- &participant
data := new(participantHTMLData)
data.SessionID = s.ID
data.Login = p.Login
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
log.Panicln(err)
data := participantHTMLData{
SessionID: s.ID,
BriefingParticipant: BriefingParticipant{Login: participant.Login},
}
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)
}
data := new(summaryHTMLData)
data.SessionID = s.ID
data.ParticipantsData = make([]participantHTMLData, len(s.Participants))
data := summaryHTMLData{
SessionID: s.ID,
ParticipantsData: make([]participantHTMLData, len(s.Participants)),
}
for i, p := range s.Participants {
data.ParticipantsData[i].SessionID = s.ID
data.ParticipantsData[i].BriefingParticipant = *p
@ -127,9 +123,10 @@ 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
data := participantHTMLData{
SessionID: session.ID,
BriefingParticipant: BriefingParticipant{Login: participant.Login},
}
template.Must(template.ParseFiles("templates/participant.html")).ExecuteTemplate(w, "content", data)
} else {
@ -150,11 +147,14 @@ func (s *Session) HandleParticipant(db *data.DB, p *BriefingParticipant) http.Ha
log.Panicln(err)
}
data := new(questionHTMLData)
data.SessionID = s.ID
data.Login = p.Login
data.Question = s.Questions[0]
data.QuestionID = 1
data := questionHTMLData{
participantHTMLData: participantHTMLData{
SessionID: s.ID,
BriefingParticipant: BriefingParticipant{Login: p.Login},
},
QuestionID: 1,
Question: s.Questions[0],
}
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)
}
data := new(questionHTMLData)
data.SessionID = s.ID
data.Login = p.Login
data.Question = s.Questions[i]
data.QuestionID = i + 1
data := questionHTMLData{
participantHTMLData: participantHTMLData{
SessionID: s.ID,
BriefingParticipant: BriefingParticipant{Login: p.Login},
},
QuestionID: i + 1,
Question: s.Questions[i],
}
template.Must(template.ParseFiles("templates/question.html")).ExecuteTemplate(w, "content", data)
} else {
@ -188,10 +191,13 @@ func (s *Session) HandleAnswer(db *data.DB, p *BriefingParticipant, i int64) htt
}
}
data := new(resultHTMLData)
data.SessionID = s.ID
data.BriefingParticipant = *p
data.Questions = makeHTMLQuestions(s.Questions, p.GivenAnswers)
data := resultHTMLData{
participantHTMLData: participantHTMLData{
SessionID: s.ID,
BriefingParticipant: *p,
},
Questions: makeHTMLQuestions(s.Questions, p.GivenAnswers),
}
if data.NoIncorrect == 0 {
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
(*i) = 0
data := new(questionHTMLData)
data.SessionID = s.ID
data.Login = p.Login
data.Question = s.Questions[*i]
data.QuestionID = int64(*i + 1)
data := questionHTMLData{
participantHTMLData: participantHTMLData{
SessionID: s.ID,
BriefingParticipant: BriefingParticipant{Login: p.Login},
},
QuestionID: int64(*i + 1),
Question: s.Questions[*i],
}
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)
data := resultHTMLData{
participantHTMLData: participantHTMLData{
SessionID: s.ID,
BriefingParticipant: *p,
},
Questions: makeHTMLQuestions(s.Questions, p.GivenAnswers),
}
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 {
return func(w http.ResponseWriter, r *http.Request) {
data := new(participantHTMLData)
data.SessionID = s.ID
data.BriefingParticipant = *p
data := participantHTMLData{
SessionID: s.ID,
BriefingParticipant: *p,
}
template.Must(template.ParseFiles("templates/summary.html")).ExecuteTemplate(w, "participant", data)
}

View File

@ -54,15 +54,15 @@ func handleGivenAnswer(p *BriefingParticipant, i int64, r *http.Request) error {
}
func makeHTMLQuestions(sq []data.Question, givenAnswers []int) []resultQuestion {
questions := make([]resultQuestion, 0)
questions := make([]resultQuestion, len(sq))
for i, q := range sq {
question := new(resultQuestion)
question.Text = q.Text
question := resultQuestion{
Text: q.Text,
Answers: make([]resultAnswer, len(q.Answers)),
}
question.Answers = make([]resultAnswer, 0)
for j, a := range q.Answers {
answer := new(resultAnswer)
answer.Text = a.Text
answer := resultAnswer{Text: a.Text}
if j+1 == q.Correct {
answer.Correct = true
@ -75,9 +75,9 @@ func makeHTMLQuestions(sq []data.Question, givenAnswers []int) []resultQuestion
} else {
answer.Chosen = false
}
question.Answers = append(question.Answers, *answer)
question.Answers[j] = answer
}
questions = append(questions, *question)
questions[i] = question
}
return questions

View File

@ -17,8 +17,8 @@ type participantHTMLData struct {
type questionHTMLData struct {
participantHTMLData
Question data.Question
QuestionID int64
Question data.Question
}
type resultAnswer struct {

View File

@ -24,9 +24,7 @@ func (mux *Mux) handleParticipants(db *data.DB, cp <-chan *BriefingParticipant,
}
func NewMux() *Mux {
mux := new(Mux)
mux.ServeMux = http.NewServeMux()
return mux
return &Mux{ServeMux: http.NewServeMux()}
}
func (mux *Mux) HandleSessions(db *data.DB, cs <-chan *Session, ss *[]*Session) {