diff options
author | Gabriel Arakaki Giovanini <mail@gabrielgio.me> | 2023-06-29 23:33:02 +0200 |
---|---|---|
committer | Gabriel Arakaki Giovanini <mail@gabrielgio.me> | 2023-06-29 23:33:02 +0200 |
commit | 7a414da9a802d5eeee911b3536790a061e1d7503 (patch) | |
tree | e009cd94e72bf908701ca6067833ed14f6860b43 | |
parent | 1ae70dbd9124675d4a510954619b01edd5f1f6c3 (diff) | |
download | lens-7a414da9a802d5eeee911b3536790a061e1d7503.tar.gz lens-7a414da9a802d5eeee911b3536790a061e1d7503.tar.bz2 lens-7a414da9a802d5eeee911b3536790a061e1d7503.zip |
ref: Move all controller under the same folder
Move all controller to the same folder and rename them to service.
Moving them to the same folder allow an easier setup for testing.
-rw-r--r-- | cmd/server/main.go | 7 | ||||
-rw-r--r-- | pkg/components/errors.go | 8 | ||||
-rw-r--r-- | pkg/components/user/controller.go | 1 | ||||
-rw-r--r-- | pkg/service/auth.go (renamed from pkg/components/auth/controller.go) | 17 | ||||
-rw-r--r-- | pkg/service/auth_test.go (renamed from pkg/components/auth/controller_test.go) | 6 | ||||
-rw-r--r-- | pkg/service/errors.go | 8 | ||||
-rw-r--r-- | pkg/service/filesystem.go (renamed from pkg/components/filesystem/controller.go) | 10 | ||||
-rw-r--r-- | pkg/service/main_test.go (renamed from pkg/components/auth/mock_test.go) | 2 | ||||
-rw-r--r-- | pkg/view/auth.go | 6 | ||||
-rw-r--r-- | pkg/view/filesystem.go | 8 |
10 files changed, 35 insertions, 38 deletions
diff --git a/cmd/server/main.go b/cmd/server/main.go index 76cf0c0..702ca6e 100644 --- a/cmd/server/main.go +++ b/cmd/server/main.go @@ -17,12 +17,11 @@ import ( "gorm.io/gorm" "git.sr.ht/~gabrielgio/img" - "git.sr.ht/~gabrielgio/img/pkg/components/auth" - "git.sr.ht/~gabrielgio/img/pkg/components/filesystem" "git.sr.ht/~gabrielgio/img/pkg/database/localfs" "git.sr.ht/~gabrielgio/img/pkg/database/repository" "git.sr.ht/~gabrielgio/img/pkg/database/sql" "git.sr.ht/~gabrielgio/img/pkg/ext" + "git.sr.ht/~gabrielgio/img/pkg/service" "git.sr.ht/~gabrielgio/img/pkg/view" "git.sr.ht/~gabrielgio/img/pkg/worker" ) @@ -97,8 +96,8 @@ func main() { // controller var ( - userController = auth.NewController(userRepository, userRepository, hexKey) - fileSystemController = filesystem.NewController(fileSystemRepository) + userController = service.NewAuthController(userRepository, userRepository, hexKey) + fileSystemController = service.NewFileSystemController(fileSystemRepository) ) // view diff --git a/pkg/components/errors.go b/pkg/components/errors.go deleted file mode 100644 index aedbe88..0000000 --- a/pkg/components/errors.go +++ /dev/null @@ -1,8 +0,0 @@ -package components - -import "errors" - -var ( - NotFound = errors.New("Not found") - InvlidaInput = errors.New("Invalid Input") -) diff --git a/pkg/components/user/controller.go b/pkg/components/user/controller.go deleted file mode 100644 index a00006b..0000000 --- a/pkg/components/user/controller.go +++ /dev/null @@ -1 +0,0 @@ -package user diff --git a/pkg/components/auth/controller.go b/pkg/service/auth.go index 0b08fcc..4358a8a 100644 --- a/pkg/components/auth/controller.go +++ b/pkg/service/auth.go @@ -1,34 +1,33 @@ -package auth +package service import ( "context" "golang.org/x/crypto/bcrypt" - "git.sr.ht/~gabrielgio/img/pkg/components" "git.sr.ht/~gabrielgio/img/pkg/database/repository" "git.sr.ht/~gabrielgio/img/pkg/ext" ) -type Controller struct { +type AuthController struct { authRepository repository.AuthRepository userRepository repository.UserRepository key []byte } -func NewController( +func NewAuthController( authRepository repository.AuthRepository, userRepository repository.UserRepository, key []byte, -) *Controller { - return &Controller{ +) *AuthController { + return &AuthController{ authRepository: authRepository, userRepository: userRepository, key: key, } } -func (c *Controller) Login(ctx context.Context, username, password []byte) ([]byte, error) { +func (c *AuthController) Login(ctx context.Context, username, password []byte) ([]byte, error) { id, err := c.authRepository.GetIDByUsername(ctx, string(username)) if err != nil { return nil, err @@ -52,14 +51,14 @@ func (c *Controller) Login(ctx context.Context, username, password []byte) ([]by // InitialRegister register a initial user, it will validate if there is another // user stored already. If so an error `InvlidaInput` will be returned -func (c *Controller) InitialRegister(ctx context.Context, username, password []byte, path []byte) error { +func (c *AuthController) InitialRegister(ctx context.Context, username, password []byte, path []byte) error { exist, err := c.userRepository.Any(ctx) if err != nil { return err } if exist { - return components.InvlidaInput + return InvlidInput } hash, err := bcrypt.GenerateFromPassword(password, bcrypt.MinCost) diff --git a/pkg/components/auth/controller_test.go b/pkg/service/auth_test.go index b1ca065..35b2475 100644 --- a/pkg/components/auth/controller_test.go +++ b/pkg/service/auth_test.go @@ -1,6 +1,6 @@ //go:build unit -package auth +package service import ( "context" @@ -16,7 +16,7 @@ type ( ctx context.Context authRepository repository.AuthRepository userRepository repository.UserRepository - controller *Controller + controller *AuthController } ) @@ -30,7 +30,7 @@ func setUp() *scene { ctx: context.Background(), authRepository: userRepository, userRepository: userRepository, - controller: NewController(userRepository, userRepository, key), + controller: NewAuthController(userRepository, userRepository, key), } } diff --git a/pkg/service/errors.go b/pkg/service/errors.go new file mode 100644 index 0000000..6598464 --- /dev/null +++ b/pkg/service/errors.go @@ -0,0 +1,8 @@ +package service + +import "errors" + +var ( + NotFound = errors.New("Not found") + InvlidInput = errors.New("Invalid Input") +) diff --git a/pkg/components/filesystem/controller.go b/pkg/service/filesystem.go index 6c613a3..3516ce2 100644 --- a/pkg/components/filesystem/controller.go +++ b/pkg/service/filesystem.go @@ -1,4 +1,4 @@ -package filesystem +package service import ( "io/fs" @@ -10,7 +10,7 @@ import ( ) type ( - Controller struct { + FileSystemController struct { repository repository.FileSystemRepository } @@ -30,8 +30,8 @@ type ( } ) -func NewController(repository repository.FileSystemRepository) *Controller { - return &Controller{ +func NewFileSystemController(repository repository.FileSystemRepository) *FileSystemController { + return &FileSystemController{ repository: repository, } } @@ -63,7 +63,7 @@ func getHistory(filepath string) []*DirectoryParam { return result } -func (self *Controller) GetPage(filepath string) (*Page, error) { +func (self *FileSystemController) GetPage(filepath string) (*Page, error) { decodedPath, err := url.QueryUnescape(filepath) if err != nil { return nil, err diff --git a/pkg/components/auth/mock_test.go b/pkg/service/main_test.go index 885f643..5c10ecd 100644 --- a/pkg/components/auth/mock_test.go +++ b/pkg/service/main_test.go @@ -1,6 +1,6 @@ //go:build unit -package auth +package service import ( "context" diff --git a/pkg/view/auth.go b/pkg/view/auth.go index 3f9e414..6b096d9 100644 --- a/pkg/view/auth.go +++ b/pkg/view/auth.go @@ -6,15 +6,15 @@ import ( "github.com/valyala/fasthttp" "git.sr.ht/~gabrielgio/img" - "git.sr.ht/~gabrielgio/img/pkg/components/auth" "git.sr.ht/~gabrielgio/img/pkg/ext" + "git.sr.ht/~gabrielgio/img/pkg/service" ) type AuthView struct { - userController *auth.Controller + userController *service.AuthController } -func NewAuthView(userController *auth.Controller) *AuthView { +func NewAuthView(userController *service.AuthController) *AuthView { return &AuthView{ userController: userController, } diff --git a/pkg/view/filesystem.go b/pkg/view/filesystem.go index d2ebff4..d598b88 100644 --- a/pkg/view/filesystem.go +++ b/pkg/view/filesystem.go @@ -4,25 +4,25 @@ import ( "github.com/valyala/fasthttp" "git.sr.ht/~gabrielgio/img" - "git.sr.ht/~gabrielgio/img/pkg/components/filesystem" "git.sr.ht/~gabrielgio/img/pkg/database/repository" "git.sr.ht/~gabrielgio/img/pkg/ext" + "git.sr.ht/~gabrielgio/img/pkg/service" ) type ( FileSystemView struct { - controller filesystem.Controller + controller service.FileSystemController settings repository.SettingsRepository } FilePage struct { - Page *filesystem.Page + Page *service.Page ShowMode bool ShowOwner bool } ) func NewFileSystemView( - controller filesystem.Controller, + controller service.FileSystemController, settingsRepository repository.SettingsRepository, ) *FileSystemView { return &FileSystemView{ |