-
Notifications
You must be signed in to change notification settings - Fork 1
/
model.go
182 lines (151 loc) · 4.38 KB
/
model.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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
package main
import (
"database/sql"
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"os"
"strings"
"text/template"
)
type Model struct {
ID int
Name string
Files []string
}
var Models []Model
func FindModel(id int, models []Model) *Model {
for i := 0; i < len(models); i++ {
if models[i].ID == id {
return &models[i]
}
}
return nil
}
func LoadModels(db *sql.DB) ([]Model, error) {
rows, err := db.Query("SELECT id, name FROM model")
if err != nil {
return nil, err
}
var models []Model
for rows.Next() {
var id int
var name string
if err := rows.Scan(&id, &name); err != nil {
return nil, err
}
models = append(models, Model{ID: id, Name: name})
}
rows.Close()
for i := 0; i < len(models); i++ {
rows, err = db.Query("SELECT name FROM model_file WHERE model_id = ?", models[i].ID)
if err != nil {
return nil, err
}
for rows.Next() {
var name string
if err := rows.Scan(&name); err != nil {
return nil, err
}
models[i].Files = append(models[i].Files, name)
}
rows.Close()
}
return models, nil
}
func modelHandler(w http.ResponseWriter, r *http.Request) {
type JSONResponse struct {
Success bool `json:"success"`
Message string `json:"message"`
}
funcMap := template.FuncMap{
"join": func(in []string) string {
return strings.Join(in, ", ")
},
}
t, err := template.New("model.html").Funcs(funcMap).ParseFiles("model.html")
if err != nil {
json.NewEncoder(w).Encode(JSONResponse{Success: false, Message: err.Error()})
return
}
if err := t.Execute(w, Models); err != nil {
json.NewEncoder(w).Encode(JSONResponse{Success: false, Message: err.Error()})
return
}
}
func modelAddHandler(w http.ResponseWriter, r *http.Request) {
type JSONResponse struct {
Success bool `json:"success"`
Message string `json:"message"`
Model *Model `json:"model"`
}
if err := r.ParseMultipartForm(1 * 1024 * 1024); err != nil {
json.NewEncoder(w).Encode(JSONResponse{Success: false, Message: "Unable to parse form", Model: nil})
return
}
name := r.FormValue("name")
if name == "" {
json.NewEncoder(w).Encode(JSONResponse{Success: false, Message: "Missing Name", Model: nil})
return
}
if err := os.Mkdir("data/"+name, 0755); err != nil {
json.NewEncoder(w).Encode(JSONResponse{Success: false, Message: "Unable to create folder", Model: nil})
return
}
model := Model{Name: name}
res, err := DB.Exec("insert into model(name) values (?)", model.Name)
if err != nil {
json.NewEncoder(w).Encode(JSONResponse{Success: false, Message: err.Error()})
return
}
id, err := res.LastInsertId()
if err != nil {
json.NewEncoder(w).Encode(JSONResponse{Success: false, Message: err.Error()})
return
}
model.ID = int(id)
for _, fileHeaders := range r.MultipartForm.File {
for _, fileHeader := range fileHeaders {
file, _ := fileHeader.Open()
buf, _ := ioutil.ReadAll(file)
ioutil.WriteFile(fmt.Sprintf("data/%s/%s", name, fileHeader.Filename), buf, os.ModePerm)
model.Files = append(model.Files, fileHeader.Filename)
if _, err := DB.Exec("insert into model_file(name, model_id) values (?, ?)", fileHeader.Filename, id); err != nil {
json.NewEncoder(w).Encode(JSONResponse{Success: false, Message: err.Error()})
return
}
}
}
json.NewEncoder(w).Encode(JSONResponse{Success: true, Message: "", Model: &model})
}
func modelRemoveHandler(w http.ResponseWriter, r *http.Request) {
type JSONResponse struct {
Success bool `json:"success"`
Message string `json:"message"`
}
w.Header().Set("Content-Type", "application/json")
id := r.FormValue("id")
if id == "" {
json.NewEncoder(w).Encode(JSONResponse{Success: false, Message: "Missing Data"})
return
}
var name string
if err := DB.QueryRow("SELECT name FROM model WHERE id = ?", id).Scan(&name); err != nil {
json.NewEncoder(w).Encode(JSONResponse{Success: false, Message: err.Error()})
return
}
if err := os.RemoveAll("data/" + name); err != nil {
json.NewEncoder(w).Encode(JSONResponse{Success: false, Message: err.Error()})
return
}
if _, err := DB.Exec("DELETE FROM model_file WHERE model_id = ?", id); err != nil {
json.NewEncoder(w).Encode(JSONResponse{Success: false, Message: err.Error()})
return
}
if _, err := DB.Exec("DELETE FROM model WHERE id = ?", id); err != nil {
json.NewEncoder(w).Encode(JSONResponse{Success: false, Message: err.Error()})
return
}
json.NewEncoder(w).Encode(JSONResponse{Success: true})
}