diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 0000000..13566b8 --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,8 @@ +# Default ignored files +/shelf/ +/workspace.xml +# Editor-based HTTP Client requests +/httpRequests/ +# Datasource local storage ignored files +/dataSources/ +/dataSources.local.xml diff --git a/.idea/cetvorke.iml b/.idea/cetvorke.iml new file mode 100644 index 0000000..5e764c4 --- /dev/null +++ b/.idea/cetvorke.iml @@ -0,0 +1,9 @@ + + + + + + + + + \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml new file mode 100644 index 0000000..838dcdc --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..35eb1dd --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/cethttp/cethttp.go b/cethttp/cethttp.go index bf83606..5916cac 100644 --- a/cethttp/cethttp.go +++ b/cethttp/cethttp.go @@ -32,7 +32,6 @@ func StartServer(waitgroup *sync.WaitGroup) { r.HandleFunc("/y/{year}/m/{month}/", monthHandler) r.HandleFunc("/editor/e/{postID}", editHandler) r.HandleFunc("/editor/n/", newHandler) - r.HandeeFunc("/editor", editorHandler) err = http.ListenAndServe(":8018", r) if err != nil { diff --git a/cethttp/editor.go b/cethttp/editor.go index 8e10285..ca7cf1c 100644 --- a/cethttp/editor.go +++ b/cethttp/editor.go @@ -1,16 +1,97 @@ package cethttp -const editorPage = ` -
- - -
+import ( + "html/template" + "strings" + + "github.com/senaduka/cetvorke/database" +) + +const editorTemplate = ` + + + + + IslamBosna + + + + + + + +
+
+ ☙ IslamBosna +
+
+ +
+ + + +
+
{{.MarkdownContent}}
+
+
+
+ + + ` + +func editorHtml5Page(post *database.Post) string { + t1 := template.New("editorHtml5Page") + t1, _ = t1.Parse(editorTemplate) + result := new(strings.Builder) + t1.Execute(result, post) + return result.String() +} diff --git a/cethttp/editor_handlers.go b/cethttp/editor_handlers.go index 058794d..ad9372d 100644 --- a/cethttp/editor_handlers.go +++ b/cethttp/editor_handlers.go @@ -1 +1,65 @@ package cethttp + +import ( + "net/http" + "strconv" + + "github.com/gorilla/mux" + "github.com/senaduka/cetvorke/database" +) + +// editHandler handles the editing of an existing post. +func editHandler(w http.ResponseWriter, r *http.Request) { + vars := mux.Vars(r) + postID, err := strconv.Atoi(vars["postID"]) + if err != nil { + http.Error(w, err.Error(), http.StatusInternalServerError) + return + } + + // Fetch the post from the database. + post, err := database.GetPost(postID) + if err != nil { + http.Error(w, err.Error(), http.StatusInternalServerError) + return + } + + // Check if the request method is POST. + if r.Method == "POST" { + // Get the edited content from the form. + editedContent := r.FormValue("editor") + editedTitle := r.FormValue("title") + + // Update the post content. + post.Content = editedContent + post.Title = editedTitle + + // Save the edited post to the database. + err := database.UpdatePost(post) + if err != nil { + http.Error(w, err.Error(), http.StatusInternalServerError) + return + } + + // Redirect to the edited post. + http.Redirect(w, r, "/p/"+vars["postID"]+"/"+post.TitleSlug, http.StatusSeeOther) + return + } + + // Display the edit form. + response := editorHtml5Page(post) + w.Write([]byte(response)) +} + +// newHandler handles the creation of a new post. +func newHandler(w http.ResponseWriter, r *http.Request) { + // Create a new empty post in the database. + newPost, err := database.CreateNewPost() + if err != nil { + http.Error(w, err.Error(), http.StatusInternalServerError) + return + } + + // Redirect to the editHandler for the new post. + http.Redirect(w, r, "/editor/e/"+strconv.Itoa(newPost.ID), http.StatusSeeOther) +} diff --git a/cethttp/template.go b/cethttp/template.go index 2318d3d..7d98910 100644 --- a/cethttp/template.go +++ b/cethttp/template.go @@ -51,10 +51,7 @@ const html5Template = ` color: #e6fbfb; } - - - - +
diff --git a/cetvorke b/cetvorke index f00b687..e7c2c3b 100755 Binary files a/cetvorke and b/cetvorke differ diff --git a/database/post.go b/database/post.go index 1c4fd38..e2334c5 100644 --- a/database/post.go +++ b/database/post.go @@ -3,6 +3,7 @@ package database import ( "database/sql" "fmt" + "time" "github.com/russross/blackfriday/v2" ) @@ -39,3 +40,33 @@ func GetPost(id int) (*Post, error) { } return post, nil } + +func UpdatePost(p *Post) error { + db := GetDB() + _, err := db.Exec("UPDATE posts SET post_title = ?, markdown_content = ?, gemtext_content = ?, title_slug = ?, year = ?, month = ?, day = ? WHERE id = ?", p.Title, p.MarkdownContent, p.GemtextContent, p.TitleSlug, p.Year, p.Month, p.Day, p.ID) + if err != nil { + return err + } + return nil +} + +func CreateNewPost() (*Post, error) { + db := GetDB() + day := time.Now().Day() + month := time.Now().Month() + year := time.Now().Year() + + result, err := db.Exec("INSERT INTO posts (post_title, markdown_content, gemtext_content, title_slug, year, month, day) VALUES (?, ?, ?, ?, ?, ?, ?)", "Novi Clanak", "", "", "new-post", year, month, day) + if err != nil { + return nil, err + } + postID, err := result.LastInsertId() + if err != nil { + return nil, err + } + post, err := GetPost(int(postID)) + if err != nil { + return nil, err + } + return post, nil +}