forked from brianstarke/dogfort
-
Notifications
You must be signed in to change notification settings - Fork 0
/
channel.go
84 lines (66 loc) · 1.7 KB
/
channel.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
package domain
import (
"fmt"
"github.com/nu7hatch/gouuid"
"labix.org/v2/mgo"
"labix.org/v2/mgo/bson"
)
type Channel struct {
Uid string `json:"uid"`
Name string `json:"name",binding:"required"`
Description string `json:"description",binding:"required"`
Private bool `json:"isPrivate",binding:"required"`
CreatedBy UserUid `json:"createdBy"` // the user UID of the admin
Members []UserUid `json:"members"`
}
type ChannelDomain struct {
Collection *mgo.Collection
}
/*
Creates a new channel, returns the Channel ID
*/
func (cd ChannelDomain) CreateChannel(channel *Channel, userUid *UserUid) (*string, error) {
// check to see if there is already a channel with this name
n, err := cd.Collection.Find(bson.M{"name": channel.Name}).Count()
if err != nil {
return nil, err
}
if n > 0 {
return nil, fmt.Errorf("There is already a channel with that name [%s]", channel.Name)
}
// create our own UID (ignore Mongo ObjectId)
uid, err := uuid.NewV4()
if err != nil {
return nil, err
}
channel.CreatedBy = *userUid
channel.Uid = uid.String()
channel.Members = []UserUid{*userUid}
err = cd.Collection.Insert(&channel)
if err != nil {
return nil, err
} else {
return &channel.Uid, nil
}
}
func (cd ChannelDomain) ListChannels() (*[]Channel, error) {
c := []Channel{}
err := cd.Collection.Find(bson.M{}).All(&c)
if err != nil {
return nil, err
} else {
return &c, nil
}
}
/*
Returns all channels this user is in
*/
func (cd ChannelDomain) GetUserChannels(userUid *UserUid) (*[]Channel, error) {
c := []Channel{}
err := cd.Collection.Find(bson.M{"members": *userUid}).All(&c)
if err != nil {
return nil, err
} else {
return &c, nil
}
}