From 7a414da9a802d5eeee911b3536790a061e1d7503 Mon Sep 17 00:00:00 2001 From: Gabriel Arakaki Giovanini Date: Thu, 29 Jun 2023 23:33:02 +0200 Subject: 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. --- pkg/components/auth/controller.go | 77 --------------------------------------- 1 file changed, 77 deletions(-) delete mode 100644 pkg/components/auth/controller.go (limited to 'pkg/components/auth/controller.go') diff --git a/pkg/components/auth/controller.go b/pkg/components/auth/controller.go deleted file mode 100644 index 0b08fcc..0000000 --- a/pkg/components/auth/controller.go +++ /dev/null @@ -1,77 +0,0 @@ -package auth - -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 { - authRepository repository.AuthRepository - userRepository repository.UserRepository - key []byte -} - -func NewController( - authRepository repository.AuthRepository, - userRepository repository.UserRepository, - key []byte, -) *Controller { - return &Controller{ - authRepository: authRepository, - userRepository: userRepository, - key: key, - } -} - -func (c *Controller) Login(ctx context.Context, username, password []byte) ([]byte, error) { - id, err := c.authRepository.GetIDByUsername(ctx, string(username)) - if err != nil { - return nil, err - } - - hashedPassword, err := c.authRepository.GetPassword(ctx, id) - if err != nil { - return nil, err - } - - if err := bcrypt.CompareHashAndPassword(hashedPassword, password); err != nil { - return nil, err - } - - token := &ext.Token{ - UserID: id, - Username: string(username), - } - return ext.WriteToken(token, c.key) -} - -// 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 { - exist, err := c.userRepository.Any(ctx) - if err != nil { - return err - } - - if exist { - return components.InvlidaInput - } - - hash, err := bcrypt.GenerateFromPassword(password, bcrypt.MinCost) - if err != nil { - return err - } - - _, err = c.userRepository.Create(ctx, &repository.CreateUser{ - Username: string(username), - Password: hash, - Path: string(path), - }) - - return err -} -- cgit v1.2.3