Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
514 changes: 514 additions & 0 deletions controller/communityController.js

Large diffs are not rendered by default.

77 changes: 77 additions & 0 deletions controller/communityImageController.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
require('dotenv').config();
const multer = require('multer');
const path = require('path');

// Configure multer for memory storage
const storage = multer.memoryStorage();
const upload = multer({
storage: storage,
limits: {
fileSize: 5 * 1024 * 1024, // 5MB limit
},
fileFilter: (req, file, cb) => {
const allowedTypes = ['image/jpeg', 'image/jpg', 'image/png', 'image/gif', 'image/webp'];
if (allowedTypes.includes(file.mimetype)) {
cb(null, true);
} else {
cb(new Error('Only image files (JPEG, PNG, GIF, WebP) are allowed'), false);
}
}
}).single('image');

// Upload community post image
const uploadCommunityImage = async (req, res) => {
upload(req, res, async (err) => {
if (err) {
return res.status(400).json({
success: false,
error: err.message
});
}

if (!req.file) {
return res.status(400).json({
success: false,
error: 'No image file provided'
});
}

try {
const { user_id } = req.body;

if (!user_id) {
return res.status(400).json({
success: false,
error: 'User ID is required'
});
}

// For now, let's use a simpler approach - convert image to base64
// This avoids Supabase storage permission issues
const base64Image = req.file.buffer.toString('base64');
const mimeType = req.file.mimetype;
const dataUrl = `data:${mimeType};base64,${base64Image}`;

return res.status(200).json({
success: true,
message: 'Image processed successfully',
data: {
image_url: dataUrl,
file_name: req.file.originalname,
file_size: req.file.size
}
});

} catch (error) {
console.error('Community image upload error:', error);
return res.status(500).json({
success: false,
error: 'Internal server error during image upload'
});
}
});
};

module.exports = {
uploadCommunityImage
};
113 changes: 113 additions & 0 deletions controller/communityShareController.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
const { supabase } = require('../database/supabase');

// Share a community post
const sharePost = async (req, res) => {
try {
const { user_id, post_id, share_platform = 'copy_link' } = req.body;

if (!user_id || !post_id) {
return res.status(400).json({
success: false,
error: 'User ID and Post ID are required'
});
}

// Check if post exists
const { data: post, error: postError } = await supabase
.from('community_posts')
.select('id')
.eq('id', post_id)
.single();

if (postError || !post) {
return res.status(404).json({
success: false,
error: 'Post not found'
});
}

// Create share record
const { data: shareData, error: shareError } = await supabase
.from('community_shares')
.insert([{
user_id: parseInt(user_id),
post_id: parseInt(post_id),
share_platform: share_platform
}])
.select();

if (shareError) {
console.error('Error creating share:', shareError);
return res.status(500).json({
success: false,
error: 'Failed to record share',
details: shareError.message
});
}

// Update post shares count
const { data: sharesCount, error: countError } = await supabase
.from('community_shares')
.select('id', { count: 'exact' })
.eq('post_id', post_id);

if (!countError && sharesCount) {
await supabase
.from('community_posts')
.update({ shares_count: sharesCount.length })
.eq('id', post_id);
}

return res.status(201).json({
success: true,
message: 'Post shared successfully',
data: shareData[0]
});

} catch (error) {
console.error('Share post error:', error);
return res.status(500).json({
success: false,
error: 'Internal server error'
});
}
};

// Get share count for a post
const getShareCount = async (req, res) => {
try {
const { postId } = req.params;

const { data: shares, error } = await supabase
.from('community_shares')
.select('id', { count: 'exact' })
.eq('post_id', postId);

if (error) {
console.error('Error getting share count:', error);
return res.status(500).json({
success: false,
error: 'Failed to get share count'
});
}

return res.status(200).json({
success: true,
data: {
share_count: shares ? shares.length : 0
}
});

} catch (error) {
console.error('Get share count error:', error);
return res.status(500).json({
success: false,
error: 'Internal server error'
});
}
};

module.exports = {
sharePost,
getShareCount
};
Loading
Loading