Spaces:
Sleeping
Sleeping
Download src/controllers/plant.controller.js from shard-c6/green-guard-api: direct link, hf CLI and curl.
- Browser
- Download file 8.97 kB
-
https://huggingface.co/spaces/shard-c6/green-guard-api/resolve/main/src/controllers/plant.controller.js
- Command line
-
hf download hf://spaces/shard-c6/green-guard-api/src/controllers/plant.controller.js
-
curl -L -o plant.controller.js https://huggingface.co/spaces/shard-c6/green-guard-api/resolve/main/src/controllers/plant.controller.js
8.97 kB
| const { supabaseAdmin } = require('../config/supabase'); | |
| const { success, created, error, notFound, forbidden, serverError } = require('../utils/response'); | |
| const { parsePagination } = require('../utils/pagination'); | |
| const { uploadMultipleToStorage } = require('../services/storage.service'); | |
| /** | |
| * POST /api/plants β create a new plant (NGO only) | |
| */ | |
| async function createPlant(req, res) { | |
| try { | |
| const { plant_name, species, description, latitude, longitude, address, planted_date, care_info } = req.body; | |
| // Upload images to Supabase Storage | |
| let imageUrls = []; | |
| if (req.files && req.files.length > 0) { | |
| imageUrls = await uploadMultipleToStorage('plant-images', req.files); | |
| } | |
| // Parse care_info if it's a string (from form-data) | |
| let parsedCareInfo = care_info; | |
| if (typeof care_info === 'string') { | |
| try { parsedCareInfo = JSON.parse(care_info); } catch { parsedCareInfo = null; } | |
| } | |
| const { data, error: dbError } = await supabaseAdmin | |
| .from('plants') | |
| .insert({ | |
| ngo_id: req.user.id, | |
| plant_name, | |
| species: species || null, | |
| description: description || null, | |
| image_urls: imageUrls, | |
| latitude: latitude ? parseFloat(latitude) : null, | |
| longitude: longitude ? parseFloat(longitude) : null, | |
| // `location` is trigger-maintained from latitude/longitude β do not set it here | |
| address: address || null, | |
| planted_date: planted_date || new Date().toISOString().split('T')[0], | |
| care_info: parsedCareInfo || null, | |
| }) | |
| .select() | |
| .single(); | |
| if (dbError) { | |
| console.error('createPlant error:', dbError); | |
| return error(res, dbError.message, 400); | |
| } | |
| return created(res, data); | |
| } catch (err) { | |
| console.error('createPlant error:', err); | |
| return serverError(res); | |
| } | |
| } | |
| /** | |
| * GET /api/plants β list plants (paginated, filterable) | |
| */ | |
| async function listPlants(req, res) { | |
| try { | |
| const { page, limit, offset } = parsePagination(req.query); | |
| const { status, ngo_id } = req.query; | |
| let query = supabaseAdmin | |
| .from('plants') | |
| .select('*, profiles!plants_ngo_id_fkey(username, display_name)', { count: 'exact' }); | |
| if (status) query = query.eq('adoption_status', status); | |
| if (ngo_id) query = query.eq('ngo_id', ngo_id); | |
| const { data, error: dbError, count } = await query | |
| .order('created_at', { ascending: false }) | |
| .range(offset, offset + limit - 1); | |
| if (dbError) return error(res, dbError.message, 400); | |
| return success(res, data, { page, limit, total: count }); | |
| } catch (err) { | |
| console.error('listPlants error:', err); | |
| return serverError(res); | |
| } | |
| } | |
| /** | |
| * GET /api/plants/:id β get single plant details | |
| */ | |
| async function getPlant(req, res) { | |
| try { | |
| const { data, error: dbError } = await supabaseAdmin | |
| .from('plants') | |
| .select('*, profiles!plants_ngo_id_fkey(username, display_name, avatar_url)') | |
| .eq('id', req.params.id) | |
| .single(); | |
| if (dbError || !data) return notFound(res, 'Plant not found'); | |
| return success(res, data); | |
| } catch (err) { | |
| console.error('getPlant error:', err); | |
| return serverError(res); | |
| } | |
| } | |
| /** | |
| * PUT /api/plants/:id β update plant (NGO owner only) | |
| */ | |
| async function updatePlant(req, res) { | |
| try { | |
| const { plant_name, species, description, address, care_info } = req.body; | |
| // Verify ownership | |
| const { data: existing } = await supabaseAdmin | |
| .from('plants') | |
| .select('ngo_id') | |
| .eq('id', req.params.id) | |
| .single(); | |
| if (!existing) return notFound(res, 'Plant not found'); | |
| if (existing.ngo_id !== req.user.id) return forbidden(res, 'You can only edit your own plants'); | |
| const updates = { updated_at: new Date().toISOString() }; | |
| if (plant_name !== undefined) updates.plant_name = plant_name; | |
| if (species !== undefined) updates.species = species; | |
| if (description !== undefined) updates.description = description; | |
| if (address !== undefined) updates.address = address; | |
| if (care_info !== undefined) { | |
| updates.care_info = typeof care_info === 'string' ? JSON.parse(care_info) : care_info; | |
| } | |
| const { data, error: dbError } = await supabaseAdmin | |
| .from('plants') | |
| .update(updates) | |
| .eq('id', req.params.id) | |
| .select() | |
| .single(); | |
| if (dbError) return error(res, dbError.message, 400); | |
| return success(res, data); | |
| } catch (err) { | |
| console.error('updatePlant error:', err); | |
| return serverError(res); | |
| } | |
| } | |
| /** | |
| * DELETE /api/plants/:id β delete plant (NGO owner, only if available) | |
| */ | |
| async function deletePlant(req, res) { | |
| try { | |
| const { data: existing } = await supabaseAdmin | |
| .from('plants') | |
| .select('ngo_id, adoption_status') | |
| .eq('id', req.params.id) | |
| .single(); | |
| if (!existing) return notFound(res, 'Plant not found'); | |
| if (existing.ngo_id !== req.user.id) return forbidden(res, 'You can only delete your own plants'); | |
| if (existing.adoption_status !== 'available') { | |
| return error(res, 'Cannot delete a plant with pending or completed adoptions', 400); | |
| } | |
| // The status filter closes the gap between the check above and the delete: | |
| // an application arriving in between must not lose its plant. | |
| const { data: deleted, error: deleteError } = await supabaseAdmin | |
| .from('plants') | |
| .delete() | |
| .eq('id', req.params.id) | |
| .eq('adoption_status', 'available') | |
| .select('id'); | |
| if (deleteError) { | |
| console.error('deletePlant error:', deleteError); | |
| return serverError(res, 'Could not delete the plant'); | |
| } | |
| if (!deleted || deleted.length === 0) { | |
| return error(res, 'Cannot delete a plant with pending or completed adoptions', 400); | |
| } | |
| return success(res, { message: 'Plant deleted successfully' }); | |
| } catch (err) { | |
| console.error('deletePlant error:', err); | |
| return serverError(res); | |
| } | |
| } | |
| /** | |
| * GET /api/plants/nearby β PostGIS radius search | |
| * Query: ?lat=19.07&lng=72.87&radius=10000 (meters, default 10km) | |
| */ | |
| /** | |
| * GET /api/plants/nearby β PostGIS radius search | |
| * Query: ?lat=19.07&lng=72.87&radius=10000 (meters, default 10km) | |
| * Marks plants the requesting user has adopted so the "near me" map | |
| * can render them in a distinct colour (issue #209). | |
| */ | |
| async function nearbyPlants(req, res) { | |
| try { | |
| const lat = parseFloat(req.query.lat); | |
| const lng = parseFloat(req.query.lng); | |
| const radius = parseInt(req.query.radius, 10) || 10000; | |
| const { data, error: dbError } = await supabaseAdmin.rpc('nearby_plants', { | |
| user_lat: lat, | |
| user_lng: lng, | |
| radius_meters: radius, | |
| }); | |
| if (dbError) { | |
| console.error('nearbyPlants RPC error:', dbError); | |
| return error(res, dbError.message, 400); | |
| } | |
| // Cross-reference against the requesting user's own adoptions so the | |
| // client can render "my plant" markers differently without a second | |
| // round trip. | |
| const { data: myAdoptions } = await supabaseAdmin | |
| .from('adoptions') | |
| .select('plant_id') | |
| .eq('adopter_id', req.user.id) | |
| .eq('status', 'approved'); | |
| const myPlantIds = new Set((myAdoptions || []).map((a) => a.plant_id)); | |
| const enriched = (data || []).map((plant) => ({ | |
| ...plant, | |
| is_mine: myPlantIds.has(plant.id), | |
| })); | |
| return success(res, enriched, { radius_meters: radius, total: enriched.length }); | |
| } catch (err) { | |
| console.error('nearbyPlants error:', err); | |
| return serverError(res); | |
| } | |
| } | |
| /** | |
| * GET /api/plants/map β all plants with location + status for map rendering | |
| */ | |
| async function mapPlants(req, res) { | |
| try { | |
| const { minLng, minLat, maxLng, maxLat } = req.query; | |
| if ([minLng, minLat, maxLng, maxLat].some((v) => v === undefined)) { | |
| return error(res, 'minLng, minLat, maxLng, maxLat query params are required', 400); | |
| } | |
| const bbox = { | |
| min_lng: parseFloat(minLng), | |
| min_lat: parseFloat(minLat), | |
| max_lng: parseFloat(maxLng), | |
| max_lat: parseFloat(maxLat), | |
| }; | |
| if (Object.values(bbox).some((v) => Number.isNaN(v))) { | |
| return error(res, 'bbox query params must be valid numbers', 400); | |
| } | |
| const MAX_MAP_RESULTS = 500; | |
| const { data, error: dbError } = await supabaseAdmin.rpc('plants_in_bbox', { | |
| ...bbox, | |
| max_results: MAX_MAP_RESULTS, | |
| }); | |
| if (dbError) { | |
| console.error('mapPlants RPC error:', dbError); | |
| return error(res, dbError.message, 400); | |
| } | |
| // Authenticated endpoint: cache in the user's browser only, never a shared cache. | |
| res.set('Cache-Control', 'private, max-age=60, stale-while-revalidate=60'); | |
| return success(res, data, { truncated: (data?.length || 0) >= MAX_MAP_RESULTS }); | |
| } catch (err) { | |
| console.error('mapPlants error:', err); | |
| return serverError(res); | |
| } | |
| } | |
| module.exports = { createPlant, listPlants, getPlant, updatePlant, deletePlant, nearbyPlants, mapPlants }; | |