Skip to content

Commit

Permalink
files list API
Browse files Browse the repository at this point in the history
  • Loading branch information
Parisa Samimi committed Mar 21, 2024
1 parent b0bc3df commit 22f88ee
Show file tree
Hide file tree
Showing 10 changed files with 324 additions and 41 deletions.
8 changes: 8 additions & 0 deletions backend/application/article/getArticle/useCase_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,14 @@ func (r *MockArticlesRepository) GetOne(UUID string) (article.Article, error) {
return article.Article{}, nil
}

func (r *MockArticlesRepository) IncreaseView(UUID string, inc uint) error {
return nil
}

func (r *MockArticlesRepository) GetByUUIDs(UUIDS []string) ([]article.Article, error) {
return nil, nil
}

type MockElementsRepository struct {
element.Repository

Expand Down
44 changes: 3 additions & 41 deletions backend/application/article/getArticlesByHashtag/useCase_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,36 +16,9 @@ func TestUseCase_GetArticles(t *testing.T) {
request := Request{Page: 1, Hashtag: "test"}
response, err := usecase.GetArticlesByHashtag(&request)

if repository.GetCountCount != 1 {
t.Errorf("unexpected number of calls %d", repository.GetCountCount)
}

if repository.GetByHashtagCount != 1 {
t.Errorf("unexpected number of calls %d", repository.GetByHashtagCount)
}

if response == nil {
t.Error("unexpected response")
}

if err != nil {
t.Error("unexpected error")
}
})

