-
-
Notifications
You must be signed in to change notification settings - Fork 41
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
1 changed file
with
58 additions
and
0 deletions.
There are no files selected for viewing
58 changes: 58 additions & 0 deletions
58
blockchain_integration/PiSure/server/controllers/policy.controller.js
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,58 @@ | ||
import { Policy } from '../models/policy.model'; | ||
import { policyService } from '../services/policy.service'; | ||
|
||
const createPolicy = async (req, res) => { | ||
try { | ||
const policy = new Policy(req.body); | ||
const result = await policyService.createPolicy(policy); | ||
res.status(201).json(result); | ||
} catch (error) { | ||
console.error(error); | ||
res.status(500).json({ message: 'Error creating policy' }); | ||
} | ||
}; | ||
|
||
const getPolicies = async (req, res) => { | ||
try { | ||
const policies = await policyService.getPolicies(); | ||
res.json(policies); | ||
} catch (error) { | ||
console.error(error); | ||
res.status(500).json({ message: 'Error fetching policies' }); | ||
} | ||
}; | ||
|
||
const getPolicy = async (req, res) => { | ||
try { | ||
const id = req.params.id; | ||
const policy = await policyService.getPolicy(id); | ||
res.json(policy); | ||
} catch (error) { | ||
console.error(error); | ||
res.status(404).json({ message: 'Policy not found' }); | ||
} | ||
}; | ||
|
||
const updatePolicy = async (req, res) => { | ||
try { | ||
const id = req.params.id; | ||
const policy = await policyService.updatePolicy(id, req.body); | ||
res.json(policy); | ||
} catch (error) { | ||
console.error(error); | ||
res.status(500).json({ message: 'Error updating policy' }); | ||
} | ||
}; | ||
|
||
const deletePolicy = async (req, res) => { | ||
try { | ||
const id = req.params.id; | ||
await policyService.deletePolicy(id); | ||
res.status(204).json({ message: 'Policy deleted' }); | ||
} catch (error) { | ||
console.error(error); | ||
res.status(500).json({ message: 'Error deleting policy' }); | ||
} | ||
}; | ||
|
||
export { createPolicy, getPolicies, getPolicy, updatePolicy, deletePolicy }; |