-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
18 changed files
with
6,809 additions
and
52 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
package legalentities | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/google/uuid" | ||
) | ||
|
||
// Service содержит бизнес-логику для работы с LegalEntity. | ||
type Service struct { | ||
repo *Repository | ||
} | ||
|
||
// NewService возвращает новый экземпляр Service. | ||
func NewService(repo *Repository) *Service { | ||
return &Service{repo: repo} | ||
} | ||
|
||
// CreateLegalEntity создаёт новую запись LegalEntity. | ||
func (s *Service) CreateLegalEntity(ctx context.Context, name string) (uuid.UUID, error) { | ||
entity := &LegalEntity{ | ||
Name: name, | ||
} | ||
err := s.repo.Create(ctx, entity) | ||
return entity.UUID, err | ||
} | ||
|
||
// GetAllLegalEntities возвращает список всех LegalEntity (не удалённых). | ||
func (s *Service) GetAllLegalEntities(ctx context.Context) ([]LegalEntity, error) { | ||
return s.repo.GetAll(ctx) | ||
} | ||
|
||
// GetLegalEntity возвращает конкретную LegalEntity по UUID. | ||
func (s *Service) GetLegalEntity(ctx context.Context, id uuid.UUID) (LegalEntity, error) { | ||
return s.repo.GetByUUID(ctx, id) | ||
} | ||
|
||
// UpdateLegalEntity обновляет поля существующей записи LegalEntity. | ||
func (s *Service) UpdateLegalEntity(ctx context.Context, id uuid.UUID, newName string) error { | ||
// Сначала получаем существующую запись | ||
entity, err := s.repo.GetByUUID(ctx, id) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
// Меняем поля | ||
entity.Name = newName | ||
|
||
// Сохраняем изменения | ||
return s.repo.Update(ctx, &entity) | ||
} | ||
|
||
// DeleteLegalEntity «мягко» удаляет LegalEntity (soft delete). | ||
func (s *Service) DeleteLegalEntity(ctx context.Context, id uuid.UUID) error { | ||
return s.repo.Delete(ctx, id) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package legalentities | ||
|
||
import ( | ||
"time" | ||
|
||
"github.com/google/uuid" | ||
) | ||
|
||
// LegalEntity представляет ORM-модель для юридических лиц | ||
// с полями: uuid, name, created_at, updated_at, deleted_at. | ||
type LegalEntity struct { | ||
UUID uuid.UUID `json:"uuid" gorm:"primaryKey;type:uuid;default:uuid_generate_v4()"` | ||
Name string `json:"name" gorm:"not null;type:varchar(255)"` | ||
CreatedAt time.Time `json:"created_at" gorm:"<-:create;autoCreateTime"` | ||
UpdatedAt time.Time `json:"updated_at" gorm:"autoUpdateTime"` | ||
DeletedAt *time.Time `json:"deleted_at"` | ||
} | ||
|
||
// TableName указывает, какую таблицу в БД будет использовать ORM. | ||
func (LegalEntity) TableName() string { | ||
return "legal_entities" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
package legalentities | ||
|
||
import ( | ||
"context" | ||
"time" | ||
|
||
"github.com/google/uuid" | ||
"gorm.io/gorm" | ||
) | ||
|
||
// Repository предоставляет методы для работы с сущностями LegalEntity в БД. | ||
type Repository struct { | ||
db *gorm.DB | ||
} | ||
|
||
// NewRepository возвращает новый экземпляр репозитория. | ||
func NewRepository(db *gorm.DB) *Repository { | ||
return &Repository{db: db} | ||
} | ||
|
||
// Create добавляет новую сущность LegalEntity в БД. | ||
func (r *Repository) Create(ctx context.Context, entity *LegalEntity) error { | ||
return r.db.WithContext(ctx).Create(entity).Error | ||
} | ||
|
||
// GetByUUID возвращает LegalEntity по его UUID. | ||
func (r *Repository) GetByUUID(ctx context.Context, id uuid.UUID) (LegalEntity, error) { | ||
var entity LegalEntity | ||
err := r.db.WithContext(ctx). | ||
Where("uuid = ?", id). | ||
First(&entity).Error | ||
return entity, err | ||
} | ||
|
||
// GetAll возвращает все LegalEntity, у которых нет метки удалённости (deleted_at). | ||
func (r *Repository) GetAll(ctx context.Context) ([]LegalEntity, error) { | ||
var entities []LegalEntity | ||
err := r.db.WithContext(ctx). | ||
Where("deleted_at IS NULL"). | ||
Find(&entities).Error | ||
return entities, err | ||
} | ||
|
||
// Update обновляет существующую запись LegalEntity. | ||
func (r *Repository) Update(ctx context.Context, entity *LegalEntity) error { | ||
// Если вам нужно полностью перезаписать все поля, можно использовать сохранение. | ||
// Если только отдельные поля, используйте Updates или конкретно UpdateColumn. | ||
return r.db.WithContext(ctx).Save(entity).Error | ||
} | ||
|
||
// Delete помечает LegalEntity как удалённую (soft delete), устанавливая deleted_at. | ||
func (r *Repository) Delete(ctx context.Context, id uuid.UUID) error { | ||
now := time.Now() | ||
result := r.db.WithContext(ctx). | ||
Model(&LegalEntity{}). | ||
Where("uuid = ?", id). | ||
Where("deleted_at IS NULL"). | ||
Update("deleted_at", &now) | ||
if result.Error != nil { | ||
return result.Error | ||
} | ||
if result.RowsAffected == 0 { | ||
return gorm.ErrRecordNotFound | ||
} | ||
return nil | ||
} |
Oops, something went wrong.