chore: update api data convert

This commit is contained in:
Steven 2023-06-22 22:29:59 +08:00
parent f67e0adea4
commit 89740fa160
3 changed files with 81 additions and 10 deletions

View File

@ -58,7 +58,7 @@ func (s *APIV1Service) registerAuthRoutes(g *echo.Group, secret string) {
if err := auth.GenerateTokensAndSetCookies(c, user, secret); err != nil { if err := auth.GenerateTokensAndSetCookies(c, user, secret); err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to generate tokens").SetInternal(err) return echo.NewHTTPError(http.StatusInternalServerError, "Failed to generate tokens").SetInternal(err)
} }
return c.JSON(http.StatusOK, user) return c.JSON(http.StatusOK, convertUserFromStore(user))
}) })
g.POST("/auth/signup", func(c echo.Context) error { g.POST("/auth/signup", func(c echo.Context) error {
@ -98,7 +98,7 @@ func (s *APIV1Service) registerAuthRoutes(g *echo.Group, secret string) {
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to generate tokens").SetInternal(err) return echo.NewHTTPError(http.StatusInternalServerError, "Failed to generate tokens").SetInternal(err)
} }
return c.JSON(http.StatusOK, user) return c.JSON(http.StatusOK, convertUserFromStore(user))
}) })
g.POST("/auth/logout", func(c echo.Context) error { g.POST("/auth/logout", func(c echo.Context) error {

View File

@ -1,6 +1,7 @@
package v1 package v1
import ( import (
"context"
"encoding/json" "encoding/json"
"fmt" "fmt"
"net/http" "net/http"
@ -90,7 +91,11 @@ func (s *APIV1Service) registerShortcutRoutes(g *echo.Group) {
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to create shortcut").SetInternal(err) return echo.NewHTTPError(http.StatusInternalServerError, "Failed to create shortcut").SetInternal(err)
} }
return c.JSON(http.StatusOK, shortcut) shortcutMessage, err := s.composeShortcut(ctx, convertShortcutFromStore(shortcut))
if err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to compose shortcut").SetInternal(err)
}
return c.JSON(http.StatusOK, shortcutMessage)
}) })
g.PATCH("/shortcut/:shortcutId", func(c echo.Context) error { g.PATCH("/shortcut/:shortcutId", func(c echo.Context) error {
@ -133,7 +138,11 @@ func (s *APIV1Service) registerShortcutRoutes(g *echo.Group) {
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, shortcut) shortcutMessage, err := s.composeShortcut(ctx, convertShortcutFromStore(shortcut))
if err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to compose shortcut").SetInternal(err)
}
return c.JSON(http.StatusOK, shortcutMessage)
}) })
g.GET("/shortcut", func(c echo.Context) error { g.GET("/shortcut", func(c echo.Context) error {
@ -164,7 +173,15 @@ func (s *APIV1Service) registerShortcutRoutes(g *echo.Group) {
} }
list = append(list, privateShortcutList...) list = append(list, privateShortcutList...)
return c.JSON(http.StatusOK, list) shortcutMessageList := []*Shortcut{}
for _, shortcut := range list {
shortcutMessage, err := s.composeShortcut(ctx, convertShortcutFromStore(shortcut))
if err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to compose shortcut").SetInternal(err)
}
shortcutMessageList = append(shortcutMessageList, shortcutMessage)
}
return c.JSON(http.StatusOK, shortcutMessageList)
}) })
g.GET("/shortcut/:id", func(c echo.Context) error { g.GET("/shortcut/:id", func(c echo.Context) error {
@ -181,7 +198,11 @@ func (s *APIV1Service) registerShortcutRoutes(g *echo.Group) {
return echo.NewHTTPError(http.StatusInternalServerError, fmt.Sprintf("Failed to fetch shortcut by ID %d", shortcutID)).SetInternal(err) return echo.NewHTTPError(http.StatusInternalServerError, fmt.Sprintf("Failed to fetch shortcut by ID %d", shortcutID)).SetInternal(err)
} }
return c.JSON(http.StatusOK, shortcut) shortcutMessage, err := s.composeShortcut(ctx, convertShortcutFromStore(shortcut))
if err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to compose shortcut").SetInternal(err)
}
return c.JSON(http.StatusOK, shortcutMessage)
}) })
g.DELETE("/shortcut/:id", func(c echo.Context) error { g.DELETE("/shortcut/:id", func(c echo.Context) error {
@ -201,6 +222,24 @@ func (s *APIV1Service) registerShortcutRoutes(g *echo.Group) {
}) })
} }
func (s *APIV1Service) composeShortcut(ctx context.Context, shortcut *Shortcut) (*Shortcut, error) {
if shortcut == nil {
return nil, nil
}
if shortcut.CreatorID != 0 {
user, err := s.Store.GetUser(ctx, &store.FindUser{
ID: &shortcut.CreatorID,
})
if err != nil {
return nil, err
}
shortcut.Creator = convertUserFromStore(user)
}
return shortcut, nil
}
func convertVisibilityToStore(visibility Visibility) store.Visibility { func convertVisibilityToStore(visibility Visibility) store.Visibility {
switch visibility { switch visibility {
case VisibilityPrivate: case VisibilityPrivate:
@ -213,3 +252,17 @@ func convertVisibilityToStore(visibility Visibility) store.Visibility {
return store.VisibilityPrivate return store.VisibilityPrivate
} }
} }
func convertShortcutFromStore(shortcut *store.Shortcut) *Shortcut {
return &Shortcut{
ID: shortcut.ID,
CreatedTs: shortcut.CreatedTs,
UpdatedTs: shortcut.UpdatedTs,
CreatorID: shortcut.CreatorID,
Name: shortcut.Name,
Link: shortcut.Link,
Description: shortcut.Description,
Visibility: Visibility(shortcut.Visibility),
RowStatus: RowStatus(shortcut.RowStatus),
}
}

View File

@ -87,11 +87,15 @@ type UserDelete struct {
func (s *APIV1Service) registerUserRoutes(g *echo.Group) { func (s *APIV1Service) registerUserRoutes(g *echo.Group) {
g.GET("/user", func(c echo.Context) error { g.GET("/user", func(c echo.Context) error {
ctx := c.Request().Context() ctx := c.Request().Context()
userList, err := s.Store.ListUsers(ctx, &store.FindUser{}) list, err := s.Store.ListUsers(ctx, &store.FindUser{})
if err != nil { if err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to find user list").SetInternal(err) return echo.NewHTTPError(http.StatusInternalServerError, "Failed to find user list").SetInternal(err)
} }
userList := []*User{}
for _, user := range list {
userList = append(userList, convertUserFromStore(user))
}
return c.JSON(http.StatusOK, userList) return c.JSON(http.StatusOK, userList)
}) })
@ -110,7 +114,7 @@ func (s *APIV1Service) registerUserRoutes(g *echo.Group) {
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to find user").SetInternal(err) return echo.NewHTTPError(http.StatusInternalServerError, "Failed to find user").SetInternal(err)
} }
return c.JSON(http.StatusOK, user) return c.JSON(http.StatusOK, convertUserFromStore(user))
}) })
g.GET("/user/:id", func(c echo.Context) error { g.GET("/user/:id", func(c echo.Context) error {
@ -127,7 +131,7 @@ func (s *APIV1Service) registerUserRoutes(g *echo.Group) {
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to find user").SetInternal(err) return echo.NewHTTPError(http.StatusInternalServerError, "Failed to find user").SetInternal(err)
} }
return c.JSON(http.StatusOK, user) return c.JSON(http.StatusOK, convertUserFromStore(user))
}) })
g.PATCH("/user/:id", func(c echo.Context) error { g.PATCH("/user/:id", func(c echo.Context) error {
@ -172,7 +176,7 @@ func (s *APIV1Service) registerUserRoutes(g *echo.Group) {
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to update user").SetInternal(err) return echo.NewHTTPError(http.StatusInternalServerError, "Failed to update user").SetInternal(err)
} }
return c.JSON(http.StatusOK, user) return c.JSON(http.StatusOK, convertUserFromStore(user))
}) })
g.DELETE("/user/:id", func(c echo.Context) error { g.DELETE("/user/:id", func(c echo.Context) error {
@ -216,3 +220,17 @@ func validateEmail(email string) bool {
} }
return true return true
} }
// convertUserFromStore converts a store user to a user.
func convertUserFromStore(user *store.User) *User {
return &User{
ID: user.ID,
CreatedTs: user.CreatedTs,
UpdatedTs: user.UpdatedTs,
RowStatus: RowStatus(user.RowStatus),
Username: user.Username,
Nickname: user.Nickname,
Email: user.Email,
Role: Role(user.Role),
}
}