-
Notifications
You must be signed in to change notification settings - Fork 16
/
service.go
63 lines (54 loc) · 1.62 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
package notebook
import (
"context"
"fmt"
"github.com/Bio-OS/bioos/pkg/errors"
)
type Service interface {
Upsert(context.Context, *Notebook) error
Create(context.Context, *Notebook) error
Update(context.Context, *Notebook) error
Delete(ctx context.Context, path string) error
}
func NewService(repo Repository) Service {
return &service{
repository: repo,
}
}
type service struct {
repository Repository
}
func (s *service) Upsert(ctx context.Context, nb *Notebook) error {
if err := s.repository.Save(ctx, nb); err != nil {
return errors.NewInternalError(err)
}
return nil
}
func (s *service) Create(ctx context.Context, nb *Notebook) error {
if stored, err := s.repository.Get(ctx, nb.Path()); err != nil {
return errors.NewInternalError(fmt.Errorf("check notebook exist fail: %w", err))
} else if stored != nil {
return errors.NewAlreadyExistError("notebook", nb.Name)
}
return s.Upsert(ctx, nb)
}
func (s *service) Update(ctx context.Context, nb *Notebook) error {
if stored, err := s.repository.Get(ctx, nb.Path()); err != nil {
return errors.NewInternalError(fmt.Errorf("check notebook exist fail: %w", err))
} else if stored == nil {
return errors.NewNotFoundError("notebook", nb.Name)
}
return s.Upsert(ctx, nb)
}
func (s *service) Delete(ctx context.Context, path string) error {
nb, err := s.repository.Get(ctx, path)
if err != nil {
return errors.NewInternalError(fmt.Errorf("check notebook exist fail: %w", err))
} else if nb == nil {
return errors.NewNotFoundError("notebook", path)
}
if err = s.repository.Delete(ctx, nb); err != nil {
return errors.NewInternalError(err)
}
return nil
}