chore(go): use `json` instead of `jsonapi`

pull/26/head
email 3 years ago
parent a8f0c9a7b1
commit d6418f5ff9

@ -1,11 +1,11 @@
package api package api
type Login struct { type Login struct {
Name string `jsonapi:"attr,name"` Name string `json:"name"`
Password string `jsonapi:"attr,password"` Password string `json:"password"`
} }
type Signup struct { type Signup struct {
Name string `jsonapi:"attr,name"` Name string `json:"name"`
Password string `jsonapi:"attr,password"` Password string `json:"password"`
} }

@ -1,17 +1,17 @@
package api package api
type Memo struct { type Memo struct {
Id int `jsonapi:"primary,memo"` Id int `json:"id"`
CreatedTs int64 `jsonapi:"attr,createdTs"` CreatedTs int64 `json:"createdTs"`
UpdatedTs int64 `jsonapi:"attr,updatedTs"` UpdatedTs int64 `json:"updatedTs"`
RowStatus string `jsonapi:"attr,rowStatus"` RowStatus string `json:"rowStatus"`
Content string `jsonapi:"attr,content"` Content string `json:"content"`
CreatorId int `jsonapi:"attr,creatorId"` CreatorId int `json:"creatorId"`
} }
type MemoCreate struct { type MemoCreate struct {
Content string `jsonapi:"attr,content"` Content string `json:"content"`
CreatorId int CreatorId int
} }
@ -28,7 +28,7 @@ type MemoFind struct {
} }
type MemoDelete struct { type MemoDelete struct {
Id *int `jsonapi:"primary,memo"` Id *int `json:"id"`
CreatorId *int CreatorId *int
} }

