Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions api/handler.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package main

import (
"encoding/json"
"net/http"

"github.com/labstack/echo/v4"
Expand Down Expand Up @@ -41,6 +42,25 @@ func handlerGetPost(c echo.Context) error {
return c.JSON(http.StatusOK, post)
}

func handlerGetPostReturnHTML(c echo.Context) error {
var post Post
postID := c.Param("post_id")
query := `
SELECT
id, title, content, create_at
FROM
posts
WHERE
id = ?`
if err := db.Get(&post, query, postID); err != nil {
return c.String(http.StatusNotFound, "記事がありません")
}

p, _ := json.Marshal(post)

return c.HTML(http.StatusOK, string(p))
}

func handlerGetComments(c echo.Context) error {
var comments []Comment
postID := c.Param("post_id")
Expand Down
1 change: 1 addition & 0 deletions api/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ func main() {
e := echo.New()
e.GET("/post/list", handlerGetPostList)
e.GET("/post/:post_id", handlerGetPost)
e.GET("/post/:post_id/html", handlerGetPostReturnHTML)
e.GET("/post/:post_id/comments", handlerGetComments)

e.POST("/post", handlerCreatePost)
Expand Down