From 870e768b5013950d577a5758d7e3235961b7d4e9 Mon Sep 17 00:00:00 2001 From: kavos Date: Mon, 12 Aug 2024 22:11:19 +0900 Subject: [PATCH] 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) }