added create watch templates
This commit is contained in:
parent
2916965154
commit
e9873a7c85
6 changed files with 139 additions and 51 deletions
94
main.go
94
main.go
|
@ -27,7 +27,7 @@ import (
|
|||
_ "embed"
|
||||
)
|
||||
|
||||
//go:embed templates static
|
||||
//go:embed templates static watchTemplates
|
||||
var EMBED_FS embed.FS
|
||||
|
||||
type Web struct {
|
||||
|
@ -102,6 +102,7 @@ func (web *Web) initRouter() {
|
|||
|
||||
web.router.GET("/watch/view/:id", web.watchView)
|
||||
web.router.GET("/watch/edit/:id", web.watchEdit)
|
||||
web.router.GET("/watch/create", web.watchCreate)
|
||||
web.router.POST("/watch/create", web.watchCreatePost)
|
||||
web.router.POST("/watch/update", web.watchUpdate)
|
||||
web.router.POST("/watch/delete", web.deleteWatch)
|
||||
|
@ -125,6 +126,7 @@ func (web *Web) initTemplates() {
|
|||
web.templates.Add("index", template.Must(template.ParseFS(templatesFS, "base.html", "index.html")))
|
||||
|
||||
web.templates.Add("watchView", template.Must(template.ParseFS(templatesFS, "base.html", "watch/view.html")))
|
||||
web.templates.Add("watchCreate", template.Must(template.ParseFS(templatesFS, "base.html", "watch/create.html")))
|
||||
web.templates.Add("watchEdit", template.Must(template.ParseFS(templatesFS, "base.html", "watch/edit.html")))
|
||||
|
||||
web.templates.Add("cacheView", template.Must(template.ParseFS(templatesFS, "base.html", "cache/view.html")))
|
||||
|
@ -247,6 +249,23 @@ func (web *Web) index(c *gin.Context) {
|
|||
c.HTML(http.StatusOK, "index", watches)
|
||||
}
|
||||
|
||||
func (web *Web) watchCreate(c *gin.Context) {
|
||||
templateFiles, err := EMBED_FS.ReadDir("watchTemplates")
|
||||
if err != nil {
|
||||
log.Fatalln("Could not load templates from embed FS")
|
||||
}
|
||||
templates := make([]string, 0, len(templateFiles))
|
||||
templates = append(templates, "None")
|
||||
for _, template := range templateFiles {
|
||||
templateFile := template.Name()
|
||||
templateName := templateFile[:len(templateFile)-len(".json")]
|
||||
templates = append(templates, templateName)
|
||||
}
|
||||
c.HTML(http.StatusOK, "watchCreate", gin.H{
|
||||
"templates": templates,
|
||||
})
|
||||
}
|
||||
|
||||
func (web *Web) watchCreatePost(c *gin.Context) {
|
||||
var watch Watch
|
||||
errMap, err := bindAndValidateWatch(&watch, c)
|
||||
|
@ -254,7 +273,80 @@ func (web *Web) watchCreatePost(c *gin.Context) {
|
|||
c.HTML(http.StatusBadRequest, "500", errMap)
|
||||
return
|
||||
}
|
||||
|
||||
templateID, err := strconv.Atoi(c.PostForm("template"))
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
templateID = 0
|
||||
}
|
||||
|
||||
if templateID == 0 {
|
||||
web.db.Create(&watch)
|
||||
c.Redirect(http.StatusSeeOther, fmt.Sprintf("/watch/edit/%d", watch.ID))
|
||||
return
|
||||
}
|
||||
|
||||
templateFiles, err := EMBED_FS.ReadDir("watchTemplates")
|
||||
if err != nil {
|
||||
log.Fatalln("Could not load templates from embed FS")
|
||||
}
|
||||
|
||||
if templateID >= len(templateFiles) {
|
||||
log.Println("/watch/create POSTed with", templateID, "but only", len(templateFiles), "templates")
|
||||
c.AbortWithError(http.StatusBadRequest, err)
|
||||
return
|
||||
}
|
||||
|
||||
template := templateFiles[templateID-1] // -1 because of "None" option
|
||||
templatePath := fmt.Sprintf("watchTemplates/%s", template.Name())
|
||||
jsn, err := EMBED_FS.ReadFile(templatePath)
|
||||
if err != nil {
|
||||
log.Println("Could not read template from embed.FS:", err)
|
||||
c.AbortWithError(http.StatusBadRequest, err)
|
||||
return
|
||||
}
|
||||
|
||||
export := WatchExport{}
|
||||
if err := json.Unmarshal(jsn, &export); err != nil {
|
||||
log.Println("Could not unmarshel JSON:", err)
|
||||
c.AbortWithError(http.StatusBadRequest, err)
|
||||
return
|
||||
}
|
||||
|
||||
web.db.Create(&watch)
|
||||
|
||||
filterMap := make(map[uint]*Filter)
|
||||
for i := range export.Filters {
|
||||
filter := &export.Filters[i]
|
||||
filterMap[filter.ID] = filter
|
||||
filter.ID = 0
|
||||
filter.WatchID = watch.ID
|
||||
}
|
||||
if len(export.Filters) > 0 {
|
||||
tx := web.db.Create(&export.Filters)
|
||||
if tx.Error != nil {
|
||||
log.Println("Create filters transaction failed:", err)
|
||||
c.AbortWithError(http.StatusBadRequest, err)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
for i := range export.Connections {
|
||||
connection := &export.Connections[i]
|
||||
connection.ID = 0
|
||||
connection.WatchID = watch.ID
|
||||
connection.OutputID = filterMap[connection.OutputID].ID
|
||||
connection.InputID = filterMap[connection.InputID].ID
|
||||
}
|
||||
if len(export.Connections) > 0 {
|
||||
tx := web.db.Create(&export.Connections)
|
||||
if tx.Error != nil {
|
||||
log.Println("Create connections transaction failed:", err)
|
||||
c.AbortWithError(http.StatusBadRequest, err)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
c.Redirect(http.StatusSeeOther, fmt.Sprintf("/watch/edit/%d", watch.ID))
|
||||
}
|
||||
|
||||
|
|
|
@ -1,23 +0,0 @@
|
|||
function newWatch() {
|
||||
var response = prompt("Name of new Watch", "");
|
||||
if (response == null || response == "") {
|
||||
return; // do nothing
|
||||
}
|
||||
var data = new URLSearchParams();
|
||||
data.append("watch_name", response);
|
||||
fetch("/watch/create", {
|
||||
method: "POST",
|
||||
body: data
|
||||
}).then(function (response) {
|
||||
if (!response.ok) {
|
||||
alert("Could not create watch");
|
||||
return;
|
||||
}
|
||||
window.location.href = response.url;
|
||||
});
|
||||
}
|
||||
function newWatchLinkInit() {
|
||||
var newWatchLink = document.getElementById("newWatchLink");
|
||||
newWatchLink.onclick = newWatch;
|
||||
}
|
||||
document.addEventListener('DOMContentLoaded', newWatchLinkInit, false);
|
|
@ -1,25 +0,0 @@
|
|||
function newWatch(){
|
||||
let response = prompt("Name of new Watch", "");
|
||||
if (response == null || response == ""){
|
||||
return // do nothing
|
||||
}
|
||||
let data = new URLSearchParams();
|
||||
data.append("watch_name", response);
|
||||
fetch("/watch/create", {
|
||||
method: "POST",
|
||||
body: data,
|
||||
}).then((response) => {
|
||||
if(!response.ok){
|
||||
alert("Could not create watch");
|
||||
return;
|
||||
}
|
||||
window.location.href = response.url;
|
||||
});
|
||||
}
|
||||
|
||||
function newWatchLinkInit(){
|
||||
let newWatchLink = document.getElementById("newWatchLink") as HTMLElement;
|
||||
newWatchLink.onclick = newWatch;
|
||||
}
|
||||
|
||||
document.addEventListener('DOMContentLoaded', newWatchLinkInit, false);
|
|
@ -24,7 +24,7 @@
|
|||
<a class="nav-link active" aria-current="page" href="/">Home</a>
|
||||
</li>
|
||||
<li class="nav-item">
|
||||
<a class="nav-link" aria-current="page" id="newWatchLink" href="#">New</a>
|
||||
<a class="nav-link" aria-current="page" id="newWatchLink" href="/watch/create">New</a>
|
||||
</li>
|
||||
{{ template "navbar" .}}
|
||||
</ul>
|
||||
|
|
44
templates/watch/create.html
Normal file
44
templates/watch/create.html
Normal file
|
@ -0,0 +1,44 @@
|
|||
{{define "title"}}
|
||||
GoWatch {{ .Watch.Name }}
|
||||
{{end}}
|
||||
|
||||
{{define "content"}}
|
||||
|
||||
<form action="/watch/create" method="POST">
|
||||
<div class="container">
|
||||
<div class="row g-3 justify-content-center">
|
||||
<div class="col-auto">
|
||||
<div class="form-floating">
|
||||
<input type="text" class="form-control form-control-lg" width="20" id="watch_name" name="watch_name" placeholder="Watch Name">
|
||||
<label for="watch_name">Watch Name</label>
|
||||
</div>
|
||||
</div>
|
||||
<div class="col-auto">
|
||||
<span id="submitInline" class="form-text">
|
||||
<button class="btn btn-primary btn-lg">Create</button>
|
||||
</span>
|
||||
</div>
|
||||
</div>
|
||||
<div class="row justify-content-center mt-5">
|
||||
<div class="row h3 justify-content-center">With Watch Templates:</div>
|
||||
<div class="col-2"> </div>
|
||||
<div class="col-2">
|
||||
{{ range $i, $name := .templates }}
|
||||
<div class="form-check d-flex my-2">
|
||||
{{ if eq $i 0 }}
|
||||
<input class="form-check-input" type="radio" name="template" value="{{ $i }}" id="template_{{ $i }}" checked>
|
||||
{{ else }}
|
||||
<input class="form-check-input" type="radio" name="template" value="{{ $i }}" id="template_{{ $i }}">
|
||||
{{ end }}
|
||||
<label class="form-check-label h5 mx-3" for="template_{{ $i }}">
|
||||
{{ $name }}
|
||||
</label>
|
||||
</div>
|
||||
{{ end }}
|
||||
</div>
|
||||
<div class="col-2"> </div>
|
||||
</div>
|
||||
</div>
|
||||
</form>
|
||||
|
||||
{{end}}
|
2
todo.md
2
todo.md
|
@ -7,7 +7,7 @@
|
|||
- trusted proxies in conf?
|
||||
- log things to db for cron runs
|
||||
- comments
|
||||
- provide template/example watches?
|
||||
- ~~provide template/example watches~~
|
||||
- Amazon
|
||||
- NewEgg
|
||||
- ebay
|
||||
|
|
Loading…
Add table
Reference in a new issue