chore(go): use json instead of jsonapi

This commit is contained in:
email
2022-02-04 16:51:48 +08:00
parent a8f0c9a7b1
commit d6418f5ff9
16 changed files with 131 additions and 109 deletions

View File

@ -1,11 +1,11 @@
package server
import (
"encoding/json"
"memos/api"
"memos/common"
"net/http"
"github.com/google/jsonapi"
"github.com/labstack/echo/v4"
)
@ -21,9 +21,35 @@ func (s *Server) registerUserRoutes(g *echo.Group) {
}
c.Response().Header().Set(echo.HeaderContentType, echo.MIMEApplicationJSONCharsetUTF8)
if err := jsonapi.MarshalPayload(c.Response().Writer, user); err != nil {
if err := json.NewEncoder(c.Response().Writer).Encode(user); err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to marshal user response").SetInternal(err)
}
return nil
})
g.POST("/user/rename_check", func(c echo.Context) error {
userRenameCheck := &api.UserRenameCheck{}
if err := json.NewDecoder(c.Request().Body).Decode(userRenameCheck); err != nil {
return echo.NewHTTPError(http.StatusBadRequest, "Malformatted post user rename check request").SetInternal(err)
}
if userRenameCheck.Name == "" {
return echo.NewHTTPError(http.StatusBadRequest, "Malformatted post user rename check request")
}
userFind := &api.UserFind{
Name: &userRenameCheck.Name,
}
user, err := s.UserService.FindUser(userFind)
if err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to find user").SetInternal(err)
}
c.Response().Header().Set(echo.HeaderContentType, echo.MIMEApplicationJSONCharsetUTF8)
if err := json.NewEncoder(c.Response().Writer).Encode(user); err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to marshal user response").SetInternal(err)
}
return nil
})
g.PATCH("user/me", func(c echo.Context) error {
@ -31,7 +57,7 @@ func (s *Server) registerUserRoutes(g *echo.Group) {
userPatch := &api.UserPatch{
Id: userId,
}
if err := jsonapi.UnmarshalPayload(c.Request().Body, userPatch); err != nil {
if err := json.NewDecoder(c.Request().Body).Decode(userPatch); err != nil {
return echo.NewHTTPError(http.StatusBadRequest, "Malformatted patch user request").SetInternal(err)
}
@ -46,7 +72,7 @@ func (s *Server) registerUserRoutes(g *echo.Group) {
}
c.Response().Header().Set(echo.HeaderContentType, echo.MIMEApplicationJSONCharsetUTF8)
if err := jsonapi.MarshalPayload(c.Response().Writer, user); err != nil {
if err := json.NewEncoder(c.Response().Writer).Encode(user); err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to marshal user response").SetInternal(err)
}