Skip to content

Commit

Permalink
feat: improve returned results from items: null to items = [] (#70)
Browse files Browse the repository at this point in the history
  • Loading branch information
ayoubfaouzi authored Jul 10, 2024
1 parent 7ed41cb commit 72424ca
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 3 deletions.
9 changes: 7 additions & 2 deletions internal/file/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -368,8 +368,13 @@ func (r resource) comments(c echo.Context) error {
if err != nil {
return err
}
count := file.CommentsCount
pages := pagination.NewFromRequest(c.Request(), *count)

count := 0
if file.CommentsCount != nil {
count = *file.CommentsCount
}

pages := pagination.NewFromRequest(c.Request(), count)
comments, err := r.service.Comments(
ctx, c.Param("sha256"), pages.Offset(), pages.Limit())
if err != nil {
Expand Down
6 changes: 5 additions & 1 deletion internal/file/repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ func (r repository) Query(ctx context.Context, offset, limit int, fields []strin
for _, u := range res.([]interface{}) {
file := entity.File{}
b, _ := json.Marshal(u)
_ = json.Unmarshal(b, &file)
_ = json.Unmarshal(b, &file)
files = append(files, file)
}
return files, nil
Expand Down Expand Up @@ -204,6 +204,10 @@ func (r repository) Comments(ctx context.Context, id string, offset,
if err != nil {
return nil, err
}

if len(results.([]interface{})) == 0 {
return []interface{}{}, nil
}
return results.([]interface{}), nil
}

Expand Down
15 changes: 15 additions & 0 deletions internal/user/repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,9 @@ func (r repository) Likes(ctx context.Context, id string, offset,
if err != nil {
return nil, err
}
if len(results.([]interface{})) == 0 {
return []interface{}{}, nil
}
return results.([]interface{}), nil
}

Expand Down Expand Up @@ -274,6 +277,9 @@ func (r repository) Followers(ctx context.Context, id string, offset,
if err != nil {
return nil, err
}
if len(results.([]interface{})) == 0 {
return []interface{}{}, nil
}
return results.([]interface{}), nil
}

Expand Down Expand Up @@ -304,6 +310,9 @@ func (r repository) Following(ctx context.Context, id string, offset,
if err != nil {
return nil, err
}
if len(results.([]interface{})) == 0 {
return []interface{}{}, nil
}
return results.([]interface{}), nil
}

Expand Down Expand Up @@ -333,6 +342,9 @@ func (r repository) Submissions(ctx context.Context, id string, offset,
if err != nil {
return nil, err
}
if len(results.([]interface{})) == 0 {
return []interface{}{}, nil
}
return results.([]interface{}), nil
}

Expand Down Expand Up @@ -363,6 +375,9 @@ func (r repository) Comments(ctx context.Context, id string, offset,
if err != nil {
return nil, err
}
if len(results.([]interface{})) == 0 {
return []interface{}{}, nil
}
return results.([]interface{}), nil
}

Expand Down

0 comments on commit 72424ca

Please sign in to comment.