-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy paththerapist.go
83 lines (63 loc) · 1.8 KB
/
therapist.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
package sqlite
import (
"context"
"github.com/brittonhayes/therapy/api"
"github.com/uptrace/bun"
)
func (r *repository) therapistFilterQuery(query *bun.SelectQuery, params *api.GetTherapistParams) (*bun.SelectQuery, error) {
if params == nil {
return query, nil
}
if params.Limit != nil {
query = query.Limit(*params.Limit)
}
if params.Offset != nil {
query = query.Offset(*params.Offset)
}
if params.Title != nil {
query.Where("? LIKE ?", bun.Ident("title"), *params.Title+"%")
}
if params.Credentials != nil {
query.Where("? LIKE ?", bun.Ident("credentials"), "%"+*params.Credentials+"%")
}
if params.Verified != nil {
query.Where("? LIKE ?", bun.Ident("verified"), "%"+*params.Verified+"%")
}
if params.Statement != nil {
query.Where("? LIKE ?", bun.Ident("statement"), "%"+*params.Statement+"%")
}
if params.Phone != nil {
query.Where("? LIKE ?", bun.Ident("phone"), "%"+*params.Phone+"%")
}
if params.Location != nil {
query.Where("? LIKE ?", bun.Ident("location"), "%"+*params.Location+"%")
}
return query, nil
}
func (r *repository) Save(ctx context.Context, therapist api.Therapist) error {
_, err := r.db.NewInsert().Model(&therapist).Exec(ctx)
if err != nil {
return err
}
return nil
}
func (r *repository) Find(ctx context.Context, params *api.GetTherapistParams) ([]api.Therapist, error) {
var therapists []api.Therapist
query, err := r.therapistFilterQuery(r.db.NewSelect().Model(&therapists), params)
if err != nil {
return nil, err
}
err = query.Scan(ctx, &therapists)
if err != nil {
return nil, err
}
return therapists, nil
}
func (r *repository) List(ctx context.Context) ([]api.Therapist, error) {
var therapists []api.Therapist
err := r.db.NewSelect().Model(&therapists).Scan(ctx)
if err != nil {
return nil, err
}
return therapists, nil
}