initial commit

This commit is contained in:
m.zare
2026-04-10 18:25:21 +03:30
commit 77ca6c34a3
263 changed files with 34470 additions and 0 deletions

View File

@@ -0,0 +1,112 @@
package profile
import (
"context"
"errors"
"time"
"github.com/google/uuid"
"go.uber.org/fx"
"gorm.io/gorm"
domainProfile "base/internal/domain/profile"
)
type roleRepository struct {
db *gorm.DB
}
// NewRoleRepository creates a RoleRepository for profile_roles.
func NewRoleRepository(lc fx.Lifecycle, db *gorm.DB) domainProfile.RoleRepository {
lc.Append(
fx.Hook{
OnStart: func(ctx context.Context) error { return nil },
OnStop: func(ctx context.Context) error { return nil },
})
return &roleRepository{db: db}
}
func (r *roleRepository) FindByID(ctx context.Context, id uuid.UUID) (*domainProfile.Role, error) {
var model RoleModel
if err := r.db.WithContext(ctx).Where("id = ?", id).First(&model).Error; err != nil {
if errors.Is(err, gorm.ErrRecordNotFound) {
return nil, domainProfile.ErrRoleNotFound
}
return nil, err
}
return roleModelToDomain(&model), nil
}
func (r *roleRepository) FindAll(ctx context.Context) ([]*domainProfile.Role, error) {
var models []RoleModel
if err := r.db.WithContext(ctx).Order("status DESC, title ASC").Find(&models).Error; err != nil {
return nil, err
}
out := make([]*domainProfile.Role, len(models))
for i := range models {
out[i] = roleModelToDomain(&models[i])
}
return out, nil
}
func (r *roleRepository) List(ctx context.Context, limit, offset int) ([]*domainProfile.Role, error) {
var models []RoleModel
q := r.db.WithContext(ctx).Order("status DESC, title ASC")
if limit > 0 {
q = q.Limit(limit)
}
if offset > 0 {
q = q.Offset(offset)
}
if err := q.Find(&models).Error; err != nil {
return nil, err
}
out := make([]*domainProfile.Role, len(models))
for i := range models {
out[i] = roleModelToDomain(&models[i])
}
return out, nil
}
func roleModelToDomain(m *RoleModel) *domainProfile.Role {
return &domainProfile.Role{
ID: m.ID,
Title: m.Title,
}
}
func (r *roleRepository) Create(ctx context.Context, role *domainProfile.Role) error {
now := time.Now()
model := &RoleModel{
ID: role.ID,
Title: role.Title,
CreatedAt: now,
UpdatedAt: now,
}
if err := r.db.WithContext(ctx).Create(model).Error; err != nil {
return err
}
return nil
}
func (r *roleRepository) Update(ctx context.Context, role *domainProfile.Role) error {
result := r.db.WithContext(ctx).Model(&RoleModel{}).Where("id = ?", role.ID).Updates(map[string]interface{}{"title": role.Title})
if result.Error != nil {
return result.Error
}
if result.RowsAffected == 0 {
return domainProfile.ErrRoleNotFound
}
return nil
}
func (r *roleRepository) Delete(ctx context.Context, id uuid.UUID) error {
result := r.db.WithContext(ctx).Delete(&RoleModel{}, "id = ?", id)
if result.Error != nil {
return result.Error
}
if result.RowsAffected == 0 {
return domainProfile.ErrRoleNotFound
}
return nil
}