Improve date parsing in subscription handlers #37

Closed
opened 2025-08-23 13:02:09 -04:00 by bscott · 1 comment
bscott commented 2025-08-23 13:02:09 -04:00 (Migrated from github.com)

Overview

During the review of PR #36, GitHub Copilot identified opportunities to improve the date parsing logic in our subscription handlers. This issue tracks these improvements for future implementation.

Suggested Improvements

1. Add Error Handling for Date Parsing

Currently, date parsing errors are silently ignored in both CreateSubscription and UpdateSubscription handlers. Consider adding validation to provide user feedback when invalid date formats are submitted.

Current behavior:

if startDateStr := c.PostForm("start_date"); startDateStr != "" {
    if startDate, err := time.Parse("2006-01-02", startDateStr); err == nil {
        subscription.StartDate = &startDate
    }
    // Error is silently ignored
}

Suggested improvement:

  • Add error logging or return validation errors to users
  • Provide clear feedback about expected date format (YYYY-MM-DD)

2. Extract Date Parsing into Helper Function

The date parsing logic is duplicated multiple times across handlers. This could be refactored into a reusable helper function.

Current code has repetition in:

  • internal/handlers/subscription.go:148-163 (CreateSubscription)
  • internal/handlers/subscription.go:243-260 (UpdateSubscription)

Suggested helper function:

func parseDatePtr(dateStr string) *time.Time {
    if dateStr == "" {
        return nil
    }
    if date, err := time.Parse("2006-01-02", dateStr); err == nil {
        return &date
    }
    return nil
}

Benefits

  • Reduces code duplication
  • Improves maintainability
  • Centralizes date parsing logic
  • Makes it easier to add consistent error handling

Implementation Notes

  • Maintain backward compatibility
  • Consider whether to log errors or return them to users
  • Ensure consistency across all handlers
  • HTML5 date inputs typically provide correct format, but server-side validation is still good practice
  • PR #36: v0.4.1 - Bug fixes and version display improvement
  • Issue #35: [BUG] Cannot update subscriptions

Priority

Low - Current implementation works correctly with HTML5 date inputs. This is a code quality improvement rather than a bug fix.

## Overview During the review of PR #36, GitHub Copilot identified opportunities to improve the date parsing logic in our subscription handlers. This issue tracks these improvements for future implementation. ## Suggested Improvements ### 1. Add Error Handling for Date Parsing Currently, date parsing errors are silently ignored in both `CreateSubscription` and `UpdateSubscription` handlers. Consider adding validation to provide user feedback when invalid date formats are submitted. **Current behavior:** ```go if startDateStr := c.PostForm("start_date"); startDateStr != "" { if startDate, err := time.Parse("2006-01-02", startDateStr); err == nil { subscription.StartDate = &startDate } // Error is silently ignored } ``` **Suggested improvement:** - Add error logging or return validation errors to users - Provide clear feedback about expected date format (YYYY-MM-DD) ### 2. Extract Date Parsing into Helper Function The date parsing logic is duplicated multiple times across handlers. This could be refactored into a reusable helper function. **Current code has repetition in:** - `internal/handlers/subscription.go:148-163` (CreateSubscription) - `internal/handlers/subscription.go:243-260` (UpdateSubscription) **Suggested helper function:** ```go func parseDatePtr(dateStr string) *time.Time { if dateStr == "" { return nil } if date, err := time.Parse("2006-01-02", dateStr); err == nil { return &date } return nil } ``` ## Benefits - Reduces code duplication - Improves maintainability - Centralizes date parsing logic - Makes it easier to add consistent error handling ## Implementation Notes - Maintain backward compatibility - Consider whether to log errors or return them to users - Ensure consistency across all handlers - HTML5 date inputs typically provide correct format, but server-side validation is still good practice ## Related - PR #36: v0.4.1 - Bug fixes and version display improvement - Issue #35: [BUG] Cannot update subscriptions ## Priority Low - Current implementation works correctly with HTML5 date inputs. This is a code quality improvement rather than a bug fix.
bscott commented 2025-11-14 14:16:20 -05:00 (Migrated from github.com)

Implemented date parsing refactor with parseDatePtr() helper function. Reduced code duplication by 83% and added error logging. Resolved in v0.4.7.

Implemented date parsing refactor with `parseDatePtr()` helper function. Reduced code duplication by 83% and added error logging. Resolved in v0.4.7.
Sign in to join this conversation.
No labels
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
bscott/subtrackr#37
No description provided.