summaryrefslogtreecommitdiffstats
path: root/pkg/entries/entries.go
blob: 466f340ac61a627a9daf9d84be0f7f5c6273bb4a (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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
package entries

import (
	"context"
	"database/sql"
	"errors"
	"fmt"

	"git.leonardobishop.net/stash/pkg/database/sqlc"
)

type Service interface {
	CreateEntry(title, kind, url, description string) (*sqlc.Entry, error)
	UpdateEntryKind(id int64, kind string) (*sqlc.Entry, error)
	DeleteEntry(id int64) error
	GetEntries() ([]EntryRow, error)
	GetEntriesByKind(kind string) ([]EntryRow, error)
	GetEntryURLs() ([]string, error)
	GetEntryByUrl(url string) (*sqlc.GetEntryByUrlRow, error)
}

var (
	ErrEntryNotFound = errors.New("entry not found")
)

type service struct {
	db *sql.DB
}

func NewService(db *sql.DB) Service {
	return &service{
		db: db,
	}
}

func (s *service) CreateEntry(title, kind, url, description string) (*sqlc.Entry, error) {
	queries := sqlc.New(s.db)

	entry, err := queries.CreateEntryWithKindName(context.Background(), sqlc.CreateEntryWithKindNameParams{
		Title:       title,
		Url:         url,
		Description: description,
		Name:        kind,
	})

	if err != nil {
		return nil, fmt.Errorf("could not create entry: %w", err)
	}

	return &entry, nil
}

func (s *service) UpdateEntryKind(id int64, kind string) (*sqlc.Entry, error) {
	queries := sqlc.New(s.db)

	entry, err := queries.UpdateEntryKind(context.Background(), sqlc.UpdateEntryKindParams{
		ID:   id,
		Name: kind,
	})
	if err != nil {
		if errors.Is(err, sql.ErrNoRows) {
			return nil, ErrEntryNotFound
		}
		return nil, fmt.Errorf("could not update entry: %w", err)
	}

	return &entry, nil
}

func (s *service) DeleteEntry(id int64) error {
	queries := sqlc.New(s.db)

	_, err := queries.DeleteEntry(context.Background(), id)
	return err
}

func (s *service) GetEntries() ([]EntryRow, error) {
	queries := sqlc.New(s.db)

	response, err := queries.GetEntries(context.Background())
	entries := make([]EntryRow, len(response))
	if err != nil {
		return entries, fmt.Errorf("could not get entries: %w", err)
	}

	for i := range response {
		entries[i].ScanGetEntriesRow(response[i])
	}

	return entries, nil
}

func (s *service) GetEntriesByKind(kind string) ([]EntryRow, error) {
	queries := sqlc.New(s.db)

	response, err := queries.GetEntriesByKind(context.Background(), kind)
	entries := make([]EntryRow, len(response))
	if err != nil {
		return entries, fmt.Errorf("could not get entries: %w", err)
	}

	for i := range response {
		entries[i].ScanGetEntriesByKindRow(response[i])
	}

	return entries, nil
}

func (s *service) GetEntryURLs() ([]string, error) {
	queries := sqlc.New(s.db)

	entries, err := queries.GetEntryURLs(context.Background())
	if err != nil {
		return make([]string, 0), fmt.Errorf("could not get entries: %w", err)
	}

	return entries, nil
}

func (s *service) GetEntryByUrl(url string) (*sqlc.GetEntryByUrlRow, error) {
	queries := sqlc.New(s.db)

	entry, err := queries.GetEntryByUrl(context.Background(), url)
	if err != nil {
		if errors.Is(err, sql.ErrNoRows) {
			return nil, ErrEntryNotFound
		}
		return nil, fmt.Errorf("could not get entry: %w", err)
	}

	return &entry, nil
}