Skip to content

Commit 7d6c1ab

Browse files
committed
Refactor GetBoardArticles and GetBoardArticleRecords to include offset and length parameters
1 parent 0cb3d82 commit 7d6c1ab

File tree

9 files changed

+24
-18
lines changed

9 files changed

+24
-18
lines changed

internal/delivery/http/route_boards_articles.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,7 @@ func (delivery *Delivery) getBoardArticles(w http.ResponseWriter, r *http.Reques
166166
}
167167

168168
items := []interface{}{}
169-
articles := delivery.usecase.GetBoardArticles(context.Background(), boardID, searchCond)
169+
articles := delivery.usecase.GetBoardArticles(context.Background(), boardID, 0, ^uint(0), searchCond)
170170

171171
// Articles to output format, please refer: https://docs.google.com/document/d/18DsZOyrlr5BIl2kKxZH7P2QxFLG02xL2SO0PzVHVY3k/edit#heading=h.bnhpxsiwnbey
172172
for _, a := range articles {

internal/delivery/http/route_users.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -297,7 +297,7 @@ func (delivery *Delivery) getUserComments(w http.ResponseWriter, r *http.Request
297297

298298
searchCond := &usecase.ArticleSearchCond{}
299299
ctx := context.Background()
300-
articles := delivery.usecase.GetBoardArticles(ctx, board.BoardID(), searchCond)
300+
articles := delivery.usecase.GetBoardArticles(ctx, board.BoardID(), 0, ^uint(0), searchCond)
301301

302302
for _, article := range articles {
303303
if article.Filename() == board.Filename() {

internal/repository/article.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ func (repo *repository) GetPopularArticles(ctx context.Context) ([]PopularArticl
8282
var result []PopularArticleRecord
8383
boards := repo.GetBoards(ctx)
8484
for _, board := range boards {
85-
articles, err := repo.GetBoardArticleRecords(ctx, board.BoardID())
85+
articles, err := repo.GetBoardArticleRecords(ctx, board.BoardID(), 0, ^uint(0))
8686
if err != nil {
8787
return nil, err
8888
}

internal/repository/board.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package repository
33
import (
44
"context"
55
"fmt"
6+
"log/slog"
67

78
"github.com/Ptt-official-app/Ptt-backend/internal/logging"
89
"github.com/Ptt-official-app/go-bbs"
@@ -20,15 +21,16 @@ import (
2021
// }
2122

2223
func (repo *repository) GetBoards(_ context.Context) []bbs.BoardRecord {
24+
slog.Info("GetBoards", "boardRecords", len(repo.boardRecords))
2325
return repo.boardRecords
2426
}
2527

2628
func (repo *repository) GetBoardArticle(_ context.Context, boardID, filename string) ([]byte, error) {
2729
return repo.db.ReadBoardArticleFile(boardID, filename)
2830
}
2931

30-
func (repo *repository) GetBoardArticleRecords(_ context.Context, boardID string) ([]bbs.ArticleRecord, error) {
31-
return repo.db.ReadBoardArticleRecordsFile(boardID)
32+
func (repo *repository) GetBoardArticleRecords(_ context.Context, boardID string, offset, length uint) ([]bbs.ArticleRecord, error) {
33+
return repo.db.ReadBoardArticleRecordsFile(boardID, offset, length)
3234
}
3335

3436
func (repo *repository) GetBoardTreasureRecords(_ context.Context, boardID string, treasureIDs []string) ([]bbs.ArticleRecord, error) {

internal/repository/repository.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ type Repository interface {
1616
// GetBoardArticle returns an article file in a specified board and filename
1717
GetBoardArticle(ctx context.Context, boardID, filename string) ([]byte, error)
1818
// GetBoardArticleRecords returns article records of a board
19-
GetBoardArticleRecords(ctx context.Context, boardID string) ([]bbs.ArticleRecord, error)
19+
GetBoardArticleRecords(ctx context.Context, boardID string, offset, length uint) ([]bbs.ArticleRecord, error)
2020
// GetBoardTreasureRecords returns treasure article records of a board
2121
GetBoardTreasureRecords(ctx context.Context, boardID string, treasureIDs []string) ([]bbs.ArticleRecord, error)
2222
// GetBoardPostsLimit returns posts limited record of a board

internal/repository/user.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ func (repo *repository) GetUserFavoriteRecords(ctx context.Context, userID strin
6060
}
6161

6262
func (repo *repository) GetUserArticles(_ context.Context, boardID string) ([]bbs.ArticleRecord, error) {
63-
return repo.db.ReadBoardArticleRecordsFile(boardID)
63+
return repo.db.ReadBoardArticleRecordsFile(boardID, 0, ^uint(0))
6464
}
6565

6666
// TODO: no required method in go-bbs and we use a mock, replace it when available

internal/usecase/article.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ func (usecase *usecase) GetPopularArticles(ctx context.Context) ([]repository.Po
2121
}
2222

2323
func (usecase *usecase) UpdateUsefulness(ctx context.Context, userID, boardID, filename, appendType string) (repository.PushRecord, error) {
24-
articleRecords, err := usecase.repo.GetBoardArticleRecords(ctx, boardID)
24+
articleRecords, err := usecase.repo.GetBoardArticleRecords(ctx, boardID, 0, ^uint(0))
2525

2626
if err != nil {
2727
return nil, fmt.Errorf("UpdateUsefulness error: %w", err)
@@ -96,7 +96,7 @@ func (usecase *usecase) ForwardArticleToBoard(ctx context.Context, userID, board
9696

9797
// ForwardArticleToEmail returns forwarding to email results
9898
func (usecase *usecase) ForwardArticleToEmail(ctx context.Context, userID, boardID, filename, email string) error {
99-
articleRecords, err := usecase.repo.GetBoardArticleRecords(ctx, boardID)
99+
articleRecords, err := usecase.repo.GetBoardArticleRecords(ctx, boardID, 0, ^uint(0))
100100
if err != nil {
101101
return fmt.Errorf("GetBoardArticleRecords error: %w", err)
102102
}

internal/usecase/board.go

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package usecase
33
import (
44
"context"
55
"fmt"
6+
"log/slog"
67
"strings"
78

89
"github.com/Ptt-official-app/go-bbs"
@@ -28,7 +29,7 @@ type BoardPostLimitation struct {
2829

2930
func (usecase *usecase) GetBoardByID(ctx context.Context, boardID string) (bbs.BoardRecord, error) {
3031
for _, it := range usecase.repo.GetBoards(ctx) {
31-
if boardID == it.BoardID() {
32+
if strings.ToLower(boardID) == strings.ToLower(it.BoardID()) {
3233
return it, nil
3334
}
3435
}
@@ -37,6 +38,7 @@ func (usecase *usecase) GetBoardByID(ctx context.Context, boardID string) (bbs.B
3738

3839
func (usecase *usecase) GetBoards(ctx context.Context, userID string) []bbs.BoardRecord {
3940
boards := make([]bbs.BoardRecord, 0)
41+
slog.Info("GetBoards", "boardRecords a", len(boards))
4042
for _, board := range usecase.repo.GetBoards(ctx) {
4143
// TODO: Show Board by user level
4244
if board.IsClass() {
@@ -47,6 +49,7 @@ func (usecase *usecase) GetBoards(ctx context.Context, userID string) []bbs.Boar
4749
}
4850
boards = append(boards, board)
4951
}
52+
slog.Info("GetBoards", "boardRecords b", len(boards))
5053
return boards
5154
}
5255

@@ -80,10 +83,10 @@ func (usecase *usecase) GetPopularBoards(ctx context.Context) ([]bbs.BoardRecord
8083
func shouldBeDisplayOnPouplarList(board bbs.BoardRecord) bool {
8184
// Initially filter boards by board status or other values
8285
// TODO:Need to add filter conditions,here is an example
83-
if classID := board.ClassID(); classID != "" && !board.IsClass() {
84-
return true
85-
}
86-
return false
86+
// if classID := board.ClassID(); classID != "" && !board.IsClass() {
87+
return true
88+
// }
89+
// return false
8790
}
8891

8992
func (usecase *usecase) GetBoardPostsLimitation(ctx context.Context, boardID string) (*BoardPostLimitation, error) {
@@ -121,15 +124,16 @@ func (usecase *usecase) GetClasses(ctx context.Context, userID, classID string)
121124
return boards, nil
122125
}
123126

124-
func (usecase *usecase) GetBoardArticles(ctx context.Context, boardID string, cond *ArticleSearchCond) []bbs.ArticleRecord {
127+
func (usecase *usecase) GetBoardArticles(ctx context.Context, boardID string, offset, length uint, cond *ArticleSearchCond) []bbs.ArticleRecord {
125128
var articles []bbs.ArticleRecord
126-
articleRecords, err := usecase.repo.GetBoardArticleRecords(ctx, boardID)
129+
articleRecords, err := usecase.repo.GetBoardArticleRecords(ctx, boardID, offset, length)
127130
if err != nil {
128131
usecase.logger.Warningf("open directory file error: %v", err)
129132
// The board may not contain any article
130133
}
131134

132-
if len(strings.TrimSpace(cond.Title)) > 0 ||
135+
if cond != nil &&
136+
len(strings.TrimSpace(cond.Title)) > 0 ||
133137
len(strings.TrimSpace(cond.Author)) > 0 ||
134138
cond.RecommendCountGreaterEqualIsSet ||
135139
cond.RecommendCountLessEqualIsSet {

internal/usecase/usecase.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ type Usecase interface {
4242
// GetClasses returns board records in a class
4343
GetClasses(ctx context.Context, userID, classID string) ([]bbs.BoardRecord, error)
4444
// GetBoardArticles returns articles of a board
45-
GetBoardArticles(ctx context.Context, boardID string, cond *ArticleSearchCond) []bbs.ArticleRecord // FIXME: use concrete type rather than []interface{}
45+
GetBoardArticles(ctx context.Context, boardID string, offset, length uint, cond *ArticleSearchCond) []bbs.ArticleRecord // FIXME: use concrete type rather than []interface{}
4646
// GetBoardArticle returns an article file given board id and file name
4747
GetBoardArticle(ctx context.Context, boardID, filename string) ([]byte, error)
4848
// GetBoardTreasures returns treasures of a board

0 commit comments

Comments
 (0)