Skip to content

Commit

Permalink
Create policy.controller.js
Browse files Browse the repository at this point in the history
  • Loading branch information
KOSASIH authored Aug 6, 2024
1 parent 41cf745 commit 385d65b
Showing 1 changed file with 58 additions and 0 deletions.
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 };

0 comments on commit 385d65b

Please sign in to comment.