api-qobiltu-dev / space /services /RegisterService.go
lifedebugger's picture
Deploy files from GitHub repository
8b40e41
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"
}