Skip to content

Commit 6a248e6

Browse files
committed
add importing and exporting
1 parent 8a35bb3 commit 6a248e6

File tree

4 files changed

+81
-1
lines changed

4 files changed

+81
-1
lines changed

main.go

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package main
22

33
import (
4+
"encoding/json"
45
"flag"
56
"fmt"
67
"html/template"
@@ -78,6 +79,7 @@ func main() {
7879
"projects-list.html",
7980
"projects-description.html",
8081
"projects-steps-table.html",
82+
"port.html",
8183
))
8284

8385
} else {
@@ -86,6 +88,7 @@ func main() {
8688
"/usr/share/pm/html/projects-list.html",
8789
"/usr/share/pm/html/projects-description.html",
8890
"/usr/share/pm/html/projects-steps-table.html",
91+
"/usr/share/pm/html/port.html",
8992
))
9093
}
9194
mux := http.NewServeMux()
@@ -182,6 +185,43 @@ func main() {
182185
http.Redirect(w, r, "/projects/"+projectId, http.StatusSeeOther)
183186
})
184187

188+
mux.HandleFunc("GET /export", func(w http.ResponseWriter, r *http.Request) {
189+
JsonRep, err := json.Marshal(repo.GetAllProjects())
190+
panicIfErr(err)
191+
viewData := struct {
192+
ExportData, PageTitle string
193+
DisplaySubmit bool
194+
}{
195+
ExportData: string(JsonRep),
196+
PageTitle: "Export Data",
197+
DisplaySubmit: false,
198+
}
199+
templates.ExecuteTemplate(w, "port", viewData)
200+
})
201+
202+
mux.HandleFunc("GET /import", func(w http.ResponseWriter, r *http.Request) {
203+
viewData := struct {
204+
ExportData, PageTitle string
205+
DisplaySubmit bool
206+
}{
207+
ExportData: "",
208+
PageTitle: "Import Data",
209+
DisplaySubmit: true,
210+
}
211+
templates.ExecuteTemplate(w, "port", viewData)
212+
})
213+
214+
mux.HandleFunc("POST /import", func(w http.ResponseWriter, r *http.Request) {
215+
payload := r.FormValue("payload")
216+
if payload == "" || payload == " " {
217+
fmt.Println("Payload value was empty")
218+
} else {
219+
repo.ImportJson(payload)
220+
}
221+
222+
http.Redirect(w, r, "/", http.StatusSeeOther)
223+
})
224+
185225
port := "0.0.0.0:8080"
186226
fmt.Println("Listening on http://localhost" + port)
187227
http.ListenAndServe(port, mux)

main.html

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,13 @@
1818
<button hx-get="/" hx-target="body" hx-swap="innerHTML" hx-push-url="true">
1919
Home
2020
</button>
21-
<div class="row">
21+
<button hx-get="/import" hx-target=".main">
22+
Import
23+
</button>
24+
<button hx-get="/export" hx-target=".main">
25+
Export
26+
</button>
27+
<div class="row main">
2228
<div class="col-6" id="projects-list">
2329
{{ template "projectsList" . }}
2430
</div>

port.html

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
{{define "port"}}
2+
3+
<h1>{{.PageTitle}}</h1>
4+
5+
<form action="/import" method="POST" style="width: 90vw; margin: auto;">
6+
<textarea name="payload" id="payload" style="display: block; width: 100%">
7+
{{.ExportData}}
8+
</textarea>
9+
{{if .DisplaySubmit}}
10+
<button>Submit</button>
11+
{{end}}
12+
</form>
13+
14+
{{end}}

repository.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
package main
22

33
import (
4+
"encoding/json"
45
"errors"
6+
"fmt"
57

68
"github.com/google/uuid"
79
)
@@ -102,3 +104,21 @@ func (r *Repository) AddStepToProject(projectId string, step ProjectStep) (bool,
102104

103105
return true, nil
104106
}
107+
108+
func (r *Repository) ImportJson(jsonData string) {
109+
var tempProjects []Project
110+
err := json.Unmarshal([]byte(jsonData), &tempProjects)
111+
panicIfErr(err)
112+
113+
fmt.Println(tempProjects)
114+
for _, p := range tempProjects {
115+
_, err := r.GetProjectById(p.Id)
116+
if err == nil {
117+
continue
118+
}
119+
r.AddProject(p)
120+
for _, ps := range p.Steps {
121+
r.AddStepToProject(p.Id, ps)
122+
}
123+
}
124+
}

0 commit comments

Comments
 (0)