mirror of
https://github.com/aykhans/slash-e.git
synced 2025-04-18 21:19:44 +00:00
chore: migrate part of shortcut store
This commit is contained in:
parent
98d4bb40b2
commit
b365355610
@ -8,6 +8,7 @@ import (
|
|||||||
"net/url"
|
"net/url"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
|
storepb "github.com/boojack/slash/proto/gen/store"
|
||||||
"github.com/boojack/slash/store"
|
"github.com/boojack/slash/store"
|
||||||
"github.com/labstack/echo/v4"
|
"github.com/labstack/echo/v4"
|
||||||
"github.com/pkg/errors"
|
"github.com/pkg/errors"
|
||||||
@ -30,12 +31,12 @@ func (s *APIV1Service) registerRedirectorRoutes(g *echo.Group) {
|
|||||||
if shortcut == nil {
|
if shortcut == nil {
|
||||||
return echo.NewHTTPError(http.StatusNotFound, fmt.Sprintf("not found shortcut with name: %s", shortcutName))
|
return echo.NewHTTPError(http.StatusNotFound, fmt.Sprintf("not found shortcut with name: %s", shortcutName))
|
||||||
}
|
}
|
||||||
if shortcut.Visibility != store.VisibilityPublic {
|
if shortcut.Visibility != storepb.Visibility_PUBLIC {
|
||||||
userID, ok := c.Get(UserIDContextKey).(int32)
|
userID, ok := c.Get(UserIDContextKey).(int32)
|
||||||
if !ok {
|
if !ok {
|
||||||
return echo.NewHTTPError(http.StatusUnauthorized, "Unauthorized")
|
return echo.NewHTTPError(http.StatusUnauthorized, "Unauthorized")
|
||||||
}
|
}
|
||||||
if shortcut.Visibility == store.VisibilityPrivate && shortcut.CreatorID != userID {
|
if shortcut.Visibility == storepb.Visibility_PRIVATE && shortcut.CreatorId != userID {
|
||||||
return echo.NewHTTPError(http.StatusUnauthorized, "Unauthorized")
|
return echo.NewHTTPError(http.StatusUnauthorized, "Unauthorized")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -48,9 +49,9 @@ func (s *APIV1Service) registerRedirectorRoutes(g *echo.Group) {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
func redirectToShortcut(c echo.Context, shortcut *store.Shortcut) error {
|
func redirectToShortcut(c echo.Context, shortcut *storepb.Shortcut) error {
|
||||||
isValidURL := isValidURLString(shortcut.Link)
|
isValidURL := isValidURLString(shortcut.Link)
|
||||||
if shortcut.OpenGraphMetadata == nil || (shortcut.OpenGraphMetadata.Title == "" && shortcut.OpenGraphMetadata.Description == "" && shortcut.OpenGraphMetadata.Image == "") {
|
if shortcut.OgMetadata == nil || (shortcut.OgMetadata.Title == "" && shortcut.OgMetadata.Description == "" && shortcut.OgMetadata.Image == "") {
|
||||||
if isValidURL {
|
if isValidURL {
|
||||||
return c.Redirect(http.StatusSeeOther, shortcut.Link)
|
return c.Redirect(http.StatusSeeOther, shortcut.Link)
|
||||||
}
|
}
|
||||||
@ -59,16 +60,16 @@ func redirectToShortcut(c echo.Context, shortcut *store.Shortcut) error {
|
|||||||
|
|
||||||
htmlTemplate := `<html><head>%s</head><body>%s</body></html>`
|
htmlTemplate := `<html><head>%s</head><body>%s</body></html>`
|
||||||
metadataList := []string{
|
metadataList := []string{
|
||||||
fmt.Sprintf(`<title>%s</title>`, shortcut.OpenGraphMetadata.Title),
|
fmt.Sprintf(`<title>%s</title>`, shortcut.OgMetadata.Title),
|
||||||
fmt.Sprintf(`<meta name="description" content="%s" />`, shortcut.OpenGraphMetadata.Description),
|
fmt.Sprintf(`<meta name="description" content="%s" />`, shortcut.OgMetadata.Description),
|
||||||
fmt.Sprintf(`<meta property="og:title" content="%s" />`, shortcut.OpenGraphMetadata.Title),
|
fmt.Sprintf(`<meta property="og:title" content="%s" />`, shortcut.OgMetadata.Title),
|
||||||
fmt.Sprintf(`<meta property="og:description" content="%s" />`, shortcut.OpenGraphMetadata.Description),
|
fmt.Sprintf(`<meta property="og:description" content="%s" />`, shortcut.OgMetadata.Description),
|
||||||
fmt.Sprintf(`<meta property="og:image" content="%s" />`, shortcut.OpenGraphMetadata.Image),
|
fmt.Sprintf(`<meta property="og:image" content="%s" />`, shortcut.OgMetadata.Image),
|
||||||
`<meta property="og:type" content="website" />`,
|
`<meta property="og:type" content="website" />`,
|
||||||
// Twitter related metadata.
|
// Twitter related metadata.
|
||||||
fmt.Sprintf(`<meta name="twitter:title" content="%s" />`, shortcut.OpenGraphMetadata.Title),
|
fmt.Sprintf(`<meta name="twitter:title" content="%s" />`, shortcut.OgMetadata.Title),
|
||||||
fmt.Sprintf(`<meta name="twitter:description" content="%s" />`, shortcut.OpenGraphMetadata.Description),
|
fmt.Sprintf(`<meta name="twitter:description" content="%s" />`, shortcut.OgMetadata.Description),
|
||||||
fmt.Sprintf(`<meta name="twitter:image" content="%s" />`, shortcut.OpenGraphMetadata.Image),
|
fmt.Sprintf(`<meta name="twitter:image" content="%s" />`, shortcut.OgMetadata.Image),
|
||||||
`<meta name="twitter:card" content="summary_large_image" />`,
|
`<meta name="twitter:card" content="summary_large_image" />`,
|
||||||
}
|
}
|
||||||
if isValidURL {
|
if isValidURL {
|
||||||
@ -84,9 +85,9 @@ func redirectToShortcut(c echo.Context, shortcut *store.Shortcut) error {
|
|||||||
return c.HTML(http.StatusOK, htmlString)
|
return c.HTML(http.StatusOK, htmlString)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *APIV1Service) createShortcutViewActivity(c echo.Context, shortcut *store.Shortcut) error {
|
func (s *APIV1Service) createShortcutViewActivity(c echo.Context, shortcut *storepb.Shortcut) error {
|
||||||
payload := &ActivityShorcutViewPayload{
|
payload := &ActivityShorcutViewPayload{
|
||||||
ShortcutID: shortcut.ID,
|
ShortcutID: shortcut.Id,
|
||||||
IP: c.RealIP(),
|
IP: c.RealIP(),
|
||||||
Referer: c.Request().Referer(),
|
Referer: c.Request().Referer(),
|
||||||
UserAgent: c.Request().UserAgent(),
|
UserAgent: c.Request().UserAgent(),
|
||||||
|
@ -90,15 +90,15 @@ func (s *APIV1Service) registerShortcutRoutes(g *echo.Group) {
|
|||||||
return echo.NewHTTPError(http.StatusBadRequest, fmt.Sprintf("malformatted post shortcut request, err: %s", err)).SetInternal(err)
|
return echo.NewHTTPError(http.StatusBadRequest, fmt.Sprintf("malformatted post shortcut request, err: %s", err)).SetInternal(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
shortcut, err := s.Store.CreateShortcut(ctx, &store.Shortcut{
|
shortcut, err := s.Store.CreateShortcut(ctx, &storepb.Shortcut{
|
||||||
CreatorID: userID,
|
CreatorId: userID,
|
||||||
Name: strings.ToLower(create.Name),
|
Name: strings.ToLower(create.Name),
|
||||||
Link: create.Link,
|
Link: create.Link,
|
||||||
Title: create.Title,
|
Title: create.Title,
|
||||||
Description: create.Description,
|
Description: create.Description,
|
||||||
Visibility: store.Visibility(create.Visibility.String()),
|
Visibility: convertVisibilityToStorepb(create.Visibility),
|
||||||
Tag: strings.Join(create.Tags, " "),
|
Tags: create.Tags,
|
||||||
OpenGraphMetadata: &store.OpenGraphMetadata{
|
OgMetadata: &storepb.OpenGraphMetadata{
|
||||||
Title: create.OpenGraphMetadata.Title,
|
Title: create.OpenGraphMetadata.Title,
|
||||||
Description: create.OpenGraphMetadata.Description,
|
Description: create.OpenGraphMetadata.Description,
|
||||||
Image: create.OpenGraphMetadata.Image,
|
Image: create.OpenGraphMetadata.Image,
|
||||||
@ -112,7 +112,7 @@ func (s *APIV1Service) registerShortcutRoutes(g *echo.Group) {
|
|||||||
return echo.NewHTTPError(http.StatusInternalServerError, fmt.Sprintf("failed to create shortcut activity, err: %s", err)).SetInternal(err)
|
return echo.NewHTTPError(http.StatusInternalServerError, fmt.Sprintf("failed to create shortcut activity, err: %s", err)).SetInternal(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
shortcutMessage, err := s.composeShortcut(ctx, convertShortcutFromStore(shortcut))
|
shortcutMessage, err := s.composeShortcut(ctx, convertShortcutFromStorepb(shortcut))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return echo.NewHTTPError(http.StatusInternalServerError, fmt.Sprintf("failed to compose shortcut, err: %s", err)).SetInternal(err)
|
return echo.NewHTTPError(http.StatusInternalServerError, fmt.Sprintf("failed to compose shortcut, err: %s", err)).SetInternal(err)
|
||||||
}
|
}
|
||||||
@ -145,7 +145,7 @@ func (s *APIV1Service) registerShortcutRoutes(g *echo.Group) {
|
|||||||
if shortcut == nil {
|
if shortcut == nil {
|
||||||
return echo.NewHTTPError(http.StatusNotFound, fmt.Sprintf("not found shortcut with id: %d", shortcutID))
|
return echo.NewHTTPError(http.StatusNotFound, fmt.Sprintf("not found shortcut with id: %d", shortcutID))
|
||||||
}
|
}
|
||||||
if shortcut.CreatorID != userID && currentUser.Role != store.RoleAdmin {
|
if shortcut.CreatorId != userID && currentUser.Role != store.RoleAdmin {
|
||||||
return echo.NewHTTPError(http.StatusForbidden, "unauthorized to update shortcut")
|
return echo.NewHTTPError(http.StatusForbidden, "unauthorized to update shortcut")
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -187,7 +187,7 @@ func (s *APIV1Service) registerShortcutRoutes(g *echo.Group) {
|
|||||||
return echo.NewHTTPError(http.StatusInternalServerError, fmt.Sprintf("failed to patch shortcut, err: %s", err)).SetInternal(err)
|
return echo.NewHTTPError(http.StatusInternalServerError, fmt.Sprintf("failed to patch shortcut, err: %s", err)).SetInternal(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
shortcutMessage, err := s.composeShortcut(ctx, convertShortcutFromStore(shortcut))
|
shortcutMessage, err := s.composeShortcut(ctx, convertShortcutFromStorepb(shortcut))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return echo.NewHTTPError(http.StatusInternalServerError, fmt.Sprintf("failed to compose shortcut, err: %s", err)).SetInternal(err)
|
return echo.NewHTTPError(http.StatusInternalServerError, fmt.Sprintf("failed to compose shortcut, err: %s", err)).SetInternal(err)
|
||||||
}
|
}
|
||||||
@ -208,7 +208,7 @@ func (s *APIV1Service) registerShortcutRoutes(g *echo.Group) {
|
|||||||
|
|
||||||
list := []*storepb.Shortcut{}
|
list := []*storepb.Shortcut{}
|
||||||
find.VisibilityList = []store.Visibility{store.VisibilityWorkspace, store.VisibilityPublic}
|
find.VisibilityList = []store.Visibility{store.VisibilityWorkspace, store.VisibilityPublic}
|
||||||
visibleShortcutList, err := s.Store.ListShortcutsV1(ctx, find)
|
visibleShortcutList, err := s.Store.ListShortcuts(ctx, find)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return echo.NewHTTPError(http.StatusInternalServerError, fmt.Sprintf("failed to fetch shortcut list, err: %s", err)).SetInternal(err)
|
return echo.NewHTTPError(http.StatusInternalServerError, fmt.Sprintf("failed to fetch shortcut list, err: %s", err)).SetInternal(err)
|
||||||
}
|
}
|
||||||
@ -216,7 +216,7 @@ func (s *APIV1Service) registerShortcutRoutes(g *echo.Group) {
|
|||||||
|
|
||||||
find.VisibilityList = []store.Visibility{store.VisibilityPrivate}
|
find.VisibilityList = []store.Visibility{store.VisibilityPrivate}
|
||||||
find.CreatorID = &userID
|
find.CreatorID = &userID
|
||||||
privateShortcutList, err := s.Store.ListShortcutsV1(ctx, find)
|
privateShortcutList, err := s.Store.ListShortcuts(ctx, find)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return echo.NewHTTPError(http.StatusInternalServerError, fmt.Sprintf("failed to fetch private shortcut list, err: %s", err)).SetInternal(err)
|
return echo.NewHTTPError(http.StatusInternalServerError, fmt.Sprintf("failed to fetch private shortcut list, err: %s", err)).SetInternal(err)
|
||||||
}
|
}
|
||||||
@ -250,7 +250,7 @@ func (s *APIV1Service) registerShortcutRoutes(g *echo.Group) {
|
|||||||
return echo.NewHTTPError(http.StatusNotFound, fmt.Sprintf("not found shortcut with id: %d", shortcutID))
|
return echo.NewHTTPError(http.StatusNotFound, fmt.Sprintf("not found shortcut with id: %d", shortcutID))
|
||||||
}
|
}
|
||||||
|
|
||||||
shortcutMessage, err := s.composeShortcut(ctx, convertShortcutFromStore(shortcut))
|
shortcutMessage, err := s.composeShortcut(ctx, convertShortcutFromStorepb(shortcut))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return echo.NewHTTPError(http.StatusInternalServerError, fmt.Sprintf("failed to compose shortcut, err: %s", err)).SetInternal(err)
|
return echo.NewHTTPError(http.StatusInternalServerError, fmt.Sprintf("failed to compose shortcut, err: %s", err)).SetInternal(err)
|
||||||
}
|
}
|
||||||
@ -283,7 +283,7 @@ func (s *APIV1Service) registerShortcutRoutes(g *echo.Group) {
|
|||||||
if shortcut == nil {
|
if shortcut == nil {
|
||||||
return echo.NewHTTPError(http.StatusNotFound, fmt.Sprintf("not found shortcut with id: %d", shortcutID))
|
return echo.NewHTTPError(http.StatusNotFound, fmt.Sprintf("not found shortcut with id: %d", shortcutID))
|
||||||
}
|
}
|
||||||
if shortcut.CreatorID != userID && currentUser.Role != store.RoleAdmin {
|
if shortcut.CreatorId != userID && currentUser.Role != store.RoleAdmin {
|
||||||
return echo.NewHTTPError(http.StatusForbidden, "Unauthorized to delete shortcut")
|
return echo.NewHTTPError(http.StatusForbidden, "Unauthorized to delete shortcut")
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -324,32 +324,6 @@ func (s *APIV1Service) composeShortcut(ctx context.Context, shortcut *Shortcut)
|
|||||||
return shortcut, nil
|
return shortcut, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func convertShortcutFromStore(shortcut *store.Shortcut) *Shortcut {
|
|
||||||
tags := []string{}
|
|
||||||
if shortcut.Tag != "" {
|
|
||||||
tags = append(tags, strings.Split(shortcut.Tag, " ")...)
|
|
||||||
}
|
|
||||||
|
|
||||||
return &Shortcut{
|
|
||||||
ID: shortcut.ID,
|
|
||||||
CreatedTs: shortcut.CreatedTs,
|
|
||||||
UpdatedTs: shortcut.UpdatedTs,
|
|
||||||
CreatorID: shortcut.CreatorID,
|
|
||||||
Name: shortcut.Name,
|
|
||||||
Link: shortcut.Link,
|
|
||||||
Title: shortcut.Title,
|
|
||||||
Description: shortcut.Description,
|
|
||||||
Visibility: Visibility(shortcut.Visibility),
|
|
||||||
RowStatus: RowStatus(shortcut.RowStatus),
|
|
||||||
Tags: tags,
|
|
||||||
OpenGraphMetadata: &OpenGraphMetadata{
|
|
||||||
Title: shortcut.OpenGraphMetadata.Title,
|
|
||||||
Description: shortcut.OpenGraphMetadata.Description,
|
|
||||||
Image: shortcut.OpenGraphMetadata.Image,
|
|
||||||
},
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func convertShortcutFromStorepb(shortcut *storepb.Shortcut) *Shortcut {
|
func convertShortcutFromStorepb(shortcut *storepb.Shortcut) *Shortcut {
|
||||||
return &Shortcut{
|
return &Shortcut{
|
||||||
ID: shortcut.Id,
|
ID: shortcut.Id,
|
||||||
@ -371,16 +345,27 @@ func convertShortcutFromStorepb(shortcut *storepb.Shortcut) *Shortcut {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *APIV1Service) createShortcutCreateActivity(ctx context.Context, shortcut *store.Shortcut) error {
|
func convertVisibilityToStorepb(visibility Visibility) storepb.Visibility {
|
||||||
|
switch visibility {
|
||||||
|
case VisibilityPublic:
|
||||||
|
return storepb.Visibility_PUBLIC
|
||||||
|
case VisibilityPrivate:
|
||||||
|
return storepb.Visibility_PRIVATE
|
||||||
|
default:
|
||||||
|
return storepb.Visibility_PUBLIC
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *APIV1Service) createShortcutCreateActivity(ctx context.Context, shortcut *storepb.Shortcut) error {
|
||||||
payload := &ActivityShorcutCreatePayload{
|
payload := &ActivityShorcutCreatePayload{
|
||||||
ShortcutID: shortcut.ID,
|
ShortcutID: shortcut.Id,
|
||||||
}
|
}
|
||||||
payloadStr, err := json.Marshal(payload)
|
payloadStr, err := json.Marshal(payload)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return errors.Wrap(err, "Failed to marshal activity payload")
|
return errors.Wrap(err, "Failed to marshal activity payload")
|
||||||
}
|
}
|
||||||
activity := &store.Activity{
|
activity := &store.Activity{
|
||||||
CreatorID: shortcut.CreatorID,
|
CreatorID: shortcut.CreatorId,
|
||||||
Type: store.ActivityShortcutCreate,
|
Type: store.ActivityShortcutCreate,
|
||||||
Level: store.ActivityInfo,
|
Level: store.ActivityInfo,
|
||||||
Payload: string(payloadStr),
|
Payload: string(payloadStr),
|
||||||
|
@ -24,7 +24,7 @@ func (e RowStatus) String() string {
|
|||||||
return ""
|
return ""
|
||||||
}
|
}
|
||||||
|
|
||||||
func convertStorepbRowStatus(status string) storepb.RowStatus {
|
func convertRowStatusStringToStorepb(status string) storepb.RowStatus {
|
||||||
switch status {
|
switch status {
|
||||||
case "NORMAL":
|
case "NORMAL":
|
||||||
return storepb.RowStatus_NORMAL
|
return storepb.RowStatus_NORMAL
|
||||||
|
@ -41,25 +41,6 @@ type OpenGraphMetadata struct {
|
|||||||
Image string `json:"image"`
|
Image string `json:"image"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type Shortcut struct {
|
|
||||||
ID int32
|
|
||||||
|
|
||||||
// Standard fields
|
|
||||||
CreatorID int32
|
|
||||||
CreatedTs int64
|
|
||||||
UpdatedTs int64
|
|
||||||
RowStatus RowStatus
|
|
||||||
|
|
||||||
// Domain specific fields
|
|
||||||
Name string
|
|
||||||
Link string
|
|
||||||
Title string
|
|
||||||
Description string
|
|
||||||
Visibility Visibility
|
|
||||||
Tag string
|
|
||||||
OpenGraphMetadata *OpenGraphMetadata
|
|
||||||
}
|
|
||||||
|
|
||||||
type UpdateShortcut struct {
|
type UpdateShortcut struct {
|
||||||
ID int32
|
ID int32
|
||||||
|
|
||||||
@ -86,40 +67,7 @@ type DeleteShortcut struct {
|
|||||||
ID int32
|
ID int32
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *Store) CreateShortcut(ctx context.Context, create *Shortcut) (*Shortcut, error) {
|
func (s *Store) CreateShortcut(ctx context.Context, create *storepb.Shortcut) (*storepb.Shortcut, error) {
|
||||||
set := []string{"creator_id", "name", "link", "title", "description", "visibility", "tag"}
|
|
||||||
args := []any{create.CreatorID, create.Name, create.Link, create.Title, create.Description, create.Visibility, create.Tag}
|
|
||||||
placeholder := []string{"?", "?", "?", "?", "?", "?", "?"}
|
|
||||||
if create.OpenGraphMetadata != nil {
|
|
||||||
set = append(set, "og_metadata")
|
|
||||||
openGraphMetadataBytes, err := json.Marshal(create.OpenGraphMetadata)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
args = append(args, string(openGraphMetadataBytes))
|
|
||||||
placeholder = append(placeholder, "?")
|
|
||||||
}
|
|
||||||
|
|
||||||
stmt := `
|
|
||||||
INSERT INTO shortcut (
|
|
||||||
` + strings.Join(set, ", ") + `
|
|
||||||
)
|
|
||||||
VALUES (` + strings.Join(placeholder, ",") + `)
|
|
||||||
RETURNING id, created_ts, updated_ts, row_status
|
|
||||||
`
|
|
||||||
if err := s.db.QueryRowContext(ctx, stmt, args...).Scan(
|
|
||||||
&create.ID,
|
|
||||||
&create.CreatedTs,
|
|
||||||
&create.UpdatedTs,
|
|
||||||
&create.RowStatus,
|
|
||||||
); err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
return create, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (s *Store) CreateShortcutV1(ctx context.Context, create *storepb.Shortcut) (*storepb.Shortcut, error) {
|
|
||||||
set := []string{"creator_id", "name", "link", "title", "description", "visibility", "tag"}
|
set := []string{"creator_id", "name", "link", "title", "description", "visibility", "tag"}
|
||||||
args := []any{create.CreatorId, create.Name, create.Link, create.Title, create.Description, create.Visibility.String(), strings.Join(create.Tags, " ")}
|
args := []any{create.CreatorId, create.Name, create.Link, create.Title, create.Description, create.Visibility.String(), strings.Join(create.Tags, " ")}
|
||||||
placeholder := []string{"?", "?", "?", "?", "?", "?", "?"}
|
placeholder := []string{"?", "?", "?", "?", "?", "?", "?"}
|
||||||
@ -149,12 +97,13 @@ func (s *Store) CreateShortcutV1(ctx context.Context, create *storepb.Shortcut)
|
|||||||
); err != nil {
|
); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
create.RowStatus = convertStorepbRowStatus(rowStatus)
|
create.RowStatus = convertRowStatusStringToStorepb(rowStatus)
|
||||||
|
shortcut := create
|
||||||
return create, nil
|
s.shortcutCache.Store(shortcut.Id, shortcut)
|
||||||
|
return shortcut, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *Store) UpdateShortcut(ctx context.Context, update *UpdateShortcut) (*Shortcut, error) {
|
func (s *Store) UpdateShortcut(ctx context.Context, update *UpdateShortcut) (*storepb.Shortcut, error) {
|
||||||
set, args := []string{}, []any{}
|
set, args := []string{}, []any{}
|
||||||
if update.RowStatus != nil {
|
if update.RowStatus != nil {
|
||||||
set, args = append(set, "row_status = ?"), append(args, update.RowStatus.String())
|
set, args = append(set, "row_status = ?"), append(args, update.RowStatus.String())
|
||||||
@ -197,125 +146,36 @@ func (s *Store) UpdateShortcut(ctx context.Context, update *UpdateShortcut) (*Sh
|
|||||||
id = ?
|
id = ?
|
||||||
RETURNING id, creator_id, created_ts, updated_ts, row_status, name, link, title, description, visibility, tag, og_metadata
|
RETURNING id, creator_id, created_ts, updated_ts, row_status, name, link, title, description, visibility, tag, og_metadata
|
||||||
`
|
`
|
||||||
shortcut := &Shortcut{}
|
shortcut := &storepb.Shortcut{}
|
||||||
openGraphMetadataString := ""
|
var rowStatus, visibility, tags, openGraphMetadataString string
|
||||||
if err := s.db.QueryRowContext(ctx, stmt, args...).Scan(
|
if err := s.db.QueryRowContext(ctx, stmt, args...).Scan(
|
||||||
&shortcut.ID,
|
&shortcut.Id,
|
||||||
&shortcut.CreatorID,
|
&shortcut.CreatorId,
|
||||||
&shortcut.CreatedTs,
|
&shortcut.CreatedTs,
|
||||||
&shortcut.UpdatedTs,
|
&shortcut.UpdatedTs,
|
||||||
&shortcut.RowStatus,
|
&rowStatus,
|
||||||
&shortcut.Name,
|
&shortcut.Name,
|
||||||
&shortcut.Link,
|
&shortcut.Link,
|
||||||
&shortcut.Title,
|
&shortcut.Title,
|
||||||
&shortcut.Description,
|
&shortcut.Description,
|
||||||
&shortcut.Visibility,
|
&visibility,
|
||||||
&shortcut.Tag,
|
&tags,
|
||||||
&openGraphMetadataString,
|
&openGraphMetadataString,
|
||||||
); err != nil {
|
); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
if openGraphMetadataString != "" {
|
shortcut.RowStatus = convertRowStatusStringToStorepb(rowStatus)
|
||||||
shortcut.OpenGraphMetadata = &OpenGraphMetadata{}
|
shortcut.Visibility = convertVisibilityStringToStorepb(visibility)
|
||||||
if err := json.Unmarshal([]byte(openGraphMetadataString), shortcut.OpenGraphMetadata); err != nil {
|
var ogMetadata storepb.OpenGraphMetadata
|
||||||
return nil, err
|
if err := protojson.Unmarshal([]byte(openGraphMetadataString), &ogMetadata); err != nil {
|
||||||
}
|
return nil, err
|
||||||
}
|
}
|
||||||
|
shortcut.OgMetadata = &ogMetadata
|
||||||
s.shortcutCache.Store(shortcut.ID, shortcut)
|
s.shortcutCache.Store(shortcut.Id, shortcut)
|
||||||
return shortcut, nil
|
return shortcut, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *Store) ListShortcuts(ctx context.Context, find *FindShortcut) ([]*Shortcut, error) {
|
func (s *Store) ListShortcuts(ctx context.Context, find *FindShortcut) ([]*storepb.Shortcut, error) {
|
||||||
where, args := []string{"1 = 1"}, []any{}
|
|
||||||
if v := find.ID; v != nil {
|
|
||||||
where, args = append(where, "id = ?"), append(args, *v)
|
|
||||||
}
|
|
||||||
if v := find.CreatorID; v != nil {
|
|
||||||
where, args = append(where, "creator_id = ?"), append(args, *v)
|
|
||||||
}
|
|
||||||
if v := find.RowStatus; v != nil {
|
|
||||||
where, args = append(where, "row_status = ?"), append(args, *v)
|
|
||||||
}
|
|
||||||
if v := find.Name; v != nil {
|
|
||||||
where, args = append(where, "name = ?"), append(args, *v)
|
|
||||||
}
|
|
||||||
if v := find.VisibilityList; len(v) != 0 {
|
|
||||||
list := []string{}
|
|
||||||
for _, visibility := range v {
|
|
||||||
list = append(list, fmt.Sprintf("$%d", len(args)+1))
|
|
||||||
args = append(args, visibility)
|
|
||||||
}
|
|
||||||
where = append(where, fmt.Sprintf("visibility in (%s)", strings.Join(list, ",")))
|
|
||||||
}
|
|
||||||
if v := find.Tag; v != nil {
|
|
||||||
where, args = append(where, "tag LIKE ?"), append(args, "%"+*v+"%")
|
|
||||||
}
|
|
||||||
|
|
||||||
rows, err := s.db.QueryContext(ctx, `
|
|
||||||
SELECT
|
|
||||||
id,
|
|
||||||
creator_id,
|
|
||||||
created_ts,
|
|
||||||
updated_ts,
|
|
||||||
row_status,
|
|
||||||
name,
|
|
||||||
link,
|
|
||||||
title,
|
|
||||||
description,
|
|
||||||
visibility,
|
|
||||||
tag,
|
|
||||||
og_metadata
|
|
||||||
FROM shortcut
|
|
||||||
WHERE `+strings.Join(where, " AND ")+`
|
|
||||||
ORDER BY created_ts DESC`,
|
|
||||||
args...,
|
|
||||||
)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
defer rows.Close()
|
|
||||||
|
|
||||||
list := make([]*Shortcut, 0)
|
|
||||||
for rows.Next() {
|
|
||||||
shortcut := &Shortcut{}
|
|
||||||
openGraphMetadataString := ""
|
|
||||||
if err := rows.Scan(
|
|
||||||
&shortcut.ID,
|
|
||||||
&shortcut.CreatorID,
|
|
||||||
&shortcut.CreatedTs,
|
|
||||||
&shortcut.UpdatedTs,
|
|
||||||
&shortcut.RowStatus,
|
|
||||||
&shortcut.Name,
|
|
||||||
&shortcut.Link,
|
|
||||||
&shortcut.Title,
|
|
||||||
&shortcut.Description,
|
|
||||||
&shortcut.Visibility,
|
|
||||||
&shortcut.Tag,
|
|
||||||
&openGraphMetadataString,
|
|
||||||
); err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
if openGraphMetadataString != "" {
|
|
||||||
shortcut.OpenGraphMetadata = &OpenGraphMetadata{}
|
|
||||||
if err := json.Unmarshal([]byte(openGraphMetadataString), shortcut.OpenGraphMetadata); err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
}
|
|
||||||
list = append(list, shortcut)
|
|
||||||
}
|
|
||||||
|
|
||||||
if err := rows.Err(); err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
for _, shortcut := range list {
|
|
||||||
s.shortcutCache.Store(shortcut.ID, shortcut)
|
|
||||||
}
|
|
||||||
return list, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (s *Store) ListShortcutsV1(ctx context.Context, find *FindShortcut) ([]*storepb.Shortcut, error) {
|
|
||||||
where, args := []string{"1 = 1"}, []any{}
|
where, args := []string{"1 = 1"}, []any{}
|
||||||
if v := find.ID; v != nil {
|
if v := find.ID; v != nil {
|
||||||
where, args = append(where, "id = ?"), append(args, *v)
|
where, args = append(where, "id = ?"), append(args, *v)
|
||||||
@ -385,7 +245,7 @@ func (s *Store) ListShortcutsV1(ctx context.Context, find *FindShortcut) ([]*sto
|
|||||||
); err != nil {
|
); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
shortcut.RowStatus = convertStorepbRowStatus(rowStatus)
|
shortcut.RowStatus = convertRowStatusStringToStorepb(rowStatus)
|
||||||
shortcut.Visibility = storepb.Visibility(storepb.Visibility_value[visibility])
|
shortcut.Visibility = storepb.Visibility(storepb.Visibility_value[visibility])
|
||||||
shortcut.Tags = strings.Split(tags, " ")
|
shortcut.Tags = strings.Split(tags, " ")
|
||||||
var ogMetadata storepb.OpenGraphMetadata
|
var ogMetadata storepb.OpenGraphMetadata
|
||||||
@ -399,13 +259,16 @@ func (s *Store) ListShortcutsV1(ctx context.Context, find *FindShortcut) ([]*sto
|
|||||||
if err := rows.Err(); err != nil {
|
if err := rows.Err(); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
for _, shortcut := range list {
|
||||||
|
s.shortcutCache.Store(shortcut.Id, shortcut)
|
||||||
|
}
|
||||||
return list, nil
|
return list, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *Store) GetShortcut(ctx context.Context, find *FindShortcut) (*Shortcut, error) {
|
func (s *Store) GetShortcut(ctx context.Context, find *FindShortcut) (*storepb.Shortcut, error) {
|
||||||
if find.ID != nil {
|
if find.ID != nil {
|
||||||
if cache, ok := s.shortcutCache.Load(*find.ID); ok {
|
if cache, ok := s.shortcutCache.Load(*find.ID); ok {
|
||||||
return cache.(*Shortcut), nil
|
return cache.(*storepb.Shortcut), nil
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -419,21 +282,7 @@ func (s *Store) GetShortcut(ctx context.Context, find *FindShortcut) (*Shortcut,
|
|||||||
}
|
}
|
||||||
|
|
||||||
shortcut := shortcuts[0]
|
shortcut := shortcuts[0]
|
||||||
s.shortcutCache.Store(shortcut.ID, shortcut)
|
s.shortcutCache.Store(shortcut.Id, shortcut)
|
||||||
return shortcut, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (s *Store) GetShortcutV1(ctx context.Context, find *FindShortcut) (*storepb.Shortcut, error) {
|
|
||||||
shortcuts, err := s.ListShortcutsV1(ctx, find)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
if len(shortcuts) == 0 {
|
|
||||||
return nil, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
shortcut := shortcuts[0]
|
|
||||||
return shortcut, nil
|
return shortcut, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -465,3 +314,7 @@ func vacuumShortcut(ctx context.Context, tx *sql.Tx) error {
|
|||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func convertVisibilityStringToStorepb(visibility string) storepb.Visibility {
|
||||||
|
return storepb.Visibility(storepb.Visibility_value[visibility])
|
||||||
|
}
|
||||||
|
@ -14,7 +14,7 @@ func TestShortcutStore(t *testing.T) {
|
|||||||
ts := NewTestingStore(ctx, t)
|
ts := NewTestingStore(ctx, t)
|
||||||
user, err := createTestingAdminUser(ctx, ts)
|
user, err := createTestingAdminUser(ctx, ts)
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
shortcut, err := ts.CreateShortcutV1(ctx, &storepb.Shortcut{
|
shortcut, err := ts.CreateShortcut(ctx, &storepb.Shortcut{
|
||||||
CreatorId: user.ID,
|
CreatorId: user.ID,
|
||||||
Name: "test",
|
Name: "test",
|
||||||
Link: "https://test.link",
|
Link: "https://test.link",
|
||||||
@ -24,7 +24,7 @@ func TestShortcutStore(t *testing.T) {
|
|||||||
OgMetadata: &storepb.OpenGraphMetadata{},
|
OgMetadata: &storepb.OpenGraphMetadata{},
|
||||||
})
|
})
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
shortcuts, err := ts.ListShortcutsV1(ctx, &store.FindShortcut{
|
shortcuts, err := ts.ListShortcuts(ctx, &store.FindShortcut{
|
||||||
CreatorID: &user.ID,
|
CreatorID: &user.ID,
|
||||||
})
|
})
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
@ -38,7 +38,7 @@ func TestShortcutStore(t *testing.T) {
|
|||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
require.Equal(t, newLink, updatedShortcut.Link)
|
require.Equal(t, newLink, updatedShortcut.Link)
|
||||||
tag := "test"
|
tag := "test"
|
||||||
shortcut, err = ts.GetShortcutV1(ctx, &store.FindShortcut{
|
shortcut, err = ts.GetShortcut(ctx, &store.FindShortcut{
|
||||||
Tag: &tag,
|
Tag: &tag,
|
||||||
})
|
})
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
@ -46,7 +46,7 @@ func TestShortcutStore(t *testing.T) {
|
|||||||
ID: shortcut.Id,
|
ID: shortcut.Id,
|
||||||
})
|
})
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
shortcuts, err = ts.ListShortcutsV1(ctx, &store.FindShortcut{
|
shortcuts, err = ts.ListShortcuts(ctx, &store.FindShortcut{
|
||||||
CreatorID: &user.ID,
|
CreatorID: &user.ID,
|
||||||
})
|
})
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
|
@ -4,8 +4,8 @@ import (
|
|||||||
"context"
|
"context"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
|
storepb "github.com/boojack/slash/proto/gen/store"
|
||||||
"github.com/boojack/slash/store"
|
"github.com/boojack/slash/store"
|
||||||
|
|
||||||
"github.com/stretchr/testify/require"
|
"github.com/stretchr/testify/require"
|
||||||
"golang.org/x/crypto/bcrypt"
|
"golang.org/x/crypto/bcrypt"
|
||||||
)
|
)
|
||||||
@ -26,11 +26,11 @@ func TestUserStore(t *testing.T) {
|
|||||||
Nickname: &userPatchNickname,
|
Nickname: &userPatchNickname,
|
||||||
})
|
})
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
_, err = ts.CreateShortcut(ctx, &store.Shortcut{
|
_, err = ts.CreateShortcut(ctx, &storepb.Shortcut{
|
||||||
CreatorID: user.ID,
|
CreatorId: user.ID,
|
||||||
Name: "test_shortcut",
|
Name: "test_shortcut",
|
||||||
Link: "https://www.google.com",
|
Link: "https://www.google.com",
|
||||||
Visibility: store.VisibilityPublic,
|
Visibility: storepb.Visibility_PUBLIC,
|
||||||
})
|
})
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
require.Equal(t, userPatchNickname, user.Nickname)
|
require.Equal(t, userPatchNickname, user.Nickname)
|
||||||
@ -41,7 +41,7 @@ func TestUserStore(t *testing.T) {
|
|||||||
users, err = ts.ListUsers(ctx, &store.FindUser{})
|
users, err = ts.ListUsers(ctx, &store.FindUser{})
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
require.Equal(t, 0, len(users))
|
require.Equal(t, 0, len(users))
|
||||||
shortcuts, err := ts.ListShortcutsV1(ctx, &store.FindShortcut{})
|
shortcuts, err := ts.ListShortcuts(ctx, &store.FindShortcut{})
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
require.Equal(t, 0, len(shortcuts))
|
require.Equal(t, 0, len(shortcuts))
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user