init push
This commit is contained in:
32
internal/domain/vocation/model.go
Normal file
32
internal/domain/vocation/model.go
Normal file
@@ -0,0 +1,32 @@
|
||||
package vocation
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
"gorm.io/gorm"
|
||||
|
||||
"wucher/internal/shared/pkg/uuidv7"
|
||||
)
|
||||
|
||||
type Vocation struct {
|
||||
ID []byte `gorm:"type:binary(16);primaryKey;column:id"`
|
||||
Name string `gorm:"type:varchar(64);uniqueIndex;not null;column:name"`
|
||||
SortKey *int `gorm:"column:sortkey"`
|
||||
Note string `gorm:"type:varchar(255);column:note"`
|
||||
IsActive bool `gorm:"type:tinyint(1);index;not null;default:1;column:is_active"`
|
||||
CreatedAt time.Time
|
||||
CreatedBy []byte `gorm:"type:binary(16);column:created_by"`
|
||||
UpdatedAt time.Time
|
||||
UpdatedBy []byte `gorm:"type:binary(16);column:updated_by"`
|
||||
DeletedAt *time.Time `gorm:"column:deleted_at"`
|
||||
DeletedBy []byte `gorm:"type:binary(16);column:deleted_by"`
|
||||
}
|
||||
|
||||
func (Vocation) TableName() string { return "vocations" }
|
||||
|
||||
func (v *Vocation) BeforeCreate(tx *gorm.DB) error {
|
||||
if len(v.ID) == 0 {
|
||||
v.ID = uuidv7.MustBytes()
|
||||
}
|
||||
return nil
|
||||
}
|
||||
37
internal/domain/vocation/model_test.go
Normal file
37
internal/domain/vocation/model_test.go
Normal file
@@ -0,0 +1,37 @@
|
||||
package vocation
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
func TestVocationTableName(t *testing.T) {
|
||||
var m Vocation
|
||||
if got := m.TableName(); got != "vocations" {
|
||||
t.Fatalf("expected table name vocations, got %q", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestVocationBeforeCreate(t *testing.T) {
|
||||
t.Run("generates id when empty", func(t *testing.T) {
|
||||
m := &Vocation{}
|
||||
if err := m.BeforeCreate(&gorm.DB{}); err != nil {
|
||||
t.Fatalf("unexpected error: %v", err)
|
||||
}
|
||||
if len(m.ID) == 0 {
|
||||
t.Fatalf("expected id to be generated")
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("keeps existing id", func(t *testing.T) {
|
||||
existing := []byte("already-set-id")
|
||||
m := &Vocation{ID: append([]byte(nil), existing...)}
|
||||
if err := m.BeforeCreate(&gorm.DB{}); err != nil {
|
||||
t.Fatalf("unexpected error: %v", err)
|
||||
}
|
||||
if string(m.ID) != string(existing) {
|
||||
t.Fatalf("expected existing id to stay unchanged")
|
||||
}
|
||||
})
|
||||
}
|
||||
11
internal/domain/vocation/repository.go
Normal file
11
internal/domain/vocation/repository.go
Normal file
@@ -0,0 +1,11 @@
|
||||
package vocation
|
||||
|
||||
import "context"
|
||||
|
||||
type Repository interface {
|
||||
Create(ctx context.Context, vocation *Vocation) error
|
||||
Update(ctx context.Context, vocation *Vocation) error
|
||||
Delete(ctx context.Context, id []byte, deletedBy []byte) error
|
||||
GetByID(ctx context.Context, id []byte) (*Vocation, error)
|
||||
List(ctx context.Context, filter, sort string, limit, offset int) ([]Vocation, int64, error)
|
||||
}
|
||||
11
internal/domain/vocation/service.go
Normal file
11
internal/domain/vocation/service.go
Normal file
@@ -0,0 +1,11 @@
|
||||
package vocation
|
||||
|
||||
import "context"
|
||||
|
||||
type Service interface {
|
||||
Create(ctx context.Context, vocation *Vocation) error
|
||||
Update(ctx context.Context, vocation *Vocation) error
|
||||
Delete(ctx context.Context, id []byte, deletedBy []byte) error
|
||||
GetByID(ctx context.Context, id []byte) (*Vocation, error)
|
||||
List(ctx context.Context, filter, sort string, limit, offset int) ([]Vocation, int64, error)
|
||||
}
|
||||
Reference in New Issue
Block a user