summaryrefslogtreecommitdiff
path: root/internal/store/availability.go
blob: 0adf6642d3047c463dbdd8b21b50c4cec2cdf9be (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
package store

import (
	"task-dashboard/internal/models"
)

// CreateAvailabilityBlock inserts a new weekly availability block and returns it.
func (s *Store) CreateAvailabilityBlock(weekday int, startTime, endTime, label string) (*models.AvailabilityBlock, error) {
	id := newTaskID()
	if _, err := s.db.Exec(`
		INSERT INTO availability_blocks (id, weekday, start_time, end_time, label) VALUES (?, ?, ?, ?, ?)
	`, id, weekday, startTime, endTime, label); err != nil {
		return nil, err
	}
	return &models.AvailabilityBlock{ID: id, Weekday: weekday, StartTime: startTime, EndTime: endTime, Label: label}, nil
}

// GetAvailabilityBlocks returns every availability block, ordered by weekday
// then start time.
func (s *Store) GetAvailabilityBlocks() ([]models.AvailabilityBlock, error) {
	rows, err := s.db.Query(`
		SELECT id, weekday, start_time, end_time, label FROM availability_blocks ORDER BY weekday ASC, start_time ASC
	`)
	if err != nil {
		return nil, err
	}
	defer func() { _ = rows.Close() }()

	var blocks []models.AvailabilityBlock
	for rows.Next() {
		var b models.AvailabilityBlock
		if err := rows.Scan(&b.ID, &b.Weekday, &b.StartTime, &b.EndTime, &b.Label); err != nil {
			return nil, err
		}
		blocks = append(blocks, b)
	}
	return blocks, rows.Err()
}

// DeleteAvailabilityBlock removes an availability block. Returns
// ErrNativeTaskNotFound if id doesn't match any row (reusing the sentinel
// already shared across native-task/project not-found cases).
func (s *Store) DeleteAvailabilityBlock(id string) error {
	result, err := s.db.Exec(`DELETE FROM availability_blocks WHERE id = ?`, id)
	if err != nil {
		return err
	}
	return checkRowsAffected(result)
}