mirror of
https://github.com/SamyRai/tercul-backend.git
synced 2025-12-27 02:51:34 +00:00
The goal of this refactoring is to eliminate the global configuration singleton (`config.Cfg`) and replace it with explicit dependency injection of a `Config` struct. This commit includes the following partial changes: - The `Config` struct in `internal/platform/config/config.go` has been updated with all necessary fields. - Several platform packages (`db`, `cache`, `auth`, `http`, `jobs/sync`) have been modified to accept the `*config.Config` struct. - The API server entry point (`cmd/api/main.go`) has been updated to load and provide the configuration. - A new worker entry point (`cmd/worker/main.go`) has been created to house the background job runner, as per the architecture defined in `refactor.md`. NOTE: The build is currently broken as this refactoring is incomplete. This commit is for saving progress as requested.
134 lines
4.2 KiB
Go
134 lines
4.2 KiB
Go
package testutil
|
|
|
|
import (
|
|
"context"
|
|
"strings"
|
|
"tercul/internal/domain"
|
|
|
|
"gorm.io/gorm"
|
|
)
|
|
|
|
// MockUserRepository is a mock implementation of the UserRepository interface.
|
|
type MockUserRepository struct {
|
|
Users []*domain.User
|
|
}
|
|
|
|
// NewMockUserRepository creates a new MockUserRepository.
|
|
func NewMockUserRepository() *MockUserRepository {
|
|
return &MockUserRepository{Users: []*domain.User{}}
|
|
}
|
|
|
|
// Create adds a new user to the mock repository.
|
|
func (m *MockUserRepository) Create(ctx context.Context, user *domain.User) error {
|
|
user.ID = uint(len(m.Users) + 1)
|
|
m.Users = append(m.Users, user)
|
|
return nil
|
|
}
|
|
|
|
// GetByID retrieves a user by their ID from the mock repository.
|
|
func (m *MockUserRepository) GetByID(ctx context.Context, id uint) (*domain.User, error) {
|
|
for _, u := range m.Users {
|
|
if u.ID == id {
|
|
return u, nil
|
|
}
|
|
}
|
|
return nil, gorm.ErrRecordNotFound
|
|
}
|
|
|
|
// FindByUsername retrieves a user by their username from the mock repository.
|
|
func (m *MockUserRepository) FindByUsername(ctx context.Context, username string) (*domain.User, error) {
|
|
for _, u := range m.Users {
|
|
if strings.EqualFold(u.Username, username) {
|
|
return u, nil
|
|
}
|
|
}
|
|
return nil, gorm.ErrRecordNotFound
|
|
}
|
|
|
|
// FindByEmail retrieves a user by their email from the mock repository.
|
|
func (m *MockUserRepository) FindByEmail(ctx context.Context, email string) (*domain.User, error) {
|
|
for _, u := range m.Users {
|
|
if strings.EqualFold(u.Email, email) {
|
|
return u, nil
|
|
}
|
|
}
|
|
return nil, gorm.ErrRecordNotFound
|
|
}
|
|
|
|
// ListByRole retrieves users by their role from the mock repository.
|
|
func (m *MockUserRepository) ListByRole(ctx context.Context, role domain.UserRole) ([]domain.User, error) {
|
|
var users []domain.User
|
|
for _, u := range m.Users {
|
|
if u.Role == role {
|
|
users = append(users, *u)
|
|
}
|
|
}
|
|
return users, nil
|
|
}
|
|
|
|
// The rest of the BaseRepository methods can be stubbed out or implemented as needed.
|
|
func (m *MockUserRepository) CreateInTx(ctx context.Context, tx *gorm.DB, entity *domain.User) error {
|
|
return m.Create(ctx, entity)
|
|
}
|
|
func (m *MockUserRepository) GetByIDWithOptions(ctx context.Context, id uint, options *domain.QueryOptions) (*domain.User, error) {
|
|
return m.GetByID(ctx, id)
|
|
}
|
|
func (m *MockUserRepository) Update(ctx context.Context, entity *domain.User) error {
|
|
for i, u := range m.Users {
|
|
if u.ID == entity.ID {
|
|
m.Users[i] = entity
|
|
return nil
|
|
}
|
|
}
|
|
return gorm.ErrRecordNotFound
|
|
}
|
|
func (m *MockUserRepository) UpdateInTx(ctx context.Context, tx *gorm.DB, entity *domain.User) error {
|
|
return m.Update(ctx, entity)
|
|
}
|
|
func (m *MockUserRepository) Delete(ctx context.Context, id uint) error {
|
|
for i, u := range m.Users {
|
|
if u.ID == id {
|
|
m.Users = append(m.Users[:i], m.Users[i+1:]...)
|
|
return nil
|
|
}
|
|
}
|
|
return gorm.ErrRecordNotFound
|
|
}
|
|
func (m *MockUserRepository) DeleteInTx(ctx context.Context, tx *gorm.DB, id uint) error {
|
|
return m.Delete(ctx, id)
|
|
}
|
|
func (m *MockUserRepository) List(ctx context.Context, page, pageSize int) (*domain.PaginatedResult[domain.User], error) {
|
|
panic("not implemented")
|
|
}
|
|
func (m *MockUserRepository) ListWithOptions(ctx context.Context, options *domain.QueryOptions) ([]domain.User, error) {
|
|
panic("not implemented")
|
|
}
|
|
func (m *MockUserRepository) ListAll(ctx context.Context) ([]domain.User, error) {
|
|
var users []domain.User
|
|
for _, u := range m.Users {
|
|
users = append(users, *u)
|
|
}
|
|
return users, nil
|
|
}
|
|
func (m *MockUserRepository) Count(ctx context.Context) (int64, error) {
|
|
return int64(len(m.Users)), nil
|
|
}
|
|
func (m *MockUserRepository) CountWithOptions(ctx context.Context, options *domain.QueryOptions) (int64, error) {
|
|
panic("not implemented")
|
|
}
|
|
func (m *MockUserRepository) FindWithPreload(ctx context.Context, preloads []string, id uint) (*domain.User, error) {
|
|
return m.GetByID(ctx, id)
|
|
}
|
|
func (m *MockUserRepository) GetAllForSync(ctx context.Context, batchSize, offset int) ([]domain.User, error) {
|
|
panic("not implemented")
|
|
}
|
|
func (m *MockUserRepository) Exists(ctx context.Context, id uint) (bool, error) {
|
|
_, err := m.GetByID(ctx, id)
|
|
return err == nil, nil
|
|
}
|
|
func (m *MockUserRepository) BeginTx(ctx context.Context) (*gorm.DB, error) {
|
|
return nil, nil
|
|
}
|
|
func (m *MockUserRepository) WithTx(ctx context.Context, fn func(tx *gorm.DB) error) error {
|
|
return fn(nil)
|
|
} |