feat: add avatar_url field to user table (#1106)

refactor: add `avatar_url` field to user table
This commit is contained in:
boojack 2023-02-17 23:55:56 +08:00 committed by GitHub
parent a538b9789b
commit 096a71c58b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
13 changed files with 79 additions and 262 deletions

View File

@ -45,6 +45,7 @@ type User struct {
Nickname string `json:"nickname"` Nickname string `json:"nickname"`
PasswordHash string `json:"-"` PasswordHash string `json:"-"`
OpenID string `json:"openId"` OpenID string `json:"openId"`
AvatarURL string `json:"avatarUrl"`
UserSettingList []*UserSetting `json:"userSettingList"` UserSettingList []*UserSetting `json:"userSettingList"`
} }
@ -55,6 +56,7 @@ type UserCreate struct {
Email string `json:"email"` Email string `json:"email"`
Nickname string `json:"nickname"` Nickname string `json:"nickname"`
Password string `json:"password"` Password string `json:"password"`
AvatarURL string `json:"avatarUrl"`
PasswordHash string PasswordHash string
OpenID string OpenID string
} }
@ -94,6 +96,7 @@ type UserPatch struct {
Nickname *string `json:"nickname"` Nickname *string `json:"nickname"`
Password *string `json:"password"` Password *string `json:"password"`
ResetOpenID *bool `json:"resetOpenId"` ResetOpenID *bool `json:"resetOpenId"`
AvatarURL *string `json:"avatarUrl"`
PasswordHash *string PasswordHash *string
OpenID *string OpenID *string
} }

View File

