Skip to content

Commit

Permalink
updated feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
arpita-32 committed Nov 4, 2024
2 parents 2b0df0f + bd1a20e commit 904bc0b
Show file tree
Hide file tree
Showing 64 changed files with 4,152 additions and 1,962 deletions.
6 changes: 6 additions & 0 deletions .hintrc
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,12 @@
"backdrop-filter"
]
}
],
"axe/name-role-value": [
"default",
{
"link-name": "off"
}
]
}
}
3 changes: 3 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"liveServer.settings.port": 5501
}
17 changes: 15 additions & 2 deletions backend/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,36 @@ import dotenv from "dotenv";
import connectDB from "./utils/db.js";
import blogRoutes from "./routes/blogRoutes.js";
import userRoutes from "./routes/userRoutes.js";
import feedbackRoutes from "./routes/feebackroute.js";
import feedbackRoutes from "./routes/feedbackRoute.js";
import contactRoutes from "./routes/contactRoute.js";
import getInTouch from "./routes/getInTouchRoutes.js";
import addBlog from "./routes/addBlogRoutes.js";
import cors from "cors";
import path from "path"; // Import path module
import { fileURLToPath } from "url"; // Import fileURLToPath

dotenv.config();
const app = express();
connectDB();

app.use(express.json());

// to avoid cross origin errror
// to avoid cross-origin error
app.use(cors());

// Define __dirname for ES module
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);

// Serve static files from the uploads directory
app.use("/uploads", express.static(path.join(__dirname, "uploads"))); // Adjust path as necessary

app.use("/api/users", userRoutes);
app.use("/api/blogs", blogRoutes);
app.use("/api/feedback", feedbackRoutes);
app.use("/api/contact", contactRoutes);
app.use("/api/getInTouch", getInTouch);
app.use("/api/addBlog", addBlog);

