initial commit
This commit is contained in:
20
internal/repository/postgres/skill/model.go
Normal file
20
internal/repository/postgres/skill/model.go
Normal file
@@ -0,0 +1,20 @@
|
||||
package skill
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
"github.com/google/uuid"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
type SkillModel struct {
|
||||
ID uuid.UUID `gorm:"type:uuid;primaryKey;default:gen_random_uuid()"`
|
||||
Name string `gorm:"column:name;type:text;not null"`
|
||||
CreatedAt time.Time `gorm:"column:created_at;type:timestamptz;not null"`
|
||||
UpdatedAt time.Time `gorm:"column:updated_at;type:timestamptz;not null"`
|
||||
DeletedAt gorm.DeletedAt `gorm:"column:deleted_at;type:timestamptz;index"`
|
||||
}
|
||||
|
||||
func (SkillModel) TableName() string {
|
||||
return "skills"
|
||||
}
|
||||
49
internal/repository/postgres/skill/repository.go
Normal file
49
internal/repository/postgres/skill/repository.go
Normal file
@@ -0,0 +1,49 @@
|
||||
package skill
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
|
||||
"github.com/google/uuid"
|
||||
"go.uber.org/fx"
|
||||
"gorm.io/gorm"
|
||||
|
||||
domainSkill "base/internal/domain/skill"
|
||||
)
|
||||
|
||||
type repository struct {
|
||||
db *gorm.DB
|
||||
}
|
||||
|
||||
// NewRepository creates a Repository for the skills catalog.
|
||||
func NewRepository(lc fx.Lifecycle, db *gorm.DB) domainSkill.Repository {
|
||||
lc.Append(
|
||||
fx.Hook{
|
||||
OnStart: func(ctx context.Context) error { return nil },
|
||||
OnStop: func(ctx context.Context) error { return nil },
|
||||
})
|
||||
return &repository{db: db}
|
||||
}
|
||||
|
||||
func (r *repository) FindAll(ctx context.Context) ([]*domainSkill.Skill, error) {
|
||||
var models []SkillModel
|
||||
if err := r.db.WithContext(ctx).Order("name ASC").Find(&models).Error; err != nil {
|
||||
return nil, err
|
||||
}
|
||||
out := make([]*domainSkill.Skill, len(models))
|
||||
for i := range models {
|
||||
out[i] = &domainSkill.Skill{ID: models[i].ID, Name: models[i].Name}
|
||||
}
|
||||
return out, nil
|
||||
}
|
||||
|
||||
func (r *repository) FindByID(ctx context.Context, id uuid.UUID) (*domainSkill.Skill, error) {
|
||||
var model SkillModel
|
||||
if err := r.db.WithContext(ctx).Where("id = ?", id).First(&model).Error; err != nil {
|
||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
return nil, nil
|
||||
}
|
||||
return nil, err
|
||||
}
|
||||
return &domainSkill.Skill{ID: model.ID, Name: model.Name}, nil
|
||||
}
|
||||
Reference in New Issue
Block a user