Skip to content

Commit

Permalink
Finish Debugging man oh man
Browse files Browse the repository at this point in the history
  • Loading branch information
Edward-J-Xu committed Mar 18, 2023
1 parent b07731f commit 7035e45
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 11 deletions.
2 changes: 1 addition & 1 deletion client/src/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ function App() {
element={<CreatePost />}
></Route>
<Route path="login" element={<Login />}></Route>
<Route path="/profile/:id" component={<Profile />} />
<Route path="/profile/:id" element={<Profile />} />
<Route
path="registration"
element={<Registration />}
Expand Down
8 changes: 5 additions & 3 deletions client/src/pages/Profile.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,16 @@ import { useParams, useNavigate } from "react-router-dom";
import axios from "axios";

function Profile() {

let { id } = useParams();
let history = useNavigate();
const [username, setUsername] = useState("");
const [listOfPosts, setListOfPosts] = useState([]);

useEffect(() => {

axios.get(`http://localhost:3001/auth/basicinfo/${id}`).then((response) => {
setUsername(response.data.username);
setUsername(response.data[0][0].username);
});

axios.get(`http://localhost:3001/posts/byuserId/${id}`).then((response) => {
Expand All @@ -32,15 +34,15 @@ function Profile() {
<div
className="body"
onClick={() => {
history.push(`/post/${value.id}`);
history(`/post/${value.id}`);
}}
>
{value.postText}
</div>
<div className="footer">
<div className="username">{value.username}</div>
<div className="buttons">
<label> {value.Likes.length}</label>
<label> {value.likeCount}</label>
</div>
</div>
</div>
Expand Down
7 changes: 3 additions & 4 deletions server/createTable.sql
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@ create table if not exists posts
id int primary key auto_increment,
title varchar(50) not null,
postText varchar(500) not null,
username varchar(15) not null,
constraint post_user foreign key (username) references users (username) on delete cascade
username varchar(15) not null
);

create table if not exists users
Expand All @@ -29,7 +28,7 @@ create table if not exists likes
(
id int primary key auto_increment,
post_id int not null,
username int not null,
user_id int not null,
constraint like_post_id foreign key (post_id) references posts (id) on delete cascade,
constraint like_username foreign key (username) references users (username) on delete cascade
constraint like_user_id foreign key (user_id) references users (id) on delete cascade
);
14 changes: 11 additions & 3 deletions server/routes/Posts.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,10 @@ const { validateToken } = require("../middlewares/AuthMiddleware");
router.get("/", validateToken, async (req, res) => {
// const listOfPosts = await Posts.findAll();
const [listOfPosts, filedData] = await db.pool.query(
"select posts.*, json_arrayagg(json_object('id', l.id, 'username', l.username)) Likes from posts left join likes as l on posts.id = l.post_id group by posts.id"
"select p.*, json_arrayagg(json_object('id', l.id, 'user_id', l.user_id)) Likes, " +
"(select u.id from users as u where u.username = p.username) as UserId " +
"from posts as p left join likes as l on p.id = l.post_id " +
"group by p.id"
);

const [likedPosts, likedData] = await db.pool.query(
Expand All @@ -35,9 +38,14 @@ router.get("/byId/:id", async (req, res) => {
router.get("/byuserId/:id", async (req, res) => {
const id = req.params.id;
const listOfPosts = await db.pool.query(
"select posts.*, json_arrayagg(json_object('id', l.id, 'username', l.username)) Likes from posts left join likes as l on posts.username = l.username group by posts.username"
"select p.*, count(l.post_id) as likeCount " +
"from posts as p left join likes as l " +
"on p.id = l.post_id " +
"where p.username in (select u.username from users as u where u.id = (?)) group by p.id",
[id]
);
res.json(listOfPosts);
console.log("User's Posts: ", JSON.stringify(listOfPosts[0]));
res.json(listOfPosts[0]);
});

router.post("/", validateToken, async (req, res) => {
Expand Down

0 comments on commit 7035e45

Please sign in to comment.