@ -1,31 +1,31 @@
package api package api
type Resource struct { type Resource struct {
Id int `jsonapi:"primary,resource"` Id int `json:"id"`
CreatedTs int64 `jsonapi:"attr,createdTs"` CreatedTs int64 `json:"createdTs"`
UpdatedTs int64 `jsonapi:"attr,updatedTs"` UpdatedTs int64 `json:"updatedTs"`
Filename string `jsonapi:"attr,filename"` Filename string `json:"filename"`
Blob []byte `jsonapi:"attr,blob"` Blob []byte `json:"blob"`
Type string `jsonapi:"attr,type"` Type string `json:"type"`
Size int64 `jsonapi:"attr,size"` Size int64 `json:"size"`
CreatorId int `jsonapi:"attr,creatorId"` CreatorId int `json:"creatorId"`
} }
type ResourceCreate struct { type ResourceCreate struct {
Filename string `jsonapi:"attr,filename"` Filename string `json:"filename"`
Blob []byte `jsonapi:"attr,blob"` Blob []byte `json:"blob"`
Type string `jsonapi:"attr,type"` Type string `json:"type"`
Size int64 `jsonapi:"attr,size"` Size int64 `json:"size"`
CreatorId int `jsonapi:"attr,creatorId"` CreatorId int
} }
type ResourceFind struct { type ResourceFind struct {
Id *int Id *int `json:"id"`
CreatorId *int CreatorId *int `json:"creatorId"`
Filename *string Filename *string `json:"filename"`
} }
type ResourceDelete struct { type ResourceDelete struct {

@ -1,13 +1,13 @@
package api package api
type Shortcut struct { type Shortcut struct {
Id int `jsonapi:"primary,shortcut"` Id int `json:"id"`
CreatedTs int64 `jsonapi:"attr,createdTs"` CreatedTs int64 `json:"createdTs"`
UpdatedTs int64 `jsonapi:"attr,updatedTs"` UpdatedTs int64 `json:"updatedTs"`
Title string `jsonapi:"attr,title"` Title string `json:"title"`
Payload string `jsonapi:"attr,payload"` Payload string `json:"payload"`
PinnedTs int64 `jsonapi:"attr,pinnedTs"` RowStatus string `json:"rowStatus"`
CreatorId int CreatorId int
} }
@ -16,18 +16,16 @@ type ShortcutCreate struct {
CreatorId int CreatorId int
// Domain specific fields // Domain specific fields
Title string `jsonapi:"attr,title"` Title string `json:"title"`
Payload string `jsonapi:"attr,payload"` Payload string `json:"payload"`
} }
type ShortcutPatch struct { type ShortcutPatch struct {
Id int Id int
Title *string `jsonapi:"attr,title"` Title *string `json:"title"`
Payload *string `jsonapi:"attr,payload"` Payload *string `json:"payload"`
PinnedTs *int64 RowStatus *string `json:"rowStatus"`
Pinned *bool `jsonapi:"attr,pinned"`
} }
type ShortcutFind struct { type ShortcutFind struct {
@ -37,7 +35,7 @@ type ShortcutFind struct {
CreatorId *int CreatorId *int
// Domain specific fields // Domain specific fields
Title *string `jsonapi:"attr,title"` Title *string `json:"title"`
} }
type ShortcutDelete struct { type ShortcutDelete struct {

@ -1,19 +1,19 @@
package api package api
type User struct { type User struct {
Id int `jsonapi:"primary,user"` Id int `json:"id"`
CreatedTs int64 `jsonapi:"attr,createdTs"` CreatedTs int64 `json:"createdTs"`
UpdatedTs int64 `jsonapi:"attr,updatedTs"` UpdatedTs int64 `json:"updatedTs"`
OpenId string `jsonapi:"attr,openId"` OpenId string `json:"openId"`
Name string `jsonapi:"attr,name"` Name string `json:"name"`
Password string Password string
} }
type UserCreate struct { type UserCreate struct {
OpenId string `jsonapi:"attr,openId"` OpenId string `json:"openId"`
Name string `jsonapi:"attr,name"` Name string `json:"name"`
Password string `jsonapi:"attr,password"` Password string `json:"password"`
} }
type UserPatch struct { type UserPatch struct {
@ -21,18 +21,26 @@ type UserPatch struct {
OpenId *string OpenId *string
Name *string `jsonapi:"attr,name"` Name *string `json:"name"`
Password *string `jsonapi:"attr,password"` Password *string `json:"password"`
ResetOpenId *bool `jsonapi:"attr,resetOpenId"` ResetOpenId *bool `json:"resetOpenId"`
} }
type UserFind struct { type UserFind struct {
Id *int `jsonapi:"attr,id"` Id *int `json:"id"`
Name *string `jsonapi:"attr,name"` Name *string `json:"name"`
OpenId *string OpenId *string
} }
type UserRenameCheck struct {
Name string `json:"name"`
}
type UserPasswordCheck struct {
Password string `json:"password"`
}
type UserService interface { type UserService interface {
CreateUser(create *UserCreate) (*User, error) CreateUser(create *UserCreate) (*User, error)
PatchUser(patch *UserPatch) (*User, error) PatchUser(patch *UserPatch) (*User, error)

@ -23,7 +23,6 @@ require (
) )
require ( require (
github.com/google/jsonapi v1.0.0
github.com/gorilla/securecookie v1.1.1 // indirect github.com/gorilla/securecookie v1.1.1 // indirect
github.com/gorilla/sessions v1.2.1 github.com/gorilla/sessions v1.2.1
github.com/labstack/echo-contrib v0.12.0 github.com/labstack/echo-contrib v0.12.0

@ -133,8 +133,6 @@ github.com/google/go-cmp v0.5.1/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/
github.com/google/go-cmp v0.5.4/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= github.com/google/go-cmp v0.5.4/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg= github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
github.com/google/jsonapi v1.0.0 h1:qIGgO5Smu3yJmSs+QlvhQnrscdZfFhiV6S8ryJAglqU=
github.com/google/jsonapi v1.0.0/go.mod h1:YYHiRPJT8ARXGER8In9VuLv4qvLfDmA9ULQqptbLE4s=
github.com/google/martian v2.1.0+incompatible/go.mod h1:9I4somxYTbIHy5NJKHRl3wXiIaQGbYVAs8BPL6v8lEs= github.com/google/martian v2.1.0+incompatible/go.mod h1:9I4somxYTbIHy5NJKHRl3wXiIaQGbYVAs8BPL6v8lEs=
github.com/google/martian/v3 v3.0.0/go.mod h1:y5Zk1BBys9G+gd6Jrk0W3cC1+ELVxBWuIGO+w/tUAp0= github.com/google/martian/v3 v3.0.0/go.mod h1:y5Zk1BBys9G+gd6Jrk0W3cC1+ELVxBWuIGO+w/tUAp0=
github.com/google/pprof v0.0.0-20181206194817-3ea8567a2e57/go.mod h1:zfwlbNMJ+OItoe0UupaVj+oy1omPYYDuagoSzA8v9mc= github.com/google/pprof v0.0.0-20181206194817-3ea8567a2e57/go.mod h1:zfwlbNMJ+OItoe0UupaVj+oy1omPYYDuagoSzA8v9mc=

@ -1,19 +1,19 @@
package server package server
import ( import (
"encoding/json"
"fmt" "fmt"
"memos/api" "memos/api"
"memos/common" "memos/common"
"net/http" "net/http"
"github.com/google/jsonapi"
"github.com/labstack/echo/v4" "github.com/labstack/echo/v4"
) )
func (s *Server) registerAuthRoutes(g *echo.Group) { func (s *Server) registerAuthRoutes(g *echo.Group) {
g.POST("/auth/login", func(c echo.Context) error { g.POST("/auth/login", func(c echo.Context) error {
login := &api.Login{} login := &api.Login{}
if err := jsonapi.UnmarshalPayload(c.Request().Body, login); err != nil { if err := json.NewDecoder(c.Request().Body).Decode(login); err != nil {
return echo.NewHTTPError(http.StatusBadRequest, "Malformatted login request").SetInternal(err) return echo.NewHTTPError(http.StatusBadRequest, "Malformatted login request").SetInternal(err)
} }
@ -40,7 +40,7 @@ func (s *Server) registerAuthRoutes(g *echo.Group) {
} }
c.Response().Header().Set(echo.HeaderContentType, echo.MIMEApplicationJSONCharsetUTF8) 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 create user response").SetInternal(err) return echo.NewHTTPError(http.StatusInternalServerError, "Failed to marshal create user response").SetInternal(err)
} }
@ -57,7 +57,7 @@ func (s *Server) registerAuthRoutes(g *echo.Group) {
}) })
g.POST("/auth/signup", func(c echo.Context) error { g.POST("/auth/signup", func(c echo.Context) error {
signup := &api.Signup{} signup := &api.Signup{}
if err := jsonapi.UnmarshalPayload(c.Request().Body, signup); err != nil { if err := json.NewDecoder(c.Request().Body).Decode(signup); err != nil {
return echo.NewHTTPError(http.StatusBadRequest, "Malformatted signup request").SetInternal(err) return echo.NewHTTPError(http.StatusBadRequest, "Malformatted signup request").SetInternal(err)
} }
@ -88,7 +88,7 @@ func (s *Server) registerAuthRoutes(g *echo.Group) {
} }
c.Response().Header().Set(echo.HeaderContentType, echo.MIMEApplicationJSONCharsetUTF8) 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 create user response").SetInternal(err) return echo.NewHTTPError(http.StatusInternalServerError, "Failed to marshal create user response").SetInternal(err)
} }

@ -1,13 +1,13 @@
package server package server
import ( import (
"encoding/json"
"fmt" "fmt"
"memos/api" "memos/api"
"memos/common" "memos/common"
"net/http" "net/http"
"strconv" "strconv"
"github.com/google/jsonapi"
"github.com/labstack/echo/v4" "github.com/labstack/echo/v4"
) )
@ -17,7 +17,7 @@ func (s *Server) registerMemoRoutes(g *echo.Group) {
memoCreate := &api.MemoCreate{ memoCreate := &api.MemoCreate{
CreatorId: userId, CreatorId: userId,
} }
if err := jsonapi.UnmarshalPayload(c.Request().Body, memoCreate); err != nil { if err := json.NewDecoder(c.Request().Body).Decode(memoCreate); err != nil {
return echo.NewHTTPError(http.StatusBadRequest, "Malformatted post memo request").SetInternal(err) return echo.NewHTTPError(http.StatusBadRequest, "Malformatted post memo request").SetInternal(err)
} }
@ -27,7 +27,7 @@ func (s *Server) registerMemoRoutes(g *echo.Group) {
} }
c.Response().Header().Set(echo.HeaderContentType, echo.MIMEApplicationJSONCharsetUTF8) c.Response().Header().Set(echo.HeaderContentType, echo.MIMEApplicationJSONCharsetUTF8)
if err := jsonapi.MarshalPayload(c.Response().Writer, memo); err != nil { if err := json.NewEncoder(c.Response().Writer).Encode(memo); err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to marshal memo response").SetInternal(err) return echo.NewHTTPError(http.StatusInternalServerError, "Failed to marshal memo response").SetInternal(err)
} }
@ -42,7 +42,7 @@ func (s *Server) registerMemoRoutes(g *echo.Group) {
memoPatch := &api.MemoPatch{ memoPatch := &api.MemoPatch{
Id: memoId, Id: memoId,
} }
if err := jsonapi.UnmarshalPayload(c.Request().Body, memoPatch); err != nil { if err := json.NewDecoder(c.Request().Body).Decode(memoPatch); err != nil {
return echo.NewHTTPError(http.StatusBadRequest, "Malformatted patch memo request").SetInternal(err) return echo.NewHTTPError(http.StatusBadRequest, "Malformatted patch memo request").SetInternal(err)
} }
@ -52,7 +52,7 @@ func (s *Server) registerMemoRoutes(g *echo.Group) {
} }
c.Response().Header().Set(echo.HeaderContentType, echo.MIMEApplicationJSONCharsetUTF8) c.Response().Header().Set(echo.HeaderContentType, echo.MIMEApplicationJSONCharsetUTF8)
if err := jsonapi.MarshalPayload(c.Response().Writer, memo); err != nil { if err := json.NewEncoder(c.Response().Writer).Encode(memo); err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to marshal memo response").SetInternal(err) return echo.NewHTTPError(http.StatusInternalServerError, "Failed to marshal memo response").SetInternal(err)
} }
@ -69,7 +69,7 @@ func (s *Server) registerMemoRoutes(g *echo.Group) {
} }
c.Response().Header().Set(echo.HeaderContentType, echo.MIMEApplicationJSONCharsetUTF8) c.Response().Header().Set(echo.HeaderContentType, echo.MIMEApplicationJSONCharsetUTF8)
if err := jsonapi.MarshalPayload(c.Response().Writer, list); err != nil { if err := json.NewEncoder(c.Response().Writer).Encode(list); err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to marshal memo list response").SetInternal(err) return echo.NewHTTPError(http.StatusInternalServerError, "Failed to marshal memo list response").SetInternal(err)
} }
@ -93,7 +93,7 @@ func (s *Server) registerMemoRoutes(g *echo.Group) {
} }
c.Response().Header().Set(echo.HeaderContentType, echo.MIMEApplicationJSONCharsetUTF8) c.Response().Header().Set(echo.HeaderContentType, echo.MIMEApplicationJSONCharsetUTF8)
if err := jsonapi.MarshalPayload(c.Response().Writer, memo); err != nil { if err := json.NewEncoder(c.Response().Writer).Encode(memo); err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to marshal memo response").SetInternal(err) return echo.NewHTTPError(http.StatusInternalServerError, "Failed to marshal memo response").SetInternal(err)
} }

@ -1,13 +1,13 @@
package server package server
import ( import (
"encoding/json"
"fmt" "fmt"
"io/ioutil" "io/ioutil"
"memos/api" "memos/api"
"net/http" "net/http"
"strconv" "strconv"
"github.com/google/jsonapi"
"github.com/labstack/echo/v4" "github.com/labstack/echo/v4"
) )
@ -53,7 +53,7 @@ func (s *Server) registerResourceRoutes(g *echo.Group) {
} }
c.Response().Header().Set(echo.HeaderContentType, echo.MIMEApplicationJSONCharsetUTF8) c.Response().Header().Set(echo.HeaderContentType, echo.MIMEApplicationJSONCharsetUTF8)
if err := jsonapi.MarshalPayload(c.Response().Writer, resource); err != nil { if err := json.NewEncoder(c.Response().Writer).Encode(resource); err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to marshal shortcut response").SetInternal(err) return echo.NewHTTPError(http.StatusInternalServerError, "Failed to marshal shortcut response").SetInternal(err)
} }
@ -70,7 +70,7 @@ func (s *Server) registerResourceRoutes(g *echo.Group) {
} }
c.Response().Header().Set(echo.HeaderContentType, echo.MIMEApplicationJSONCharsetUTF8) c.Response().Header().Set(echo.HeaderContentType, echo.MIMEApplicationJSONCharsetUTF8)
if err := jsonapi.MarshalPayload(c.Response().Writer, list); err != nil { if err := json.NewEncoder(c.Response().Writer).Encode(list); err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to marshal resource list response").SetInternal(err) return echo.NewHTTPError(http.StatusInternalServerError, "Failed to marshal resource list response").SetInternal(err)
} }

