-
Notifications
You must be signed in to change notification settings - Fork 0
/
service.go
87 lines (72 loc) · 2.25 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
package user
import (
"context"
"github.com/pkg/errors"
"github.com/Kalinin-Andrey/redditclone/pkg/log"
)
const MaxLIstLimit = 1000
// IService encapsulates usecase logic for user.
type IService interface {
NewEntity() *User
Get(ctx context.Context, id uint) (*User, error)
Query(ctx context.Context, offset, limit uint) ([]User, error)
List(ctx context.Context) ([]User, error)
//Count(ctx context.Context) (uint, error)
Create(ctx context.Context, entity *User) error
//Update(ctx context.Context, id string, input *User) (*User, error)
//Delete(ctx context.Context, id string) (error)
First(ctx context.Context, user *User) (*User, error)
}
type service struct {
//Domain Domain
repo IRepository
logger log.ILogger
}
// NewService creates a new service.
func NewService(repo IRepository, logger log.ILogger) IService {
s := &service{repo, logger}
repo.SetDefaultConditions(s.defaultConditions())
return s
}
// Defaults returns defaults params
func (s service) defaultConditions() map[string]interface{} {
return map[string]interface{}{}
}
func (s service) NewEntity() *User {
return &User{}
}
// Get returns the entity with the specified ID.
func (s service) Get(ctx context.Context, id uint) (*User, error) {
entity, err := s.repo.Get(ctx, id)
if err != nil {
return nil, errors.Wrapf(err, "Can not get a user by id: %v", id)
}
return entity, nil
}
/*
// Count returns the number of items.
func (s service) Count(ctx context.Context) (uint, error) {
return s.repo.Count(ctx)
}*/
// Query returns the items with the specified offset and limit.
func (s service) Query(ctx context.Context, offset, limit uint) ([]User, error) {
items, err := s.repo.Query(ctx, offset, limit)
if err != nil {
return nil, errors.Wrapf(err, "Can not find a list of users by ctx")
}
return items, nil
}
// List returns the items list.
func (s service) List(ctx context.Context) ([]User, error) {
items, err := s.repo.Query(ctx, 0, MaxLIstLimit)
if err != nil {
return nil, errors.Wrapf(err, "Can not find a list of users by ctx")
}
return items, nil
}
func (s service) Create(ctx context.Context, entity *User) error {
return s.repo.Create(ctx, entity)
}
func (s service) First(ctx context.Context, user *User) (*User, error) {
return s.repo.First(ctx, user)
}