Skip to content

Commit

Permalink
feat: get bookmark if is toggleBookmark
Browse files Browse the repository at this point in the history
  • Loading branch information
liruchen32 committed Jul 5, 2024
1 parent 3c5253a commit e77c217
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 4 deletions.
18 changes: 17 additions & 1 deletion controllers/news_v2.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ import (
log "github.com/sirupsen/logrus"
"github.com/twreporter/go-api/globals"
"github.com/twreporter/go-api/internal/news"
f "github.com/twreporter/logformatter"
"github.com/twreporter/go-api/models"
f "github.com/twreporter/logformatter"
)

type newsV2Storage interface {
Expand All @@ -37,6 +37,7 @@ type newsV2Storage interface {

type newsV2SqlStorage interface {
GetBookmarksOfPosts(context.Context, string, []news.MetaOfPost) ([]news.MetaOfPost, error)
GetBookmarksForFullPost(context.Context, string, news.Post) (models.Bookmark, error)
}

func NewNewsV2Controller(s newsV2Storage, client news.AlgoliaSearcher, sqls newsV2SqlStorage) *newsV2Controller {
Expand Down Expand Up @@ -123,6 +124,21 @@ func (nc *newsV2Controller) GetAPost(c *gin.Context) {
if len(posts) > 0 {
post = posts[0]
}

if fullPost, ok := post.(news.Post); ok {
authUserID := c.Request.Context().Value(globals.AuthUserIDProperty)
if authUserID != nil && q.ToggleBookmark {
c.Writer.Header().Set("Cache-Control", "no-store")
authUserIdString := fmt.Sprintf("%v", authUserID)
var bookmark models.Bookmark
if bookmark, err = nc.SqlStorage.GetBookmarksForFullPost(ctx, authUserIdString, fullPost); err != nil {
log.WithField("detail", err).Errorf("%s", f.FormatStack(err))
} else {
fullPost.BookmarkID = fmt.Sprintf("%d", bookmark.ID)
}
}
post = fullPost
}
} else {
var posts []news.MetaOfPost
posts, err = nc.Storage.GetMetaOfPosts(ctx, q)
Expand Down
53 changes: 50 additions & 3 deletions storage/news_v2.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,16 @@ import (
"context"
"strconv"

"github.com/jinzhu/gorm"
"github.com/pkg/errors"
log "github.com/sirupsen/logrus"
"github.com/twreporter/go-api/globals"
"github.com/twreporter/go-api/internal/news"
"github.com/twreporter/go-api/models"
f "github.com/twreporter/logformatter"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/bson/primitive"
"go.mongodb.org/mongo-driver/mongo"
"github.com/jinzhu/gorm"
log "github.com/sirupsen/logrus"
f "github.com/twreporter/logformatter"
)

type fetchResult struct {
Expand Down Expand Up @@ -95,6 +95,53 @@ func (gs *gormStorage) GetBookmarksOfPostsTask(ctx context.Context, userID strin
return result
}

func (gs *gormStorage) GetBookmarksForFullPost(ctx context.Context, userID string, post news.Post) (models.Bookmark, error) {
if userID == "" {
log.Println("userID is required")
return models.Bookmark{}, errors.New("userID is required")
}

var targetBookmark models.Bookmark

select {
case <-ctx.Done():
return models.Bookmark{}, ctx.Err()
case result, ok := <-gs.GetBookmarksForFullPostTask(ctx, userID, post):
if !ok {
return models.Bookmark{}, errors.New("failed to get bookmark")
}
if result.Error != nil {
return models.Bookmark{}, result.Error
}

targetBookmark, ok = result.Content.(models.Bookmark)
if !ok {
return models.Bookmark{}, errors.New("type assertion failed")
}
}

return targetBookmark, nil
}


func (gs *gormStorage) GetBookmarksForFullPostTask(ctx context.Context, userID string, post news.Post) <-chan fetchResult {
result := make(chan fetchResult)
go func(ctx context.Context, userID string, post news.Post) {

slug := post.Slug

var bookmark models.Bookmark
err := gs.db.Where("id IN (?)", gs.db.Table("users_bookmarks").Select("bookmark_id").Where("user_id = ?", userID).QueryExpr()).Where("slug = ?", slug).Where("deleted_at IS NULL").Find(&bookmark).Error

if err != nil {
log.WithField("detail", err).Errorf("%s", f.FormatStack(err))
}

result <- fetchResult{Content: bookmark}
}(ctx, userID, post)
return result
}

func (m *mongoStorage) GetFullPosts(ctx context.Context, q *news.Query) ([]news.Post, error) {
var posts []news.Post

Expand Down

0 comments on commit e77c217

Please sign in to comment.