Skip to content

Commit

Permalink
Profile Backend (#46)
Browse files Browse the repository at this point in the history
* added sample user to database

* fetching user information from database

* fixed error messages

* removed hardcoded user, tested it with requestly

* added useParams hook

* changed single quotation mark to support string interpolation

* removed testing script
  • Loading branch information
alexapostolu authored Mar 25, 2024
1 parent 52fe333 commit ce27e8e
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 10 deletions.
6 changes: 4 additions & 2 deletions client/src/pages/Profile.tsx
Original file line number Diff line number Diff line change
@@ -1,19 +1,21 @@
import React, { useState, useEffect } from 'react';
import { useParams } from 'react-router-dom';
import { Container, Row, Button, Col, Card } from 'react-bootstrap';
import SolutionCard from '../components/SolutionCard';
import { User } from '../../../server/models/User';
import "./Profile.css";

const Profile = () => {
const { utorid } = useParams();
const [user, setUser] = useState(null);
const [solutions, setSolutions] = useState([]);
const [comments, setComments] = useState([]);

useEffect(() => {
fetch('/api/users/1')
fetch(`/api/users/${utorid}`)
.then(response => {
if (!response.ok) {
throw new Error('Failed to fetch user data');
throw new Error('Failed to fetch user data:(');
}
return response.json();
})
Expand Down
18 changes: 11 additions & 7 deletions server/controllers/UserController.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,18 @@ const User = db.User;
exports.get = async (req, res) => {
//const users = await User.findAll();

const sampleUser = {
username: 'Alexander Apostolu',
preferredName: 'Alex',
email: '[email protected]',
score: 100
};
const { username: reqUsername } = req.params;

res.status(200).json(sampleUser);
if (!reqUsername) {
return res.status(404).json({ message: "Requested username not found" });
}

let user = await User.findOne({ where: { username: reqUsername } });
if (!user) {
return res.status(500).json({ message: "User not found" });
}

res.status(200).json(user);
}

exports.getSolutions = async (req, res) => {
Expand Down
2 changes: 1 addition & 1 deletion server/routes/UserRoutes.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ const {
AsyncWrapController(User);

// Get a user from the database.
router.get("/:id", User.get);
router.get("/:username", User.get);

// Create a new user in the database.
router.post("/:id", User.create);
Expand Down

0 comments on commit ce27e8e

Please sign in to comment.