This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
DROP DATABASE IF EXISTS instagram; | |
CREATE DATABASE instagram; | |
USE instagram; | |
CREATE TABLE user_account ( | |
id VARCHAR(255) NOT NULL, | |
user_email VARCHAR(255) NOT NULL, | |
user_password VARCHAR(255) NOT NULL, |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require("dotenv").config(); | |
const cors = require("cors"); | |
const express = require("express"); | |
const multer = require("multer"); | |
const mysql = require("mysql2"); | |
const path = require("path"); | |
const PORT = process.env.PORT || 8080; | |
const app = express(); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const authRoutes = require("./auth"); | |
const userRoutes = require("./users"); | |
const followerRoutes = require("./followers"); | |
const notificationRoutes = require("./notifications"); | |
const postRoutes = require("./posts"); | |
const reactionRoutes = require("./reactions"); | |
module.exports = function ({ app, dbConn, upload }) { | |
authRoutes({ app, dbConn }); | |
userRoutes({ app, dbConn, upload }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module.exports = function ({ app, dbConn }) { | |
app.post("/login", (req, res) => { | |
const { email, password } = req.body; | |
if (email && password) { | |
const sql = "SELECT * FROM user_account WHERE user_email = ? AND user_password = ?"; | |
dbConn.query(sql, [email, password], function (error, response) { | |
if (response && response.length !== 0) { | |
res.status(200).jsonp({ ...response[0] }); | |
} else { | |
res.status(200).jsonp({ message: "Your username or password is not correct" }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
app.post("/users/create", upload.single("avatar"), (req, res, next) => { | |
const file = req.file; | |
if (!file) { | |
res.status(200).jsonp({ | |
message: "Please upload your avatar", | |
}); | |
} else { | |
const avatar = `/${file.filename}`; | |
const { id, email, password, fullname } = req.body; | |
if (email && password && fullname) { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
app.post('/users/followers', (req, res) => { | |
const { numberOfFollowers, id } = req.body; | |
const updateNumberOfFollowersSql = "UPDATE user_account SET user_number_of_followers = ? WHERE id = ?"; | |
dbConn.query(updateNumberOfFollowersSql, [numberOfFollowers, id], function (err, updatedUser) { | |
if (err) { | |
res.status(200).jsonp({ message: "The system error. Please try again" }); | |
} else if (updatedUser) { | |
res.status(200).jsonp({ id }); | |
} | |
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
app.post('/users/posts', (req, res) => { | |
const { numberOfPosts, id } = req.body; | |
const updateNumberOfPostsSql = "UPDATE user_account SET user_number_of_posts = ? WHERE id = ?"; | |
dbConn.query(updateNumberOfPostsSql, [numberOfPosts, id], function (err, updatedUser) { | |
if (err) { | |
res.status(200).jsonp({ message: "The system error. Please try again" }); | |
} else if (updatedUser) { | |
res.status(200).jsonp({ id }); | |
} | |
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
app.get('/users/:id', (req, res) => { | |
const userId = req.params.id; | |
if (!userId) { | |
res.status(200).jsonp({ message: 'Cannot load user information, please try again' }); | |
} | |
const getUserSql = "SELECT * FROM user_account WHERE id = ?"; | |
dbConn.query(getUserSql, [userId], function (error, response) { | |
if (response && response.length) { | |
res.status(200).jsonp(response); | |
} else { |