-
Notifications
You must be signed in to change notification settings - Fork 0
[projects] introduce module #3
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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,17 @@ | ||
| -- +goose Up | ||
| -- +goose StatementBegin | ||
| CREATE TABLE `projects` ( | ||
| `id` VARCHAR(255) NOT NULL, | ||
| `name` VARCHAR(255) NOT NULL, | ||
| `repo_url` VARCHAR(512) NOT NULL, | ||
| `created_at` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP, | ||
| `updated_at` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, | ||
| PRIMARY KEY (`id`), | ||
| UNIQUE KEY `idx_projects_name` (`name`) | ||
| ) ENGINE = InnoDB DEFAULT CHARSET = utf8mb4 COLLATE = utf8mb4_unicode_ci; | ||
| -- +goose StatementEnd | ||
| --- | ||
| -- +goose Down | ||
| -- +goose StatementBegin | ||
| DROP TABLE `projects`; | ||
| -- +goose StatementEnd |
This file contains hidden or 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,9 @@ | ||
| package projects | ||
|
|
||
| // Config holds configuration for the projects module. | ||
| // Currently empty as no module-specific configuration is needed for MVP. | ||
| // This struct can be extended in the future for feature flags or | ||
| // other configurable aspects. | ||
| type Config struct { | ||
| // Example: MaxProjectsPerPage int `env:"MAX_PROJECTS_PER_PAGE" default:"100"` | ||
| } |
This file contains hidden or 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,33 @@ | ||
| // Package projects provides project management functionality for the | ||
| // Corporate Task Tracker. Projects serve as containers for tasks and | ||
| // are linked to external BitBucket repositories. | ||
| // | ||
| // The projects module follows a clean architecture pattern with clear | ||
| // separation between domain logic, data access, and HTTP presentation. | ||
| // | ||
| // Domain Layer: | ||
| // - Project: Core business entity | ||
| // - ProjectInput: Data for creating projects | ||
| // - ProjectUpdate: Data for updating projects | ||
| // | ||
| // Repository Layer: | ||
| // - Handles all database operations | ||
| // - Uses Bun ORM for type-safe queries | ||
| // | ||
| // Service Layer: | ||
| // - Implements business rules and validation | ||
| // - Validates repository URLs | ||
| // - Ensures name uniqueness | ||
| // | ||
| // HTTP Layer: | ||
| // - RESTful API endpoints | ||
| // - Admin-only write operations | ||
| // - Authenticated read operations | ||
| // | ||
| // Usage: | ||
| // | ||
| // app := fx.New( | ||
| // projects.Module(), | ||
| // // other modules... | ||
| // ) | ||
| package projects |
This file contains hidden or 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,100 @@ | ||
| package projects | ||
|
|
||
| import ( | ||
| "fmt" | ||
| "net/url" | ||
| "strings" | ||
| "time" | ||
| ) | ||
|
|
||
| // Project represents the core business entity for a project. | ||
| // A project is a container for tasks and is linked to a BitBucket repository. | ||
| type Project struct { | ||
| ID string // Primary key | ||
| Name string // Unique project name | ||
| RepoURL string // BitBucket repository URL | ||
| CreatedAt time.Time // Creation timestamp | ||
| UpdatedAt time.Time // Last update timestamp | ||
| } | ||
|
|
||
| // ProjectInput represents the data required to create a new project. | ||
| // All fields are required and must be validated before creation. | ||
| type ProjectInput struct { | ||
| Name string // Unique project name, required | ||
| RepoURL string // BitBucket repository URL, required | ||
| } | ||
|
|
||
| // ProjectUpdate represents the data that can be updated for a project. | ||
| // All fields are optional (pointers) to support partial updates. | ||
| type ProjectUpdate struct { | ||
| Name *string // Optional new name, must be unique if provided | ||
| RepoURL *string // Optional new repository URL, must be valid if provided | ||
| } | ||
|
|
||
| // Validate checks if the input data is valid for creating a new project. | ||
| func (i ProjectInput) Validate() error { | ||
| // Trim and validate name | ||
| name := strings.TrimSpace(i.Name) | ||
| if name == "" { | ||
| return fmt.Errorf("%w: project name is required", ErrValidationFailed) | ||
| } | ||
|
|
||
| // Validate repository URL | ||
| repoURL := strings.TrimSpace(i.RepoURL) | ||
| if err := validateRepoURL(repoURL); err != nil { | ||
| return err | ||
| } | ||
|
|
||
| return nil | ||
| } | ||
|
|
||
| // IsEmpty returns true if no update fields are set. | ||
| // This prevents unnecessary database operations when no data is provided. | ||
| func (u ProjectUpdate) IsEmpty() bool { | ||
| return u.Name == nil && u.RepoURL == nil | ||
| } | ||
|
|
||
| func (u ProjectUpdate) Validate() error { | ||
| if u.Name != nil { | ||
| // Trim and validate name | ||
| name := strings.TrimSpace(*u.Name) | ||
| if name == "" { | ||
| return fmt.Errorf("%w: project name is required", ErrValidationFailed) | ||
| } | ||
| } | ||
|
|
||
| if u.RepoURL != nil { | ||
| // Validate repository URL | ||
| repoURL := strings.TrimSpace(*u.RepoURL) | ||
| if err := validateRepoURL(repoURL); err != nil { | ||
| return err | ||
| } | ||
| } | ||
|
|
||
| return nil | ||
| } | ||
|
|
||
| // validateRepoURL validates that the repository URL is in a valid format. | ||
| // Accepts HTTPS URLs (https://bitbucket.org/...). | ||
| func validateRepoURL(repoURL string) error { | ||
| if repoURL == "" { | ||
| return fmt.Errorf("%w: repository URL is required", ErrValidationFailed) | ||
| } | ||
|
|
||
| u, err := url.Parse(repoURL) | ||
| if err != nil { | ||
| return fmt.Errorf("%w: failed to parse repository URL: %w", ErrValidationFailed, err) | ||
| } | ||
|
capcom6 marked this conversation as resolved.
|
||
|
|
||
| // Accept HTTPS scheme | ||
| if u.Scheme != "https" { | ||
| return fmt.Errorf("%w: repository URL must be in HTTPS format", ErrValidationFailed) | ||
| } | ||
|
|
||
| // Basic validation: must have host | ||
| if u.Host == "" { | ||
| return fmt.Errorf("%w: repository URL must have a host", ErrValidationFailed) | ||
| } | ||
|
|
||
| return nil | ||
| } | ||
This file contains hidden or 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,19 @@ | ||
| package projects | ||
|
|
||
| import "errors" | ||
|
|
||
| var ( | ||
| // ErrValidationFailed is returned when input data fails validation. | ||
| ErrValidationFailed = errors.New("validation failed") | ||
|
|
||
| // ErrNotFound is returned when a project with the given ID does not exist. | ||
| ErrNotFound = errors.New("project not found") | ||
|
|
||
| // ErrNameAlreadyUsed is returned when attempting to create or update | ||
| // a project with a name that is already in use by another project. | ||
| ErrNameAlreadyUsed = errors.New("project name already in use") | ||
|
|
||
| // ErrInvalidURL is returned when the repository URL is not in a valid | ||
| // format. | ||
| ErrInvalidURL = errors.New("invalid repository URL") | ||
| ) |
This file contains hidden or 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,52 @@ | ||
| package projects | ||
|
|
||
| import ( | ||
| "time" | ||
|
|
||
| "github.com/go-core-fx/bunfx" | ||
| "github.com/uptrace/bun" | ||
| "github.com/uptrace/bun/schema" | ||
| ) | ||
|
|
||
| // projectModel is the database representation of a project. | ||
| // This struct is used for Bun ORM operations and maps to the `projects` table. | ||
| type projectModel struct { | ||
| bun.BaseModel `bun:"table:projects,alias:p"` | ||
| bunfx.TimedModel | ||
|
|
||
| ID string `bun:"id,pk"` // Primary key | ||
| Name string `bun:"name,notnull,unique"` | ||
| RepoURL string `bun:"repo_url,notnull"` | ||
| } | ||
|
|
||
| // newProjectModel creates a new projectModel from a ProjectInput. | ||
| // It automatically sets the ID from the name and timestamps. | ||
| func newProjectModel(input ProjectInput, slug string) *projectModel { | ||
| now := time.Now() | ||
| return &projectModel{ | ||
| BaseModel: schema.BaseModel{}, | ||
| TimedModel: bunfx.TimedModel{ | ||
| CreatedAt: now, | ||
| UpdatedAt: now, | ||
| }, | ||
|
|
||
| ID: slug, | ||
| Name: input.Name, | ||
| RepoURL: input.RepoURL, | ||
| } | ||
| } | ||
|
|
||
| // toDomain converts the database model to a domain Project entity. | ||
| // Returns nil if the model is nil. | ||
| func (m *projectModel) toDomain() *Project { | ||
| if m == nil { | ||
| return nil | ||
| } | ||
| return &Project{ | ||
| ID: m.ID, | ||
| Name: m.Name, | ||
| RepoURL: m.RepoURL, | ||
| CreatedAt: m.CreatedAt, | ||
| UpdatedAt: m.UpdatedAt, | ||
| } | ||
| } |
This file contains hidden or 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,28 @@ | ||
| package projects | ||
|
|
||
| import ( | ||
| "github.com/go-core-fx/logger" | ||
| "go.uber.org/fx" | ||
| ) | ||
|
|
||
| // Module creates and returns an FX module for the projects package. | ||
| // This module wires up all dependencies for the projects functionality: | ||
| // - Repository (private): Data access layer, only used within this module | ||
| // - Service (public): Business logic layer, can be injected into other modules | ||
| // | ||
| // The module also registers a named logger for structured logging. | ||
| func Module() fx.Option { | ||
| return fx.Module( | ||
| "projects", | ||
| // Add a named logger for this module | ||
| logger.WithNamedLogger("projects"), | ||
|
|
||
| // Provide the repository as a private dependency | ||
| // This means it can only be used within this module | ||
| fx.Provide(NewRepository, fx.Private), | ||
|
|
||
| // Provide the service as a public dependency | ||
| // This means it can be injected into other modules (e.g., tasks module) | ||
| fx.Provide(NewService), | ||
| ) | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.