-
Notifications
You must be signed in to change notification settings - Fork 0
/
template.go
65 lines (51 loc) · 1.69 KB
/
template.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
package services
import (
"context"
"fmt"
"time"
"github.com/jbaikge/boneless/models"
)
type TemplateRepository interface {
CreateTemplate(context.Context, *models.Template) error
DeleteTemplate(context.Context, string) error
GetTemplateById(context.Context, string) (models.Template, error)
GetTemplateList(context.Context, models.TemplateFilter) ([]models.Template, models.Range, error)
UpdateTemplate(context.Context, *models.Template) error
}
type TemplateService struct {
repo TemplateRepository
}
func NewTemplateService(repo TemplateRepository) TemplateService {
return TemplateService{
repo: repo,
}
}
func (s TemplateService) ById(ctx context.Context, id string) (models.Template, error) {
if !idProvider.IsValid(id) {
return models.Template{}, fmt.Errorf("invalid template ID: %s", id)
}
return s.repo.GetTemplateById(ctx, id)
}
func (s TemplateService) Create(ctx context.Context, template *models.Template) (err error) {
if template.Id != "" {
return fmt.Errorf("template already has an ID")
}
now := time.Now()
template.Id = idProvider.NewWithTime(now)
template.Created = now
template.Updated = now
return s.repo.CreateTemplate(ctx, template)
}
func (s TemplateService) Delete(ctx context.Context, id string) (err error) {
return s.repo.DeleteTemplate(ctx, id)
}
func (s TemplateService) List(ctx context.Context, filter models.TemplateFilter) ([]models.Template, models.Range, error) {
return s.repo.GetTemplateList(ctx, filter)
}
func (s TemplateService) Update(ctx context.Context, template *models.Template) (err error) {
if template.Id == "" {
return fmt.Errorf("template has no ID")
}
template.Updated = time.Now()
return s.repo.UpdateTemplate(ctx, template)
}