Files
yourpart3/backend/controllers/friendshipController.js
Torsten Schulz (local) 19ee6ba0a1 Add password reset localization and chat configuration
- Implemented German and English localization for password reset functionality.
- Added WebSocket URL resolution logic in chat services to support various environments and configurations.
- Created centralized chat configuration for event keys and payload mappings.
- Developed RoomsView component for admin chat room management, including create, edit, and delete functionalities.
2025-08-18 07:44:56 +02:00

92 lines
3.5 KiB
JavaScript

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;