@ -29,9 +29,10 @@ func NewServer() *Server {
e.HidePort = false e.HidePort = false
e.Use(middleware.StaticWithConfig(middleware.StaticConfig{ e.Use(middleware.StaticWithConfig(middleware.StaticConfig{
Root: "web/dist", Skipper: middleware.DefaultSkipper,
Browse: false, Root: "web/dist",
HTML5: true, Browse: false,
HTML5: true,
})) }))
e.Use(session.Middleware(sessions.NewCookieStore([]byte(common.GenUUID())))) e.Use(session.Middleware(sessions.NewCookieStore([]byte(common.GenUUID()))))

@ -1,13 +1,12 @@
package server package server
import ( import (
"encoding/json"
"fmt" "fmt"
"memos/api" "memos/api"
"net/http" "net/http"
"strconv" "strconv"
"time"
"github.com/google/jsonapi"
"github.com/labstack/echo/v4" "github.com/labstack/echo/v4"
) )
@ -17,7 +16,7 @@ func (s *Server) registerShortcutRoutes(g *echo.Group) {
shortcutCreate := &api.ShortcutCreate{ shortcutCreate := &api.ShortcutCreate{
CreatorId: userId, CreatorId: userId,
} }
if err := jsonapi.UnmarshalPayload(c.Request().Body, shortcutCreate); err != nil { if err := json.NewDecoder(c.Request().Body).Decode(shortcutCreate); err != nil {
return echo.NewHTTPError(http.StatusBadRequest, "Malformatted post shortcut request").SetInternal(err) return echo.NewHTTPError(http.StatusBadRequest, "Malformatted post shortcut request").SetInternal(err)
} }
@ -27,7 +26,7 @@ func (s *Server) registerShortcutRoutes(g *echo.Group) {
} }
c.Response().Header().Set(echo.HeaderContentType, echo.MIMEApplicationJSONCharsetUTF8) c.Response().Header().Set(echo.HeaderContentType, echo.MIMEApplicationJSONCharsetUTF8)
if err := jsonapi.MarshalPayload(c.Response().Writer, shortcut); err != nil { if err := json.NewEncoder(c.Response().Writer).Encode(shortcut); err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to marshal shortcut response").SetInternal(err) return echo.NewHTTPError(http.StatusInternalServerError, "Failed to marshal shortcut response").SetInternal(err)
} }
@ -42,25 +41,17 @@ func (s *Server) registerShortcutRoutes(g *echo.Group) {
shortcutPatch := &api.ShortcutPatch{ shortcutPatch := &api.ShortcutPatch{
Id: shortcutId, Id: shortcutId,
} }
if err := jsonapi.UnmarshalPayload(c.Request().Body, shortcutPatch); err != nil { if err := json.NewDecoder(c.Request().Body).Decode(shortcutPatch); err != nil {
return echo.NewHTTPError(http.StatusBadRequest, "Malformatted patch shortcut request").SetInternal(err) return echo.NewHTTPError(http.StatusBadRequest, "Malformatted patch shortcut request").SetInternal(err)
} }
if shortcutPatch.Pinned != nil {
pinnedTs := int64(0)
if *shortcutPatch.Pinned {
pinnedTs = time.Now().Unix()
}
shortcutPatch.PinnedTs = &pinnedTs
}
shortcut, err := s.ShortcutService.PatchShortcut(shortcutPatch) shortcut, err := s.ShortcutService.PatchShortcut(shortcutPatch)
if err != nil { if err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to patch shortcut").SetInternal(err) return echo.NewHTTPError(http.StatusInternalServerError, "Failed to patch shortcut").SetInternal(err)
} }
c.Response().Header().Set(echo.HeaderContentType, echo.MIMEApplicationJSONCharsetUTF8) c.Response().Header().Set(echo.HeaderContentType, echo.MIMEApplicationJSONCharsetUTF8)
if err := jsonapi.MarshalPayload(c.Response().Writer, shortcut); err != nil { if err := json.NewEncoder(c.Response().Writer).Encode(shortcut); err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to marshal shortcut response").SetInternal(err) return echo.NewHTTPError(http.StatusInternalServerError, "Failed to marshal shortcut response").SetInternal(err)
} }
@ -71,13 +62,13 @@ func (s *Server) registerShortcutRoutes(g *echo.Group) {
shortcutFind := &api.ShortcutFind{ shortcutFind := &api.ShortcutFind{
CreatorId: &userId, CreatorId: &userId,
} }
list, err := s.ShortcutService.FindShortcut(shortcutFind) list, err := s.ShortcutService.FindShortcutList(shortcutFind)
if err != nil { if err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to fetch shortcut list").SetInternal(err) return echo.NewHTTPError(http.StatusInternalServerError, "Failed to fetch shortcut list").SetInternal(err)
} }
c.Response().Header().Set(echo.HeaderContentType, echo.MIMEApplicationJSONCharsetUTF8) c.Response().Header().Set(echo.HeaderContentType, echo.MIMEApplicationJSONCharsetUTF8)
if err := jsonapi.MarshalPayload(c.Response().Writer, list); err != nil { if err := json.NewEncoder(c.Response().Writer).Encode(list); err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to marshal shortcut list response").SetInternal(err) return echo.NewHTTPError(http.StatusInternalServerError, "Failed to marshal shortcut list response").SetInternal(err)
} }
@ -98,7 +89,7 @@ func (s *Server) registerShortcutRoutes(g *echo.Group) {
} }
c.Response().Header().Set(echo.HeaderContentType, echo.MIMEApplicationJSONCharsetUTF8) c.Response().Header().Set(echo.HeaderContentType, echo.MIMEApplicationJSONCharsetUTF8)
if err := jsonapi.MarshalPayload(c.Response().Writer, shortcut); err != nil { if err := json.NewEncoder(c.Response().Writer).Encode(shortcut); err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to marshal shortcut response").SetInternal(err) return echo.NewHTTPError(http.StatusInternalServerError, "Failed to marshal shortcut response").SetInternal(err)
} }

