-
Notifications
You must be signed in to change notification settings - Fork 4
/
items.resolvers.go
89 lines (79 loc) · 2.38 KB
/
items.resolvers.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
package graph
// This file will be automatically regenerated based on the schema, any resolver implementations
// will be copied through when generating and any unknown code will be moved to the end.
// Code generated by github.com/99designs/gqlgen version v0.17.39
import (
"context"
"strconv"
"github.com/bcc-code/bcc-media-platform/backend/common"
"github.com/bcc-code/bcc-media-platform/backend/graph/api/generated"
"github.com/bcc-code/bcc-media-platform/backend/graph/api/model"
"github.com/bcc-code/bcc-media-platform/backend/user"
"github.com/bcc-code/bcc-media-platform/backend/utils"
"github.com/samber/lo"
)
// URL is the resolver for the url field.
func (r *linkResolver) URL(ctx context.Context, obj *model.Link) (string, error) {
l, err := r.Loaders.LinkLoader.Get(ctx, utils.AsInt(obj.ID))
if err != nil {
return "", err
}
if !l.ComputedDataGroupID.Valid {
return l.URL, nil
}
ginCtx, _ := utils.GinCtx(ctx)
u := user.GetFromCtx(ginCtx)
if u == nil || !u.ActiveBCC {
return l.URL, nil
}
data, err := r.Loaders.ComputedDataLoader.Get(ctx, l.ComputedDataGroupID.UUID)
if err != nil {
return "", err
}
for _, i := range data {
if lo.EveryBy(i.Conditions, func(i common.ComputedCondition) bool {
switch i.Type {
case "user_church":
switch i.Operator {
case "==":
intValue, _ := strconv.ParseInt(i.Value, 10, 64)
return lo.Contains(u.ChurchIDs, int(intValue))
}
case "user_age":
intValue, _ := strconv.ParseInt(i.Value, 10, 64)
switch i.Operator {
case "<":
return u.Age < int(intValue)
case ">":
return u.Age > int(intValue)
case "==":
return u.Age == int(intValue)
}
}
return false
}) {
return i.Result, nil
}
}
return l.URL, nil
}
// Image is the resolver for the image field.
func (r *linkResolver) Image(ctx context.Context, obj *model.Link, style *model.ImageStyle) (*string, error) {
l, err := r.Loaders.LinkLoader.Get(ctx, utils.AsInt(obj.ID))
if err != nil {
return nil, err
}
if l == nil {
return nil, nil
}
if style == nil {
s := model.ImageStyleDefault
style = &s
}
ginCtx, _ := utils.GinCtx(ctx)
languages := user.GetLanguagesFromCtx(ginCtx)
return l.Images.GetDefault(languages, style.String()), nil
}
// Link returns generated.LinkResolver implementation.
func (r *Resolver) Link() generated.LinkResolver { return &linkResolver{r} }
type linkResolver struct{ *Resolver }