/
moderator.go
90 lines (80 loc) · 2.55 KB
/
moderator.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
90
package datastore
import (
"context"
"database/sql"
"github.com/jinzhu/gorm"
"github.com/nedrocks/delphisbe/graph/model"
"github.com/sirupsen/logrus"
)
func (d *delphisDB) GetModeratorByID(ctx context.Context, id string) (*model.Moderator, error) {
logrus.Debug("GetModeratorByID::SQL Query")
moderator := model.Moderator{}
if err := d.sql.Preload("UserProfile").First(&moderator, model.Moderator{ID: id}).Error; err != nil {
if gorm.IsRecordNotFoundError(err) {
return nil, nil
}
logrus.WithError(err).Errorf("GetModeratorByID::Failed to get moderator")
return nil, err
}
return &moderator, nil
}
func (d *delphisDB) CreateModerator(ctx context.Context, moderator model.Moderator) (*model.Moderator, error) {
logrus.Debugf("CreateModerator::SQL Insert")
found := model.Moderator{}
if err := d.sql.Create(&moderator).First(&found, model.Moderator{ID: moderator.ID}).Error; err != nil {
logrus.WithError(err).Errorf("Failed to create moderator")
return nil, err
}
return &found, nil
}
func (d *delphisDB) GetModeratorByUserID(ctx context.Context, id string) (*model.Moderator, error) {
logrus.Debug("GetModeratorByUserID::SQL Query")
if err := d.initializeStatements(ctx); err != nil {
logrus.WithError(err).Error("GetModeratorByUserID failed to initialize statements")
return nil, err
}
moderator := model.Moderator{}
if err := d.prepStmts.getModeratorByUserIDStmt.QueryRowContext(
ctx,
id,
).Scan(
&moderator.ID,
&moderator.CreatedAt,
&moderator.UpdatedAt,
&moderator.DeletedAt,
&moderator.UserProfileID,
); err != nil {
if err == sql.ErrNoRows {
return nil, nil
}
logrus.WithError(err).Error("failed to execute getModeratorByUserIDStmt")
return nil, err
}
return &moderator, nil
}
func (d *delphisDB) GetModeratorByUserIDAndDiscussionID(ctx context.Context, userID, discussionID string) (*model.Moderator, error) {
logrus.Debug("GetModeratorByUserIDAndDiscussionID::SQL Query")
if err := d.initializeStatements(ctx); err != nil {
logrus.WithError(err).Error("GetModeratorByUserIDAndDiscussionID failed to initialize statements")
return nil, err
}
moderator := model.Moderator{}
if err := d.prepStmts.getModeratorByUserIDAndDiscussionIDStmt.QueryRowContext(
ctx,
userID,
discussionID,
).Scan(
&moderator.ID,
&moderator.CreatedAt,
&moderator.UpdatedAt,
&moderator.DeletedAt,
&moderator.UserProfileID,
); err != nil {
if err == sql.ErrNoRows {
return nil, nil
}
logrus.WithError(err).Error("failed to execute getModeratorByUserIDAndDiscussionIDStmt")
return nil, err
}
return &moderator, nil
}