|
| 1 | +package projects |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "net/url" |
| 6 | + "strings" |
| 7 | + "time" |
| 8 | +) |
| 9 | + |
| 10 | +// Project represents the core business entity for a project. |
| 11 | +// A project is a container for tasks and is linked to a BitBucket repository. |
| 12 | +type Project struct { |
| 13 | + ID string // Primary key |
| 14 | + Name string // Unique project name |
| 15 | + RepoURL string // BitBucket repository URL |
| 16 | + CreatedAt time.Time // Creation timestamp |
| 17 | + UpdatedAt time.Time // Last update timestamp |
| 18 | +} |
| 19 | + |
| 20 | +// ProjectInput represents the data required to create a new project. |
| 21 | +// All fields are required and must be validated before creation. |
| 22 | +type ProjectInput struct { |
| 23 | + Name string // Unique project name, required |
| 24 | + RepoURL string // BitBucket repository URL, required |
| 25 | +} |
| 26 | + |
| 27 | +// ProjectUpdate represents the data that can be updated for a project. |
| 28 | +// All fields are optional (pointers) to support partial updates. |
| 29 | +type ProjectUpdate struct { |
| 30 | + Name *string // Optional new name, must be unique if provided |
| 31 | + RepoURL *string // Optional new repository URL, must be valid if provided |
| 32 | +} |
| 33 | + |
| 34 | +// Validate checks if the input data is valid for creating a new project. |
| 35 | +func (i ProjectInput) Validate() error { |
| 36 | + // Trim and validate name |
| 37 | + name := strings.TrimSpace(i.Name) |
| 38 | + if name == "" { |
| 39 | + return fmt.Errorf("%w: project name is required", ErrValidationFailed) |
| 40 | + } |
| 41 | + |
| 42 | + // Validate repository URL |
| 43 | + repoURL := strings.TrimSpace(i.RepoURL) |
| 44 | + if err := validateRepoURL(repoURL); err != nil { |
| 45 | + return err |
| 46 | + } |
| 47 | + |
| 48 | + return nil |
| 49 | +} |
| 50 | + |
| 51 | +// IsEmpty returns true if no update fields are set. |
| 52 | +// This prevents unnecessary database operations when no data is provided. |
| 53 | +func (u ProjectUpdate) IsEmpty() bool { |
| 54 | + return u.Name == nil && u.RepoURL == nil |
| 55 | +} |
| 56 | + |
| 57 | +func (u ProjectUpdate) Validate() error { |
| 58 | + if u.Name != nil { |
| 59 | + // Trim and validate name |
| 60 | + name := strings.TrimSpace(*u.Name) |
| 61 | + if name == "" { |
| 62 | + return fmt.Errorf("%w: project name is required", ErrValidationFailed) |
| 63 | + } |
| 64 | + } |
| 65 | + |
| 66 | + if u.RepoURL != nil { |
| 67 | + // Validate repository URL |
| 68 | + repoURL := strings.TrimSpace(*u.RepoURL) |
| 69 | + if err := validateRepoURL(repoURL); err != nil { |
| 70 | + return err |
| 71 | + } |
| 72 | + } |
| 73 | + |
| 74 | + return nil |
| 75 | +} |
| 76 | + |
| 77 | +// validateRepoURL validates that the repository URL is in a valid format. |
| 78 | +// Accepts HTTPS URLs (https://bitbucket.org/...). |
| 79 | +func validateRepoURL(repoURL string) error { |
| 80 | + if repoURL == "" { |
| 81 | + return fmt.Errorf("%w: repository URL is required", ErrValidationFailed) |
| 82 | + } |
| 83 | + |
| 84 | + u, err := url.Parse(repoURL) |
| 85 | + if err != nil { |
| 86 | + return fmt.Errorf("%w: failed to parse repository URL: %w", ErrValidationFailed, err) |
| 87 | + } |
| 88 | + |
| 89 | + // Accept HTTPS scheme |
| 90 | + if u.Scheme != "https" { |
| 91 | + return fmt.Errorf("%w: repository URL must be in HTTPS format", ErrValidationFailed) |
| 92 | + } |
| 93 | + |
| 94 | + // Basic validation: must have host |
| 95 | + if u.Host == "" { |
| 96 | + return fmt.Errorf("%w: repository URL must have a host", ErrValidationFailed) |
| 97 | + } |
| 98 | + |
| 99 | + return nil |
| 100 | +} |
0 commit comments