init push
This commit is contained in:
89
internal/domain/medicine/model.go
Normal file
89
internal/domain/medicine/model.go
Normal file
@@ -0,0 +1,89 @@
|
||||
package medicine
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
"gorm.io/gorm"
|
||||
|
||||
"wucher/internal/shared/pkg/uuidv7"
|
||||
)
|
||||
|
||||
type Medicine struct {
|
||||
ID []byte `gorm:"type:binary(16);primaryKey;column:id"`
|
||||
Name string `gorm:"type:varchar(128);not null;column:name"`
|
||||
SortKey *int `gorm:"column:sortkey"`
|
||||
Note string `gorm:"type:varchar(255);column:note"`
|
||||
Pack string `gorm:"type:varchar(128);column:pack"`
|
||||
Unit string `gorm:"type:varchar(50);column:unit"`
|
||||
Weight *int64 `gorm:"type:bigint;column:weight"`
|
||||
Column int64 `gorm:"type:bigint;column:column"`
|
||||
MedicineGroupID []byte `gorm:"type:binary(16);index;column:medicine_group_id"`
|
||||
MotorReactionID []byte `gorm:"type:binary(16);index;column:motor_reaction_id"`
|
||||
IsActive bool `gorm:"type:tinyint(1);index;not null;default:1;column:is_active"`
|
||||
CreatedAt time.Time `gorm:"column:created_at"`
|
||||
CreatedBy []byte `gorm:"type:binary(16);column:created_by"`
|
||||
UpdatedAt time.Time `gorm:"column:updated_at"`
|
||||
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"`
|
||||
MedicineGroup *MedicineGroup `gorm:"foreignKey:MedicineGroupID;references:ID"`
|
||||
MotorReaction *MotorReaction `gorm:"foreignKey:MotorReactionID;references:ID"`
|
||||
MotorReactions []MotorReaction `gorm:"many2many:medicine_motor_reactions;joinForeignKey:MedicineID;joinReferences:MotorReactionID"`
|
||||
}
|
||||
|
||||
func (Medicine) TableName() string { return "medicines" }
|
||||
|
||||
func (m *Medicine) BeforeCreate(tx *gorm.DB) error {
|
||||
if len(m.ID) == 0 {
|
||||
m.ID = uuidv7.MustBytes()
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
type MedicineGroup struct {
|
||||
ID []byte `gorm:"type:binary(16);primaryKey;column:id"`
|
||||
Name string `gorm:"type:varchar(255);not null;column:name"`
|
||||
CreatedAt time.Time `gorm:"column:created_at"`
|
||||
CreatedBy []byte `gorm:"type:binary(16);column:created_by"`
|
||||
UpdatedAt time.Time `gorm:"column:updated_at"`
|
||||
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 (MedicineGroup) TableName() string { return "medicine_groups" }
|
||||
|
||||
func (g *MedicineGroup) BeforeCreate(tx *gorm.DB) error {
|
||||
if len(g.ID) == 0 {
|
||||
g.ID = uuidv7.MustBytes()
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
type MotorReaction struct {
|
||||
ID []byte `gorm:"type:binary(16);primaryKey;column:id"`
|
||||
Name string `gorm:"type:varchar(255);not null;column:name"`
|
||||
Score *int64 `gorm:"type:bigint;column:score"`
|
||||
CreatedAt time.Time `gorm:"column:created_at"`
|
||||
CreatedBy []byte `gorm:"type:binary(16);column:created_by"`
|
||||
UpdatedAt time.Time `gorm:"column:updated_at"`
|
||||
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 (MotorReaction) TableName() string { return "motor_reactions" }
|
||||
|
||||
func (r *MotorReaction) BeforeCreate(tx *gorm.DB) error {
|
||||
if len(r.ID) == 0 {
|
||||
r.ID = uuidv7.MustBytes()
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
type MedicineMotorReaction struct {
|
||||
MedicineID []byte `gorm:"type:binary(16);primaryKey;column:medicine_id"`
|
||||
MotorReactionID []byte `gorm:"type:binary(16);primaryKey;column:motor_reaction_id"`
|
||||
}
|
||||
|
||||
func (MedicineMotorReaction) TableName() string { return "medicine_motor_reactions" }
|
||||
37
internal/domain/medicine/model_test.go
Normal file
37
internal/domain/medicine/model_test.go
Normal file
@@ -0,0 +1,37 @@
|
||||
package medicine
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
func TestMedicineTableName(t *testing.T) {
|
||||
var m Medicine
|
||||
if got := m.TableName(); got != "medicines" {
|
||||
t.Fatalf("expected table name medicines, got %q", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestMedicineBeforeCreate(t *testing.T) {
|
||||
t.Run("generates id when empty", func(t *testing.T) {
|
||||
m := &Medicine{}
|
||||
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 := &Medicine{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/medicine/repository.go
Normal file
11
internal/domain/medicine/repository.go
Normal file
@@ -0,0 +1,11 @@
|
||||
package medicine
|
||||
|
||||
import "context"
|
||||
|
||||
type Repository interface {
|
||||
Create(ctx context.Context, medicine *Medicine) error
|
||||
Update(ctx context.Context, medicine *Medicine) error
|
||||
Delete(ctx context.Context, id []byte, deletedBy []byte) error
|
||||
GetByID(ctx context.Context, id []byte) (*Medicine, error)
|
||||
List(ctx context.Context, filter, sort string, limit, offset int) ([]Medicine, int64, error)
|
||||
}
|
||||
11
internal/domain/medicine/service.go
Normal file
11
internal/domain/medicine/service.go
Normal file
@@ -0,0 +1,11 @@
|
||||
package medicine
|
||||
|
||||
import "context"
|
||||
|
||||
type Service interface {
|
||||
Create(ctx context.Context, medicine *Medicine) error
|
||||
Update(ctx context.Context, medicine *Medicine) error
|
||||
Delete(ctx context.Context, id []byte, deletedBy []byte) error
|
||||
GetByID(ctx context.Context, id []byte) (*Medicine, error)
|
||||
List(ctx context.Context, filter, sort string, limit, offset int) ([]Medicine, int64, error)
|
||||
}
|
||||
Reference in New Issue
Block a user