mirror of
https://github.com/usememos/memos.git
synced 2024-12-20 09:41:58 +03:00
feat: add /api/tag
(#82)
This commit is contained in:
parent
cc54be0d1d
commit
9f81362027
@ -43,7 +43,11 @@ type MemoFind struct {
|
|||||||
|
|
||||||
// Domain specific fields
|
// Domain specific fields
|
||||||
Pinned *bool
|
Pinned *bool
|
||||||
Tag *string
|
ContentSearch *string
|
||||||
|
|
||||||
|
// Pagination
|
||||||
|
Limit int
|
||||||
|
Offset int
|
||||||
}
|
}
|
||||||
|
|
||||||
type MemoDelete struct {
|
type MemoDelete struct {
|
||||||
|
@ -77,7 +77,14 @@ func (s *Server) registerMemoRoutes(g *echo.Group) {
|
|||||||
}
|
}
|
||||||
tag := c.QueryParam("tag")
|
tag := c.QueryParam("tag")
|
||||||
if tag != "" {
|
if tag != "" {
|
||||||
memoFind.Tag = &tag
|
contentSearch := "#" + tag + " "
|
||||||
|
memoFind.ContentSearch = &contentSearch
|
||||||
|
}
|
||||||
|
if limit, err := strconv.Atoi(c.QueryParam("limit")); err == nil {
|
||||||
|
memoFind.Limit = limit
|
||||||
|
}
|
||||||
|
if offset, err := strconv.Atoi(c.QueryParam("offset")); err == nil {
|
||||||
|
memoFind.Offset = offset
|
||||||
}
|
}
|
||||||
|
|
||||||
list, err := s.Store.FindMemoList(memoFind)
|
list, err := s.Store.FindMemoList(memoFind)
|
||||||
@ -177,4 +184,25 @@ func (s *Server) registerMemoRoutes(g *echo.Group) {
|
|||||||
|
|
||||||
return nil
|
return nil
|
||||||
})
|
})
|
||||||
|
|
||||||
|
g.GET("/memo/amount", func(c echo.Context) error {
|
||||||
|
userID := c.Get(getUserIDContextKey()).(int)
|
||||||
|
normalRowStatus := api.Normal
|
||||||
|
memoFind := &api.MemoFind{
|
||||||
|
CreatorID: &userID,
|
||||||
|
RowStatus: &normalRowStatus,
|
||||||
|
}
|
||||||
|
|
||||||
|
memoList, err := s.Store.FindMemoList(memoFind)
|
||||||
|
if err != nil {
|
||||||
|
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to find memo list").SetInternal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
c.Response().Header().Set(echo.HeaderContentType, echo.MIMEApplicationJSONCharsetUTF8)
|
||||||
|
if err := json.NewEncoder(c.Response().Writer).Encode(len(memoList)); err != nil {
|
||||||
|
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to encode memo amount").SetInternal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
@ -70,6 +70,7 @@ func NewServer(profile *profile.Profile) *Server {
|
|||||||
s.registerMemoRoutes(apiGroup)
|
s.registerMemoRoutes(apiGroup)
|
||||||
s.registerShortcutRoutes(apiGroup)
|
s.registerShortcutRoutes(apiGroup)
|
||||||
s.registerResourceRoutes(apiGroup)
|
s.registerResourceRoutes(apiGroup)
|
||||||
|
s.registerTagRoutes(apiGroup)
|
||||||
|
|
||||||
return s
|
return s
|
||||||
}
|
}
|
||||||
|
51
server/tag.go
Normal file
51
server/tag.go
Normal file
@ -0,0 +1,51 @@
|
|||||||
|
package server
|
||||||
|
|
||||||
|
import (
|
||||||
|
"encoding/json"
|
||||||
|
"memos/api"
|
||||||
|
"net/http"
|
||||||
|
"regexp"
|
||||||
|
|
||||||
|
"github.com/labstack/echo/v4"
|
||||||
|
)
|
||||||
|
|
||||||
|
func (s *Server) registerTagRoutes(g *echo.Group) {
|
||||||
|
g.GET("/tag", func(c echo.Context) error {
|
||||||
|
userID := c.Get(getUserIDContextKey()).(int)
|
||||||
|
contentSearch := "#"
|
||||||
|
memoFind := api.MemoFind{
|
||||||
|
CreatorID: &userID,
|
||||||
|
ContentSearch: &contentSearch,
|
||||||
|
}
|
||||||
|
|
||||||
|
memoList, err := s.Store.FindMemoList(&memoFind)
|
||||||
|
if err != nil {
|
||||||
|
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to find memo list").SetInternal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
tagMapSet := make(map[string]bool)
|
||||||
|
|
||||||
|
r, err := regexp.Compile("#(.+?) ")
|
||||||
|
if err != nil {
|
||||||
|
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to compile regexp").SetInternal(err)
|
||||||
|
}
|
||||||
|
for _, memo := range memoList {
|
||||||
|
for _, rawTag := range r.FindAllString(memo.Content, -1) {
|
||||||
|
tag := r.ReplaceAllString(rawTag, "$1")
|
||||||
|
tagMapSet[tag] = true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
tagList := []string{}
|
||||||
|
for tag := range tagMapSet {
|
||||||
|
tagList = append(tagList, tag)
|
||||||
|
}
|
||||||
|
|
||||||
|
c.Response().Header().Set(echo.HeaderContentType, echo.MIMEApplicationJSONCharsetUTF8)
|
||||||
|
if err := json.NewEncoder(c.Response().Writer).Encode(tagList); err != nil {
|
||||||
|
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to encode tags response").SetInternal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
})
|
||||||
|
}
|
@ -114,7 +114,14 @@ func (s *Server) registerWebhookRoutes(g *echo.Group) {
|
|||||||
}
|
}
|
||||||
tag := c.QueryParam("tag")
|
tag := c.QueryParam("tag")
|
||||||
if tag != "" {
|
if tag != "" {
|
||||||
memoFind.Tag = &tag
|
contentSearch := tag + " "
|
||||||
|
memoFind.ContentSearch = &contentSearch
|
||||||
|
}
|
||||||
|
if limit, err := strconv.Atoi(c.QueryParam("limit")); err == nil {
|
||||||
|
memoFind.Limit = limit
|
||||||
|
}
|
||||||
|
if offset, err := strconv.Atoi(c.QueryParam("offset")); err == nil {
|
||||||
|
memoFind.Offset = offset
|
||||||
}
|
}
|
||||||
|
|
||||||
list, err := s.Store.FindMemoList(memoFind)
|
list, err := s.Store.FindMemoList(memoFind)
|
||||||
|
@ -209,10 +209,20 @@ func findMemoRawList(db *sql.DB, find *api.MemoFind) ([]*memoRaw, error) {
|
|||||||
if v := find.Pinned; v != nil {
|
if v := find.Pinned; v != nil {
|
||||||
where = append(where, "id in (SELECT memo_id FROM memo_organizer WHERE pinned = 1 AND user_id = memo.creator_id )")
|
where = append(where, "id in (SELECT memo_id FROM memo_organizer WHERE pinned = 1 AND user_id = memo.creator_id )")
|
||||||
}
|
}
|
||||||
if v := find.Tag; v != nil {
|
if v := find.ContentSearch; v != nil {
|
||||||
where, args = append(where, "content LIKE ?"), append(args, "%#"+*v+" %")
|
where, args = append(where, "content LIKE ?"), append(args, "%"+*v+"%")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pagination := ""
|
||||||
|
if find.Limit > 0 {
|
||||||
|
pagination = fmt.Sprintf("%s LIMIT %d", pagination, find.Limit)
|
||||||
|
if find.Offset > 0 {
|
||||||
|
pagination = fmt.Sprintf("%s OFFSET %d", pagination, find.Offset)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
println(pagination)
|
||||||
|
|
||||||
rows, err := db.Query(`
|
rows, err := db.Query(`
|
||||||
SELECT
|
SELECT
|
||||||
id,
|
id,
|
||||||
@ -223,7 +233,7 @@ func findMemoRawList(db *sql.DB, find *api.MemoFind) ([]*memoRaw, error) {
|
|||||||
row_status
|
row_status
|
||||||
FROM memo
|
FROM memo
|
||||||
WHERE `+strings.Join(where, " AND ")+`
|
WHERE `+strings.Join(where, " AND ")+`
|
||||||
ORDER BY created_ts DESC`,
|
ORDER BY created_ts DESC`+pagination,
|
||||||
args...,
|
args...,
|
||||||
)
|
)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
Loading…
Reference in New Issue
Block a user