planner/plan/storage/sqlite/event.go

115 lines
2.8 KiB
Go
Raw Normal View History

2024-10-03 07:32:48 +02:00
package sqlite
import (
"database/sql"
"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 {
2024-12-19 12:06:03 +01:00
var recurStr string
if event.Recurrer != nil {
recurStr = event.Recurrer.String()
}
2024-10-03 07:32:48 +02:00
if _, err := s.db.Exec(`
INSERT INTO events
2024-12-19 12:06:03 +01:00
(id, title, date, time, duration, recurrer)
2024-10-03 07:32:48 +02:00
VALUES
2024-12-19 12:06:03 +01:00
(?, ?, ?, ?, ?, ?)
2024-10-03 07:32:48 +02:00
ON CONFLICT(id) DO UPDATE
SET
title=?,
2024-12-19 12:06:03 +01:00
date=?,
time=?,
duration=?,
recurrer=?
`,
event.ID, event.Title, event.Date.String(), event.Time.String(), event.Duration.String(), recurStr,
event.Title, event.Date.String(), event.Time.String(), event.Duration.String(), recurStr); err != nil {
2024-10-03 07:32:48 +02:00
return fmt.Errorf("%w: %v", ErrSqliteFailure, err)
}
return nil
}
func (s *SqliteEvent) Find(id string) (item.Event, error) {
var event item.Event
2024-12-19 12:06:03 +01:00
var dateStr, timeStr, recurStr, durStr string
2024-10-03 07:32:48 +02:00
err := s.db.QueryRow(`
2024-12-19 12:06:03 +01:00
SELECT id, title, date, time, duration, recurrer
2024-10-03 07:32:48 +02:00
FROM events
2024-12-19 12:06:03 +01:00
WHERE id = ?`, id).Scan(&event.ID, &event.Title, &dateStr, &timeStr, &durStr, &recurStr)
2024-10-03 07:32:48 +02:00
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)
}
2024-12-19 12:06:03 +01:00
event.Date = item.NewDateFromString(dateStr)
event.Time = item.NewTimeFromString(timeStr)
2024-10-03 07:32:48 +02:00
dur, err := time.ParseDuration(durStr)
if err != nil {
2024-12-19 12:06:03 +01:00
return item.Event{}, fmt.Errorf("could not unmarshal recurrer: %v", err)
2024-10-03 07:32:48 +02:00
}
event.Duration = dur
2024-12-19 12:06:03 +01:00
event.Recurrer = item.NewRecurrer(recurStr)
2024-10-03 07:32:48 +02:00
return event, nil
}
func (s *SqliteEvent) FindAll() ([]item.Event, error) {
rows, err := s.db.Query(`
2024-12-19 12:06:03 +01:00
SELECT id, title, date, time, duration, recurrer
2024-10-03 07:32:48 +02:00
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
2024-12-19 12:06:03 +01:00
var dateStr, timeStr, recurStr, durStr string
if err := rows.Scan(&event.ID, &event.Title, &dateStr, &timeStr, &durStr, &recurStr); err != nil {
2024-10-03 07:32:48 +02:00
return nil, fmt.Errorf("%w: %v", ErrSqliteFailure, err)
}
dur, err := time.ParseDuration(durStr)
if err != nil {
return nil, fmt.Errorf("%w: %v", ErrSqliteFailure, err)
}
2024-12-19 12:06:03 +01:00
event.Date = item.NewDateFromString(dateStr)
event.Time = item.NewTimeFromString(timeStr)
2024-10-03 07:32:48 +02:00
event.Duration = dur
2024-12-19 12:06:03 +01:00
event.Recurrer = item.NewRecurrer(recurStr)
2024-10-03 07:32:48 +02:00
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
}