Refactor: improve backend modularity
This commit is contained in:
@@ -0,0 +1,81 @@
|
||||
// Path: Backend/internal/bootstrap/bootstrap_helpers.go
|
||||
|
||||
package bootstrap
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
"github.com/jackc/pgx/v5"
|
||||
|
||||
"moku-backend/internal/database"
|
||||
)
|
||||
|
||||
func NewService(db *database.DB, posixRoot string) *Service {
|
||||
return &Service{db: db, posixRoot: strings.TrimSpace(posixRoot)}
|
||||
}
|
||||
|
||||
func upsertNamedRecord(ctx context.Context, tx pgx.Tx, query string, args ...any) (namedRecord, error) {
|
||||
var record namedRecord
|
||||
if err := tx.QueryRow(ctx, query, args...).Scan(&record.ID, &record.Name, &record.Slug); err != nil {
|
||||
return namedRecord{}, err
|
||||
}
|
||||
|
||||
return record, nil
|
||||
}
|
||||
|
||||
func upsertWorkspace(ctx context.Context, tx pgx.Tx, organizationID, name, slug, kind, createdByUserID string, departmentID, teamID, projectID *string) error {
|
||||
_, err := tx.Exec(ctx, `
|
||||
INSERT INTO workspaces (organization_id, name, slug, kind, created_by_user_id, department_id, team_id, project_id)
|
||||
VALUES ($1::uuid, $2, $3, $4::workspace_kind, $5::uuid, $6::uuid, $7::uuid, $8::uuid)
|
||||
ON CONFLICT (organization_id, slug) DO UPDATE
|
||||
SET
|
||||
name = EXCLUDED.name,
|
||||
kind = EXCLUDED.kind,
|
||||
created_by_user_id = EXCLUDED.created_by_user_id,
|
||||
department_id = EXCLUDED.department_id,
|
||||
team_id = EXCLUDED.team_id,
|
||||
project_id = EXCLUDED.project_id,
|
||||
updated_at = NOW();
|
||||
`, organizationID, name, slug, kind, createdByUserID, departmentID, teamID, projectID)
|
||||
|
||||
return err
|
||||
}
|
||||
|
||||
func defaultRootOrganizationName(installationName, mode, host, adminDisplayName string) string {
|
||||
trimmedInstallationName := strings.TrimSpace(installationName)
|
||||
trimmedHost := strings.TrimSpace(host)
|
||||
trimmedAdminDisplayName := strings.TrimSpace(adminDisplayName)
|
||||
|
||||
if trimmedInstallationName != "" {
|
||||
return trimmedInstallationName
|
||||
}
|
||||
|
||||
if strings.EqualFold(mode, defaultInstallationMode) {
|
||||
if trimmedAdminDisplayName != "" {
|
||||
return fmt.Sprintf("%s %s", trimmedAdminDisplayName, defaultPersonalServerSuffix)
|
||||
}
|
||||
|
||||
return defaultPersonalDisplayName
|
||||
}
|
||||
|
||||
if trimmedHost != "" {
|
||||
return trimmedHost
|
||||
}
|
||||
|
||||
return defaultOrganizationName
|
||||
}
|
||||
|
||||
func personalHomeTitle(displayName string) string {
|
||||
trimmedDisplayName := strings.TrimSpace(displayName)
|
||||
if trimmedDisplayName == "" {
|
||||
return "Home"
|
||||
}
|
||||
|
||||
if strings.HasSuffix(strings.ToLower(trimmedDisplayName), "s") {
|
||||
return fmt.Sprintf("%s' Home", trimmedDisplayName)
|
||||
}
|
||||
|
||||
return fmt.Sprintf("%s's Home", trimmedDisplayName)
|
||||
}
|
||||
Reference in New Issue
Block a user