mirror of
https://github.com/usememos/memos.git
synced 2025-06-05 22:09:59 +02:00
chore: migrate user setting to api v1 package (#1855)
* chore: migrate to api v1 package * chore: update
This commit is contained in:
@ -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,
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user