-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
182 additions
and
86 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
db.json |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
package main | ||
|
||
import ( | ||
"encoding/json" | ||
"io" | ||
"log" | ||
"os" | ||
) | ||
|
||
var dbFilePath string = "db.json" | ||
|
||
func loadTopics() map[uint64]*Topic { | ||
file, err := os.Open(dbFilePath) | ||
if err != nil { | ||
log.Fatalf("Invalid DB File Path: %s\n", dbFilePath) | ||
} | ||
defer file.Close() | ||
|
||
bytes, _ := io.ReadAll(file) | ||
var topics []Topic | ||
err = json.Unmarshal(bytes, &topics) | ||
if err != nil { | ||
log.Fatalf("DB Decoding Failed.") | ||
} | ||
|
||
topicMap := make(map[uint64]*Topic, 0) | ||
for _, topic := range topics { | ||
topicMap[topic.Id] = &topic | ||
} | ||
log.Println("Topics Loaded.") | ||
|
||
return topicMap | ||
} | ||
|
||
func saveTopics(topics map[uint64]*Topic) { | ||
file, err := os.Create(dbFilePath) | ||
if err != nil { | ||
log.Printf("Invalid DB File Path: %s\n", dbFilePath) | ||
return | ||
} | ||
defer file.Close() | ||
|
||
topicList := make([]Topic, 0) | ||
for _, topic := range topics { | ||
topicList = append(topicList, *topic) | ||
} | ||
|
||
bytes, err := json.Marshal(topicList) | ||
if err != nil { | ||
log.Printf("DB Encoding Failed. %v", err) | ||
return | ||
} | ||
|
||
_, err = file.Write(bytes) | ||
if err != nil { | ||
log.Printf("DB Write Failed. %v\n", err) | ||
return | ||
} | ||
|
||
log.Println("Topics Saved.") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
package main | ||
|
||
import "encoding/json" | ||
|
||
type Topic struct { | ||
Id uint64 | ||
Content string | ||
Choices []string | ||
Votes []uint64 | ||
Deleted bool | ||
} | ||
|
||
func (t Topic) isValidVoteIndex(index int) bool { | ||
return index < len(t.Votes) | ||
} | ||
func (t *Topic) makeJson() []byte { | ||
bytes, _ := json.Marshal(*t) | ||
return bytes | ||
} | ||
func (t *Topic) vote(index int) { | ||
t.Votes[index]++ | ||
} | ||
|
||
type TopicForm struct { | ||
Content string | ||
Choices []string | ||
} | ||
|
||
type TopicManager struct { | ||
topics map[uint64]*Topic | ||
} | ||
func (m *TopicManager) getTopicList() []Topic { | ||
topicList := make([]Topic, 0) | ||
for topicId := range m.topics { | ||
if (!m.topics[topicId].Deleted) { | ||
topicList = append(topicList, *m.topics[topicId]) | ||
} | ||
} | ||
return topicList | ||
} | ||
func (m *TopicManager) getTopic(topicId uint64) (*Topic, bool) { | ||
topic, ok := m.topics[topicId] | ||
return topic, ok | ||
} | ||
func (m *TopicManager) addTopic(topicForm *TopicForm) Topic { | ||
nextId := uint64(0) | ||
for id := range m.topics { | ||
if nextId <= id { | ||
nextId = id + 1 | ||
} | ||
} | ||
topic := Topic { | ||
nextId, | ||
topicForm.Content, | ||
topicForm.Choices, | ||
make([]uint64, len(topicForm.Choices)), | ||
false, | ||
} | ||
m.topics[topic.Id] = &topic | ||
return topic | ||
} | ||
func (m *TopicManager) deleteTopic(topicId uint64) bool { | ||
topic, ok := m.topics[topicId] | ||
if !ok { | ||
return false | ||
} | ||
topic.Deleted = true | ||
return true | ||
} | ||
func (m *TopicManager) vote(topicId uint64, voteIndex int) []uint64 { | ||
topic, ok := m.topics[topicId] | ||
if !ok { | ||
return make([]uint64, 0) | ||
} | ||
if !topic.isValidVoteIndex(voteIndex) { | ||
return make([]uint64, 0) | ||
} | ||
topic.vote(voteIndex) | ||
return topic.Votes | ||
} |