-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
69 lines (59 loc) · 1.68 KB
/
main.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
package main
import (
"encoding/json"
"html/template"
"io/ioutil"
"net/http"
model "./models"
)
func main() {
http.HandleFunc("/", handler)
http.ListenAndServe(":9000", nil)
}
func handler(w http.ResponseWriter, r *http.Request) {
page := model.Page{ID: 3, Name: "Users", Description: "Users List", URI: "/users"}
users := loadUsers()
interests := loadInterests()
interestMappings := loadInterestMappings()
var newUsers []model.User
for _, user := range users {
for _, interestMapping := range interestMappings {
if user.ID == interestMapping.UserID {
for _, interest := range interests {
if interestMapping.InterestID == interest.ID {
user.Interests = append(user.Interests, interest)
}
}
}
}
newUsers = append(newUsers, user)
}
viewModel := model.UserViewModel{Page: page, Users: newUsers}
t, _ := template.ParseFiles("template/page.html")
t.Execute(w, viewModel)
}
func loadFile(fileName string) (string, error) {
bytes, err := ioutil.ReadFile(fileName)
if err != nil {
return "", err
}
return string(bytes), nil
}
func loadUsers() []model.User {
bytes, _ := ioutil.ReadFile("json/users.json")
var users []model.User
json.Unmarshal(bytes, &users)
return users
}
func loadInterests() []model.Interest {
bytes, _ := ioutil.ReadFile("json/interests.json")
var interests []model.Interest
json.Unmarshal(bytes, &interests)
return interests
}
func loadInterestMappings() []model.InterestMapping {
bytes, _ := ioutil.ReadFile("json/userInterestMappings.json")
var InterestMappings []model.InterestMapping
json.Unmarshal(bytes, &InterestMappings)
return InterestMappings
}