Files
yourpart3/backend/controllers/friendshipController.js
Torsten Schulz (local) 1ab9407e79
All checks were successful
Deploy to production / deploy (push) Successful in 2m56s
Refactor code structure for improved readability and maintainability; optimize performance across multiple modules.
2026-07-21 09:08:15 +02:00

92 lines
3.5 KiB
JavaScript
Executable File

import friendshipService from '../services/friendshipService.js';
import Joi from 'joi';
const friendshipController = {
async endFriendship(req, res) {
const schema = Joi.object({
friendUserId: Joi.string().required()
});
const { error, value } = schema.validate(req.body);
if (error) {
return res.status(400).json({ error: error.details[0].message });
}
try {
const { userid: hashedUserId } = req.headers;
await friendshipService.endFriendship(hashedUserId, value.friendUserId);
res.status(200).json({ message: 'Friendship ended successfully' });
} catch (error) {
console.error('Error in endFriendship:', error);
res.status(400).json({ error: error.message });
}
},
async acceptFriendship(req, res) {
const schema = Joi.object({
friendUserId: Joi.string().required()
});
const { error, value } = schema.validate(req.body);
if (error) {
return res.status(400).json({ error: error.details[0].message });
}
try {
const { userid: hashedUserId } = req.headers;
await friendshipService.acceptFriendship(hashedUserId, value.friendUserId);
res.status(200).json({ message: 'Friendship accepted successfully' });
} catch (error) {
console.error('Error in acceptFriendship:', error);
res.status(400).json({ error: error.message });
}
},
async rejectFriendship(req, res) {
const schema = Joi.object({
friendUserId: Joi.string().required()
});
const { error, value } = schema.validate(req.body);
if (error) {
return res.status(400).json({ error: error.details[0].message });
}
try {
const { userid: hashedUserId } = req.headers;
await friendshipService.rejectFriendship(hashedUserId, value.friendUserId);
res.status(200).json({ message: 'Friendship rejected successfully' });
} catch (error) {
console.error('Error in rejectFriendship:', error);
res.status(400).json({ error: error.message });
}
},
async withdrawRequest(req, res) {
const schema = Joi.object({
friendUserId: Joi.string().required()
});
const { error, value } = schema.validate(req.body);
if (error) {
return res.status(400).json({ error: error.details[0].message });
}
try {
const { userid: hashedUserId } = req.headers;
await friendshipService.withdrawRequest(hashedUserId, value.friendUserId);
res.status(200).json({ message: 'Friendship request withdrawn successfully' });
} catch (error) {
console.error('Error in withdrawRequest:', error);
res.status(400).json({ error: error.message });
}
},
async getFriendships(req, res) {
try {
const { userid: hashedUserId } = req.headers;
const { acceptedOnly } = req.query;
console.log('Friendships:', acceptedOnly);
const friendships = await friendshipService.getFriendships(hashedUserId, acceptedOnly === 'true');
res.status(200).json(friendships);
} catch (error) {
console.error('Error in getFriendships:', error);
res.status(400).json({ error: error.message });
}
},
};
export default friendshipController;