Changed controllers to classes, added image functionality
This commit is contained in:
@@ -1,79 +1,94 @@
|
||||
import AdminService from '../services/adminService.js';
|
||||
import Joi from 'joi';
|
||||
|
||||
export const getOpenInterests = async (req, res) => {
|
||||
try {
|
||||
const { userid: userId} = req.headers;
|
||||
const openInterests = await AdminService.getOpenInterests(userId);
|
||||
res.status(200).json(openInterests);
|
||||
} catch (error) {
|
||||
res.status(403).json({error: error.message });
|
||||
class AdminController {
|
||||
constructor() {
|
||||
this.getOpenInterests = this.getOpenInterests.bind(this);
|
||||
this.changeInterest = this.changeInterest.bind(this);
|
||||
this.deleteInterest = this.deleteInterest.bind(this);
|
||||
this.changeTranslation = this.changeTranslation.bind(this);
|
||||
this.getOpenContacts = this.getOpenContacts.bind(this);
|
||||
this.answerContact = this.answerContact.bind(this);
|
||||
}
|
||||
}
|
||||
|
||||
export const changeInterest = async (req, res) => {
|
||||
try {
|
||||
const { userid: userId } = req.headers;
|
||||
const { id: interestId, active, adult: adultOnly} = req.body;
|
||||
AdminService.changeInterest(userId, interestId, active, adultOnly);
|
||||
res.status(200).json(AdminService.getOpenInterests(userId));
|
||||
} catch (error) {
|
||||
res.status(403).json({ error: error.message });
|
||||
}
|
||||
}
|
||||
|
||||
export const deleteInterest = async (req, res) => {
|
||||
try {
|
||||
const { userid: userId } = req.headers;
|
||||
const { id: interestId } = req.params;
|
||||
AdminService.deleteInterest(userId, interestId);
|
||||
res.status(200).json(AdminService.getOpenInterests(userId));
|
||||
} catch (error) {
|
||||
res.status(403).json({ error: error.message });
|
||||
}
|
||||
}
|
||||
|
||||
export const changeTranslation = async (req, res) => {
|
||||
try {
|
||||
const { userid: userId } = req.headers;
|
||||
const { id: interestId, translations } = req.body;
|
||||
AdminService.changeTranslation(userId, interestId, translations);
|
||||
res.status(200).json(AdminService.getOpenInterests(userId))
|
||||
} catch(error) {
|
||||
res.status(403).json({ error: error.message });
|
||||
}
|
||||
}
|
||||
|
||||
export const getOpenContacts = async (req, res) => {
|
||||
try {
|
||||
const { userid: userId } = req.headers;
|
||||
const openContacts = await AdminService.getOpenContacts(userId);
|
||||
res.status(200).json(openContacts);
|
||||
} catch (error) {
|
||||
res.status(403).json({ error: error.message });
|
||||
}
|
||||
}
|
||||
|
||||
export const answerContact = async (req, res) => {
|
||||
try {
|
||||
const schema = Joi.object({
|
||||
id: Joi.number().integer().required(),
|
||||
answer: Joi.string().min(1).required()
|
||||
});
|
||||
|
||||
const { error, value } = schema.validate(req.body);
|
||||
|
||||
if (error) {
|
||||
return res.status(400).json({ error: error.details[0].message });
|
||||
async getOpenInterests(req, res) {
|
||||
try {
|
||||
const { userid: userId } = req.headers;
|
||||
const openInterests = await AdminService.getOpenInterests(userId);
|
||||
res.status(200).json(openInterests);
|
||||
} catch (error) {
|
||||
res.status(403).json({ error: error.message });
|
||||
}
|
||||
|
||||
const { id, answer } = value;
|
||||
|
||||
await AdminService.answerContact(id, answer);
|
||||
|
||||
res.status(200).json({ status: 'ok' });
|
||||
} catch (error) {
|
||||
console.error('Error in answerContact:', error);
|
||||
res.status(error.status || 500).json({ error: error.message || 'Internal Server Error' });
|
||||
}
|
||||
};
|
||||
|
||||
async changeInterest(req, res) {
|
||||
try {
|
||||
const { userid: userId } = req.headers;
|
||||
const { id: interestId, active, adult: adultOnly } = req.body;
|
||||
await AdminService.changeInterest(userId, interestId, active, adultOnly);
|
||||
const updatedInterests = await AdminService.getOpenInterests(userId);
|
||||
res.status(200).json(updatedInterests);
|
||||
} catch (error) {
|
||||
res.status(403).json({ error: error.message });
|
||||
}
|
||||
}
|
||||
|
||||
async deleteInterest(req, res) {
|
||||
try {
|
||||
const { userid: userId } = req.headers;
|
||||
const { id: interestId } = req.params;
|
||||
await AdminService.deleteInterest(userId, interestId);
|
||||
const updatedInterests = await AdminService.getOpenInterests(userId);
|
||||
res.status(200).json(updatedInterests);
|
||||
} catch (error) {
|
||||
res.status(403).json({ error: error.message });
|
||||
}
|
||||
}
|
||||
|
||||
async changeTranslation(req, res) {
|
||||
try {
|
||||
const { userid: userId } = req.headers;
|
||||
const { id: interestId, translations } = req.body;
|
||||
await AdminService.changeTranslation(userId, interestId, translations);
|
||||
const updatedInterests = await AdminService.getOpenInterests(userId);
|
||||
res.status(200).json(updatedInterests);
|
||||
} catch (error) {
|
||||
res.status(403).json({ error: error.message });
|
||||
}
|
||||
}
|
||||
|
||||
async getOpenContacts(req, res) {
|
||||
try {
|
||||
const { userid: userId } = req.headers;
|
||||
const openContacts = await AdminService.getOpenContacts(userId);
|
||||
res.status(200).json(openContacts);
|
||||
} catch (error) {
|
||||
res.status(403).json({ error: error.message });
|
||||
}
|
||||
}
|
||||
|
||||
async answerContact(req, res) {
|
||||
try {
|
||||
const schema = Joi.object({
|
||||
id: Joi.number().integer().required(),
|
||||
answer: Joi.string().min(1).required(),
|
||||
});
|
||||
|
||||
const { error, value } = schema.validate(req.body);
|
||||
|
||||
if (error) {
|
||||
return res.status(400).json({ error: error.details[0].message });
|
||||
}
|
||||
|
||||
const { id, answer } = value;
|
||||
|
||||
await AdminService.answerContact(id, answer);
|
||||
res.status(200).json({ status: 'ok' });
|
||||
} catch (error) {
|
||||
console.error('Error in answerContact:', error);
|
||||
res.status(error.status || 500).json({ error: error.message || 'Internal Server Error' });
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export default AdminController;
|
||||
|
||||
Reference in New Issue
Block a user