init push
This commit is contained in:
55
internal/domain/user/model.go
Normal file
55
internal/domain/user/model.go
Normal file
@@ -0,0 +1,55 @@
|
||||
package user
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
"gorm.io/gorm"
|
||||
|
||||
filemanager "wucher/internal/domain/file_manager"
|
||||
"wucher/internal/shared/pkg/uuidv7"
|
||||
)
|
||||
|
||||
// User maps to the "users" table (shared with auth).
|
||||
type User struct {
|
||||
ID []byte `gorm:"type:binary(16);primaryKey;column:id"`
|
||||
RoleID []byte `gorm:"type:binary(16);index;not null;column:role_id"`
|
||||
RoleIDs [][]byte `gorm:"-"`
|
||||
ImageAttachmentID []byte `gorm:"type:binary(16);index;column:image_attachment_id"`
|
||||
ProfileAttachmentID []byte `gorm:"type:binary(16);index;column:profile_attachment_id"`
|
||||
Email string `gorm:"type:varchar(191);uniqueIndex;not null;column:email"`
|
||||
SSOEmail *string `gorm:"type:varchar(191);uniqueIndex;column:sso_email"`
|
||||
Username string `gorm:"type:varchar(100);index;column:username"`
|
||||
FirstName string `gorm:"type:varchar(100);not null;column:first_name"`
|
||||
LastName string `gorm:"type:varchar(100);not null;column:last_name"`
|
||||
MobilePhone string `gorm:"type:varchar(32);index;column:mobile_phone"`
|
||||
Timezone string `gorm:"type:varchar(64);not null;default:UTC;column:timezone"`
|
||||
Roles []RoleSummary `gorm:"-"`
|
||||
RoleName string `gorm:"->;column:role_name"`
|
||||
RoleDescription string `gorm:"->;column:role_description"`
|
||||
EmailVerifiedAt *time.Time `gorm:"column:email_verified_at"`
|
||||
IsActive bool `gorm:"not null;default:true;column:is_active"`
|
||||
|
||||
ImageAttachment *filemanager.Attachment `gorm:"foreignKey:ImageAttachmentID;references:ID;constraint:OnUpdate:CASCADE,OnDelete:SET NULL"`
|
||||
ProfileAttachment *filemanager.Attachment `gorm:"foreignKey:ProfileAttachmentID;references:ID;constraint:OnUpdate:CASCADE,OnDelete:SET NULL"`
|
||||
ProfileAttachmentPatchSet bool `gorm:"-"`
|
||||
|
||||
CreatedAt time.Time
|
||||
CreatedBy []byte `gorm:"type:binary(16);column:created_by"`
|
||||
UpdatedAt time.Time
|
||||
UpdatedBy []byte `gorm:"type:binary(16);column:updated_by"`
|
||||
}
|
||||
|
||||
type RoleSummary struct {
|
||||
ID []byte `gorm:"column:id"`
|
||||
Name string `gorm:"column:name"`
|
||||
Description string `gorm:"column:description"`
|
||||
}
|
||||
|
||||
func (User) TableName() string { return "users" }
|
||||
|
||||
func (u *User) BeforeCreate(tx *gorm.DB) error {
|
||||
if len(u.ID) == 0 {
|
||||
u.ID = uuidv7.MustBytes()
|
||||
}
|
||||
return nil
|
||||
}
|
||||
37
internal/domain/user/model_test.go
Normal file
37
internal/domain/user/model_test.go
Normal file
@@ -0,0 +1,37 @@
|
||||
package user
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
func TestUserTableName(t *testing.T) {
|
||||
var u User
|
||||
if got := u.TableName(); got != "users" {
|
||||
t.Fatalf("unexpected table name: %s", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestUserBeforeCreate(t *testing.T) {
|
||||
t.Run("generates id when empty", func(t *testing.T) {
|
||||
u := &User{}
|
||||
if err := u.BeforeCreate(&gorm.DB{}); err != nil {
|
||||
t.Fatalf("BeforeCreate error: %v", err)
|
||||
}
|
||||
if len(u.ID) != 16 {
|
||||
t.Fatalf("expected generated 16-byte ID, got %d", len(u.ID))
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("keeps existing id", func(t *testing.T) {
|
||||
existing := []byte("1234567890abcdef")
|
||||
u := &User{ID: append([]byte(nil), existing...)}
|
||||
if err := u.BeforeCreate(&gorm.DB{}); err != nil {
|
||||
t.Fatalf("BeforeCreate error: %v", err)
|
||||
}
|
||||
if string(u.ID) != string(existing) {
|
||||
t.Fatalf("expected existing id to stay unchanged")
|
||||
}
|
||||
})
|
||||
}
|
||||
14
internal/domain/user/repository.go
Normal file
14
internal/domain/user/repository.go
Normal file
@@ -0,0 +1,14 @@
|
||||
package user
|
||||
|
||||
import "context"
|
||||
|
||||
type Repository interface {
|
||||
Create(ctx context.Context, user *User) error
|
||||
Update(ctx context.Context, user *User) error
|
||||
Delete(ctx context.Context, id []byte) error
|
||||
GetByID(ctx context.Context, id []byte) (*User, error)
|
||||
GetByEmail(ctx context.Context, email string) (*User, error)
|
||||
GetByUsername(ctx context.Context, username string) (*User, error)
|
||||
List(ctx context.Context, filter string, sort string, limit, offset int) ([]User, int64, error)
|
||||
GetLastNameByID(ctx context.Context, id []byte) (string, error)
|
||||
}
|
||||
13
internal/domain/user/service.go
Normal file
13
internal/domain/user/service.go
Normal file
@@ -0,0 +1,13 @@
|
||||
package user
|
||||
|
||||
import "context"
|
||||
|
||||
type Service interface {
|
||||
Create(ctx context.Context, user *User) error
|
||||
Update(ctx context.Context, user *User) error
|
||||
Delete(ctx context.Context, id []byte) error
|
||||
GetByID(ctx context.Context, id []byte) (*User, error)
|
||||
GetByEmail(ctx context.Context, email string) (*User, error)
|
||||
GetByUsername(ctx context.Context, username string) (*User, error)
|
||||
List(ctx context.Context, filter string, sort string, limit, offset int) ([]User, int64, error)
|
||||
}
|
||||
Reference in New Issue
Block a user