@ -47,12 +47,7 @@ func (s *Server) registerAuthRoutes(g *echo.Group) {
if err := s.createUserAuthSignInActivity(c, user); err != nil { if err := s.createUserAuthSignInActivity(c, user); err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to create activity").SetInternal(err) return echo.NewHTTPError(http.StatusInternalServerError, "Failed to create activity").SetInternal(err)
} }
return c.JSON(http.StatusOK, composeResponse(user))
c.Response().Header().Set(echo.HeaderContentType, echo.MIMEApplicationJSONCharsetUTF8)
if err := json.NewEncoder(c.Response().Writer).Encode(composeResponse(user)); err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to encode user response").SetInternal(err)
}
return nil
}) })
g.POST("/auth/signup", func(c echo.Context) error { g.POST("/auth/signup", func(c echo.Context) error {
@ -122,12 +117,7 @@ func (s *Server) registerAuthRoutes(g *echo.Group) {
if err != nil { if err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to set signup session").SetInternal(err) return echo.NewHTTPError(http.StatusInternalServerError, "Failed to set signup session").SetInternal(err)
} }
return c.JSON(http.StatusOK, composeResponse(user))
c.Response().Header().Set(echo.HeaderContentType, echo.MIMEApplicationJSONCharsetUTF8)
if err := json.NewEncoder(c.Response().Writer).Encode(composeResponse(user)); err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to encode created user response").SetInternal(err)
}
return nil
}) })
g.POST("/auth/signout", func(c echo.Context) error { g.POST("/auth/signout", func(c echo.Context) error {
@ -146,7 +136,7 @@ func (s *Server) createUserAuthSignInActivity(c echo.Context, user *api.User) er
UserID: user.ID, UserID: user.ID,
IP: echo.ExtractIPFromRealIPHeader()(c.Request()), IP: echo.ExtractIPFromRealIPHeader()(c.Request()),
} }
payloadStr, err := json.Marshal(payload) payloadBytes, err := json.Marshal(payload)
if err != nil { if err != nil {
return errors.Wrap(err, "failed to marshal activity payload") return errors.Wrap(err, "failed to marshal activity payload")
} }
@ -154,7 +144,7 @@ func (s *Server) createUserAuthSignInActivity(c echo.Context, user *api.User) er
CreatorID: user.ID, CreatorID: user.ID,
Type: api.ActivityUserAuthSignIn, Type: api.ActivityUserAuthSignIn,
Level: api.ActivityInfo, Level: api.ActivityInfo,
Payload: string(payloadStr), Payload: string(payloadBytes),
}) })
if err != nil || activity == nil { if err != nil || activity == nil {
return errors.Wrap(err, "failed to create activity") return errors.Wrap(err, "failed to create activity")
@ -171,7 +161,7 @@ func (s *Server) createUserAuthSignUpActivity(c echo.Context, user *api.User) er
Username: user.Username, Username: user.Username,
IP: echo.ExtractIPFromRealIPHeader()(c.Request()), IP: echo.ExtractIPFromRealIPHeader()(c.Request()),
} }
payloadStr, err := json.Marshal(payload) payloadBytes, err := json.Marshal(payload)
if err != nil { if err != nil {
return errors.Wrap(err, "failed to marshal activity payload") return errors.Wrap(err, "failed to marshal activity payload")
} }
@ -179,7 +169,7 @@ func (s *Server) createUserAuthSignUpActivity(c echo.Context, user *api.User) er
CreatorID: user.ID, CreatorID: user.ID,
Type: api.ActivityUserAuthSignUp, Type: api.ActivityUserAuthSignUp,
Level: api.ActivityInfo, Level: api.ActivityInfo,
Payload: string(payloadStr), Payload: string(payloadBytes),
}) })
if err != nil || activity == nil { if err != nil || activity == nil {
return errors.Wrap(err, "failed to create activity") return errors.Wrap(err, "failed to create activity")

View File

@ -1,7 +1,6 @@
package server package server
import ( import (
"encoding/json"
"fmt" "fmt"
"net/http" "net/http"
"net/url" "net/url"
@ -24,12 +23,7 @@ func registerGetterPublicRoutes(g *echo.Group) {
if err != nil { if err != nil {
return echo.NewHTTPError(http.StatusNotAcceptable, fmt.Sprintf("Failed to get website meta with url: %s", urlStr)).SetInternal(err) return echo.NewHTTPError(http.StatusNotAcceptable, fmt.Sprintf("Failed to get website meta with url: %s", urlStr)).SetInternal(err)
} }
return c.JSON(http.StatusOK, composeResponse(htmlMeta))
c.Response().Header().Set(echo.HeaderContentType, echo.MIMEApplicationJSONCharsetUTF8)
if err := json.NewEncoder(c.Response().Writer).Encode(composeResponse(htmlMeta)); err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to encode website HTML meta").SetInternal(err)
}
return nil
}) })
g.GET("/get/image", func(c echo.Context) error { g.GET("/get/image", func(c echo.Context) error {

View File

@ -93,12 +93,7 @@ func (s *Server) registerMemoRoutes(g *echo.Group) {
if err != nil { if err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to compose memo").SetInternal(err) return echo.NewHTTPError(http.StatusInternalServerError, "Failed to compose memo").SetInternal(err)
} }
return c.JSON(http.StatusOK, composeResponse(memo))
c.Response().Header().Set(echo.HeaderContentType, echo.MIMEApplicationJSONCharsetUTF8)
if err := json.NewEncoder(c.Response().Writer).Encode(composeResponse(memo)); err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to encode memo response").SetInternal(err)
}
return nil
}) })
g.PATCH("/memo/:memoId", func(c echo.Context) error { g.PATCH("/memo/:memoId", func(c echo.Context) error {
@ -150,12 +145,7 @@ func (s *Server) registerMemoRoutes(g *echo.Group) {
if err != nil { if err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to compose memo").SetInternal(err) return echo.NewHTTPError(http.StatusInternalServerError, "Failed to compose memo").SetInternal(err)
} }
return c.JSON(http.StatusOK, composeResponse(memo))
c.Response().Header().Set(echo.HeaderContentType, echo.MIMEApplicationJSONCharsetUTF8)
if err := json.NewEncoder(c.Response().Writer).Encode(composeResponse(memo)); err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to encode memo response").SetInternal(err)
}
return nil
}) })
g.GET("/memo", func(c echo.Context) error { g.GET("/memo", func(c echo.Context) error {
@ -238,12 +228,7 @@ func (s *Server) registerMemoRoutes(g *echo.Group) {
if memoFind.Limit != 0 { if memoFind.Limit != 0 {
memoList = memoList[memoFind.Offset:common.Min(len(memoList), memoFind.Offset+memoFind.Limit)] memoList = memoList[memoFind.Offset:common.Min(len(memoList), memoFind.Offset+memoFind.Limit)]
} }
return c.JSON(http.StatusOK, composeResponse(memoList))
c.Response().Header().Set(echo.HeaderContentType, echo.MIMEApplicationJSONCharsetUTF8)
if err := json.NewEncoder(c.Response().Writer).Encode(composeResponse(memoList)); err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to encode memo list response").SetInternal(err)
}
return nil
}) })
g.GET("/memo/:memoId", func(c echo.Context) error { g.GET("/memo/:memoId", func(c echo.Context) error {
@ -275,12 +260,7 @@ func (s *Server) registerMemoRoutes(g *echo.Group) {
return echo.NewHTTPError(http.StatusForbidden, "this memo is protected, missing user in session") return echo.NewHTTPError(http.StatusForbidden, "this memo is protected, missing user in session")
} }
} }
return c.JSON(http.StatusOK, composeResponse(memo))
c.Response().Header().Set(echo.HeaderContentType, echo.MIMEApplicationJSONCharsetUTF8)
if err := json.NewEncoder(c.Response().Writer).Encode(composeResponse(memo)); err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to encode memo response").SetInternal(err)
}
return nil
}) })
g.POST("/memo/:memoId/organizer", func(c echo.Context) error { g.POST("/memo/:memoId/organizer", func(c echo.Context) error {
@ -316,12 +296,7 @@ func (s *Server) registerMemoRoutes(g *echo.Group) {
return echo.NewHTTPError(http.StatusInternalServerError, fmt.Sprintf("Failed to find memo by ID: %v", memoID)).SetInternal(err) return echo.NewHTTPError(http.StatusInternalServerError, fmt.Sprintf("Failed to find memo by ID: %v", memoID)).SetInternal(err)
} }
return c.JSON(http.StatusOK, composeResponse(memo))
c.Response().Header().Set(echo.HeaderContentType, echo.MIMEApplicationJSONCharsetUTF8)
if err := json.NewEncoder(c.Response().Writer).Encode(composeResponse(memo)); err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to encode memo response").SetInternal(err)
}
return nil
}) })
g.POST("/memo/:memoId/resource", func(c echo.Context) error { g.POST("/memo/:memoId/resource", func(c echo.Context) error {
@ -358,12 +333,7 @@ func (s *Server) registerMemoRoutes(g *echo.Group) {
if _, err := s.Store.UpsertMemoResource(ctx, memoResourceUpsert); err != nil { if _, err := s.Store.UpsertMemoResource(ctx, memoResourceUpsert); err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to upsert memo resource").SetInternal(err) return echo.NewHTTPError(http.StatusInternalServerError, "Failed to upsert memo resource").SetInternal(err)
} }
return c.JSON(http.StatusOK, composeResponse(resource))
c.Response().Header().Set(echo.HeaderContentType, echo.MIMEApplicationJSONCharsetUTF8)
if err := json.NewEncoder(c.Response().Writer).Encode(composeResponse(resource)); err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to encode resource response").SetInternal(err)
}
return nil
}) })
g.GET("/memo/:memoId/resource", func(c echo.Context) error { g.GET("/memo/:memoId/resource", func(c echo.Context) error {
@ -380,12 +350,7 @@ func (s *Server) registerMemoRoutes(g *echo.Group) {
if err != nil { if err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to fetch resource list").SetInternal(err) return echo.NewHTTPError(http.StatusInternalServerError, "Failed to fetch resource list").SetInternal(err)
} }
return c.JSON(http.StatusOK, composeResponse(resourceList))
c.Response().Header().Set(echo.HeaderContentType, echo.MIMEApplicationJSONCharsetUTF8)
if err := json.NewEncoder(c.Response().Writer).Encode(composeResponse(resourceList)); err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to encode resource list response").SetInternal(err)
}
return nil
}) })
g.GET("/memo/amount", func(c echo.Context) error { g.GET("/memo/amount", func(c echo.Context) error {
@ -402,12 +367,7 @@ func (s *Server) registerMemoRoutes(g *echo.Group) {
if err != nil { if err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to find memo list").SetInternal(err) return echo.NewHTTPError(http.StatusInternalServerError, "Failed to find memo list").SetInternal(err)
} }
return c.JSON(http.StatusOK, composeResponse(memoList))
c.Response().Header().Set(echo.HeaderContentType, echo.MIMEApplicationJSONCharsetUTF8)
if err := json.NewEncoder(c.Response().Writer).Encode(composeResponse(len(memoList))); err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to encode memo amount").SetInternal(err)
}
return nil
}) })
g.GET("/memo/stats", func(c echo.Context) error { g.GET("/memo/stats", func(c echo.Context) error {
@ -443,12 +403,7 @@ func (s *Server) registerMemoRoutes(g *echo.Group) {
for _, memo := range list { for _, memo := range list {
displayTsList = append(displayTsList, memo.DisplayTs) displayTsList = append(displayTsList, memo.DisplayTs)
} }
return c.JSON(http.StatusOK, composeResponse(displayTsList))
c.Response().Header().Set(echo.HeaderContentType, echo.MIMEApplicationJSONCharsetUTF8)
if err := json.NewEncoder(c.Response().Writer).Encode(composeResponse(displayTsList)); err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to encode memo stats response").SetInternal(err)
}
return nil
}) })
g.GET("/memo/all", func(c echo.Context) error { g.GET("/memo/all", func(c echo.Context) error {
@ -503,12 +458,7 @@ func (s *Server) registerMemoRoutes(g *echo.Group) {
if memoFind.Limit != 0 { if memoFind.Limit != 0 {
list = list[memoFind.Offset:common.Min(len(list), memoFind.Offset+memoFind.Limit)] list = list[memoFind.Offset:common.Min(len(list), memoFind.Offset+memoFind.Limit)]
} }
return c.JSON(http.StatusOK, composeResponse(list))
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 all memo list response").SetInternal(err)
}
return nil
}) })
g.DELETE("/memo/:memoId", func(c echo.Context) error { g.DELETE("/memo/:memoId", func(c echo.Context) error {
@ -541,7 +491,6 @@ func (s *Server) registerMemoRoutes(g *echo.Group) {
} }
return echo.NewHTTPError(http.StatusInternalServerError, fmt.Sprintf("Failed to delete memo ID: %v", memoID)).SetInternal(err) return echo.NewHTTPError(http.StatusInternalServerError, fmt.Sprintf("Failed to delete memo ID: %v", memoID)).SetInternal(err)
} }
return c.JSON(http.StatusOK, true) return c.JSON(http.StatusOK, true)
}) })
@ -577,7 +526,6 @@ func (s *Server) registerMemoRoutes(g *echo.Group) {
if err := s.Store.DeleteMemoResource(ctx, memoResourceDelete); err != nil { if err := s.Store.DeleteMemoResource(ctx, memoResourceDelete); err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to fetch resource list").SetInternal(err) return echo.NewHTTPError(http.StatusInternalServerError, "Failed to fetch resource list").SetInternal(err)
} }
return c.JSON(http.StatusOK, true) return c.JSON(http.StatusOK, true)
}) })
} }
@ -588,7 +536,7 @@ func (s *Server) createMemoCreateActivity(c echo.Context, memo *api.Memo) error
Content: memo.Content, Content: memo.Content,
Visibility: memo.Visibility.String(), Visibility: memo.Visibility.String(),
} }
payloadStr, err := json.Marshal(payload) payloadBytes, err := json.Marshal(payload)
if err != nil { if err != nil {
return errors.Wrap(err, "failed to marshal activity payload") return errors.Wrap(err, "failed to marshal activity payload")
} }
@ -596,7 +544,7 @@ func (s *Server) createMemoCreateActivity(c echo.Context, memo *api.Memo) error
CreatorID: memo.CreatorID, CreatorID: memo.CreatorID,
Type: api.ActivityMemoCreate, Type: api.ActivityMemoCreate,
Level: api.ActivityInfo, Level: api.ActivityInfo,
Payload: string(payloadStr), Payload: string(payloadBytes),
}) })
if err != nil || activity == nil { if err != nil || activity == nil {
return errors.Wrap(err, "failed to create activity") return errors.Wrap(err, "failed to create activity")

View File

@ -49,12 +49,7 @@ func (s *Server) registerResourceRoutes(g *echo.Group) {
if err := s.createResourceCreateActivity(c, resource); err != nil { if err := s.createResourceCreateActivity(c, resource); err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to create activity").SetInternal(err) return echo.NewHTTPError(http.StatusInternalServerError, "Failed to create activity").SetInternal(err)
} }
return c.JSON(http.StatusOK, composeResponse(resource))
c.Response().Header().Set(echo.HeaderContentType, echo.MIMEApplicationJSONCharsetUTF8)
if err := json.NewEncoder(c.Response().Writer).Encode(composeResponse(resource)); err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to encode resource response").SetInternal(err)
}
return nil
}) })
g.POST("/resource/blob", func(c echo.Context) error { g.POST("/resource/blob", func(c echo.Context) error {
@ -140,12 +135,7 @@ func (s *Server) registerResourceRoutes(g *echo.Group) {
if err := s.createResourceCreateActivity(c, resource); err != nil { if err := s.createResourceCreateActivity(c, resource); err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to create activity").SetInternal(err) return echo.NewHTTPError(http.StatusInternalServerError, "Failed to create activity").SetInternal(err)
} }
return c.JSON(http.StatusOK, composeResponse(resource))
c.Response().Header().Set(echo.HeaderContentType, echo.MIMEApplicationJSONCharsetUTF8)
if err := json.NewEncoder(c.Response().Writer).Encode(composeResponse(resource)); err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to encode resource response").SetInternal(err)
}
return nil
}) })
g.GET("/resource", func(c echo.Context) error { g.GET("/resource", func(c echo.Context) error {
@ -171,12 +161,7 @@ func (s *Server) registerResourceRoutes(g *echo.Group) {
} }
resource.LinkedMemoAmount = len(memoResourceList) resource.LinkedMemoAmount = len(memoResourceList)
} }
return c.JSON(http.StatusOK, composeResponse(list))
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 resource list response").SetInternal(err)
}
return nil
}) })
g.GET("/resource/:resourceId", func(c echo.Context) error { g.GET("/resource/:resourceId", func(c echo.Context) error {
@ -199,12 +184,7 @@ func (s *Server) registerResourceRoutes(g *echo.Group) {
if err != nil { if err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to fetch resource").SetInternal(err) return echo.NewHTTPError(http.StatusInternalServerError, "Failed to fetch resource").SetInternal(err)
} }
return c.JSON(http.StatusOK, composeResponse(resource))
c.Response().Header().Set(echo.HeaderContentType, echo.MIMEApplicationJSONCharsetUTF8)
if err := json.NewEncoder(c.Response().Writer).Encode(composeResponse(resource)); err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to encode resource response").SetInternal(err)
}
return nil
}) })
g.GET("/resource/:resourceId/blob", func(c echo.Context) error { g.GET("/resource/:resourceId/blob", func(c echo.Context) error {
@ -227,7 +207,6 @@ func (s *Server) registerResourceRoutes(g *echo.Group) {
if err != nil { if err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to fetch resource").SetInternal(err) return echo.NewHTTPError(http.StatusInternalServerError, "Failed to fetch resource").SetInternal(err)
} }
return c.Stream(http.StatusOK, resource.Type, bytes.NewReader(resource.Blob)) return c.Stream(http.StatusOK, resource.Type, bytes.NewReader(resource.Blob))
}) })
@ -267,12 +246,7 @@ func (s *Server) registerResourceRoutes(g *echo.Group) {
if err != nil { if err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to patch resource").SetInternal(err) return echo.NewHTTPError(http.StatusInternalServerError, "Failed to patch resource").SetInternal(err)
} }
return c.JSON(http.StatusOK, composeResponse(resource))
c.Response().Header().Set(echo.HeaderContentType, echo.MIMEApplicationJSONCharsetUTF8)
if err := json.NewEncoder(c.Response().Writer).Encode(composeResponse(resource)); err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to encode resource response").SetInternal(err)
}
return nil
}) })
g.DELETE("/resource/:resourceId", func(c echo.Context) error { g.DELETE("/resource/:resourceId", func(c echo.Context) error {
@ -307,7 +281,6 @@ func (s *Server) registerResourceRoutes(g *echo.Group) {
} }
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to delete resource").SetInternal(err) return echo.NewHTTPError(http.StatusInternalServerError, "Failed to delete resource").SetInternal(err)
} }
return c.JSON(http.StatusOK, true) return c.JSON(http.StatusOK, true)
}) })
} }
@ -353,7 +326,7 @@ func (s *Server) createResourceCreateActivity(c echo.Context, resource *api.Reso
Type: resource.Type, Type: resource.Type,
Size: resource.Size, Size: resource.Size,
} }
payloadStr, err := json.Marshal(payload) payloadBytes, err := json.Marshal(payload)
if err != nil { if err != nil {
return errors.Wrap(err, "failed to marshal activity payload") return errors.Wrap(err, "failed to marshal activity payload")
} }
@ -361,7 +334,7 @@ func (s *Server) createResourceCreateActivity(c echo.Context, resource *api.Reso
CreatorID: resource.CreatorID, CreatorID: resource.CreatorID,
Type: api.ActivityResourceCreate, Type: api.ActivityResourceCreate,
Level: api.ActivityInfo, Level: api.ActivityInfo,
Payload: string(payloadStr), Payload: string(payloadBytes),
}) })
if err != nil || activity == nil { if err != nil || activity == nil {
return errors.Wrap(err, "failed to create activity") return errors.Wrap(err, "failed to create activity")

View File

@ -150,7 +150,7 @@ func (s *Server) createServerStartActivity(ctx context.Context) error {
ServerID: s.ID, ServerID: s.ID,
Profile: s.Profile, Profile: s.Profile,
} }
payloadStr, err := json.Marshal(payload) payloadBytes, err := json.Marshal(payload)
if err != nil { if err != nil {
return errors.Wrap(err, "failed to marshal activity payload") return errors.Wrap(err, "failed to marshal activity payload")
} }
@ -158,7 +158,7 @@ func (s *Server) createServerStartActivity(ctx context.Context) error {
CreatorID: api.UnknownID, CreatorID: api.UnknownID,
Type: api.ActivityServerStart, Type: api.ActivityServerStart,
Level: api.ActivityInfo, Level: api.ActivityInfo,
Payload: string(payloadStr), Payload: string(payloadBytes),
}) })
if err != nil || activity == nil { if err != nil || activity == nil {
return errors.Wrap(err, "failed to create activity") return errors.Wrap(err, "failed to create activity")

View File

@ -34,12 +34,7 @@ func (s *Server) registerShortcutRoutes(g *echo.Group) {
if err := s.createShortcutCreateActivity(c, shortcut); err != nil { if err := s.createShortcutCreateActivity(c, shortcut); err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to create activity").SetInternal(err) return echo.NewHTTPError(http.StatusInternalServerError, "Failed to create activity").SetInternal(err)
} }
return c.JSON(http.StatusOK, composeResponse(shortcut))
c.Response().Header().Set(echo.HeaderContentType, echo.MIMEApplicationJSONCharsetUTF8)
if err := json.NewEncoder(c.Response().Writer).Encode(composeResponse(shortcut)); err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to encode shortcut response").SetInternal(err)
}
return nil
}) })
g.PATCH("/shortcut/:shortcutId", func(c echo.Context) error { g.PATCH("/shortcut/:shortcutId", func(c echo.Context) error {
@ -77,12 +72,7 @@ func (s *Server) registerShortcutRoutes(g *echo.Group) {
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)
} }
return c.JSON(http.StatusOK, composeResponse(shortcut))
c.Response().Header().Set(echo.HeaderContentType, echo.MIMEApplicationJSONCharsetUTF8)
if err := json.NewEncoder(c.Response().Writer).Encode(composeResponse(shortcut)); err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to encode shortcut response").SetInternal(err)
}
return nil
}) })
g.GET("/shortcut", func(c echo.Context) error { g.GET("/shortcut", func(c echo.Context) error {
@ -99,12 +89,7 @@ func (s *Server) registerShortcutRoutes(g *echo.Group) {
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)
} }
return c.JSON(http.StatusOK, composeResponse(list))
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 shortcut list response").SetInternal(err)
}
return nil
}) })
g.GET("/shortcut/:shortcutId", func(c echo.Context) error { g.GET("/shortcut/:shortcutId", func(c echo.Context) error {
@ -121,12 +106,7 @@ func (s *Server) registerShortcutRoutes(g *echo.Group) {
if err != nil { if err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, fmt.Sprintf("Failed to fetch shortcut by ID %d", *shortcutFind.ID)).SetInternal(err) return echo.NewHTTPError(http.StatusInternalServerError, fmt.Sprintf("Failed to fetch shortcut by ID %d", *shortcutFind.ID)).SetInternal(err)
} }
return c.JSON(http.StatusOK, composeResponse(shortcut))
c.Response().Header().Set(echo.HeaderContentType, echo.MIMEApplicationJSONCharsetUTF8)
if err := json.NewEncoder(c.Response().Writer).Encode(composeResponse(shortcut)); err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to encode shortcut response").SetInternal(err)
}
return nil
}) })
g.DELETE("/shortcut/:shortcutId", func(c echo.Context) error { g.DELETE("/shortcut/:shortcutId", func(c echo.Context) error {
@ -160,7 +140,6 @@ func (s *Server) registerShortcutRoutes(g *echo.Group) {
} }
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to delete shortcut").SetInternal(err) return echo.NewHTTPError(http.StatusInternalServerError, "Failed to delete shortcut").SetInternal(err)
} }
return c.JSON(http.StatusOK, true) return c.JSON(http.StatusOK, true)
}) })
} }
@ -171,7 +150,7 @@ func (s *Server) createShortcutCreateActivity(c echo.Context, shortcut *api.Shor
Title: shortcut.Title, Title: shortcut.Title,
Payload: shortcut.Payload, Payload: shortcut.Payload,
} }
payloadStr, err := json.Marshal(payload) payloadBytes, err := json.Marshal(payload)
if err != nil { if err != nil {
return errors.Wrap(err, "failed to marshal activity payload") return errors.Wrap(err, "failed to marshal activity payload")
} }
@ -179,7 +158,7 @@ func (s *Server) createShortcutCreateActivity(c echo.Context, shortcut *api.Shor
CreatorID: shortcut.CreatorID, CreatorID: shortcut.CreatorID,
Type: api.ActivityShortcutCreate, Type: api.ActivityShortcutCreate,
Level: api.ActivityInfo, Level: api.ActivityInfo,
Payload: string(payloadStr), Payload: string(payloadBytes),
}) })
if err != nil || activity == nil { if err != nil || activity == nil {
return errors.Wrap(err, "failed to create activity") return errors.Wrap(err, "failed to create activity")

View File

@ -38,12 +38,7 @@ func (s *Server) registerStorageRoutes(g *echo.Group) {
if err != nil { if err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to create storage").SetInternal(err) return echo.NewHTTPError(http.StatusInternalServerError, "Failed to create storage").SetInternal(err)
} }
return c.JSON(http.StatusOK, composeResponse(storage))
c.Response().Header().Set(echo.HeaderContentType, echo.MIMEApplicationJSONCharsetUTF8)
if err := json.NewEncoder(c.Response().Writer).Encode(composeResponse(storage)); err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to encode storage response").SetInternal(err)
}
return nil
}) })
g.PATCH("/storage/:storageId", func(c echo.Context) error { g.PATCH("/storage/:storageId", func(c echo.Context) error {
@ -79,12 +74,7 @@ func (s *Server) registerStorageRoutes(g *echo.Group) {
if err != nil { if err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to patch storage").SetInternal(err) return echo.NewHTTPError(http.StatusInternalServerError, "Failed to patch storage").SetInternal(err)
} }
return c.JSON(http.StatusOK, composeResponse(storage))
c.Response().Header().Set(echo.HeaderContentType, echo.MIMEApplicationJSONCharsetUTF8)
if err := json.NewEncoder(c.Response().Writer).Encode(composeResponse(storage)); err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to encode memo response").SetInternal(err)
}
return nil
}) })
g.GET("/storage", func(c echo.Context) error { g.GET("/storage", func(c echo.Context) error {
@ -109,12 +99,7 @@ func (s *Server) registerStorageRoutes(g *echo.Group) {
if err != nil { if err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to find storage list").SetInternal(err) return echo.NewHTTPError(http.StatusInternalServerError, "Failed to find storage list").SetInternal(err)
} }
return c.JSON(http.StatusOK, composeResponse(storageList))
c.Response().Header().Set(echo.HeaderContentType, echo.MIMEApplicationJSONCharsetUTF8)
if err := json.NewEncoder(c.Response().Writer).Encode(composeResponse(storageList)); err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to encode storage list response").SetInternal(err)
}
return nil
}) })
g.DELETE("/storage/:storageId", func(c echo.Context) error { g.DELETE("/storage/:storageId", func(c echo.Context) error {
@ -160,7 +145,6 @@ func (s *Server) registerStorageRoutes(g *echo.Group) {
} }
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to delete storage").SetInternal(err) return echo.NewHTTPError(http.StatusInternalServerError, "Failed to delete storage").SetInternal(err)
} }
return c.JSON(http.StatusOK, true) return c.JSON(http.StatusOK, true)
}) })
} }

