-
Notifications
You must be signed in to change notification settings - Fork 0
/
dbManager.go
77 lines (64 loc) · 1.58 KB
/
dbManager.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
package main
type DbManager interface {
GetUser(uname string) UserId
SaveUser(string) UserId
GetUserChatRoom(UserId) []ChatRoomId
SaveUserChatRoom(string) []ChatRoomId
GetHistoryMessage(UserId) []ChatRoom
SaveHistoryMessage(ChatRoomId, string) bool
GetGobalChatRoom() []ChatRoomId
SaveGobalChatRoom(string) []ChatRoomId
}
type InMemory struct {
Users map[UserId]User
GlobalChatRooms []ChatRoomId
HistoryMessagePerChatRoom []ChatRoom
}
type InDataBase struct {
}
func (m InMemory) GetUser(uname string) UserId {
var uid UserId
uid.Id = 0
for _, user := range m.Users {
name := user.Uname
if name == uname {
uid = user.Uid
break
}
}
return uid
}
func (m InMemory) SaveUser(uname string) UserId {
id := len(m.Users) + 1
var uid UserId
uid.Id = id
var newUser User
newUser.Uid = uid
newUser.Uname = uname
newUser.JoinedChatRoom = nil
m.Users[uid] = newUser
return uid
}
func (m InMemory) GetUserChatRoom(uid UserId) []ChatRoomId {
return m.Users[uid].JoinedChatRoom
}
func (m InMemory) SaveUserChatRoom(string) []ChatRoomId {
return nil
}
func (m InMemory) GetHistoryMessage(uid UserId) []ChatRoom {
joinedRoomIDs := m.GetUserChatRoom(uid)
var joinedChatRooms []ChatRoom
for _, cid := range joinedRoomIDs {
joinedChatRooms = append(joinedChatRooms, m.HistoryMessagePerChatRoom[cid.Id])
}
return joinedChatRooms
}
func (m InMemory) SaveHistoryMessage(ChatRoomId, string) bool {
return false
}
func (m InMemory) GetGobalChatRoom() []ChatRoomId {
return nil
}
func (m InMemory) SaveGobalChatRoom(string) []ChatRoomId {
return nil
}