diff --git a/cmd/session/session.go b/cmd/session/session.go index 5ea746f..947a20f 100644 --- a/cmd/session/session.go +++ b/cmd/session/session.go @@ -52,8 +52,7 @@ type SessionModel struct { // store store *file.SessionFileStore lenStore int - - result []any + result []any // json InputJson string @@ -307,7 +306,7 @@ func (m *SessionModel) updateViewportContent(session *models.Session) { } -func (m *SessionModel) createMDRenderer(width int) *glamour.TermRenderer { +func (m *SessionModel) createMDRenderer() *glamour.TermRenderer { renderer, err := glamour.NewTermRenderer( glamour.WithStandardStyle("dracula"), glamour.WithWordWrap(m.viewport.GetWidth()), @@ -321,7 +320,7 @@ func (m *SessionModel) createMDRenderer(width int) *glamour.TermRenderer { func (m *SessionModel) handleWindowSize(msg tea.WindowSizeMsg) { m.group.SetSize(msg.Width, msg.Height) m.document.SetSize(msg.Width, msg.Height) - m.mdRenderer = m.createMDRenderer(msg.Width) + m.mdRenderer = m.createMDRenderer() } func (m *SessionModel) handleError(msg tea.Msg) { @@ -416,7 +415,7 @@ func (m *SessionModel) loadStore() tea.Cmd { return func() tea.Msg { sStore, err := file.NewDefaultSessionFileStore() if err != nil { - return errorMsg{err} + panic(err) } return storeLoadedMsg{sStore} diff --git a/pkg/models/exam.go b/pkg/models/exam.go index 00b9785..d967df5 100644 --- a/pkg/models/exam.go +++ b/pkg/models/exam.go @@ -15,7 +15,7 @@ type Exam struct { } func (e *Exam) String() string { - return fmt.Sprintf("Exam ID %v with %v quizzes.", e.ID, len(e.Quizzes)) + return fmt.Sprintf("%v's exam with %v quizzes.", e.Participant, len(e.Quizzes)) } func (e *Exam) GetHash() string { diff --git a/pkg/models/response.go b/pkg/models/response.go index 82eda44..71b511c 100644 --- a/pkg/models/response.go +++ b/pkg/models/response.go @@ -5,14 +5,21 @@ import ( "fmt" ) +type ParticipantAnswer struct { + Quiz *Quiz `json:"question"` + Answer *Answer `json:"answer"` + Correct bool `json:"correct"` +} + type Response struct { Meta - Questions map[string]string + SessionID string `json:"session_id"` + Answers []*ParticipantAnswer `json:"answers"` } func (r *Response) String() string { - return fmt.Sprintf("Questions/Answers: %v", r.Questions) + return fmt.Sprintf("Questions/Answers: %v", r.Answers) } func (r *Response) GetHash() string { diff --git a/pkg/models/session.go b/pkg/models/session.go index 49ff185..0e52303 100644 --- a/pkg/models/session.go +++ b/pkg/models/session.go @@ -9,8 +9,11 @@ import ( type Session struct { Meta - Title string `json:"title"` - Exams map[string]*Exam `json:"exams"` + Title string `json:"title"` + Description string `json:"description"` + Quizzes map[string]*Quiz `json:"quizzes"` + Answers map[string]*Answer `json:"answers"` + Exams map[string]*Exam `json:"exams"` } func (s *Session) String() string { diff --git a/pkg/store/file/quiz_test.go b/pkg/store/file/quiz_test.go index 30a146f..d9fcaba 100644 --- a/pkg/store/file/quiz_test.go +++ b/pkg/store/file/quiz_test.go @@ -87,7 +87,7 @@ func (t *quizTestSuite) TestCreate() { t.Equal(a.Text, quiz.Answers[i].Text) } for i, tag := range quizFromDisk.Tags { - t.Equal(tag.Name, quiz.Tags[i].Name) + t.Equal(tag, quiz.Tags[i]) } } } diff --git a/pkg/store/file/session.go b/pkg/store/file/session.go index a568357..7cbc62e 100644 --- a/pkg/store/file/session.go +++ b/pkg/store/file/session.go @@ -5,10 +5,10 @@ import ( "git.andreafazzi.eu/andrea/probo/pkg/store" ) -type SessionFileStore = FileStore[*models.Session, *store.Store[*models.Session]] +type SessionFileStore = FileStore[*models.Session, *store.SessionStore] func NewSessionFileStore(config *FileStoreConfig[*models.Session, *store.SessionStore]) (*SessionFileStore, error) { - return NewFileStore[*models.Session](config, store.NewStore[*models.Session]()) + return NewFileStore[*models.Session](config, store.NewSessionStore()) } func NewDefaultSessionFileStore() (*SessionFileStore, error) { diff --git a/pkg/store/session.go b/pkg/store/session.go index 8bb9b71..a0ac3ed 100644 --- a/pkg/store/session.go +++ b/pkg/store/session.go @@ -1,5 +1,82 @@ package store -import "git.andreafazzi.eu/andrea/probo/pkg/models" +import ( + "fmt" -type SessionStore = Store[*models.Session] + "git.andreafazzi.eu/andrea/probo/pkg/models" +) + +type SessionStore struct { + *Store[*models.Session] +} + +type ErrSessionAlreadyPresent struct { + hash string +} + +func (e *ErrSessionAlreadyPresent) Error() string { + return fmt.Sprintf("Session with hash %v is already present in the store.", e.hash) +} + +func NewSessionStore() *SessionStore { + return &SessionStore{ + Store: NewStore[*models.Session](), + } + +} + +func (s *SessionStore) Create(session *models.Session) (*models.Session, error) { + if hash := session.GetHash(); hash != "" { + session, ok := s.hashes[hash] + if ok { + return session, &ErrSessionAlreadyPresent{hash} + } + } + + session.Quizzes = make(map[string]*models.Quiz, 0) + session.Answers = make(map[string]*models.Answer, 0) + + for _, exam := range session.Exams { + for _, quiz := range exam.Quizzes { + session.Quizzes[quiz.ID] = quiz + + for _, answer := range quiz.Answers { + session.Answers[answer.ID] = answer + } + } + } + + sess, err := s.Store.Create(session) + if err != nil { + return nil, err + } + + return sess, nil +} + +func (s *SessionStore) Update(session *models.Session, id string) (*models.Session, error) { + _, err := s.Read(id) + if err != nil { + return session, err + } + + session.Quizzes = make(map[string]*models.Quiz, 0) + session.Answers = make(map[string]*models.Answer, 0) + + for _, exam := range session.Exams { + for _, quiz := range exam.Quizzes { + session.Quizzes[quiz.ID] = quiz + + for _, answer := range quiz.Answers { + session.Answers[answer.ID] = answer + } + } + } + + sess, err := s.Store.Update(session, id) + if err != nil { + return nil, err + } + + return sess, nil +}