View File

@ -15,13 +15,7 @@ import (
func (s *Server) registerSystemRoutes(g *echo.Group) { func (s *Server) registerSystemRoutes(g *echo.Group) {
g.GET("/ping", func(c echo.Context) error { g.GET("/ping", func(c echo.Context) error {
data := s.Profile return c.JSON(http.StatusOK, composeResponse(s.Profile))
c.Response().Header().Set(echo.HeaderContentType, echo.MIMEApplicationJSONCharsetUTF8)
if err := json.NewEncoder(c.Response().Writer).Encode(composeResponse(data)); err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to compose system profile").SetInternal(err)
}
return nil
}) })
g.GET("/status", func(c echo.Context) error { g.GET("/status", func(c echo.Context) error {
@ -126,12 +120,7 @@ func (s *Server) registerSystemRoutes(g *echo.Group) {
systemStatus.DBSize = fi.Size() systemStatus.DBSize = fi.Size()
} }
} }
return c.JSON(http.StatusOK, composeResponse(systemStatus))
c.Response().Header().Set(echo.HeaderContentType, echo.MIMEApplicationJSONCharsetUTF8)
if err := json.NewEncoder(c.Response().Writer).Encode(composeResponse(systemStatus)); err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to encode system status response").SetInternal(err)
}
return nil
}) })
g.POST("/system/setting", func(c echo.Context) error { g.POST("/system/setting", func(c echo.Context) error {
@ -163,12 +152,7 @@ func (s *Server) registerSystemRoutes(g *echo.Group) {
if err != nil { if err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to upsert system setting").SetInternal(err) return echo.NewHTTPError(http.StatusInternalServerError, "Failed to upsert system setting").SetInternal(err)
} }
return c.JSON(http.StatusOK, composeResponse(systemSetting))
c.Response().Header().Set(echo.HeaderContentType, echo.MIMEApplicationJSONCharsetUTF8)
if err := json.NewEncoder(c.Response().Writer).Encode(composeResponse(systemSetting)); err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to encode system setting response").SetInternal(err)
}
return nil
}) })
g.GET("/system/setting", func(c echo.Context) error { g.GET("/system/setting", func(c echo.Context) error {
@ -177,12 +161,7 @@ func (s *Server) registerSystemRoutes(g *echo.Group) {
if err != nil { if err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to find system setting list").SetInternal(err) return echo.NewHTTPError(http.StatusInternalServerError, "Failed to find system setting list").SetInternal(err)
} }
return c.JSON(http.StatusOK, composeResponse(systemSettingList))
c.Response().Header().Set(echo.HeaderContentType, echo.MIMEApplicationJSONCharsetUTF8)
if err := json.NewEncoder(c.Response().Writer).Encode(composeResponse(systemSettingList)); err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to encode system setting list response").SetInternal(err)
}
return nil
}) })
g.POST("/system/vacuum", func(c echo.Context) error { g.POST("/system/vacuum", func(c echo.Context) error {
@ -203,8 +182,7 @@ func (s *Server) registerSystemRoutes(g *echo.Group) {
if err := s.Store.Vacuum(ctx); err != nil { if err := s.Store.Vacuum(ctx); err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to vacuum database").SetInternal(err) return echo.NewHTTPError(http.StatusInternalServerError, "Failed to vacuum database").SetInternal(err)
} }
c.Response().WriteHeader(http.StatusOK) return c.JSON(http.StatusOK, true)
return nil
}) })
} }

