/
repository.go
87 lines (67 loc) · 2.22 KB
/
repository.go
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
package repository
import "github.com/wcodesoft/mosha-quote-service/data"
// Repository represents the repository interface.
type Repository interface {
// AddQuote adds a new quote to the database.
AddQuote(quote data.Quote) (string, error)
// ListAll returns all quotes in the database.
ListAll() []data.Quote
// UpdateQuote updates a quote in the database.
UpdateQuote(quote data.Quote) (data.Quote, error)
// DeleteQuote deletes a quote from the database.
DeleteQuote(id string) error
// GetQuote returns a quote from the database.
GetQuote(id string) (data.Quote, error)
// GetAuthorQuotes returns all quotes from an author.
GetAuthorQuotes(authorID string) []data.Quote
// DeleteAuthorQuotes deletes all quotes from an author.
DeleteAuthorQuotes(authorID string) error
// GetRandomQuote returns a random quote from the database.
GetRandomQuote() (data.Quote, error)
}
type repository struct {
db Database
clientRepository ClientRepository
}
// New creates a new repository.
func New(db Database, clientRepository ClientRepository) Repository {
return &repository{
db: db,
clientRepository: clientRepository,
}
}
func (r *repository) authorExist(authorId string) error {
_, err := r.clientRepository.GetAuthor(authorId)
return err
}
// AddQuote adds a new quote to the database.
func (r *repository) AddQuote(quote data.Quote) (string, error) {
if err := r.authorExist(quote.AuthorID); err != nil {
return "", err
}
return r.db.AddQuote(quote)
}
func (r *repository) ListAll() []data.Quote {
return r.db.ListAll()
}
func (r *repository) UpdateQuote(quote data.Quote) (data.Quote, error) {
if err := r.authorExist(quote.AuthorID); err != nil {
return data.Quote{}, err
}
return r.db.UpdateQuote(quote)
}
func (r *repository) DeleteQuote(id string) error {
return r.db.DeleteQuote(id)
}
func (r *repository) GetQuote(id string) (data.Quote, error) {
return r.db.GetQuote(id)
}
func (r *repository) GetAuthorQuotes(authorID string) []data.Quote {
return r.db.GetAuthorQuotes(authorID)
}
func (r *repository) DeleteAuthorQuotes(authorID string) error {
return r.db.DeleteAuthorQuotes(authorID)
}
func (r *repository) GetRandomQuote() (data.Quote, error) {
return r.db.GetRandomQuote()
}