@ -1,11 +1,11 @@
package server package server
import ( import (
"encoding/json"
"memos/api" "memos/api"
"memos/common" "memos/common"
"net/http" "net/http"
"github.com/google/jsonapi"
"github.com/labstack/echo/v4" "github.com/labstack/echo/v4"
) )
@ -21,9 +21,35 @@ func (s *Server) registerUserRoutes(g *echo.Group) {
} }
c.Response().Header().Set(echo.HeaderContentType, echo.MIMEApplicationJSONCharsetUTF8) 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 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 return nil
}) })
g.PATCH("user/me", func(c echo.Context) error { g.PATCH("user/me", func(c echo.Context) error {
@ -31,7 +57,7 @@ func (s *Server) registerUserRoutes(g *echo.Group) {
userPatch := &api.UserPatch{ userPatch := &api.UserPatch{
Id: userId, 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) 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) 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 echo.NewHTTPError(http.StatusInternalServerError, "Failed to marshal user response").SetInternal(err)
} }

@ -1,12 +1,12 @@
package server package server
import ( import (
"encoding/json"
"fmt" "fmt"
"memos/api" "memos/api"
"net/http" "net/http"
"strconv" "strconv"
"github.com/google/jsonapi"
"github.com/labstack/echo/v4" "github.com/labstack/echo/v4"
) )
@ -31,7 +31,7 @@ func (s *Server) registerWebhookRoutes(g *echo.Group) {
memoCreate := &api.MemoCreate{ memoCreate := &api.MemoCreate{
CreatorId: user.Id, CreatorId: user.Id,
} }
if err := jsonapi.UnmarshalPayload(c.Request().Body, memoCreate); err != nil { if err := json.NewDecoder(c.Request().Body).Decode(memoCreate); err != nil {
return echo.NewHTTPError(http.StatusBadRequest, "Malformatted post memo request by open api").SetInternal(err) return echo.NewHTTPError(http.StatusBadRequest, "Malformatted post memo request by open api").SetInternal(err)
} }
@ -41,7 +41,7 @@ func (s *Server) registerWebhookRoutes(g *echo.Group) {
} }
c.Response().Header().Set(echo.HeaderContentType, echo.MIMEApplicationJSONCharsetUTF8) c.Response().Header().Set(echo.HeaderContentType, echo.MIMEApplicationJSONCharsetUTF8)
if err := jsonapi.MarshalPayload(c.Response().Writer, memo); err != nil { if err := json.NewEncoder(c.Response().Writer).Encode(memo); err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to marshal memo response").SetInternal(err) return echo.NewHTTPError(http.StatusInternalServerError, "Failed to marshal memo response").SetInternal(err)
} }

@ -64,7 +64,8 @@ CREATE TABLE shortcut (
title TEXT NOT NULL DEFAULT '', title TEXT NOT NULL DEFAULT '',
payload TEXT NOT NULL DEFAULT '', payload TEXT NOT NULL DEFAULT '',
creator_id INTEGER NOT NULL, creator_id INTEGER NOT NULL,
pinned_ts BIGINT NOT NULL DEFAULT 0, -- allowed row status are 'NORMAL', 'ARCHIVED'.
row_status TEXT NOT NULL DEFAULT 'NORMAL',
FOREIGN KEY(creator_id) REFERENCES users(id) FOREIGN KEY(creator_id) REFERENCES users(id)
); );

@ -72,7 +72,7 @@ func createShortcut(db *DB, create *api.ShortcutCreate) (*api.Shortcut, error) {
creator_id, creator_id,
) )
VALUES (?, ?, ?) VALUES (?, ?, ?)
RETURNING id, title, payload, creator_id, created_ts, updated_ts, pinned_ts RETURNING id, title, payload, creator_id, created_ts, updated_ts, row_status
`, `,
create.Title, create.Title,
create.Payload, create.Payload,
@ -93,7 +93,7 @@ func createShortcut(db *DB, create *api.ShortcutCreate) (*api.Shortcut, error) {
&shortcut.CreatorId, &shortcut.CreatorId,
&shortcut.CreatedTs, &shortcut.CreatedTs,
&shortcut.UpdatedTs, &shortcut.UpdatedTs,
&shortcut.PinnedTs, &shortcut.RowStatus,
); err != nil { ); err != nil {
return nil, FormatError(err) return nil, FormatError(err)
} }
@ -109,8 +109,8 @@ func patchShortcut(db *DB, patch *api.ShortcutPatch) (*api.Shortcut, error) {
if v := patch.Payload; v != nil { if v := patch.Payload; v != nil {
set, args = append(set, "payload = ?"), append(args, *v) set, args = append(set, "payload = ?"), append(args, *v)
} }
if v := patch.PinnedTs; v != nil { if v := patch.RowStatus; v != nil {
set, args = append(set, "pinned_ts = ?"), append(args, *v) set, args = append(set, "row_status = ?"), append(args, *v)
} }
args = append(args, patch.Id) args = append(args, patch.Id)
@ -119,7 +119,7 @@ func patchShortcut(db *DB, patch *api.ShortcutPatch) (*api.Shortcut, error) {
UPDATE shortcut UPDATE shortcut
SET `+strings.Join(set, ", ")+` SET `+strings.Join(set, ", ")+`
WHERE id = ? WHERE id = ?
RETURNING id, title, payload, created_ts, updated_ts, pinned_ts RETURNING id, title, payload, created_ts, updated_ts, row_status
`, args...) `, args...)
if err != nil { if err != nil {
return nil, FormatError(err) return nil, FormatError(err)
@ -137,7 +137,7 @@ func patchShortcut(db *DB, patch *api.ShortcutPatch) (*api.Shortcut, error) {
&shortcut.Payload, &shortcut.Payload,
&shortcut.CreatedTs, &shortcut.CreatedTs,
&shortcut.UpdatedTs, &shortcut.UpdatedTs,
&shortcut.PinnedTs, &shortcut.RowStatus,
); err != nil { ); err != nil {
return nil, FormatError(err) return nil, FormatError(err)
} }
@ -166,7 +166,7 @@ func findShortcutList(db *DB, find *api.ShortcutFind) ([]*api.Shortcut, error) {
creator_id, creator_id,
created_ts, created_ts,
updated_ts, updated_ts,
pinned_ts row_status
FROM shortcut FROM shortcut
WHERE `+strings.Join(where, " AND "), WHERE `+strings.Join(where, " AND "),
args..., args...,
@ -186,7 +186,7 @@ func findShortcutList(db *DB, find *api.ShortcutFind) ([]*api.Shortcut, error) {
&shortcut.CreatorId, &shortcut.CreatorId,
&shortcut.CreatedTs, &shortcut.CreatedTs,
&shortcut.UpdatedTs, &shortcut.UpdatedTs,
&shortcut.PinnedTs, &shortcut.RowStatus,
); err != nil { ); err != nil {
return nil, FormatError(err) return nil, FormatError(err)
} }

Loading…
Cancel
Save