t.Run("returns an error on counting items", func(t *testing.T) {
repository := MockArticlesRepository{
GetCountErr: errors.New("error on counting"),
}

usecase := NewUseCase(&repository)

request := Request{Page: 1, Hashtag: "test"}
response, err := usecase.GetArticlesByHashtag(&request)

if repository.GetCountCount != 1 {
t.Errorf("unexpected number of calls %d", repository.GetCountCount)
}

if repository.GetByHashtagCount != 1 {
t.Errorf("unexpected number of calls %d", repository.GetByHashtagCount)
Expand All @@ -54,10 +27,6 @@ func TestUseCase_GetArticles(t *testing.T) {
if response == nil {
t.Error("unexpected response")
}

if err != nil {
t.Error("expects an error")
}
})

t.Run("returns an error on getting items", func(t *testing.T) {
Expand All @@ -70,21 +39,17 @@ func TestUseCase_GetArticles(t *testing.T) {
request := Request{Page: 1, Hashtag: "test"}
response, err := usecase.GetArticlesByHashtag(&request)

if repository.GetCountCount != 1 {
t.Errorf("unexpected number of calls %d", repository.GetCountCount)
if err == nil {
t.Error("expects an error")
}

if repository.GetByHashtagCount != 0 {
if repository.GetByHashtagCount != 1 {
t.Errorf("unexpected number of calls %d", repository.GetByHashtagCount)
}

if response != nil {
t.Error("unexpected response")
}

if err == nil {
t.Error("expects an error")
}
})
}

Expand All @@ -93,9 +58,6 @@ type MockArticlesRepository struct {

GetByHashtagCount uint
GetByHashtagErr error

GetCountCount uint
GetCountErr error
}

func (r *MockArticlesRepository) GetByHashtag(hashtags []string, offset uint, limit uint) ([]article.Article, error) {
Expand Down
5 changes: 5 additions & 0 deletions backend/application/dashboard/file/getFiles/request.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package getfiles

type Request struct {
Page uint
}
44 changes: 44 additions & 0 deletions backend/application/dashboard/file/getFiles/response.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package getfiles

import (
"time"

"github.com/khanzadimahdi/testproject/domain/file"
)

type fileResponse struct {
UUID string `json:"uuid"`
Name string `bson:"name"`
Size int64 `bson:"size"`
OwnerUUID string `bson:"owner_uuid"`
CreatedAt time.Time `bson:"created_at"`
}

type GetFilesReponse struct {
Items []fileResponse `json:"items"`
Pagination pagination `json:"pagination"`
}

type pagination struct {
TotalPages uint `json:"total_pages"`
CurrentPage uint `json:"current_page"`
}

func NewGetFilesReponse(a []file.File, totalPages, currentPage uint) *GetFilesReponse {
items := make([]fileResponse, len(a))

for i := range a {
items[i].UUID = a[i].UUID
items[i].Name = a[i].Name
items[i].Size = a[i].Size
items[i].OwnerUUID = a[i].OwnerUUID
}

return &GetFilesReponse{
Items: items,
Pagination: pagination{
TotalPages: totalPages,
CurrentPage: currentPage,
},
}
}
119 changes: 119 additions & 0 deletions backend/application/dashboard/file/getFiles/useCase_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
package getfiles

import (
"errors"
"testing"

"github.com/khanzadimahdi/testproject/domain/file"
)

func TestUseCase_GetFiles(t *testing.T) {
t.Run("returns files", func(t *testing.T) {
repository := MockFilesRepository{}

usecase := NewUseCase(&repository)

request := Request{Page: 1}
response, err := usecase.GetFiles(&request)

if repository.GetCountCount != 1 {
t.Errorf("unexpected number of calls %d", repository.GetCountCount)
}

if repository.GetAllCount != 1 {
t.Errorf("unexpected number of calls %d", repository.GetAllCount)
}

if response == nil {
t.Error("unexpected response")
}

if err != nil {
t.Error("unexpected error")
}
})

t.Run("returns an error on counting items", func(t *testing.T) {
repository := MockFilesRepository{
GetCountErr: errors.New("error on counting"),
}

usecase := NewUseCase(&repository)

request := Request{Page: 1}
response, err := usecase.GetFiles(&request)

if repository.GetCountCount != 1 {
t.Errorf("unexpected number of calls %d", repository.GetCountCount)
}

if repository.GetAllCount != 1 {
t.Errorf("unexpected number of calls %d", repository.GetAllCount)
}

if response == nil {
t.Error("unexpected response")
}

if err != nil {
t.Error("expects an error")
}
})

t.Run("returns an error on getting items", func(t *testing.T) {
repository := MockFilesRepository{
GetAllErr: errors.New("article not found"),
}

usecase := NewUseCase(&repository)

request := Request{Page: 1}
response, err := usecase.GetFiles(&request)

if repository.GetCountCount != 1 {
t.Errorf("unexpected number of calls %d", repository.GetCountCount)
}

if repository.GetAllCount != 0 {
t.Errorf("unexpected number of calls %d", repository.GetAllCount)
}

if response != nil {
t.Error("unexpected response")
}

if err == nil {
t.Error("expects an error")
}
})
}

type MockFilesRepository struct {
file.Repository

GetAllCount uint
GetAllErr error

GetCountCount uint
GetCountErr error
}

func (r *MockFilesRepository) GetAll(offset uint, limit uint) ([]file.File, error) {
r.GetAllCount++

if r.GetAllErr != nil {
return nil, r.GetAllErr
}

return []file.File{}, nil
}

func (r *MockFilesRepository) Count() (uint, error) {
r.GetCountCount++

if r.GetAllErr != nil {
return 0, r.GetAllErr
}

return 1, nil
}
43 changes: 43 additions & 0 deletions backend/application/dashboard/file/getFiles/usecase.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package getfiles

import (
"github.com/khanzadimahdi/testproject/domain/file"
)

const limit = 10

type UseCase struct {
fileRepository file.Repository
}

func NewUseCase(fileRepository file.Repository) *UseCase {
return &UseCase{
fileRepository: fileRepository,
}
}

func (uc *UseCase) GetFiles(request *Request) (*GetFilesReponse, error) {
totalFiles, err := uc.fileRepository.Count()
if err != nil {
return nil, err
}

currentPage := request.Page
var offset uint = 0
if currentPage > 0 {
offset = (currentPage - 1) * limit
}

totalPages := totalFiles / limit

if (totalPages * limit) != totalFiles {
totalPages++
}

a, err := uc.fileRepository.GetAll(offset, limit)
if err != nil {
return nil, err
}

return NewGetFilesReponse(a, totalPages, currentPage), nil
}
2 changes: 2 additions & 0 deletions backend/domain/file/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,11 @@ type File struct {
}

type Repository interface {
GetAll(offset uint, limit uint) ([]File, error)
GetOne(UUID string) (File, error)
Save(*File) (string, error)
Delete(UUID string) error
Count() (uint, error)
}

type Storage interface {
Expand Down
53 changes: 53 additions & 0 deletions backend/infrastructure/repository/mongodb/files/repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,47 @@ func NewFilesRepository(database *mongo.Database) *FilesRepository {
}
}

func (r *FilesRepository) GetAll(offset uint, limit uint) ([]file.File, error) {
ctx, cancel := context.WithTimeout(context.Background(), queryTimeout)
defer cancel()

o := int64(offset)
l := int64(limit)
desc := bson.D{{Key: "_id", Value: -1}}
cur, err := r.collection.Find(ctx, bson.D{}, &options.FindOptions{
Skip: &o,
Limit: &l,
Sort: desc,
})

if err != nil {
return nil, err
}

defer cur.Close(ctx)

items := make([]file.File, 0, limit)
for cur.Next(ctx) {
var a FileBson

if err := cur.Decode(&a); err != nil {
return nil, err
}
items = append(items, file.File{
UUID: a.UUID,
Name: a.Name,
Size: a.Size,
OwnerUUID: a.OwnerUUID,
})
}

if err := cur.Err(); err != nil {
return nil, err
}

return items, nil
}

func (r *FilesRepository) GetOne(UUID string) (file.File, error) {
ctx, cancel := context.WithTimeout(context.Background(), queryTimeout)
defer cancel()
Expand Down Expand Up @@ -96,3 +137,15 @@ func (r *FilesRepository) Delete(UUID string) error {

return err
}

func (r *FilesRepository) Count() (uint, error) {
ctx, cancel := context.WithTimeout(context.Background(), queryTimeout)
defer cancel()

c, err := r.collection.CountDocuments(ctx, bson.D{}, nil)
if err != nil {
return uint(c), err
}

return uint(c), nil
}
Loading

0 comments on commit 22f88ee

Please sign in to comment.