Files
yourpart3/backend/controllers/socialnetworkController.js

147 lines
5.5 KiB
JavaScript

import SocialNetworkService from '../services/socialnetworkService.js';
class SocialNetworkController {
constructor() {
this.socialNetworkService = new SocialNetworkService();
this.userSearch = this.userSearch.bind(this);
this.profile = this.profile.bind(this);
this.createFolder = this.createFolder.bind(this);
this.getFolders = this.getFolders.bind(this);
this.uploadImage = this.uploadImage.bind(this);
this.getImage = this.getImage.bind(this);
this.getImageVisibilityTypes = this.getImageVisibilityTypes.bind(this);
this.getFolderImageList = this.getFolderImageList.bind(this);
this.getImageByHash = this.getImageByHash.bind(this);
this.changeImage = this.changeImage.bind(this);
}
async userSearch(req, res) {
try {
const { username, ageFrom, ageTo, genders } = req.body;
const users = await this.socialNetworkService.searchUsers({ username, ageFrom, ageTo, genders });
res.status(200).json(users);
} catch (error) {
console.error('Error in userSearch:', error);
res.status(500).json({ error: error.message });
}
}
async profile(req, res) {
try {
const { userId } = req.params;
const requestingUserId = req.headers.userid;
if (!userId || !requestingUserId) {
return res.status(400).json({ error: 'Invalid user or requesting user ID.' });
}
const profile = await this.socialNetworkService.getProfile(userId, requestingUserId);
res.status(200).json(profile);
} catch (error) {
console.error('Error in profile:', error);
res.status(500).json({ error: error.message });
}
}
async createFolder(req, res) {
try {
const userId = req.headers.userid;
const folderData = req.body;
const folder = await this.socialNetworkService.createFolder(userId, folderData);
res.status(201).json(folder);
} catch (error) {
console.error('Error in createFolder:', error);
res.status(500).json({ error: error.message });
}
}
async getFolders(req, res) {
try {
const userId = req.headers.userid;
const folders = await this.socialNetworkService.getFolders(userId);
res.status(200).json(folders);
} catch (error) {
console.error('Error in getFolders:', error);
res.status(500).json({ error: error.message });
}
}
async getFolderImageList(req, res) {
try {
const userId = req.headers.userid;
const { folderId } = req.params;
const images = await this.socialNetworkService.getFolderImageList(userId, folderId);
res.status(200).json(images);
} catch (error) {
console.error('Error in getFolderImageList:', error);
res.status(500).json({ error: error.message });
}
}
async uploadImage(req, res) {
try {
const userId = req.headers.userid;
const file = req.file;
const formData = req.body;
const image = await this.socialNetworkService.uploadImage(userId, file, formData);
res.status(201).json(image);
} catch (error) {
console.error('Error in uploadImage:', error);
res.status(500).json({ error: error.message });
}
}
async getImage(req, res) {
try {
const { imageId } = req.params;
const image = await this.socialNetworkService.getImage(imageId);
res.status(200).json(image);
} catch (error) {
console.error('Error in getImage:', error);
res.status(500).json({ error: error.message });
}
}
async getImageVisibilityTypes(req, res) {
try {
const types = await this.socialNetworkService.getPossibleImageVisibilities();
res.status(200).json(types);
} catch (error) {
console.log(error);
res.status(500).json({ error: error.message });
}
}
async getImageByHash(req, res) {
try {
const userId = req.headers.userid;
const { hash } = req.params;
const filePath = await this.socialNetworkService.getImageFilePath(userId, hash);
console.log(filePath);
res.sendFile(filePath, err => {
if (err) {
console.error('Error sending file:', err);
res.status(500).json({ error: 'Error sending file' });
}
});
} catch (error) {
console.error('Error in getImageByHash:', error);
res.status(403).json({ error: error.message || 'Access denied or image not found' });
}
}
async changeImage(req, res) {
try {
const userId = req.headers.userid;
const { imageId } = req.params;
const { title, visibilities } = req.body;
const folderId = await this.socialNetworkService.changeImage(userId, imageId, title, visibilities);
console.log('--->', folderId);
res.status(201).json(await this.socialNetworkService.getFolderImageList(userId, folderId));
} catch (error) {
console.error('Error in getImageByHash:', error);
res.status(403).json({ error: error.message || 'Access denied or image not found' });
}
}
}
export default SocialNetworkController;