feat: 15.0 - Backend - Delete habit endpoint
This commit is contained in:
@@ -166,6 +166,12 @@ class TaskBoardHandler(SimpleHTTPRequestHandler):
|
||||
else:
|
||||
self.send_error(404)
|
||||
|
||||
def do_DELETE(self):
|
||||
if self.path.startswith('/api/habits/'):
|
||||
self.handle_habits_delete()
|
||||
else:
|
||||
self.send_error(404)
|
||||
|
||||
def handle_git_commit(self):
|
||||
"""Run git commit and push."""
|
||||
try:
|
||||
@@ -988,6 +994,59 @@ class TaskBoardHandler(SimpleHTTPRequestHandler):
|
||||
except Exception as e:
|
||||
self.send_json({'error': str(e)}, 500)
|
||||
|
||||
def handle_habits_delete(self):
|
||||
"""Delete a habit by ID."""
|
||||
try:
|
||||
# Extract habit ID from path: /api/habits/{id}
|
||||
path_parts = self.path.split('/')
|
||||
if len(path_parts) < 4:
|
||||
self.send_json({'error': 'Invalid path'}, 400)
|
||||
return
|
||||
|
||||
habit_id = path_parts[3] # /api/habits/{id} -> index 3 is id
|
||||
|
||||
# Read habits file
|
||||
habits_file = KANBAN_DIR / 'habits.json'
|
||||
if not habits_file.exists():
|
||||
self.send_json({'error': 'Habit not found'}, 404)
|
||||
return
|
||||
|
||||
try:
|
||||
habits_data = json.loads(habits_file.read_text(encoding='utf-8'))
|
||||
except (json.JSONDecodeError, IOError):
|
||||
self.send_json({'error': 'Habit not found'}, 404)
|
||||
return
|
||||
|
||||
# Find the habit by ID
|
||||
habit_index = None
|
||||
for i, h in enumerate(habits_data.get('habits', [])):
|
||||
if h.get('id') == habit_id:
|
||||
habit_index = i
|
||||
break
|
||||
|
||||
if habit_index is None:
|
||||
self.send_json({'error': 'Habit not found'}, 404)
|
||||
return
|
||||
|
||||
# Remove the habit
|
||||
deleted_habit = habits_data['habits'].pop(habit_index)
|
||||
|
||||
# Update lastUpdated timestamp
|
||||
habits_data['lastUpdated'] = datetime.now().isoformat()
|
||||
|
||||
# Write back to file
|
||||
habits_file.write_text(json.dumps(habits_data, indent=2), encoding='utf-8')
|
||||
|
||||
# Return 200 OK with success message
|
||||
self.send_json({
|
||||
'success': True,
|
||||
'message': 'Habit deleted successfully',
|
||||
'id': habit_id
|
||||
}, 200)
|
||||
|
||||
except Exception as e:
|
||||
self.send_json({'error': str(e)}, 500)
|
||||
|
||||
def handle_files_get(self):
|
||||
"""List files or get file content."""
|
||||
from urllib.parse import urlparse, parse_qs
|
||||
|
||||
Reference in New Issue
Block a user