package interview import ( "errors" "sync" ) var ErrSessionNotFound = errors.New("diagnostic session not found") type Store interface { Create(Session) (Session, error) Get(string) (Session, error) Update(Session) (Session, error) } type MemoryStore struct { mu sync.RWMutex sessions map[string]Session } func NewMemoryStore() *MemoryStore { return &MemoryStore{ sessions: make(map[string]Session), } } func (s *MemoryStore) Create(session Session) (Session, error) { s.mu.Lock() defer s.mu.Unlock() s.sessions[session.ID] = cloneSession(session) return session, nil } func (s *MemoryStore) Get(id string) (Session, error) { s.mu.RLock() defer s.mu.RUnlock() session, ok := s.sessions[id] if !ok { return Session{}, ErrSessionNotFound } return cloneSession(session), nil } func (s *MemoryStore) Update(session Session) (Session, error) { s.mu.Lock() defer s.mu.Unlock() if _, ok := s.sessions[session.ID]; !ok { return Session{}, ErrSessionNotFound } s.sessions[session.ID] = cloneSession(session) return session, nil } func cloneSession(session Session) Session { session.Stack = append([]string(nil), session.Stack...) session.Questions = append([]Question(nil), session.Questions...) session.Answers = append([]Answer(nil), session.Answers...) return session }