Skip to content

Commit dde8bd5

Browse files
committed
feat: add private,publick API's
1 parent f10412f commit dde8bd5

File tree

4 files changed

+60
-2
lines changed

4 files changed

+60
-2
lines changed

services/core/api/user_layout.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,14 @@ package api
22

33

44

5-
type GetUserLayout struct {
5+
type GetUserLayoutRequest struct {
66
UserID string `json:"user_id"`
77
}
8+
type ChangePrivacyRequest struct {
9+
UserID string `json:"user_id"`
10+
IsPrivate bool `json:"is_private"`
11+
}
12+
813
type SetUserLayout struct {
914
UserID string `json:"user_id"`
1015
LayoutConfig []map[string]any `json:"layout_config"`

services/core/db/metadata-db.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,5 +169,22 @@ func (db Database) SetUserLayout( layoutConfig models.UserLayout) error {
169169
return nil
170170

171171
}
172+
// get public layouts
172173

174+
func (db Database) GetPublicLayouts() ([]models.UserLayout, error) {
175+
var userLayouts []models.UserLayout
176+
err := db.orm.Where("is_private = ?", false).Find(&userLayouts).Error
177+
if err != nil {
178+
return nil, err
179+
}
180+
return userLayouts, nil
181+
}
182+
// change layout privacy
183+
func (db Database) ChangeLayoutPrivacy(userID string, isPrivate bool) error {
184+
err := db.orm.Model(&models.UserLayout{}).Where("user_id = ?", userID).Update("is_private", isPrivate).Error
185+
if err != nil {
186+
return err
187+
}
188+
return nil
189+
}
173190

services/core/db/models/metadata-models.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,10 @@ type QueryView struct {
5353
type UserLayout struct{
5454
UserID string `gorm:"primaryKey" ,json:"user_id"`
5555
LayoutConfig pgtype.JSONB `gorm:"type:jsonb"`
56+
Name string `gorm:"type:text"`
57+
CreatedAt time.Time `json:"created_at"`
58+
UpdatedAt time.Time `json:"updated_at"`
59+
IsPrivate bool `json:"is_private"`
5660
}
5761
// Array of widgets
5862
// example:

services/core/http_routes.go

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,9 @@ func (h HttpHandler) Register(r *echo.Echo) {
112112
v4.GET("/queries/sync", httpserver.AuthorizeHandler(h.SyncQueries, api3.ViewerRole))
113113
v4.POST("layout/get", httpserver.AuthorizeHandler(h.GetUserLayout, api3.ViewerRole))
114114
v4.POST("layout/set", httpserver.AuthorizeHandler(h.SetUserLayout, api3.ViewerRole))
115+
v4.POST("layout/change-privacy", httpserver.AuthorizeHandler(h.ChangePrivacy, api3.ViewerRole))
116+
v4.GET("layout/public", httpserver.AuthorizeHandler(h.GetPublicLayouts, api3.ViewerRole))
117+
115118

116119

117120
}
@@ -1585,7 +1588,7 @@ func (h HttpHandler) SyncQueries(echoCtx echo.Context) error {
15851588
}
15861589

15871590
func (h HttpHandler) GetUserLayout(echoCtx echo.Context) error {
1588-
var req api.GetUserLayout
1591+
var req api.GetUserLayoutRequest
15891592
if err := bindValidate(echoCtx, &req); err != nil {
15901593
return err
15911594
}
@@ -1627,3 +1630,32 @@ func (h HttpHandler) SetUserLayout(echoCtx echo.Context) error {
16271630
}
16281631
return echoCtx.NoContent(http.StatusOK)
16291632
}
1633+
1634+
func (h HttpHandler) ChangePrivacy (echoCtx echo.Context) error{
1635+
var req api.ChangePrivacyRequest
1636+
if err := bindValidate(echoCtx, &req); err != nil {
1637+
return err
1638+
}
1639+
userId := req.UserID
1640+
privacy := req.IsPrivate
1641+
err := h.db.ChangeLayoutPrivacy(userId, privacy)
1642+
if err != nil {
1643+
h.logger.Error("failed to change privacy", zap.Error(err))
1644+
return echo.NewHTTPError(http.StatusInternalServerError, "failed to change privacy")
1645+
}
1646+
return echoCtx.NoContent(http.StatusOK)
1647+
}
1648+
1649+
// get public layouts
1650+
func (h HttpHandler) GetPublicLayouts(echoCtx echo.Context) error {
1651+
1652+
layouts, err := h.db.GetPublicLayouts()
1653+
if err != nil {
1654+
h.logger.Error("failed to get public layouts", zap.Error(err))
1655+
return echo.NewHTTPError(http.StatusInternalServerError, "failed to get public layouts")
1656+
}
1657+
if layouts == nil {
1658+
return echo.NewHTTPError(http.StatusNotFound, "public layouts not found")
1659+
}
1660+
return echoCtx.JSON(http.StatusOK, layouts)
1661+
}

0 commit comments

Comments
 (0)