lifedebugger commited on
Commit
2a43be8
·
verified ·
1 Parent(s): 7d9da17

Delete services

Browse files
services/LoginService.go DELETED
@@ -1,47 +0,0 @@
1
- package services
2
-
3
- import (
4
- "errors"
5
-
6
- "api.qobiltu.id/middleware"
7
- "api.qobiltu.id/models"
8
- "api.qobiltu.id/repositories"
9
- )
10
-
11
- type LoginConstructor struct {
12
- Email string
13
- Password string
14
- }
15
-
16
- type AuthenticationService struct {
17
- Service[LoginConstructor, models.AuthenticatedUser]
18
- }
19
-
20
- func (s *AuthenticationService) Authenticate() {
21
- accountData := repositories.GetAccountbyEmail(s.Constructor.Email)
22
- if accountData.NoRecord {
23
- s.Exception.DataNotFound = true
24
- s.Exception.Message = "there is no account with given credentials!"
25
- return
26
- }
27
- if middleware.VerifyPassword(accountData.Result.Password, s.Constructor.Password) != nil {
28
- s.Exception.Unauthorized = true
29
- s.Exception.Message = "incorrect password!"
30
- return
31
- }
32
-
33
- token, err_tok := middleware.GenerateToken(&accountData.Result)
34
-
35
- if err_tok != nil {
36
- s.Error = errors.Join(s.Error, err_tok)
37
- }
38
-
39
- accountData.Result.Password = "SECRET"
40
- s.Result = models.AuthenticatedUser{
41
- Account: accountData.Result,
42
- Token: token,
43
- }
44
- s.Error = accountData.RowsError
45
- }
46
-
47
- // LoginHandler handles user login
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
services/RegisterService.go DELETED
@@ -1,40 +0,0 @@
1
- package services
2
-
3
- import (
4
- "errors"
5
-
6
- "api.qobiltu.id/middleware"
7
- "api.qobiltu.id/models"
8
- "api.qobiltu.id/repositories"
9
- uuid "github.com/satori/go.uuid"
10
- "gorm.io/gorm"
11
- )
12
-
13
- type RegisterService struct {
14
- Service[models.Account, models.Account]
15
- }
16
-
17
- func (s *RegisterService) Create() {
18
- if len(s.Constructor.Password) < 8 {
19
- s.Exception.InvalidPasswordLength = true
20
- s.Exception.Message = "Password must have at least 8 characters!"
21
- return
22
- }
23
- hashed_password, err_hash := middleware.HashPassword(s.Constructor.Password)
24
- s.Error = err_hash
25
- s.Constructor.Password = hashed_password
26
- s.Constructor.UUID = uuid.NewV4()
27
- accountCreated := repositories.CreateAccount(s.Constructor)
28
- if errors.Is(accountCreated.RowsError, gorm.ErrDuplicatedKey) {
29
- s.Exception.DataDuplicate = true
30
- s.Exception.Message = "Account with email " + s.Constructor.Email + " already exists!"
31
- return
32
- } else if errors.Is(accountCreated.RowsError, gorm.ErrModelAccessibleFieldsRequired) || errors.Is(accountCreated.RowsError, gorm.ErrInvalidData) || errors.Is(accountCreated.RowsError, gorm.ErrInvalidValue) || errors.Is(accountCreated.RowsError, gorm.ErrInvalidField) {
33
- s.Exception.BadRequest = true
34
- s.Exception.Message = "Bad request!"
35
- return
36
- }
37
- s.Error = accountCreated.RowsError
38
- s.Result = accountCreated.Result
39
- s.Result.Password = "SECRET"
40
- }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
services/login_service.go DELETED
@@ -1,47 +0,0 @@
1
- package services
2
-
3
- import (
4
- "errors"
5
-
6
- "api.qobiltu.id/middleware"
7
- "api.qobiltu.id/models"
8
- "api.qobiltu.id/repositories"
9
- )
10
-
11
- type LoginConstructor struct {
12
- Email string
13
- Password string
14
- }
15
-
16
- type AuthenticationService struct {
17
- Service[LoginConstructor, models.AuthenticatedUser]
18
- }
19
-
20
- func (s *AuthenticationService) Authenticate() {
21
- accountData := repositories.GetAccountbyEmail(s.Constructor.Email)
22
- if accountData.NoRecord {
23
- s.Exception.DataNotFound = true
24
- s.Exception.Message = "there is no account with given credentials!"
25
- return
26
- }
27
- if middleware.VerifyPassword(accountData.Result.Password, s.Constructor.Password) != nil {
28
- s.Exception.Unauthorized = true
29
- s.Exception.Message = "incorrect password!"
30
- return
31
- }
32
-
33
- token, err_tok := middleware.GenerateToken(&accountData.Result)
34
-
35
- if err_tok != nil {
36
- s.Error = errors.Join(s.Error, err_tok)
37
- }
38
-
39
- accountData.Result.Password = "SECRET"
40
- s.Result = models.AuthenticatedUser{
41
- Account: accountData.Result,
42
- Token: token,
43
- }
44
- s.Error = accountData.RowsError
45
- }
46
-
47
- // LoginHandler handles user login
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
services/register_service.go DELETED
@@ -1,40 +0,0 @@
1
- package services
2
-
3
- import (
4
- "errors"
5
-
6
- "api.qobiltu.id/middleware"
7
- "api.qobiltu.id/models"
8
- "api.qobiltu.id/repositories"
9
- uuid "github.com/satori/go.uuid"
10
- "gorm.io/gorm"
11
- )
12
-
13
- type RegisterService struct {
14
- Service[models.Account, models.Account]
15
- }
16
-
17
- func (s *RegisterService) Create() {
18
- if len(s.Constructor.Password) < 8 {
19
- s.Exception.InvalidPasswordLength = true
20
- s.Exception.Message = "Password must have at least 8 characters!"
21
- return
22
- }
23
- hashed_password, err_hash := middleware.HashPassword(s.Constructor.Password)
24
- s.Error = err_hash
25
- s.Constructor.Password = hashed_password
26
- s.Constructor.UUID = uuid.NewV4()
27
- accountCreated := repositories.CreateAccount(s.Constructor)
28
- if errors.Is(accountCreated.RowsError, gorm.ErrDuplicatedKey) {
29
- s.Exception.DataDuplicate = true
30
- s.Exception.Message = "Account with email " + s.Constructor.Email + " already exists!"
31
- return
32
- } else if errors.Is(accountCreated.RowsError, gorm.ErrModelAccessibleFieldsRequired) || errors.Is(accountCreated.RowsError, gorm.ErrInvalidData) || errors.Is(accountCreated.RowsError, gorm.ErrInvalidValue) || errors.Is(accountCreated.RowsError, gorm.ErrInvalidField) {
33
- s.Exception.BadRequest = true
34
- s.Exception.Message = "Bad request!"
35
- return
36
- }
37
- s.Error = accountCreated.RowsError
38
- s.Result = accountCreated.Result
39
- s.Result.Password = "SECRET"
40
- }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
services/service.go DELETED
@@ -1,31 +0,0 @@
1
- package services
2
-
3
- import "api.qobiltu.id/models"
4
-
5
- type (
6
- Services interface {
7
- Retrieve()
8
- Update()
9
- Create()
10
- Delete()
11
- Validate()
12
- Authenticate()
13
- Authorize()
14
- }
15
- Service[TConstructor any, TResult any] struct {
16
- Constructor TConstructor
17
- Result TResult
18
- Exception models.Exception
19
- Error error
20
- }
21
- )
22
-
23
- func Construct[TConstructor any, TResult any](constructor ...TConstructor) *Service[TConstructor, TResult] {
24
- if len(constructor) == 1 {
25
- return &Service[TConstructor, TResult]{}
26
- }
27
-
28
- return &Service[TConstructor, TResult]{
29
- Constructor: constructor[0],
30
- }
31
- }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
services/services.go DELETED
@@ -1,31 +0,0 @@
1
- package services
2
-
3
- import "api.qobiltu.id/models"
4
-
5
- type (
6
- Services interface {
7
- Retrieve()
8
- Update()
9
- Create()
10
- Delete()
11
- Validate()
12
- Authenticate()
13
- Authorize()
14
- }
15
- Service[TConstructor any, TResult any] struct {
16
- Constructor TConstructor
17
- Result TResult
18
- Exception models.Exception
19
- Error error
20
- }
21
- )
22
-
23
- func Construct[TConstructor any, TResult any](constructor ...TConstructor) *Service[TConstructor, TResult] {
24
- if len(constructor) == 1 {
25
- return &Service[TConstructor, TResult]{}
26
- }
27
-
28
- return &Service[TConstructor, TResult]{
29
- Constructor: constructor[0],
30
- }
31
- }