Skip to content

Commit f112e5f

Browse files
committed
feat: implement search file api
1 parent 304fece commit f112e5f

File tree

5 files changed

+59
-0
lines changed

5 files changed

+59
-0
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,4 @@ build/cmd
77
build/svelte
88
__debug_bin*
99
tmp/main
10+
videocms
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package controllers
2+
3+
import (
4+
"ch/kirari04/videocms/helpers"
5+
"ch/kirari04/videocms/logic"
6+
"ch/kirari04/videocms/models"
7+
8+
"github.com/labstack/echo/v4"
9+
)
10+
11+
func SearchFiles(c echo.Context) error {
12+
// parse & validate request
13+
var searchValidation models.LinkSearchValidation
14+
if status, err := helpers.Validate(c, &searchValidation); err != nil {
15+
return c.String(status, err.Error())
16+
}
17+
18+
status, response, err := logic.SearchFiles(c.Get("UserID").(uint), searchValidation.Query)
19+
if err != nil {
20+
return c.String(status, err.Error())
21+
}
22+
23+
return c.JSON(status, response)
24+
}

logic/SearchFiles.go

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package logic
2+
3+
import (
4+
"ch/kirari04/videocms/inits"
5+
"ch/kirari04/videocms/models"
6+
"log"
7+
"net/http"
8+
9+
"github.com/labstack/echo/v4"
10+
)
11+
12+
func SearchFiles(userId uint, query string) (status int, response *[]models.Link, err error) {
13+
// query all files matching the search query
14+
var links []models.Link
15+
res := inits.DB.
16+
Model(&models.Link{}).
17+
Preload("User").
18+
Preload("File").
19+
Preload("Tags").
20+
Where("user_id = ? AND name LIKE ?", userId, "%"+query+"%").
21+
Order("name ASC").
22+
Find(&links)
23+
if res.Error != nil {
24+
log.Printf("Failed to search files: %v", res.Error)
25+
return http.StatusInternalServerError, nil, echo.ErrInternalServerError
26+
}
27+
28+
return http.StatusOK, &links, nil
29+
}

models/Link.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,3 +34,7 @@ type LinksDeleteValidation struct {
3434
type LinkGetValidation struct {
3535
LinkID uint `validate:"required,number" query:"LinkID"`
3636
}
37+
38+
type LinkSearchValidation struct {
39+
Query string `validate:"required,min=1,max=120" query:"Query"`
40+
}

routes/api.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ func Api() {
5050
protectedApi.GET("/file", controllers.GetFile)
5151
protectedApi.PUT("/file", controllers.UpdateFile)
5252
protectedApi.DELETE("/file", controllers.DeleteFileController)
53+
protectedApi.GET("/files/search", controllers.SearchFiles)
5354
protectedApi.GET("/files", controllers.ListFiles)
5455
protectedApi.DELETE("/files", controllers.DeleteFilesController)
5556
protectedApi.POST("/file/tag", controllers.CreateTagController)

0 commit comments

Comments
 (0)