Working on responses

This commit is contained in:
andrea 2024-05-12 10:15:48 +02:00
parent 7eaeb36578
commit 6b34c2d29b
7 changed files with 101 additions and 15 deletions

View file

@ -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}

View file

@ -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 {

View file

@ -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 {

View file

@ -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 {

View file

@ -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])
}
}
}

View file

@ -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) {

View file

@ -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
}