-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
77 lines (61 loc) · 1.66 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
70
71
72
73
74
75
76
77
package main
import (
"encoding/json"
"fmt"
"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{Title: "Selamlar Go Bey"}
products := loadProducts()
categories := loadCategories()
relationMappings := loadIRelationMappings()
var newProducts []model.Product
for _, product := range products {
for _, relationMapping := range relationMappings {
if product.ID == relationMapping.ProductID {
for _, category := range categories {
if relationMapping.CategoryID == category.ID {
product.Categories = append(product.Categories, category)
}
}
}
}
newProducts = append(newProducts, product)
}
fmt.Printf("%+v\n", newProducts)
view := model.View{Page: page, Products: newProducts}
t, _ := template.ParseFiles("index.html")
t.Execute(w, view)
}
func loadFile(fileName string) (string, error) {
bytes, err := ioutil.ReadFile(fileName)
if err != nil {
return "", err
}
return string(bytes), nil
}
func loadProducts() []model.Product {
bytes, _ := ioutil.ReadFile("json/products.json")
var products []model.Product
json.Unmarshal(bytes, &products)
return products
}
func loadCategories() []model.Category {
bytes, _ := ioutil.ReadFile("json/categories.json")
var categories []model.Category
json.Unmarshal(bytes, &categories)
return categories
}
func loadIRelationMappings() []model.RelationMapping {
bytes, _ := ioutil.ReadFile("json/relation.json")
var relationMappings []model.RelationMapping
json.Unmarshal(bytes, &relationMappings)
return relationMappings
}