package notes

import (
	"html"
	"local/notes-server/filetree"
	"net/http"
	"path"
	"strings"
)

func (n *Notes) Create(w http.ResponseWriter, r *http.Request) {
	content := r.FormValue("base")
	content = html.UnescapeString(content)
	content = strings.ReplaceAll(content, "\r", "")
	urlPath := path.Join(r.URL.Path, content)
	p := filetree.NewPathFromURL(urlPath)
	if p.IsDir() {
		w.WriteHeader(http.StatusBadRequest)
		return
	}
	url := *r.URL
	url.Path = path.Join("/edit/", p.BaseHREF)
	http.Redirect(w, r, url.String(), http.StatusSeeOther)
}
