Impl complete testing needed

This commit is contained in:
Bel LaPointe
2019-11-21 13:12:30 -07:00
parent 3079cd163f
commit a140d0eade
15 changed files with 111 additions and 33 deletions

16
server/delete.go Executable file
View File

@@ -0,0 +1,16 @@
package server
import (
"net/http"
"path"
"strings"
)
func (s *Server) delete(w http.ResponseWriter, r *http.Request) {
if err := s.Notes.Delete(r.URL.Path); err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
r.URL.Path = strings.Replace(path.Dir(r.URL.Path), "delete", "notes", 1)
http.Redirect(w, r, r.URL.String(), http.StatusPermanentRedirect)
}

View File

@@ -23,6 +23,12 @@ func (s *Server) notes(w http.ResponseWriter, r *http.Request) {
func notesHead(w http.ResponseWriter, p filetree.Path) {
fmt.Fprintln(w, h2(p.MultiLink()))
fmt.Fprintf(w, `
<form action=%q method="post">
<input type="text" name="keywords"></input>
<button type="submit">Search</button>
</form>
`, "/search")
}
func (s *Server) dir(w http.ResponseWriter, r *http.Request) {
@@ -59,4 +65,7 @@ func fileHead(w http.ResponseWriter, baseHREF string) {
fmt.Fprintf(w, `
<a href=%q><input type="button" value="Edit"></input></a>
`, path.Join("/edit/", baseHREF))
fmt.Fprintf(w, `
<a href=%q><input type="button" value="Delete"></input></a>
`, path.Join("/delete/", baseHREF))
}

View File

@@ -24,6 +24,10 @@ func (s *Server) Routes() error {
path: fmt.Sprintf("edit/%s%s", wildcard, wildcard),
handler: s.authenticate(s.edit),
},
{
path: fmt.Sprintf("delete/%s%s", wildcard, wildcard),
handler: s.authenticate(s.delete),
},
{
path: fmt.Sprintf("submit/%s%s", wildcard, wildcard),
handler: s.authenticate(s.submit),
@@ -32,6 +36,10 @@ func (s *Server) Routes() error {
path: fmt.Sprintf("create/%s%s", wildcard, wildcard),
handler: s.authenticate(s.create),
},
{
path: fmt.Sprintf("search"),
handler: s.authenticate(s.search),
},
}
for _, endpoint := range endpoints {

26
server/search.go Executable file
View File

@@ -0,0 +1,26 @@
package server
import (
"fmt"
"html"
"local/notes-server/filetree"
"net/http"
)
func (s *Server) search(w http.ResponseWriter, r *http.Request) {
if err := r.ParseForm(); err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
keywords := r.FormValue("keywords")
keywords = html.UnescapeString(keywords)
results, err := s.Notes.Search(keywords)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
head(w, r)
fmt.Fprintln(w, h2(filetree.NewPathFromURL("/notes").MultiLink()))
fmt.Fprintln(w, h1(keywords), results)
foot(w, r)
}