mirror of
https://github.com/TracksApp/tracks.git
synced 2026-03-01 18:40:15 +01:00
Rewrite Tracks application in Golang
This commit introduces a complete rewrite of the Tracks GTD application in Go (Golang), providing a modern, performant alternative to the Ruby on Rails implementation. ## Architecture & Technology Stack - Language: Go 1.21+ - Web Framework: Gin - ORM: GORM with SQLite/MySQL/PostgreSQL support - Authentication: JWT with bcrypt password hashing - Clean Architecture: Separated models, services, handlers, and middleware ## Implemented Features ### Core Models - User: Authentication and user management - Context: GTD contexts (@home, @work, etc.) - Project: Project grouping and tracking - Todo: Task management with state machine (active, completed, deferred, pending) - Tag: Flexible tagging system with polymorphic associations - Dependency: Todo dependencies with circular dependency detection - Preference: User preferences and settings - Note: Project notes - Attachment: File attachment support (model only) - RecurringTodo: Recurring task template (model only) ### API Endpoints **Authentication:** - POST /api/auth/login - User login - POST /api/auth/register - User registration - POST /api/auth/logout - User logout - GET /api/me - Get current user **Todos:** - GET /api/todos - List todos with filtering - POST /api/todos - Create todo - GET /api/todos/:id - Get todo details - PUT /api/todos/:id - Update todo - DELETE /api/todos/:id - Delete todo - POST /api/todos/:id/complete - Mark as completed - POST /api/todos/:id/activate - Mark as active - POST /api/todos/:id/defer - Defer to future date - POST /api/todos/:id/dependencies - Add dependency - DELETE /api/todos/:id/dependencies/:successor_id - Remove dependency **Projects:** - GET /api/projects - List projects - POST /api/projects - Create project - GET /api/projects/:id - Get project details - PUT /api/projects/:id - Update project - DELETE /api/projects/:id - Delete project - POST /api/projects/:id/complete - Complete project - POST /api/projects/:id/activate - Activate project - POST /api/projects/:id/hide - Hide project - POST /api/projects/:id/review - Mark as reviewed - GET /api/projects/:id/stats - Get project statistics **Contexts:** - GET /api/contexts - List contexts - POST /api/contexts - Create context - GET /api/contexts/:id - Get context details - PUT /api/contexts/:id - Update context - DELETE /api/contexts/:id - Delete context - POST /api/contexts/:id/hide - Hide context - POST /api/contexts/:id/activate - Activate context - POST /api/contexts/:id/close - Close context - GET /api/contexts/:id/stats - Get context statistics ### Business Logic **Todo State Management:** - Active: Ready to work on - Completed: Finished tasks - Deferred: Future actions (show_from date) - Pending: Blocked by dependencies **Dependency Management:** - Create blocking relationships between todos - Automatic state transitions when blocking todos complete - Circular dependency detection - Automatic unblocking when prerequisites complete **Tag System:** - Polymorphic tagging for todos and recurring todos - Automatic tag creation on first use - Tag cloud support **Project & Context Tracking:** - State management (active, hidden, closed/completed) - Statistics and health indicators - Review tracking for projects ### Infrastructure **Configuration:** - Environment-based configuration - Support for SQLite, MySQL, and PostgreSQL - Configurable JWT secrets and token expiry - Flexible server settings **Database:** - GORM for ORM - Automatic migrations - Connection pooling - Multi-database support **Authentication & Security:** - JWT-based authentication - Bcrypt password hashing - Secure cookie support - Token refresh mechanism **Docker Support:** - Multi-stage Dockerfile for optimized builds - Docker Compose with PostgreSQL - Volume mounting for data persistence - Production-ready configuration ## Project Structure ``` cmd/tracks/ # Application entry point internal/ config/ # Configuration management database/ # Database setup and migrations handlers/ # HTTP request handlers middleware/ # Authentication middleware models/ # Database models services/ # Business logic layer ``` ## Documentation - README_GOLANG.md: Comprehensive documentation - .env.example: Configuration template - API documentation included in README - Code comments for complex logic ## Future Work The following features from the original Rails app are not yet implemented: - Recurring todo instantiation logic - Email integration (Mailgun/CloudMailin) - Advanced statistics and analytics - Import/Export functionality (CSV, YAML, XML) - File upload handling for attachments - Mobile views - RSS/Atom feeds - iCalendar export ## Benefits Over Rails Version - Performance: Compiled binary, lower resource usage - Deployment: Single binary, no runtime dependencies - Type Safety: Compile-time type checking - Concurrency: Better handling of concurrent requests - Memory: Lower memory footprint - Portability: Easy cross-platform compilation ## Testing The code structure supports testing, though tests are not yet implemented. Future work includes adding unit and integration tests.
This commit is contained in:
parent
6613d33f10
commit
f0eb4bdef5
29 changed files with 4100 additions and 104 deletions
220
internal/services/context_service.go
Normal file
220
internal/services/context_service.go
Normal file
|
|
@ -0,0 +1,220 @@
|
|||
package services
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
|
||||
"github.com/TracksApp/tracks/internal/database"
|
||||
"github.com/TracksApp/tracks/internal/models"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
// ContextService handles context business logic
|
||||
type ContextService struct{}
|
||||
|
||||
// NewContextService creates a new ContextService
|
||||
func NewContextService() *ContextService {
|
||||
return &ContextService{}
|
||||
}
|
||||
|
||||
// CreateContextRequest represents a context creation request
|
||||
type CreateContextRequest struct {
|
||||
Name string `json:"name" binding:"required"`
|
||||
}
|
||||
|
||||
// UpdateContextRequest represents a context update request
|
||||
type UpdateContextRequest struct {
|
||||
Name *string `json:"name"`
|
||||
Position *int `json:"position"`
|
||||
State *string `json:"state"`
|
||||
}
|
||||
|
||||
// GetContexts returns all contexts for a user
|
||||
func (s *ContextService) GetContexts(userID uint, state models.ContextState) ([]models.Context, error) {
|
||||
var contexts []models.Context
|
||||
|
||||
query := database.DB.Where("user_id = ?", userID)
|
||||
|
||||
if state != "" {
|
||||
query = query.Where("state = ?", state)
|
||||
}
|
||||
|
||||
if err := query.
|
||||
Order("position ASC, name ASC").
|
||||
Find(&contexts).Error; err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return contexts, nil
|
||||
}
|
||||
|
||||
// GetContext returns a single context by ID
|
||||
func (s *ContextService) GetContext(userID, contextID uint) (*models.Context, error) {
|
||||
var context models.Context
|
||||
|
||||
if err := database.DB.
|
||||
Where("id = ? AND user_id = ?", contextID, userID).
|
||||
First(&context).Error; err != nil {
|
||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
return nil, fmt.Errorf("context not found")
|
||||
}
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &context, nil
|
||||
}
|
||||
|
||||
// CreateContext creates a new context
|
||||
func (s *ContextService) CreateContext(userID uint, req CreateContextRequest) (*models.Context, error) {
|
||||
context := models.Context{
|
||||
UserID: userID,
|
||||
Name: req.Name,
|
||||
State: models.ContextStateActive,
|
||||
}
|
||||
|
||||
if err := database.DB.Create(&context).Error; err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return s.GetContext(userID, context.ID)
|
||||
}
|
||||
|
||||
// UpdateContext updates a context
|
||||
func (s *ContextService) UpdateContext(userID, contextID uint, req UpdateContextRequest) (*models.Context, error) {
|
||||
context, err := s.GetContext(userID, contextID)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if req.Name != nil {
|
||||
context.Name = *req.Name
|
||||
}
|
||||
if req.Position != nil {
|
||||
context.Position = *req.Position
|
||||
}
|
||||
if req.State != nil {
|
||||
context.State = models.ContextState(*req.State)
|
||||
}
|
||||
|
||||
if err := database.DB.Save(&context).Error; err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return s.GetContext(userID, contextID)
|
||||
}
|
||||
|
||||
// DeleteContext deletes a context
|
||||
func (s *ContextService) DeleteContext(userID, contextID uint) error {
|
||||
context, err := s.GetContext(userID, contextID)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// Check if context has active todos
|
||||
var activeTodoCount int64
|
||||
database.DB.Model(&models.Todo{}).
|
||||
Where("context_id = ? AND state = ?", contextID, models.TodoStateActive).
|
||||
Count(&activeTodoCount)
|
||||
|
||||
if activeTodoCount > 0 {
|
||||
return fmt.Errorf("cannot delete context with active todos")
|
||||
}
|
||||
|
||||
return database.DB.Delete(&context).Error
|
||||
}
|
||||
|
||||
// HideContext marks a context as hidden
|
||||
func (s *ContextService) HideContext(userID, contextID uint) (*models.Context, error) {
|
||||
context, err := s.GetContext(userID, contextID)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
context.Hide()
|
||||
|
||||
if err := database.DB.Save(&context).Error; err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return s.GetContext(userID, contextID)
|
||||
}
|
||||
|
||||
// ActivateContext marks a context as active
|
||||
func (s *ContextService) ActivateContext(userID, contextID uint) (*models.Context, error) {
|
||||
context, err := s.GetContext(userID, contextID)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
context.Activate()
|
||||
|
||||
if err := database.DB.Save(&context).Error; err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return s.GetContext(userID, contextID)
|
||||
}
|
||||
|
||||
// CloseContext marks a context as closed
|
||||
func (s *ContextService) CloseContext(userID, contextID uint) (*models.Context, error) {
|
||||
context, err := s.GetContext(userID, contextID)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// Check if context has active todos
|
||||
var activeTodoCount int64
|
||||
database.DB.Model(&models.Todo{}).
|
||||
Where("context_id = ? AND state = ?", contextID, models.TodoStateActive).
|
||||
Count(&activeTodoCount)
|
||||
|
||||
if activeTodoCount > 0 {
|
||||
return nil, fmt.Errorf("cannot close context with active todos")
|
||||
}
|
||||
|
||||
context.Close()
|
||||
|
||||
if err := database.DB.Save(&context).Error; err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return s.GetContext(userID, contextID)
|
||||
}
|
||||
|
||||
// GetContextStats returns statistics for a context
|
||||
func (s *ContextService) GetContextStats(userID, contextID uint) (map[string]interface{}, error) {
|
||||
context, err := s.GetContext(userID, contextID)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
stats := make(map[string]interface{})
|
||||
|
||||
// Count todos by state
|
||||
var activeTodos, completedTodos, deferredTodos, pendingTodos int64
|
||||
|
||||
database.DB.Model(&models.Todo{}).
|
||||
Where("context_id = ? AND state = ?", contextID, models.TodoStateActive).
|
||||
Count(&activeTodos)
|
||||
|
||||
database.DB.Model(&models.Todo{}).
|
||||
Where("context_id = ? AND state = ?", contextID, models.TodoStateCompleted).
|
||||
Count(&completedTodos)
|
||||
|
||||
database.DB.Model(&models.Todo{}).
|
||||
Where("context_id = ? AND state = ?", contextID, models.TodoStateDeferred).
|
||||
Count(&deferredTodos)
|
||||
|
||||
database.DB.Model(&models.Todo{}).
|
||||
Where("context_id = ? AND state = ?", contextID, models.TodoStatePending).
|
||||
Count(&pendingTodos)
|
||||
|
||||
stats["context"] = context
|
||||
stats["active_todos"] = activeTodos
|
||||
stats["completed_todos"] = completedTodos
|
||||
stats["deferred_todos"] = deferredTodos
|
||||
stats["pending_todos"] = pendingTodos
|
||||
stats["total_todos"] = activeTodos + completedTodos + deferredTodos + pendingTodos
|
||||
|
||||
return stats, nil
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue