Skip to content

Commit f10412f

Browse files
committed
feat: add APi's to get and add
1 parent 2967400 commit f10412f

File tree

1 file changed

+47
-0
lines changed

1 file changed

+47
-0
lines changed

services/core/http_routes.go

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,9 @@ func (h HttpHandler) Register(r *echo.Echo) {
110110
v4 := r.Group("/api/v4")
111111
v4.GET("/about", httpserver.AuthorizeHandler(h.GetAboutShort, api3.ViewerRole))
112112
v4.GET("/queries/sync", httpserver.AuthorizeHandler(h.SyncQueries, api3.ViewerRole))
113+
v4.POST("layout/get", httpserver.AuthorizeHandler(h.GetUserLayout, api3.ViewerRole))
114+
v4.POST("layout/set", httpserver.AuthorizeHandler(h.SetUserLayout, api3.ViewerRole))
115+
113116

114117
}
115118

@@ -1580,3 +1583,47 @@ func (h HttpHandler) SyncQueries(echoCtx echo.Context) error {
15801583

15811584
return echoCtx.JSON(http.StatusOK, struct{}{})
15821585
}
1586+
1587+
func (h HttpHandler) GetUserLayout(echoCtx echo.Context) error {
1588+
var req api.GetUserLayout
1589+
if err := bindValidate(echoCtx, &req); err != nil {
1590+
return err
1591+
}
1592+
userId := req.UserID
1593+
layout,err:= h.db.GetUserLayout(userId)
1594+
if( err != nil) {
1595+
h.logger.Error("failed to get user layout", zap.Error(err))
1596+
return echo.NewHTTPError(http.StatusInternalServerError, "failed to get user layout")
1597+
}
1598+
if layout == nil {
1599+
return echo.NewHTTPError(http.StatusNotFound, "user layout not found")
1600+
}
1601+
return echoCtx.JSON(http.StatusOK, layout)
1602+
1603+
}
1604+
func (h HttpHandler) SetUserLayout(echoCtx echo.Context) error {
1605+
var req api.SetUserLayout
1606+
if err := bindValidate(echoCtx, &req); err != nil {
1607+
return err
1608+
}
1609+
userId := req.UserID
1610+
// convert req.layoutconfig to jsonb
1611+
layout, err := json.Marshal(req.LayoutConfig)
1612+
if err != nil {
1613+
h.logger.Error("failed to marshal layout config", zap.Error(err))
1614+
return echo.NewHTTPError(http.StatusInternalServerError, "failed to marshal layout config")
1615+
}
1616+
layout_config := pgtype.JSONB{}
1617+
layout_config.Set(layout)
1618+
1619+
user_layout := models.UserLayout{
1620+
UserID: userId,
1621+
LayoutConfig: layout_config,
1622+
}
1623+
err = h.db.SetUserLayout(user_layout)
1624+
if err != nil {
1625+
h.logger.Error("failed to set user layout", zap.Error(err))
1626+
return echo.NewHTTPError(http.StatusInternalServerError, "failed to set user layout")
1627+
}
1628+
return echoCtx.NoContent(http.StatusOK)
1629+
}

0 commit comments

Comments
 (0)