const PORT = process.env.PORT || 5000;
app.listen(PORT, () => {
Expand Down
107 changes: 107 additions & 0 deletions backend/controllers/addBlogController.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
import BlogPost from '../models/addBlog.js';
import multer from 'multer';
import path from 'path';
import fs from 'fs';

// Configure multer for file uploads
const storage = multer.diskStorage({
destination: (req, file, cb) => {
cb(null, 'uploads/'); // Ensure this directory exists
},
filename: (req, file, cb) => {
// Set the file name to be unique
const uniqueSuffix = Date.now() + '-' + Math.round(Math.random() * 1E9);
cb(null, uniqueSuffix + path.extname(file.originalname)); // Preserve the original file extension
}
});

const upload = multer({ storage: storage });

// Function to save a new blog post
export async function saveBlog(req, res) {
try {
const { title, category, summary, excerpt, tags, publish, featuredImage } = req.body;

// If an image is uploaded, use its path
let imagePath = req.file ? req.file.path : null;

// Handle base64 image if provided
if (featuredImage && featuredImage.startsWith('data:image/')) {
// Extract the base64 part of the image data
const base64Image = featuredImage.split(';base64,').pop();
// Convert base64 string to Buffer
const buffer = Buffer.from(base64Image, 'base64');

// Create a path to save the image
const imagePathFromBase64 = `uploads/${Date.now()}.png`; // Use a unique name based on timestamp

// Save the image buffer to a file
await fs.promises.writeFile(imagePathFromBase64, buffer);
imagePath = imagePathFromBase64; // Set imagePath to the saved file path
}

// Log request body and file info


// Check if required fields are provided
if (!title || !category || !summary || !excerpt) {
return res.status(400).json({ message: "All required fields must be filled out." });
}

// Create a new blog document
const newBlog = new BlogPost({
title,
category,
summary,
excerpt,
tags,
publish,
featuredImage: imagePath // Use the determined image path
});

console.log(newBlog);

// Save the blog post to the database
await newBlog.save();

// Respond with success message
res.status(201).json({
message: "Blog post created successfully!",
data: newBlog
});
} catch (error) {
console.error("Error saving blog post:", error);
res.status(500).json({ message: "Failed to create blog post.", error });
}
}


// Function to retrieve all blog posts
export async function getAllBlog(req, res) {
try {
const blogs = await BlogPost.find(); // Retrieve all blog posts
res.status(200).json(blogs); // Respond with the list of blog posts
} catch (error) {
console.error("Error retrieving blog posts:", error);
res.status(500).json({ message: "Failed to retrieve blog posts.", error });
}
}

// Function to retrieve a specific blog post by ID
export async function getBlog(req, res) {
try {
const { id } = req.params;

// Retrieve the specific blog post by ID
const blog = await BlogPost.findById(id);
if (!blog) {
return res.status(404).json({ message: "Blog post not found." });
}
return res.status(200).json(blog);
} catch (error) {
console.error("Error retrieving blog post:", error);
res.status(500).json({ message: "Failed to retrieve blog post.", error });
}
}

export { upload };
49 changes: 49 additions & 0 deletions backend/controllers/feedController.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import Feedback from "../models/feedback.js";

// Controller to handle feedback submission
export async function submitFeedback(req, res) { // Changed resp to res
console.log('feedback form getting submit')
try {
// Extract feedback data from the request body
const {
overallExperience,
featuresUsed,
mostHelpfulFeature,
improvement,
newFeatures,
recommendation,
additionalComments
} = req.body;

// Validate required fields
if (!overallExperience || !featuresUsed || !mostHelpfulFeature || !recommendation) {
return res.status(400).json({ message: 'Please provide all required fields.' });
}

// Create a new Feedback instance with the extracted data
const feedback = new Feedback({
overallExperience,
featuresUsed,
mostHelpfulFeature,
improvement,
newFeatures,
recommendation,
additionalComments
});

// Save the feedback data to MongoDB
await feedback.save();

// Respond with a success message
res.status(201).json({ message: 'Feedback submitted successfully', feedback });
} catch (error) {
// Handle errors and send a failure response
console.error('Error saving feedback:', error);
res.status(500).json({ message: 'Failed to submit feedback', error: error.message });
}
}


export async function getFeed(req, res) {
res.send("feedback form")
}
24 changes: 0 additions & 24 deletions backend/controllers/feedbackcontroller.js

This file was deleted.

31 changes: 31 additions & 0 deletions backend/controllers/getInTouchController.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// Import the GetInTouch model
import GetInTouch from '../models/getInTouch.js';

// Controller function to handle form submission
export const submitGetInTouch = async (req, res) => {
try {
// Create a new GetInTouch document using request data
const { name, email, message } = req.body;

// Validate request data
if (!name || !email || !message) {
return res.status(400).json({ error: 'All fields are required.' });
}

const newMessage = new GetInTouch({
name,
email,
message,
});

// Save the new document to the database
await newMessage.save();

// Send a success response
res.status(201).json({ message: 'Thank you! Your message has been received.' });
} catch (error) {
// Handle errors
console.error('Error submitting message:', error);
res.status(500).json({ error: 'There was an error submitting your message. Please try again.' });
}
};
54 changes: 54 additions & 0 deletions backend/models/addBlog.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import mongoose from "mongoose";

const blogPostSchema = new mongoose.Schema({
title: {
type: String,
required: true,
trim: true
},
category: {
type: String,
required: true,
enum: ['vocabulary', 'grammar', 'pronunciation', 'culture', 'learning-tips'] // Add more categories as needed
},
summary: {
type: String,
required: true,
trim: true
},
excerpt: {
type: String,
required: true,
trim: true
},
tags: {
type: [String],
default: []
},
publish: {
type: Boolean,
default: false
},
featuredImage: {
type: String,
default: null
},
createdAt: {
type: Date,
default: Date.now
},
updatedAt: {
type: Date,
default: Date.now
}
});

// Add a pre-save hook to update the `updatedAt` field
blogPostSchema.pre('save', function (next) {
this.updatedAt = Date.now();
next();
});

const BlogPost = mongoose.model('BlogPost', blogPostSchema);

export default BlogPost;
Loading

0 comments on commit 904bc0b

Please sign in to comment.