/
service.go
126 lines (104 loc) · 3.15 KB
/
service.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
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
package story
import (
"database/sql"
"strconv"
"github.com/AdhityaRamadhanus/chronicle"
"github.com/AdhityaRamadhanus/chronicle/function"
"github.com/pkg/errors"
)
var (
//ErrNoStoryFound sub-domain specific error
ErrNoStoryFound = errors.New("Cannot find Story")
)
//Service provide an interface to story domain service
type Service interface {
CreateStory(story chronicle.Story) (createdStory chronicle.Story, err error)
UpdateStory(story chronicle.Story) (updatedStory chronicle.Story, err error)
GetStories(filter chronicle.StoryFilterOptions, option chronicle.PagingOptions) (chronicle.Stories, int, error)
GetStoryByID(id int) (chronicle.Story, error)
GetStoryBySlug(slug string) (chronicle.Story, error)
DeleteStoryByID(id int) error
}
func NewService(storyRepository chronicle.StoryRepository) Service {
return &service{
storyRepository: storyRepository,
}
}
type service struct {
storyRepository chronicle.StoryRepository
}
func (s *service) CreateStory(story chronicle.Story) (createdStory chronicle.Story, err error) {
defer func() {
if err != nil && err != ErrNoStoryFound {
err = errors.Wrap(err, function.GetFunctionName(s.CreateStory))
}
}()
return s.storyRepository.Insert(story)
}
func (s *service) UpdateStory(story chronicle.Story) (updatedStory chronicle.Story, err error) {
defer func() {
if err != nil && err != ErrNoStoryFound {
err = errors.Wrap(err, function.GetFunctionName(s.UpdateStory))
}
}()
return s.storyRepository.Update(story)
}
func (s *service) GetStories(filter chronicle.StoryFilterOptions, option chronicle.PagingOptions) (stories chronicle.Stories, storiesCount int, err error) {
defer func() {
if err != nil && err != ErrNoStoryFound {
err = errors.Wrap(err, function.GetFunctionName(s.GetStories))
}
}()
// filter by status only
if filter.Status != "" && filter.Topic == "" {
return s.storyRepository.FindByStatus(filter.Status, option)
}
// filter by topic only
if filter.Topic != "" {
topicId, _ := strconv.Atoi(filter.Topic)
return s.storyRepository.FindByTopicAndStatus(topicId, filter.Status, option)
}
return s.storyRepository.All(option)
}
func (s *service) GetStoryByID(id int) (story chronicle.Story, err error) {
defer func() {
if err != nil && err != ErrNoStoryFound {
err = errors.Wrap(err, function.GetFunctionName(s.GetStoryByID))
}
}()
story, err = s.storyRepository.Find(id)
if err != nil {
switch err {
case sql.ErrNoRows:
return story, ErrNoStoryFound
default:
return story, err
}
}
return story, nil
}
func (s *service) GetStoryBySlug(slug string) (story chronicle.Story, err error) {
defer func() {
if err != nil && err != ErrNoStoryFound {
err = errors.Wrap(err, function.GetFunctionName(s.GetStoryBySlug))
}
}()
story, err = s.storyRepository.FindBySlug(slug)
if err != nil {
switch err {
case sql.ErrNoRows:
return story, ErrNoStoryFound
default:
return story, err
}
}
return story, nil
}
func (s *service) DeleteStoryByID(id int) (err error) {
defer func() {
if err != nil && err != ErrNoStoryFound {
err = errors.Wrap(err, function.GetFunctionName(s.DeleteStoryByID))
}
}()
return s.storyRepository.Delete(id)
}