-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Parisa Samimi
committed
Mar 21, 2024
1 parent
b0bc3df
commit 22f88ee
Showing
10 changed files
with
324 additions
and
41 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
package getfiles | ||
|
||
type Request struct { | ||
Page uint | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
119
backend/application/dashboard/file/getFiles/useCase_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.