From 3b9c8aa26c55c1749e376567b88b7f336fc23ede Mon Sep 17 00:00:00 2001 From: kavos Date: Mon, 12 Aug 2024 21:35:16 +0900 Subject: [PATCH 1/5] impl getlecturebyfolderidhandler --- schema.sql | 2 +- src/handler/handler.go | 73 ++++++++++++++++++++++++++++++++++++++++++ src/handler/model.go | 15 +++++++++ 3 files changed, 89 insertions(+), 1 deletion(-) create mode 100644 src/handler/model.go diff --git a/schema.sql b/schema.sql index 668afbf..1d33dcc 100644 --- a/schema.sql +++ b/schema.sql @@ -27,6 +27,6 @@ CREATE TABLE tags_in_wiki ( CREATE TABLE folders ( id INT(11) NOT NULL PRIMARY KEY AUTO_INCREMENT, name TEXT NOT NULL, - parent_id INT(11), + parent_id INT(11), -- 0 if root UNIQUE KEY (name, parent_id) ); \ No newline at end of file diff --git a/src/handler/handler.go b/src/handler/handler.go index 791a10b..c3de353 100644 --- a/src/handler/handler.go +++ b/src/handler/handler.go @@ -1,7 +1,11 @@ package handler import ( + "database/sql" + "errors" + "log" "net/http" + "strconv" "github.com/jmoiron/sqlx" "github.com/labstack/echo" @@ -18,3 +22,72 @@ func NewHandler(db *sqlx.DB) *Handler { func (h *Handler) PingHandler(c echo.Context) error { return c.String(http.StatusOK, "pong") } + +func (h *Handler) GetLectureByFolderIDHandler(c echo.Context) error { + folderID, err := strconv.Atoi(c.Param("folderId")) + if err != nil { + log.Printf("failed to convert folderId to int: %v", err) + return c.JSON(http.StatusBadRequest, err) + } + + lectures := []LectureFromDB{} + err = h.db.Select(&lectures, "SELECT * FROM lectures WHERE folder_id = ?", folderID) + if err != nil { + if errors.Is(err, sql.ErrNoRows) { + return c.JSON(http.StatusNotFound, err) + } + log.Printf("failed to get lectures: %v", err) + return c.JSON(http.StatusInternalServerError, err) + } + + folderTree := []string{} + folderName := "" + err = h.db.Get(&folderName, "SELECT name FROM folders WHERE id = ?", folderID) + if err != nil { + log.Printf("failed to get folder name: %v", err) + return c.JSON(http.StatusInternalServerError, err) + } + folderTree = append(folderTree, folderName) + for { + parentID := 0 + err = h.db.Get(&parentID, "SELECT parent_id FROM folders WHERE id = ?", folderID) + if err != nil { + log.Printf("failed to get parent_id: %v", err) + return c.JSON(http.StatusInternalServerError, err) + } + + // root folder + if parentID == 0 { + break + } + + err = h.db.Get(&folderName, "SELECT name FROM folders WHERE id = ?", parentID) + if err != nil { + log.Printf("failed to get folder name: %v", err) + return c.JSON(http.StatusInternalServerError, err) + } + folderTree = append(folderTree, folderName) + folderID = parentID + } + folderPath := "" + for i, name := range folderTree { + if i == 0 { + folderPath = name + } else { + folderPath = name + "/" + folderPath + } + } + folderPath = "/" + folderPath + + lecturesWithFolderPath := []Lecture{} + for _, lecture := range lectures { + lecturesWithFolderPath = append(lecturesWithFolderPath, Lecture{ + ID: lecture.ID, + Title: lecture.Title, + Content: lecture.Content, + FolderPath: folderPath, + }) + } + + return c.JSON(http.StatusOK, lecturesWithFolderPath) +} diff --git a/src/handler/model.go b/src/handler/model.go new file mode 100644 index 0000000..293c9ff --- /dev/null +++ b/src/handler/model.go @@ -0,0 +1,15 @@ +package handler + +type LectureFromDB struct { + ID int `db:"id"` + Title string `db:"title"` + Content string `db:"content"` + FolderID int `db:"folder_id"` +} + +type Lecture struct { + ID int `json:"id"` + Title string `json:"title"` + Content string `json:"content"` + FolderPath string `json:"folderpath"` +} \ No newline at end of file From ad0af4324f43940b19093353115e2a63b3fa6d0d Mon Sep 17 00:00:00 2001 From: kavos Date: Mon, 12 Aug 2024 22:00:26 +0900 Subject: [PATCH 2/5] update openapi folderpath --- docs/openapi.yaml | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/docs/openapi.yaml b/docs/openapi.yaml index 17da479..1922ce2 100644 --- a/docs/openapi.yaml +++ b/docs/openapi.yaml @@ -91,7 +91,8 @@ paths: required: true schema: type: string - description: フォルダパス + example: School-ComputerScience-Python + description: フォルダパス ハイフン区切り responses: '200': description: OK From 38fcff8c3449a288c5510c7aa2e319a0145d0d67 Mon Sep 17 00:00:00 2001 From: kavos Date: Mon, 12 Aug 2024 22:09:14 +0900 Subject: [PATCH 3/5] add folderpath in db --- schema.sql | 1 + src/handler/handler.go | 54 +++++++++--------------------------------- src/handler/model.go | 17 ++++++------- 3 files changed, 21 insertions(+), 51 deletions(-) diff --git a/schema.sql b/schema.sql index 1d33dcc..078c6c7 100644 --- a/schema.sql +++ b/schema.sql @@ -12,6 +12,7 @@ CREATE TABLE lectures ( title TEXT NOT NULL, content TEXT NOT NULL, folder_id INT(11) + folder_path TEXT NOT NULL, ); CREATE TABLE tags ( id INT(11) NOT NULL PRIMARY KEY AUTO_INCREMENT, diff --git a/src/handler/handler.go b/src/handler/handler.go index c3de353..51c9485 100644 --- a/src/handler/handler.go +++ b/src/handler/handler.go @@ -6,6 +6,7 @@ import ( "log" "net/http" "strconv" + "strings" "github.com/jmoiron/sqlx" "github.com/labstack/echo" @@ -40,54 +41,21 @@ func (h *Handler) GetLectureByFolderIDHandler(c echo.Context) error { return c.JSON(http.StatusInternalServerError, err) } - folderTree := []string{} - folderName := "" - err = h.db.Get(&folderName, "SELECT name FROM folders WHERE id = ?", folderID) - if err != nil { - log.Printf("failed to get folder name: %v", err) - return c.JSON(http.StatusInternalServerError, err) - } - folderTree = append(folderTree, folderName) - for { - parentID := 0 - err = h.db.Get(&parentID, "SELECT parent_id FROM folders WHERE id = ?", folderID) - if err != nil { - log.Printf("failed to get parent_id: %v", err) - return c.JSON(http.StatusInternalServerError, err) - } - - // root folder - if parentID == 0 { - break - } - - err = h.db.Get(&folderName, "SELECT name FROM folders WHERE id = ?", parentID) - if err != nil { - log.Printf("failed to get folder name: %v", err) - return c.JSON(http.StatusInternalServerError, err) - } - folderTree = append(folderTree, folderName) - folderID = parentID - } - folderPath := "" - for i, name := range folderTree { - if i == 0 { - folderPath = name - } else { - folderPath = name + "/" + folderPath - } - } - folderPath = "/" + folderPath - lecturesWithFolderPath := []Lecture{} for _, lecture := range lectures { lecturesWithFolderPath = append(lecturesWithFolderPath, Lecture{ - ID: lecture.ID, - Title: lecture.Title, - Content: lecture.Content, - FolderPath: folderPath, + ID: lecture.ID, + Title: lecture.Title, + Content: lecture.Content, + FolderPath: lecture.FolderPath, }) } return c.JSON(http.StatusOK, lecturesWithFolderPath) } + +func (h *Handler) GetLectureByFolderPath(c echo.Context) error { + folderPath := c.Param("folderPath") + + folderTree := strings.Split(folderPath, "-") +} diff --git a/src/handler/model.go b/src/handler/model.go index 293c9ff..193f69d 100644 --- a/src/handler/model.go +++ b/src/handler/model.go @@ -1,15 +1,16 @@ package handler type LectureFromDB struct { - ID int `db:"id"` - Title string `db:"title"` - Content string `db:"content"` - FolderID int `db:"folder_id"` + ID int `db:"id"` + Title string `db:"title"` + Content string `db:"content"` + FolderID int `db:"folder_id"` + FolderPath string `db:"folderpath"` } type Lecture struct { - ID int `json:"id"` - Title string `json:"title"` - Content string `json:"content"` + ID int `json:"id"` + Title string `json:"title"` + Content string `json:"content"` FolderPath string `json:"folderpath"` -} \ No newline at end of file +} From 870e768b5013950d577a5758d7e3235961b7d4e9 Mon Sep 17 00:00:00 2001 From: kavos Date: Mon, 12 Aug 2024 22:11:19 +0900 Subject: [PATCH 4/5] impl getlecturebyfolderpathhandler --- src/handler/handler.go | 25 +++++++++++++++++++++++-- 1 file changed, 23 insertions(+), 2 deletions(-) diff --git a/src/handler/handler.go b/src/handler/handler.go index 51c9485..89141f6 100644 --- a/src/handler/handler.go +++ b/src/handler/handler.go @@ -54,8 +54,29 @@ func (h *Handler) GetLectureByFolderIDHandler(c echo.Context) error { return c.JSON(http.StatusOK, lecturesWithFolderPath) } -func (h *Handler) GetLectureByFolderPath(c echo.Context) error { +func (h *Handler) GetLectureByFolderPathHandler(c echo.Context) error { folderPath := c.Param("folderPath") - folderTree := strings.Split(folderPath, "-") + folderPath = "/" + strings.ReplaceAll(folderPath, "-", " /") + lectures := []LectureFromDB{} + err := h.db.Select(&lectures, "SELECT * FROM lectures WHERE folderpath = ?", folderPath) + if err != nil { + if errors.Is(err, sql.ErrNoRows) { + return c.JSON(http.StatusNotFound, err) + } + log.Printf("failed to get lectures: %v", err) + return c.JSON(http.StatusInternalServerError, err) + } + + lecturesWithFolderPath := []Lecture{} + for _, lecture := range lectures { + lecturesWithFolderPath = append(lecturesWithFolderPath, Lecture{ + ID: lecture.ID, + Title: lecture.Title, + Content: lecture.Content, + FolderPath: lecture.FolderPath, + }) + } + + return c.JSON(http.StatusOK, lecturesWithFolderPath) } From 1c1fa6e8f513ff99b922870aab7d65f53faedf6b Mon Sep 17 00:00:00 2001 From: kavos Date: Mon, 12 Aug 2024 22:28:27 +0900 Subject: [PATCH 5/5] impl getlecturechildfolderhandler --- src/handler/handler.go | 48 +++++++++++++++++++++++++++++++++++++++++- src/handler/model.go | 16 ++++++++++++++ 2 files changed, 63 insertions(+), 1 deletion(-) diff --git a/src/handler/handler.go b/src/handler/handler.go index 89141f6..faf8bda 100644 --- a/src/handler/handler.go +++ b/src/handler/handler.go @@ -20,10 +20,12 @@ func NewHandler(db *sqlx.DB) *Handler { return &Handler{db: db} } +// /ping func (h *Handler) PingHandler(c echo.Context) error { return c.String(http.StatusOK, "pong") } +// /lecture/byFolder/id/:folderId func (h *Handler) GetLectureByFolderIDHandler(c echo.Context) error { folderID, err := strconv.Atoi(c.Param("folderId")) if err != nil { @@ -54,8 +56,9 @@ func (h *Handler) GetLectureByFolderIDHandler(c echo.Context) error { return c.JSON(http.StatusOK, lecturesWithFolderPath) } +// /lecture/byFolder/path func (h *Handler) GetLectureByFolderPathHandler(c echo.Context) error { - folderPath := c.Param("folderPath") + folderPath := c.QueryParam("folderPath") folderPath = "/" + strings.ReplaceAll(folderPath, "-", " /") lectures := []LectureFromDB{} @@ -80,3 +83,46 @@ func (h *Handler) GetLectureByFolderPathHandler(c echo.Context) error { return c.JSON(http.StatusOK, lecturesWithFolderPath) } + +// /lecture/folder/:folderId +func (h *Handler) GetLectureChildFolderHandler(c echo.Context) error { + folderID, err := strconv.Atoi(c.Param("folderId")) + if err != nil { + log.Printf("failed to convert folderId to int: %v", err) + return c.JSON(http.StatusBadRequest, err) + } + + files := []File{} + + childFolders := []FolderFromDB{} + err = h.db.Select(&childFolders, "SELECT * FROM folders WHERE parent_id = ?", folderID) + if err != nil && !errors.Is(err, sql.ErrNoRows) { + log.Printf("failed to get child folders: %v", err) + return c.JSON(http.StatusInternalServerError, err) + } + + for _, folder := range childFolders { + files = append(files, File{ + ID: folder.ID, + Name: folder.Name, + IsFolder: true, + }) + } + + childLectures := []LectureOnlyName{} + err = h.db.Select(&childLectures, "SELECT id, title FROM lectures WHERE folder_id = ?", folderID) + if err != nil && !errors.Is(err, sql.ErrNoRows) { + log.Printf("failed to get child lectures: %v", err) + return c.JSON(http.StatusInternalServerError, err) + } + + for _, lecture := range childLectures { + files = append(files, File{ + ID: lecture.ID, + Name: lecture.Title, + IsFolder: false, + }) + } + + return c.JSON(http.StatusOK, files) +} \ No newline at end of file diff --git a/src/handler/model.go b/src/handler/model.go index 193f69d..607bb2c 100644 --- a/src/handler/model.go +++ b/src/handler/model.go @@ -14,3 +14,19 @@ type Lecture struct { Content string `json:"content"` FolderPath string `json:"folderpath"` } + +type FolderFromDB struct { + ID int `db:"id"` + Name string `db:"name"` +} + +type File struct { + ID int `json:"id"` + Name string `json:"name"` + IsFolder bool `json:"isFolder"` +} + +type LectureOnlyName struct { + ID int `db:"id"` + Title string `db:"title"` +}