mirror of
https://github.com/usememos/memos.git
synced 2025-03-01 07:48:19 +08:00
feat: open api for get memos
This commit is contained in:
parent
017bbfa6d6
commit
c77996a891
4 changed files with 48 additions and 6 deletions
|
@ -30,8 +30,7 @@ func (s *Server) registerAuthRoutes(g *echo.Group) {
|
|||
|
||||
// Compare the stored password
|
||||
if login.Password != user.Password {
|
||||
// If the two passwords don't match, return a 401 status.
|
||||
return echo.NewHTTPError(http.StatusUnauthorized, "Incorrect password").SetInternal(err)
|
||||
return echo.NewHTTPError(http.StatusBadRequest, "Incorrect password").SetInternal(err)
|
||||
}
|
||||
|
||||
err = setUserSession(c, user)
|
||||
|
|
|
@ -72,14 +72,14 @@ func BasicAuthMiddleware(us api.UserService, next echo.HandlerFunc) echo.Handler
|
|||
|
||||
userId, err := strconv.Atoi(fmt.Sprintf("%v", userIdValue))
|
||||
if err != nil {
|
||||
return echo.NewHTTPError(http.StatusUnauthorized, "Failed to malformatted user id in the session.")
|
||||
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to malformatted user id in the session.").SetInternal(err)
|
||||
}
|
||||
|
||||
// Even if there is no error, we still need to make sure the user still exists.
|
||||
principalFind := &api.UserFind{
|
||||
userFind := &api.UserFind{
|
||||
Id: &userId,
|
||||
}
|
||||
user, err := us.FindUser(principalFind)
|
||||
user, err := us.FindUser(userFind)
|
||||
if err != nil {
|
||||
return echo.NewHTTPError(http.StatusInternalServerError, fmt.Sprintf("Failed to find user by ID: %d", userId)).SetInternal(err)
|
||||
}
|
||||
|
@ -89,6 +89,7 @@ func BasicAuthMiddleware(us api.UserService, next echo.HandlerFunc) echo.Handler
|
|||
|
||||
// Stores userId into context.
|
||||
c.Set(getUserIdContextKey(), userId)
|
||||
|
||||
return next(c)
|
||||
}
|
||||
}
|
||||
|
|
|
@ -98,8 +98,9 @@ func (s *Server) registerMemoRoutes(g *echo.Group) {
|
|||
memo, err := s.MemoService.FindMemo(memoFind)
|
||||
if err != nil {
|
||||
if common.ErrorCode(err) == common.NotFound {
|
||||
return echo.NewHTTPError(http.StatusNotFound, fmt.Sprintf("Memo ID not found: %d", memoId))
|
||||
return echo.NewHTTPError(http.StatusNotFound, fmt.Sprintf("Memo ID not found: %d", memoId)).SetInternal(err)
|
||||
}
|
||||
|
||||
return echo.NewHTTPError(http.StatusInternalServerError, fmt.Sprintf("Failed to delete memo ID: %v", memoId)).SetInternal(err)
|
||||
}
|
||||
|
||||
|
|
|
@ -47,6 +47,47 @@ func (s *Server) registerWebhookRoutes(g *echo.Group) {
|
|||
|
||||
return nil
|
||||
})
|
||||
g.GET("/:openId/memo", func(c echo.Context) error {
|
||||
openId := c.Param("openId")
|
||||
|
||||
userFind := &api.UserFind{
|
||||
OpenId: &openId,
|
||||
}
|
||||
user, err := s.UserService.FindUser(userFind)
|
||||
if err != nil {
|
||||
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to find user by open_id").SetInternal(err)
|
||||
}
|
||||
if user == nil {
|
||||
return echo.NewHTTPError(http.StatusUnauthorized, fmt.Sprintf("Unauthorized: %s", openId))
|
||||
}
|
||||
|
||||
memoFind := &api.MemoFind{
|
||||
CreatorId: &user.Id,
|
||||
}
|
||||
showHiddenMemo, err := strconv.ParseBool(c.QueryParam("hidden"))
|
||||
if err != nil {
|
||||
showHiddenMemo = false
|
||||
}
|
||||
|
||||
rowStatus := "NORMAL"
|
||||
if showHiddenMemo {
|
||||
rowStatus = "HIDDEN"
|
||||
}
|
||||
memoFind.RowStatus = &rowStatus
|
||||
|
||||
list, err := s.MemoService.FindMemoList(memoFind)
|
||||
if err != nil {
|
||||
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to fetch memo list").SetInternal(err)
|
||||
}
|
||||
|
||||
c.Response().Header().Set(echo.HeaderContentType, echo.MIMEApplicationJSONCharsetUTF8)
|
||||
if err := json.NewEncoder(c.Response().Writer).Encode(composeResponse(list)); err != nil {
|
||||
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to encode memo list response").SetInternal(err)
|
||||
}
|
||||
|
||||
return nil
|
||||
})
|
||||
|
||||
g.GET("/r/:resourceId/:filename", func(c echo.Context) error {
|
||||
resourceId, err := strconv.Atoi(c.Param("resourceId"))
|
||||
if err != nil {
|
||||
|
|
Loading…
Reference in a new issue