-
Notifications
You must be signed in to change notification settings - Fork 4
/
util.go
80 lines (72 loc) · 1.91 KB
/
util.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
package svc
import (
"context"
"github.com/xh-polaris/meowchat-system-rpc/common/errorx"
"github.com/xh-polaris/meowchat-system-rpc/internal/model"
"github.com/xh-polaris/meowchat-system-rpc/pb"
"go.mongodb.org/mongo-driver/bson/primitive"
)
func ConvertAdmin(in *model.Admin) *pb.Admin {
return &pb.Admin{
Id: in.ID.Hex(),
CommunityId: in.CommunityId,
Name: in.Name,
Title: in.Title,
Phone: in.Phone,
Wechat: in.Wechat,
AvatarUrl: in.AvatarUrl,
}
}
func ConvertNotice(in *model.Notice) *pb.Notice {
return &pb.Notice{
Id: in.ID.Hex(),
CommunityId: in.CommunityId,
Text: in.Text,
CreateAt: in.CreateAt.Unix(),
UpdateAt: in.UpdateAt.Unix(),
}
}
func ConvertNews(in *model.News) *pb.News {
return &pb.News{
Id: in.ID.Hex(),
CommunityId: in.CommunityId,
ImageUrl: in.ImageUrl,
LinkUrl: in.LinkUrl,
Type: in.Type,
IsPublic: in.IsPublic,
}
}
func ConvertCommunity(in *model.Community) *pb.Community {
pid := ""
if in.ParentId != primitive.NilObjectID {
pid = in.ParentId.Hex()
}
return &pb.Community{
Id: in.ID.Hex(),
Name: in.Name,
ParentId: pid,
}
}
func (s *ServiceContext) CheckCommunityIdExist(ctx context.Context, id string) (primitive.ObjectID, error) {
if id == "" {
return primitive.NilObjectID, nil
}
r, err := s.CommunityModel.FindOne(ctx, id)
if err != nil {
return primitive.NilObjectID, errorx.ErrCommunityIdNotFound
}
return r.ID, nil
}
func (s *ServiceContext) CheckParentCommunityId(ctx context.Context, parentId string) (primitive.ObjectID, error) {
if parentId == "" {
return primitive.NilObjectID, nil
}
r, err := s.CommunityModel.FindOne(ctx, parentId)
if err != nil {
return primitive.NilObjectID, errorx.ErrCommunityIdNotFound
}
if r.ParentId != primitive.NilObjectID {
return primitive.NilObjectID, errorx.ErrChildCommunityNotAllowed
}
return r.ID, nil
}