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"` +}