File size: 1,377 Bytes
3f70c4e
 
 
 
 
8b40e41
 
 
6f9e96f
3f70c4e
 
 
 
 
 
 
 
49102c7
 
 
 
 
6596fcc
 
3f70c4e
6f9e96f
3f70c4e
 
 
c991d6d
3f70c4e
 
 
 
 
 
6596fcc
3f70c4e
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
package services

import (
	"errors"

	"api.qobiltu.id/middleware"
	"api.qobiltu.id/models"
	"api.qobiltu.id/repositories"
	uuid "github.com/satori/go.uuid"
	"gorm.io/gorm"
)

type RegisterService struct {
	Service[models.Account, models.Account]
}

func (s *RegisterService) Create() {
	if len(s.Constructor.Password) < 8 {
		s.Exception.InvalidPasswordLength = true
		s.Exception.Message = "Password must have at least 8 characters!"
		return
	}
	hashed_password, err_hash := middleware.HashPassword(s.Constructor.Password)
	s.Error = err_hash
	s.Constructor.Password = hashed_password
	s.Constructor.UUID = uuid.NewV4()
	accountCreated := repositories.CreateAccount(s.Constructor)
	if errors.Is(accountCreated.RowsError, gorm.ErrDuplicatedKey) {
		s.Exception.DataDuplicate = true
		s.Exception.Message = "Account with email " + s.Constructor.Email + " already exists!"
		return
	} 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) {
		s.Exception.BadRequest = true
		s.Exception.Message = "Bad request!"
		return
	}
	s.Error = accountCreated.RowsError
	s.Result = accountCreated.Result
	s.Result.Password = "SECRET"
}