-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
111 additions
and
21 deletions.
There are no files selected for viewing
This file contains 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 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 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 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,87 @@ | ||
import express from 'express' | ||
import supabase from '../supabaseClient' | ||
|
||
const router = express.Router() | ||
|
||
// Get all reports | ||
router.get('/all', async (req, res) => { | ||
try { | ||
const { data, error } = await supabase.from('reports').select('*'); | ||
if (error) throw new Error(error.message); | ||
res.json(data); | ||
} catch (error: unknown) { | ||
res.status(500).json({ error: error instanceof Error ? error.message : 'An unknown error occurred' }); | ||
} | ||
}); | ||
|
||
// Get reports for a specific user | ||
router.get('/user/:userId', async (req, res) => { | ||
const userId = parseInt(req.params.userId); | ||
try { | ||
const { data, error } = await supabase | ||
.from('reports') | ||
.select('*') | ||
.eq('user_id', userId); | ||
if (error) throw new Error(error.message); | ||
res.json(data); | ||
} catch (error: unknown) { | ||
res.status(500).json({ error: error instanceof Error ? error.message : 'An unknown error occurred' }); | ||
} | ||
}); | ||
|
||
// Create a new report | ||
router.post('/', async (req, res) => { | ||
try { | ||
const { data, error } = await supabase | ||
.from('reports') | ||
.insert([req.body]) | ||
.select() | ||
if (error) throw new Error(error.message) | ||
res.status(201).json(data[0]) | ||
} catch (error: unknown) { | ||
res.status(500).json({ | ||
error: | ||
error instanceof Error ? error.message : 'An unknown error occurred', | ||
}) | ||
} | ||
}); | ||
|
||
// Update a report | ||
router.put('/:id', async (req, res) => { | ||
const reportId = parseInt(req.params.id); | ||
try { | ||
const { data, error } = await supabase | ||
.from('reports') | ||
.update(req.body) | ||
.eq('id', reportId) | ||
.select() | ||
if (error) throw new Error(error.message) | ||
if (data.length === 0) return res.status(404).json({ error: 'Report not found' }) | ||
res.json(data[0]) | ||
} catch (error: unknown) { | ||
res.status(500).json({ | ||
error: | ||
error instanceof Error ? error.message : 'An unknown error occurred', | ||
}) | ||
} | ||
}); | ||
|
||
// Delete a report | ||
router.delete('/:id', async (req, res) => { | ||
const reportId = parseInt(req.params.id); | ||
try { | ||
const { error } = await supabase | ||
.from('reports') | ||
.delete() | ||
.eq('id', reportId) | ||
if (error) throw new Error(error.message) | ||
res.status(204).send() | ||
} catch (error: unknown) { | ||
res.status(500).json({ | ||
error: | ||
error instanceof Error ? error.message : 'An unknown error occurred', | ||
}) | ||
} | ||
}); | ||
|
||
export default router |
This file contains 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