feat: use uuid instead of unit type ids
This commit is contained in:
parent
4c2c91a2bd
commit
173a55137f
|
@ -5,6 +5,7 @@ import (
|
||||||
|
|
||||||
"github.com/authorizerdev/authorizer/server/constants"
|
"github.com/authorizerdev/authorizer/server/constants"
|
||||||
"github.com/authorizerdev/authorizer/server/enum"
|
"github.com/authorizerdev/authorizer/server/enum"
|
||||||
|
"github.com/google/uuid"
|
||||||
"gorm.io/driver/mysql"
|
"gorm.io/driver/mysql"
|
||||||
"gorm.io/driver/postgres"
|
"gorm.io/driver/postgres"
|
||||||
"gorm.io/driver/sqlite"
|
"gorm.io/driver/sqlite"
|
||||||
|
@ -18,7 +19,7 @@ type Manager interface {
|
||||||
GetUsers() ([]User, error)
|
GetUsers() ([]User, error)
|
||||||
GetUserByEmail(email string) (User, error)
|
GetUserByEmail(email string) (User, error)
|
||||||
GetUserByID(email string) (User, error)
|
GetUserByID(email string) (User, error)
|
||||||
UpdateVerificationTime(verifiedAt int64, id uint) error
|
UpdateVerificationTime(verifiedAt int64, id uuid.UUID) error
|
||||||
AddVerification(verification VerificationRequest) (VerificationRequest, error)
|
AddVerification(verification VerificationRequest) (VerificationRequest, error)
|
||||||
GetVerificationByToken(token string) (VerificationRequest, error)
|
GetVerificationByToken(token string) (VerificationRequest, error)
|
||||||
DeleteToken(email string) error
|
DeleteToken(email string) error
|
||||||
|
|
|
@ -1,15 +1,31 @@
|
||||||
package db
|
package db
|
||||||
|
|
||||||
import "log"
|
import (
|
||||||
|
"log"
|
||||||
|
|
||||||
|
"github.com/google/uuid"
|
||||||
|
"gorm.io/gorm"
|
||||||
|
"gorm.io/gorm/clause"
|
||||||
|
)
|
||||||
|
|
||||||
type Role struct {
|
type Role struct {
|
||||||
ID uint `gorm:"primaryKey"`
|
ID uuid.UUID `gorm:"type:uuid;"`
|
||||||
Role string
|
Role string `gorm:"unique"`
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *Role) BeforeCreate(tx *gorm.DB) (err error) {
|
||||||
|
r.ID = uuid.New()
|
||||||
|
|
||||||
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// SaveRoles function to save roles
|
// SaveRoles function to save roles
|
||||||
func (mgr *manager) SaveRoles(roles []Role) error {
|
func (mgr *manager) SaveRoles(roles []Role) error {
|
||||||
res := mgr.db.Create(&roles)
|
res := mgr.db.Clauses(
|
||||||
|
clause.OnConflict{
|
||||||
|
OnConstraint: "authorizer_roles_role_key",
|
||||||
|
DoNothing: true,
|
||||||
|
}).Create(&roles)
|
||||||
if res.Error != nil {
|
if res.Error != nil {
|
||||||
log.Println(`Error saving roles`)
|
log.Println(`Error saving roles`)
|
||||||
return res.Error
|
return res.Error
|
||||||
|
|
|
@ -4,11 +4,13 @@ import (
|
||||||
"log"
|
"log"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"github.com/google/uuid"
|
||||||
|
"gorm.io/gorm"
|
||||||
"gorm.io/gorm/clause"
|
"gorm.io/gorm/clause"
|
||||||
)
|
)
|
||||||
|
|
||||||
type User struct {
|
type User struct {
|
||||||
ID uint `gorm:"primaryKey"`
|
ID uuid.UUID `gorm:"type:uuid;"`
|
||||||
FirstName string
|
FirstName string
|
||||||
LastName string
|
LastName string
|
||||||
Email string `gorm:"unique"`
|
Email string `gorm:"unique"`
|
||||||
|
@ -21,6 +23,12 @@ type User struct {
|
||||||
Roles string
|
Roles string
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (u *User) BeforeCreate(tx *gorm.DB) (err error) {
|
||||||
|
u.ID = uuid.New()
|
||||||
|
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
// SaveUser function to add user even with email conflict
|
// SaveUser function to add user even with email conflict
|
||||||
func (mgr *manager) SaveUser(user User) (User, error) {
|
func (mgr *manager) SaveUser(user User) (User, error) {
|
||||||
result := mgr.db.Clauses(
|
result := mgr.db.Clauses(
|
||||||
|
@ -42,7 +50,7 @@ func (mgr *manager) UpdateUser(user User) (User, error) {
|
||||||
result := mgr.db.Clauses(
|
result := mgr.db.Clauses(
|
||||||
clause.OnConflict{
|
clause.OnConflict{
|
||||||
UpdateAll: true,
|
UpdateAll: true,
|
||||||
Columns: []clause.Column{{Name: "id"}},
|
Columns: []clause.Column{{Name: "email"}},
|
||||||
}).Create(&user)
|
}).Create(&user)
|
||||||
|
|
||||||
if result.Error != nil {
|
if result.Error != nil {
|
||||||
|
@ -85,7 +93,7 @@ func (mgr *manager) GetUserByID(id string) (User, error) {
|
||||||
return user, nil
|
return user, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (mgr *manager) UpdateVerificationTime(verifiedAt int64, id uint) error {
|
func (mgr *manager) UpdateVerificationTime(verifiedAt int64, id uuid.UUID) error {
|
||||||
user := &User{
|
user := &User{
|
||||||
ID: id,
|
ID: id,
|
||||||
}
|
}
|
||||||
|
|
|
@ -3,12 +3,14 @@ package db
|
||||||
import (
|
import (
|
||||||
"log"
|
"log"
|
||||||
|
|
||||||
|
"github.com/google/uuid"
|
||||||
|
"gorm.io/gorm"
|
||||||
"gorm.io/gorm/clause"
|
"gorm.io/gorm/clause"
|
||||||
)
|
)
|
||||||
|
|
||||||
type VerificationRequest struct {
|
type VerificationRequest struct {
|
||||||
ID uint `gorm:"primaryKey"`
|
ID uuid.UUID `gorm:"type:uuid;"`
|
||||||
Token string `gorm:"index"`
|
Token string `gorm:"index"`
|
||||||
Identifier string
|
Identifier string
|
||||||
ExpiresAt int64
|
ExpiresAt int64
|
||||||
CreatedAt int64 `gorm:"autoCreateTime"`
|
CreatedAt int64 `gorm:"autoCreateTime"`
|
||||||
|
@ -16,6 +18,12 @@ type VerificationRequest struct {
|
||||||
Email string `gorm:"unique"`
|
Email string `gorm:"unique"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (v *VerificationRequest) BeforeCreate(tx *gorm.DB) (err error) {
|
||||||
|
v.ID = uuid.New()
|
||||||
|
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
// AddVerification function to add verification record
|
// AddVerification function to add verification record
|
||||||
func (mgr *manager) AddVerification(verification VerificationRequest) (VerificationRequest, error) {
|
func (mgr *manager) AddVerification(verification VerificationRequest) (VerificationRequest, error) {
|
||||||
result := mgr.db.Clauses(clause.OnConflict{
|
result := mgr.db.Clauses(clause.OnConflict{
|
||||||
|
|
Loading…
Reference in New Issue
Block a user