package sqlite import ( "database/sql" "encoding/json" "fmt" "time" "go-mod.ewintr.nl/planner/item" "go-mod.ewintr.nl/planner/plan/storage" ) type SqliteEvent struct { db *sql.DB } func (s *SqliteEvent) Store(event item.Event) error { var recurStr *string if event.Recurrer != nil { recurBytes, err := json.Marshal(event.Recurrer) if err != nil { return fmt.Errorf("could not marshal recurrer: %v", err) } rs := string(recurBytes) recurStr = &rs } if _, err := s.db.Exec(` INSERT INTO events (id, title, start, duration, recur) VALUES (?, ?, ?, ?, ?) ON CONFLICT(id) DO UPDATE SET title=?, start=?, duration=?, recur=? `, event.ID, event.Title, event.Start.Format(timestampFormat), event.Duration.String(), recurStr, event.Title, event.Start.Format(timestampFormat), event.Duration.String(), recurStr); err != nil { return fmt.Errorf("%w: %v", ErrSqliteFailure, err) } return nil } func (s *SqliteEvent) Find(id string) (item.Event, error) { var event item.Event var durStr string var recurStr *string err := s.db.QueryRow(` SELECT id, title, start, duration, recur FROM events WHERE id = ?`, id).Scan(&event.ID, &event.Title, &event.Start, &durStr, &recurStr) switch { case err == sql.ErrNoRows: return item.Event{}, fmt.Errorf("event not found: %w", err) case err != nil: return item.Event{}, fmt.Errorf("%w: %v", ErrSqliteFailure, err) } dur, err := time.ParseDuration(durStr) if err != nil { return item.Event{}, fmt.Errorf("could not unmarshal recurrer: %v", err) } event.Duration = dur if recurStr != nil { var rec item.Recur if err := json.Unmarshal([]byte(*recurStr), &rec); err != nil { return item.Event{}, fmt.Errorf("%w: %v", ErrSqliteFailure, err) } event.Recurrer = &rec } return event, nil } func (s *SqliteEvent) FindAll() ([]item.Event, error) { rows, err := s.db.Query(` SELECT id, title, start, duration, recur FROM events`) if err != nil { return nil, fmt.Errorf("%w: %v", ErrSqliteFailure, err) } result := make([]item.Event, 0) defer rows.Close() for rows.Next() { var event item.Event var durStr string var recurStr *string if err := rows.Scan(&event.ID, &event.Title, &event.Start, &durStr, &recurStr); err != nil { return nil, fmt.Errorf("%w: %v", ErrSqliteFailure, err) } dur, err := time.ParseDuration(durStr) if err != nil { return nil, fmt.Errorf("%w: %v", ErrSqliteFailure, err) } event.Duration = dur if recurStr != nil { var rec item.Recur if err := json.Unmarshal([]byte(*recurStr), &rec); err != nil { return nil, fmt.Errorf("could not unmarshal recurrer: %v", err) } event.Recurrer = &rec } result = append(result, event) } return result, nil } func (s *SqliteEvent) Delete(id string) error { result, err := s.db.Exec(` DELETE FROM events WHERE id = ?`, id) if err != nil { return fmt.Errorf("%w: %v", ErrSqliteFailure, err) } rowsAffected, err := result.RowsAffected() if err != nil { return fmt.Errorf("%w: %v", ErrSqliteFailure, err) } if rowsAffected == 0 { return storage.ErrNotFound } return nil }