Skip to content

Commit

Permalink
impl getlecturechildfolderhandler
Browse files Browse the repository at this point in the history
  • Loading branch information
kavos113 committed Aug 12, 2024
1 parent 870e768 commit 1c1fa6e
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 1 deletion.
48 changes: 47 additions & 1 deletion src/handler/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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{}
Expand All @@ -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)
}
16 changes: 16 additions & 0 deletions src/handler/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
}

0 comments on commit 1c1fa6e

Please sign in to comment.