chore: migrate user setting to api v1 package (#1855)

* chore: migrate to api v1 package

* chore: update
This commit is contained in:
boojack
2023-06-26 23:06:53 +08:00
committed by GitHub
parent 07e82c3f4a
commit b44f2b5ffb
20 changed files with 764 additions and 173 deletions

View File

@ -9,7 +9,9 @@ import (
"github.com/pkg/errors"
"github.com/usememos/memos/api"
apiv1 "github.com/usememos/memos/api/v1"
"github.com/usememos/memos/common"
"github.com/usememos/memos/store"
"github.com/labstack/echo/v4"
"golang.org/x/crypto/bcrypt"
@ -83,7 +85,7 @@ func (s *Server) registerUserRoutes(g *echo.Group) {
return echo.NewHTTPError(http.StatusUnauthorized, "Missing auth session")
}
userSettingUpsert := &api.UserSettingUpsert{}
userSettingUpsert := &apiv1.UserSettingUpsert{}
if err := json.NewDecoder(c.Request().Body).Decode(userSettingUpsert); err != nil {
return echo.NewHTTPError(http.StatusBadRequest, "Malformatted post user setting upsert request").SetInternal(err)
}
@ -92,10 +94,15 @@ func (s *Server) registerUserRoutes(g *echo.Group) {
}
userSettingUpsert.UserID = userID
userSetting, err := s.Store.UpsertUserSetting(ctx, userSettingUpsert)
userSettingMessage, err := s.Store.UpsertUserSettingV1(ctx, &store.UserSettingMessage{
UserID: userID,
Key: userSettingUpsert.Key.String(),
Value: userSettingUpsert.Value,
})
if err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to upsert user setting").SetInternal(err)
}
userSetting := convertUserSettingFromStore(userSettingMessage)
return c.JSON(http.StatusOK, composeResponse(userSetting))
})
@ -115,12 +122,21 @@ func (s *Server) registerUserRoutes(g *echo.Group) {
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to find user").SetInternal(err)
}
userSettingList, err := s.Store.FindUserSettingList(ctx, &api.UserSettingFind{
userSettingMessageList, err := s.Store.ListUserSettings(ctx, &store.FindUserSettingMessage{
UserID: &userID,
})
if err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to find userSettingList").SetInternal(err)
}
userSettingList := []*api.UserSetting{}
for _, userSettingMessage := range userSettingMessageList {
userSettingV1 := convertUserSettingFromStore(userSettingMessage)
userSettingList = append(userSettingList, &api.UserSetting{
UserID: userSettingV1.UserID,
Key: api.UserSettingKey(userSettingV1.Key),
Value: userSettingV1.Value,
})
}
user.UserSettingList = userSettingList
return c.JSON(http.StatusOK, composeResponse(user))
})
@ -202,12 +218,21 @@ func (s *Server) registerUserRoutes(g *echo.Group) {
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to patch user").SetInternal(err)
}
userSettingList, err := s.Store.FindUserSettingList(ctx, &api.UserSettingFind{
userSettingMessageList, err := s.Store.ListUserSettings(ctx, &store.FindUserSettingMessage{
UserID: &userID,
})
if err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to find userSettingList").SetInternal(err)
}
userSettingList := []*api.UserSetting{}
for _, userSettingMessage := range userSettingMessageList {
userSettingV1 := convertUserSettingFromStore(userSettingMessage)
userSettingList = append(userSettingList, &api.UserSetting{
UserID: userSettingV1.UserID,
Key: api.UserSettingKey(userSettingV1.Key),
Value: userSettingV1.Value,
})
}
user.UserSettingList = userSettingList
return c.JSON(http.StatusOK, composeResponse(user))
})
@ -271,3 +296,11 @@ func (s *Server) createUserCreateActivity(c echo.Context, user *api.User) error
}
return err
}
func convertUserSettingFromStore(userSetting *store.UserSettingMessage) *apiv1.UserSetting {
return &apiv1.UserSetting{
UserID: userSetting.UserID,
Key: apiv1.UserSettingKey(userSetting.Key),
Value: userSetting.Value,
}
}