/
channel.go
55 lines (42 loc) · 1.2 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
package are_hub
import (
"context"
"fmt"
)
// Represents a group of users listening to a data stream.
type Channel struct {
Name string `json:"name"`
Password password
Common `bson:",inline"`
}
// Create a new channel.
func NewChannel(name, pw string) *Channel {
return &Channel{Name: name, Password: NewPassword(pw)}
}
// Get a channel from a context.
func ChannelFromCtx(ctx context.Context) (*Channel, error) {
v := ctx.Value(keyChannel)
c, ok := v.(*Channel)
if !ok {
return nil, fmt.Errorf("Could not assert %v as *Channel\n", v)
}
return c, nil
}
// Insert a channel into context.
func (c *Channel) ToCtx(ctx context.Context) context.Context {
return context.WithValue(ctx, keyChannel, c)
}
type ChannelRepo interface {
// Get all channels
All(context.Context) ([]Channel, error)
// Create a new channel
Insert(context.Context, Archetype) error
// Find a channel by its ID.
FindID(context.Context, string) (*Channel, error)
// Find and update a channel by its ID.
UpdateID(context.Context, string, Archetype) error
// Find and delete a channel by its ID.
DeleteID(context.Context, string) (*Channel, error)
// Get a count of channels.
Count(context.Context) (int64, error)
}