View File

@ -39,12 +39,7 @@ func (s *Server) registerTagRoutes(g *echo.Group) {
if err := s.createTagCreateActivity(c, tag); err != nil { if err := s.createTagCreateActivity(c, tag); err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to create activity").SetInternal(err) return echo.NewHTTPError(http.StatusInternalServerError, "Failed to create activity").SetInternal(err)
} }
return c.JSON(http.StatusOK, composeResponse(tag.Name))
c.Response().Header().Set(echo.HeaderContentType, echo.MIMEApplicationJSONCharsetUTF8)
if err := json.NewEncoder(c.Response().Writer).Encode(composeResponse(tag.Name)); err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to encode tag response").SetInternal(err)
}
return nil
}) })
g.GET("/tag", func(c echo.Context) error { g.GET("/tag", func(c echo.Context) error {
@ -66,12 +61,7 @@ func (s *Server) registerTagRoutes(g *echo.Group) {
for _, tag := range tagList { for _, tag := range tagList {
tagNameList = append(tagNameList, tag.Name) tagNameList = append(tagNameList, tag.Name)
} }
return c.JSON(http.StatusOK, composeResponse(tagNameList))
c.Response().Header().Set(echo.HeaderContentType, echo.MIMEApplicationJSONCharsetUTF8)
if err := json.NewEncoder(c.Response().Writer).Encode(composeResponse(tagNameList)); err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to encode tags response").SetInternal(err)
}
return nil
}) })
g.GET("/tag/suggestion", func(c echo.Context) error { g.GET("/tag/suggestion", func(c echo.Context) error {
@ -118,12 +108,7 @@ func (s *Server) registerTagRoutes(g *echo.Group) {
tagList = append(tagList, tag) tagList = append(tagList, tag)
} }
sort.Strings(tagList) sort.Strings(tagList)
return c.JSON(http.StatusOK, composeResponse(tagList))
c.Response().Header().Set(echo.HeaderContentType, echo.MIMEApplicationJSONCharsetUTF8)
if err := json.NewEncoder(c.Response().Writer).Encode(composeResponse(tagList)); err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to encode tags response").SetInternal(err)
}
return nil
}) })
g.POST("/tag/delete", func(c echo.Context) error { g.POST("/tag/delete", func(c echo.Context) error {
@ -148,7 +133,6 @@ func (s *Server) registerTagRoutes(g *echo.Group) {
} }
return echo.NewHTTPError(http.StatusInternalServerError, fmt.Sprintf("Failed to delete tag name: %v", tagDelete.Name)).SetInternal(err) return echo.NewHTTPError(http.StatusInternalServerError, fmt.Sprintf("Failed to delete tag name: %v", tagDelete.Name)).SetInternal(err)
} }
return c.JSON(http.StatusOK, true) return c.JSON(http.StatusOK, true)
}) })
} }
@ -176,7 +160,7 @@ func (s *Server) createTagCreateActivity(c echo.Context, tag *api.Tag) error {
payload := api.ActivityTagCreatePayload{ payload := api.ActivityTagCreatePayload{
TagName: tag.Name, TagName: tag.Name,
} }
payloadStr, err := json.Marshal(payload) payloadBytes, err := json.Marshal(payload)
if err != nil { if err != nil {
return errors.Wrap(err, "failed to marshal activity payload") return errors.Wrap(err, "failed to marshal activity payload")
} }
@ -184,7 +168,7 @@ func (s *Server) createTagCreateActivity(c echo.Context, tag *api.Tag) error {
CreatorID: tag.CreatorID, CreatorID: tag.CreatorID,
Type: api.ActivityTagCreate, Type: api.ActivityTagCreate,
Level: api.ActivityInfo, Level: api.ActivityInfo,
Payload: string(payloadStr), Payload: string(payloadBytes),
}) })
if err != nil || activity == nil { if err != nil || activity == nil {
return errors.Wrap(err, "failed to create activity") return errors.Wrap(err, "failed to create activity")

View File

@ -59,12 +59,7 @@ func (s *Server) registerUserRoutes(g *echo.Group) {
if err := s.createUserCreateActivity(c, user); err != nil { if err := s.createUserCreateActivity(c, user); err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to create activity").SetInternal(err) return echo.NewHTTPError(http.StatusInternalServerError, "Failed to create activity").SetInternal(err)
} }
return c.JSON(http.StatusOK, composeResponse(user))
c.Response().Header().Set(echo.HeaderContentType, echo.MIMEApplicationJSONCharsetUTF8)
if err := json.NewEncoder(c.Response().Writer).Encode(composeResponse(user)); err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to encode user response").SetInternal(err)
}
return nil
}) })
g.GET("/user", func(c echo.Context) error { g.GET("/user", func(c echo.Context) error {
@ -79,12 +74,7 @@ func (s *Server) registerUserRoutes(g *echo.Group) {
user.OpenID = "" user.OpenID = ""
user.Email = "" user.Email = ""
} }
return c.JSON(http.StatusOK, composeResponse(userList))
c.Response().Header().Set(echo.HeaderContentType, echo.MIMEApplicationJSONCharsetUTF8)
if err := json.NewEncoder(c.Response().Writer).Encode(composeResponse(userList)); err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to encode user list response").SetInternal(err)
}
return nil
}) })
g.POST("/user/setting", func(c echo.Context) error { g.POST("/user/setting", func(c echo.Context) error {
@ -107,12 +97,7 @@ func (s *Server) registerUserRoutes(g *echo.Group) {
if err != nil { if err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to upsert user setting").SetInternal(err) return echo.NewHTTPError(http.StatusInternalServerError, "Failed to upsert user setting").SetInternal(err)
} }
return c.JSON(http.StatusOK, composeResponse(userSetting))
c.Response().Header().Set(echo.HeaderContentType, echo.MIMEApplicationJSONCharsetUTF8)
if err := json.NewEncoder(c.Response().Writer).Encode(composeResponse(userSetting)); err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to encode user setting response").SetInternal(err)
}
return nil
}) })
// GET /api/user/me is used to check if the user is logged in. // GET /api/user/me is used to check if the user is logged in.
@ -138,12 +123,7 @@ func (s *Server) registerUserRoutes(g *echo.Group) {
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to find userSettingList").SetInternal(err) return echo.NewHTTPError(http.StatusInternalServerError, "Failed to find userSettingList").SetInternal(err)
} }
user.UserSettingList = userSettingList user.UserSettingList = userSettingList
return c.JSON(http.StatusOK, composeResponse(user))
c.Response().Header().Set(echo.HeaderContentType, echo.MIMEApplicationJSONCharsetUTF8)
if err := json.NewEncoder(c.Response().Writer).Encode(composeResponse(user)); err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to encode user response").SetInternal(err)
}
return nil
}) })
g.GET("/user/:id", func(c echo.Context) error { g.GET("/user/:id", func(c echo.Context) error {
@ -165,12 +145,7 @@ func (s *Server) registerUserRoutes(g *echo.Group) {
user.OpenID = "" user.OpenID = ""
user.Email = "" user.Email = ""
} }
return c.JSON(http.StatusOK, composeResponse(user))
c.Response().Header().Set(echo.HeaderContentType, echo.MIMEApplicationJSONCharsetUTF8)
if err := json.NewEncoder(c.Response().Writer).Encode(composeResponse(user)); err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to encode user response").SetInternal(err)
}
return nil
}) })
g.PATCH("/user/:id", func(c echo.Context) error { g.PATCH("/user/:id", func(c echo.Context) error {
@ -234,12 +209,7 @@ func (s *Server) registerUserRoutes(g *echo.Group) {
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to find userSettingList").SetInternal(err) return echo.NewHTTPError(http.StatusInternalServerError, "Failed to find userSettingList").SetInternal(err)
} }
user.UserSettingList = userSettingList user.UserSettingList = userSettingList
return c.JSON(http.StatusOK, composeResponse(user))
c.Response().Header().Set(echo.HeaderContentType, echo.MIMEApplicationJSONCharsetUTF8)
if err := json.NewEncoder(c.Response().Writer).Encode(composeResponse(user)); err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to encode user response").SetInternal(err)
}
return nil
}) })
g.DELETE("/user/:id", func(c echo.Context) error { g.DELETE("/user/:id", func(c echo.Context) error {
@ -286,7 +256,7 @@ func (s *Server) createUserCreateActivity(c echo.Context, user *api.User) error
Username: user.Username, Username: user.Username,
Role: user.Role, Role: user.Role,
} }
payloadStr, err := json.Marshal(payload) payloadBytes, err := json.Marshal(payload)
if err != nil { if err != nil {
return errors.Wrap(err, "failed to marshal activity payload") return errors.Wrap(err, "failed to marshal activity payload")
} }
@ -294,7 +264,7 @@ func (s *Server) createUserCreateActivity(c echo.Context, user *api.User) error
CreatorID: user.ID, CreatorID: user.ID,
Type: api.ActivityUserCreate, Type: api.ActivityUserCreate,
Level: api.ActivityInfo, Level: api.ActivityInfo,
Payload: string(payloadStr), Payload: string(payloadBytes),
}) })
if err != nil || activity == nil { if err != nil || activity == nil {
return errors.Wrap(err, "failed to create activity") return errors.Wrap(err, "failed to create activity")

View File

@ -23,7 +23,8 @@ CREATE TABLE user (
email TEXT NOT NULL DEFAULT '', email TEXT NOT NULL DEFAULT '',
nickname TEXT NOT NULL DEFAULT '', nickname TEXT NOT NULL DEFAULT '',
password_hash TEXT NOT NULL, password_hash TEXT NOT NULL,
open_id TEXT NOT NULL UNIQUE open_id TEXT NOT NULL UNIQUE,
avatar_url TEXT NOT NULL DEFAULT ''
); );
-- user_setting -- user_setting

View File

@ -27,6 +27,7 @@ type userRaw struct {
Nickname string Nickname string
PasswordHash string PasswordHash string
OpenID string OpenID string
AvatarURL string
} }
func (raw *userRaw) toUser() *api.User { func (raw *userRaw) toUser() *api.User {
@ -43,6 +44,7 @@ func (raw *userRaw) toUser() *api.User {
Nickname: raw.Nickname, Nickname: raw.Nickname,
PasswordHash: raw.PasswordHash, PasswordHash: raw.PasswordHash,
OpenID: raw.OpenID, OpenID: raw.OpenID,
AvatarURL: raw.AvatarURL,
} }
} }
@ -180,10 +182,11 @@ func createUser(ctx context.Context, tx *sql.Tx, create *api.UserCreate) (*userR
email, email,
nickname, nickname,
password_hash, password_hash,
open_id open_id,
avatar_url
) )
VALUES (?, ?, ?, ?, ?, ?) VALUES (?, ?, ?, ?, ?, ?, ?)
RETURNING id, username, role, email, nickname, password_hash, open_id, created_ts, updated_ts, row_status RETURNING id, username, role, email, nickname, password_hash, open_id, avatar_url, created_ts, updated_ts, row_status
` `
var userRaw userRaw var userRaw userRaw
if err := tx.QueryRowContext(ctx, query, if err := tx.QueryRowContext(ctx, query,
@ -193,6 +196,7 @@ func createUser(ctx context.Context, tx *sql.Tx, create *api.UserCreate) (*userR
create.Nickname, create.Nickname,
create.PasswordHash, create.PasswordHash,
create.OpenID, create.OpenID,
create.AvatarURL,
).Scan( ).Scan(
&userRaw.ID, &userRaw.ID,
&userRaw.Username, &userRaw.Username,
@ -201,6 +205,7 @@ func createUser(ctx context.Context, tx *sql.Tx, create *api.UserCreate) (*userR
&userRaw.Nickname, &userRaw.Nickname,
&userRaw.PasswordHash, &userRaw.PasswordHash,
&userRaw.OpenID, &userRaw.OpenID,
&userRaw.AvatarURL,
&userRaw.CreatedTs, &userRaw.CreatedTs,
&userRaw.UpdatedTs, &userRaw.UpdatedTs,
&userRaw.RowStatus, &userRaw.RowStatus,
@ -229,6 +234,9 @@ func patchUser(ctx context.Context, tx *sql.Tx, patch *api.UserPatch) (*userRaw,
if v := patch.Nickname; v != nil { if v := patch.Nickname; v != nil {
set, args = append(set, "nickname = ?"), append(args, *v) set, args = append(set, "nickname = ?"), append(args, *v)
} }
if v := patch.AvatarURL; v != nil {
set, args = append(set, "avatar_url = ?"), append(args, *v)
}
if v := patch.PasswordHash; v != nil { if v := patch.PasswordHash; v != nil {
set, args = append(set, "password_hash = ?"), append(args, *v) set, args = append(set, "password_hash = ?"), append(args, *v)
} }
@ -242,7 +250,7 @@ func patchUser(ctx context.Context, tx *sql.Tx, patch *api.UserPatch) (*userRaw,
UPDATE user UPDATE user
SET ` + strings.Join(set, ", ") + ` SET ` + strings.Join(set, ", ") + `
WHERE id = ? WHERE id = ?
RETURNING id, username, role, email, nickname, password_hash, open_id, created_ts, updated_ts, row_status RETURNING id, username, role, email, nickname, password_hash, open_id, avatar_url, created_ts, updated_ts, row_status
` `
var userRaw userRaw var userRaw userRaw
if err := tx.QueryRowContext(ctx, query, args...).Scan( if err := tx.QueryRowContext(ctx, query, args...).Scan(
@ -253,6 +261,7 @@ func patchUser(ctx context.Context, tx *sql.Tx, patch *api.UserPatch) (*userRaw,
&userRaw.Nickname, &userRaw.Nickname,
&userRaw.PasswordHash, &userRaw.PasswordHash,
&userRaw.OpenID, &userRaw.OpenID,
&userRaw.AvatarURL,
&userRaw.CreatedTs, &userRaw.CreatedTs,
&userRaw.UpdatedTs, &userRaw.UpdatedTs,
&userRaw.RowStatus, &userRaw.RowStatus,
@ -294,6 +303,7 @@ func findUserList(ctx context.Context, tx *sql.Tx, find *api.UserFind) ([]*userR
nickname, nickname,
password_hash, password_hash,
open_id, open_id,
avatar_url,
created_ts, created_ts,
updated_ts, updated_ts,
row_status row_status
@ -318,13 +328,13 @@ func findUserList(ctx context.Context, tx *sql.Tx, find *api.UserFind) ([]*userR
&userRaw.Nickname, &userRaw.Nickname,
&userRaw.PasswordHash, &userRaw.PasswordHash,
&userRaw.OpenID, &userRaw.OpenID,
&userRaw.AvatarURL,
&userRaw.CreatedTs, &userRaw.CreatedTs,
&userRaw.UpdatedTs, &userRaw.UpdatedTs,
&userRaw.RowStatus, &userRaw.RowStatus,
); err != nil { ); err != nil {
return nil, FormatError(err) return nil, FormatError(err)
} }
userRawList = append(userRawList, &userRaw) userRawList = append(userRawList, &userRaw)
} }
@ -343,7 +353,10 @@ func deleteUser(ctx context.Context, tx *sql.Tx, delete *api.UserDelete) error {
return FormatError(err) return FormatError(err)
} }
rows, _ := result.RowsAffected() rows, err := result.RowsAffected()
if err != nil {
return err
}
if rows == 0 { if rows == 0 {
return &common.Error{Code: common.NotFound, Err: fmt.Errorf("user not found")} return &common.Error{Code: common.NotFound, Err: fmt.Errorf("user not found")}
} }