forked from ashishps1/awesome-low-level-design
-
Notifications
You must be signed in to change notification settings - Fork 0
/
stackOverFlow.go
43 lines (37 loc) · 1011 Bytes
/
stackOverFlow.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
package main
type StackOverFlow struct {
questions map[string][]*Question
tags map[string]struct{}
}
func NewStackOverFlow() *StackOverFlow {
return &StackOverFlow{
questions: make(map[string][]*Question),
tags: make(map[string]struct{}),
}
}
func (s *StackOverFlow) PostQuestion(user *User, title string, body string, tags []string) *Question {
ques := NewQuestion(title, body, user)
tagsToAdd := make(map[string]struct{})
for _, tag := range tags {
if _, isPresent := s.tags[tag]; !isPresent {
tagsToAdd[tag] = struct{}{}
s.tags[tag] = struct{}{}
}
}
ques.tags = tagsToAdd
s.questions[user.Email] = append(s.questions[user.Email], ques)
return ques
}
func (s *StackOverFlow) GetQuestionsByTags(tags []string) []*Question {
result := []*Question{}
for _, questions := range s.questions {
for _, question := range questions {
for _, tag := range tags {
if question.IsTagPresent(tag) {
result = append(result, question)
}
}
}
}
return result
}