Add edit function

This commit is contained in:
Wancat
2022-11-04 23:36:23 +08:00
parent 7c63223aa0
commit b1d0bc826f
6 changed files with 68 additions and 5 deletions

View File

@@ -1,6 +1,7 @@
package main package main
import ( import (
"io/ioutil"
"log" "log"
"text/template" "text/template"
@@ -78,5 +79,31 @@ func router() *gin.Engine {
} }
}) })
authZone.GET("/edit", func(c *gin.Context) {
user := getUser(c)
f, err := user.ReadFile(DEFAULT_JOURNAL)
if err != nil {
panic(err)
}
defer f.Close()
data, err := ioutil.ReadAll(f)
if err != nil {
panic(err)
}
HTML(c, 200, "edit.html", string(data))
})
authZone.POST("/edit", func(c *gin.Context) {
user := getUser(c)
data := c.PostForm("data")
err := user.overwriteFile(data)
if err != nil {
panic(err)
}
HTML(c, 200, "success.html", struct {
Tx string
}{data})
})
return r return r
} }

8
templates/edit.html Normal file
View File

@@ -0,0 +1,8 @@
{{ define "title" }}編輯{{ end }}
{{ define "main" }}
<h1>編輯帳本</h1>
<form action="/edit" method="POST">
<textarea name="data" rows="15" cols="40">{{ . }}</textarea><br>
<input type="submit" value="儲存">
</form>
{{ end }}

View File

@@ -1,3 +1,3 @@
{{ define "main" }} {{ define "main" }}
<h1>Ledger Quick Note</h1> <h1>Ledger 速記</h1>
{{ end }} {{ end }}

View File

@@ -1,13 +1,17 @@
<!DOCTYPE html> <!DOCTYPE html>
<html> <html>
<head> <head>
<title>{{ block "title" . }}Ledger Quick Note{{ end }}</title> <title>{{ block "title" . }}Ledger 速記{{ end }}</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta name="viewport" content="width=device-width, initial-scale=1.0">
</head> </head>
<body> <body>
<nav> <nav>
<ul>
{{ with .User }} {{ with .User }}
{{ .Email }} {{ .Email }}
<li><a href="edit">編輯</a></li>
<li><a href="download">下載</a></li>
<li><a href="exec">查詢</a></li>
{{ end }} {{ end }}
</nav> </nav>
{{ block "main" .Data }} {{ block "main" .Data }}

14
tx.go
View File

@@ -25,7 +25,19 @@ func newTx(data TxData) (result string, err error) {
} }
func (u *User) appendToFile(tx string) (err error) { func (u *User) appendToFile(tx string) (err error) {
f, err := u.File(DEFAULT_JOURNAL) f, err := u.AppendFile(DEFAULT_JOURNAL)
if err != nil {
return err
}
defer f.Close()
buf := strings.NewReader(strings.ReplaceAll(tx, "\r", "")) // Remove CR generated from browser
_, err = io.Copy(f, buf)
return err
}
func (u *User) overwriteFile(tx string) (err error) {
f, err := u.WriteFile(DEFAULT_JOURNAL)
if err != nil { if err != nil {
return err return err
} }

16
user.go
View File

@@ -23,6 +23,18 @@ func (u *User) FilePath(name string) string {
return path.Join(u.Dir(), name) return path.Join(u.Dir(), name)
} }
func (u *User) File(name string) (*os.File, error) { func (u *User) File(name string, mode int) (*os.File, error) {
return os.OpenFile(u.FilePath(name), os.O_WRONLY|os.O_CREATE|os.O_APPEND, 0644) return os.OpenFile(u.FilePath(name), mode, 0644)
}
func (u *User) AppendFile(name string) (*os.File, error) {
return u.File(name, os.O_WRONLY|os.O_CREATE|os.O_APPEND)
}
func (u *User) ReadFile(name string) (*os.File, error) {
return u.File(name, os.O_RDONLY|os.O_CREATE)
}
func (u *User) WriteFile(name string) (*os.File, error) {
return u.File(name, os.O_WRONLY|os.O_CREATE|os.O_TRUNC)
} }