Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mood #2

Open
wants to merge 9 commits into
base: main
Choose a base branch
from
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
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
2 changes: 1 addition & 1 deletion Server/.env
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
PORT=3001
PORT=8080
MONGO_URL="mongodb://localhost:27017/chatDB"
TOKEN_KEY="flightlieutenantakroutgustywingman"
password = "S*$E(x4,R<vGM3L<"
Expand Down
2 changes: 1 addition & 1 deletion Server/controllers/messageController.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ module.exports.getMessage = async (req, res, next) => {
try {
const { from, to } = req.body;

// Find messages between the specified users, sorted by updatedAt in ascending order
// Find messages between the specified sender and receiver, sorted by createdAt in ascending order
const selectQuery = `
SELECT m.message AS message, m.createdAt AS time,
(m.sender_mail = ?) AS fromSelf
Expand Down
52 changes: 52 additions & 0 deletions Server/controllers/moodController.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
const connection = require('../db');

// Controller method to add a new mood
module.exports.addMood = async (req, res, next) => {
try {
const { sender_mail, receiver_mail, mood_id } = req.body;

// Create a new mood entry in the database
const insertQuery = `
INSERT INTO mood_table (sender_mail, receiver_mail, mood_id)
VALUES (?, ?, ?);
`;
const values = [sender_mail, receiver_mail, mood_id];

connection.query(insertQuery, values, (err, result) => {
if (err) {
console.error('Error adding mood: ', err);
return res.json({ msg: 'Failed to add mood' });
}

return res.json({ msg: 'Mood added successfully' });
});
} catch (ex) {
next(ex); // Pass the error to the next error-handling middleware
}
};

// Controller method to retrieve moods between two users
module.exports.getMoods = async (req, res, next) => {
try {
const { sender_mail, receiver_mail } = req.body;

// Find moods between the specified users
const selectQuery = `
SELECT *
FROM mood_table
WHERE sender_mail = ? AND receiver_mail = ?;
`;
const values = [sender_mail, receiver_mail];

connection.query(selectQuery, values, (err, rows) => {
if (err) {
console.error('Error retrieving moods: ', err);
return res.json([]);
}

res.json(rows);
});
} catch (ex) {
next(ex); // Pass the error to the next error-handling middleware
}
};
44 changes: 15 additions & 29 deletions Server/index.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
// Import necessary dependencies
const express = require("express");
const cors = require("cors");
const mongoose = require("mongoose");
const http = require("http");
const socketIO = require("socket.io");
const cookieParser = require("cookie-parser");
const connection = require("./db");
const app = express();
const initSocketServer = require("./socket");

// Set up CORS to allow requests from the client
// app.use(cors({ origin: "https://swingyy-c0af7.web.app", credentials: true }));
app.use(cors({ origin: "http://localhost:3000", credentials: true }));

// Load environment variables from the .env file
Expand All @@ -17,49 +16,36 @@ require("dotenv").config();
// Import route files
const userRoutes = require("./routes/userRoutes");
const messageRoutes = require("./routes/messageRoutes");
const moodRoutes = require("./routes/moodRoutes");

// Create an instance of the Express application

const server = http.createServer(app);
// const io = socketIO(server);
const io = require("socket.io")(server, {
cors: {
origin: "http://localhost:3000",
methods: ["GET", "POST"],
},
});

// Set up middleware and configuration
app.use(express.json());
app.use(cookieParser());

// Define routes

app.use("/api/auth", userRoutes);
app.use("/api/messages", messageRoutes);
app.use("/api/mood", moodRoutes);
app.get("/api/test", (req, res) => {
connection.query('SELECT * FROM user_table', function (error, results, fields) {
if (error) throw error;

console.log('The solution is: ', results);
res.json(results);
});
});
connection.query('SELECT * FROM user_table', function (error, results, fields) {
if (error) throw error;


// Set up Socket.IO server and define event handlers for real-time chat
io.on("connection", (socket) => {
socket.on("add-user", (userId) => {
socket.join(userId); // Join a room with the user's ID for private messaging
});

socket.on("send-msg", (data) => {
io.to(data.to).emit("msg-received", data.message); // Emit the message to the specified user's room
console.log('The solution is: ', results);
res.json(results);
});
});

// Start the server
const port = process.env.PORT || 3001;
const port = process.env.PORT || 8080;
server.listen(port, () => {
console.log(`Server connected on port: ${port}`);
});

// Initialize Socket.IO server
initSocketServer(server);

// Export the server for testing purposes
module.exports = server;
59 changes: 59 additions & 0 deletions Server/model/moodModel.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
const mongoose = require('mongoose');

// Controller method to send a mood
module.exports.sendMood = async (req, res, next) => {
try {
const { moodNumber, to } = req.body;

// Validate the moodNumber
if (typeof moodNumber !== 'number' || moodNumber < 1 || moodNumber > 6) {
return res.json({ status: false, message: 'Invalid mood number' });
}

// Emit the mood to the specified user's room
io.to(to).emit('mood-received', moodNumber);

return res.json({ status: true, message: 'Mood sent successfully' });
} catch (ex) {
next(ex); // Pass the error to the next error-handling middleware
}
};

// Controller method to handle the received mood
module.exports.handleMood = async (socket, moodNumber) => {
try {
// Handle the mood based on the moodNumber
switch (moodNumber) {
case 1:
console.log('Received mood: Happy');
// Handle happy mood logic here
break;
case 2:
console.log('Received mood: Sad');
// Handle sad mood logic here
break;
case 3:
console.log('Received mood: Excited');
// Handle excited mood logic here
break;
case 4:
console.log('Received mood: Angry');
// Handle angry mood logic here
break;
case 5:
console.log('Received mood: Funny');
// Handle funny mood logic here
break;
case 6:
console.log('Received mood: Love');
// Handle love mood logic here
break;
default:
console.log('Invalid mood number');
break;
}
} catch (ex) {
console.error('Error handling mood:', ex);
}
};

12 changes: 12 additions & 0 deletions Server/node_modules/.bin/JSONStream

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 17 additions & 0 deletions Server/node_modules/.bin/JSONStream.cmd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

28 changes: 28 additions & 0 deletions Server/node_modules/.bin/JSONStream.ps1

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 12 additions & 0 deletions Server/node_modules/.bin/base64url

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 17 additions & 0 deletions Server/node_modules/.bin/base64url.cmd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

28 changes: 28 additions & 0 deletions Server/node_modules/.bin/base64url.ps1

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 12 additions & 0 deletions Server/node_modules/.bin/gcs-upload

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 17 additions & 0 deletions Server/node_modules/.bin/gcs-upload.cmd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

28 changes: 28 additions & 0 deletions Server/node_modules/.bin/gcs-upload.ps1

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading