김채리

user detail update and more

......@@ -37,8 +37,14 @@ npm run dev:server
```
<br>
### API reference
[Programming Quotes API](quotes.stormconsultancy.co.uk/random.json)
<br>
[Trending-GitHub API](https://docs.trending-github.com/)
### License
[MIT](https://choosealicense.com/licenses/mit/)
......
......@@ -47,8 +47,6 @@ const gitTrend = async (req, res) => {
};
};
export const handleHome = async (req, res) => {
const quote = await getQuote();
const trend = await gitTrend();
......@@ -73,55 +71,64 @@ export const handleHome = async (req, res) => {
export const getUserDetail = async (req, res) => {
const quote = await getQuote();
const id = req.params.id;
const user = await User.findById(id);
console.log(user.tech);
res.render("userDetail", {
pagetTitle: "User Detail",
quote: quote.quote,
author: quote.author,
user,
});
};
export const getEditProfile = async (req,res)=> {
const{
user:{_id:id}
} = req;
try{
const user = await User.findById(id);
if(user.id !== id){
throw Error();
} else{
res.render("editProfile",{pageTitle:"Edit Profile", user});
}
}catch(error){
console.log(error);
export const getEditProfile = async (req, res) => {
const {
user: { _id: id },
} = req;
try {
const user = await User.findById(id);
if (user.id !== id) {
throw Error();
} else {
res.render("editProfile", { pageTitle: "Edit Profile", user });
}
} catch (error) {
console.log(error);
}
};
export const postEditProfile = async (req,res) =>{
const {
user:{_id:id},
body: {name, email, school, blogUrl, tech, career, introduction},
file
} = req;
try{
const updatedUser = await User.findByIdAndUpdate(id, {
avatarUrl: file ? file.path : req.session.passport.user.avatarUrl,
name,
email,
school,
blogUrl,
tech: User.formatTech(tech),
career: User.formatCareer(career),
introduction
},
{
new: true
});
req.session.passport.user = updatedUser;
res.redirect("/users/edit-profile");
}catch(error){
console.log(error);
res.redirect("/");
}
export const postEditProfile = async (req, res) => {
const {
user: { _id: id },
body: { name, email, school, blogUrl, tech, career, introduction },
file,
} = req;
try {
const updatedUser = await User.findByIdAndUpdate(
id,
{
avatarUrl: file ? file.path : req.session.passport.user.avatarUrl,
name,
email,
school,
blogUrl,
tech: User.formatTech(tech),
career: User.formatCareer(career),
introduction,
},
{
new: true,
}
);
req.session.passport.user = updatedUser;
//console.log(updatedUser);
res.redirect("/users/edit-profile");
} catch (error) {
console.log(error);
res.redirect("/");
}
};
export const getJoin = (req, res) => {
......@@ -151,26 +158,25 @@ export const githubLoginCallback = async (_, __, profile, done) => {
},
} = profile;
try{
const user = await User.findOne({githubId});
if(user){
user.githubId = githubId,
user.githubName = githubName
await user.save();
return done(null, user);
}else{
const newUser = await User.create({
githubId,
githubName,
avatarUrl,
name,
email
});
return done(null, newUser);
}
}catch(error){
return done(error);
try {
const user = await User.findOne({ githubId });
if (user) {
(user.githubId = githubId), (user.githubName = githubName);
await user.save();
return done(null, user);
} else {
const newUser = await User.create({
githubId,
githubName,
avatarUrl,
name,
email,
});
return done(null, newUser);
}
} catch (error) {
return done(error);
}
};
export const postGithubLogin = (req, res) => {
......
import mongoose from "mongoose";
const UserSchema = new mongoose.Schema({
name: {
type: String,
trim: true
},
email: {
type: String,
trim: true,
unique: true
},
avatarUrl: String,
githubId: {
type: Number,
required: "GitHub id is required",
unique: true
},
githubName: {
type: String,
required: "Github nickname is required",
trim: true
},
school: {
type: String,
trim: true
},
tech: [{ type: String, trim: true }],
career: [{ type: String, trim: true }],
introduction: { type: String, maxLength: 500},
createdAt: {
type: Date,
default: Date.now
},
blogUrl: {
type: String,
default: "#"
},
githubUrl: {
type: String,
default: "#"
}
name: {
type: String,
trim: true,
},
email: {
type: String,
trim: true,
unique: true,
},
avatarUrl: String,
githubId: {
type: Number,
required: "GitHub ID is required",
unique: true,
},
githubName: {
type: String,
required: "Github nickname is required",
trim: true,
},
school: {
type: String,
trim: true,
},
tech: [{ type: String, trim: true }],
career: [{ type: String, trim: true }],
introduction: { type: String, maxLength: 500 },
createdAt: {
type: Date,
default: Date.now,
},
blogUrl: {
type: String,
default: "#",
},
githubUrl: {
type: String,
default: "#",
},
});
UserSchema.static("formatTech", function(tech){
return tech.split(",");
UserSchema.static("formatTech", function (tech) {
return tech.split(",");
});
UserSchema.static("formatCareer",function(career){
return career.split(",");
UserSchema.static("formatCareer", function (career) {
return career.split(",");
});
const User = mongoose.model("User", UserSchema);
export default User;
\ No newline at end of file
export default User;
......
import express from "express";
import { getEditProfile, getUserDetail, handleUsers, postEditProfile } from "../controllers/userController";
import {
getEditProfile,
getUserDetail,
handleUsers,
postEditProfile,
} from "../controllers/userController";
import { onlyPrivate, uploadFiles } from "../middlewares";
const userRouter = express.Router();
......@@ -7,7 +12,12 @@ const userRouter = express.Router();
userRouter.get("/", handleUsers);
userRouter.get("/edit-profile", onlyPrivate, getEditProfile);
userRouter.post("/edit-profile", onlyPrivate, uploadFiles.single("photo"),postEditProfile);
userRouter.post(
"/edit-profile",
onlyPrivate,
uploadFiles.single("photo"),
postEditProfile
);
userRouter.get("/:id", getUserDetail);
......
......@@ -16,7 +16,7 @@ block content
else
a(href=`/users/${loggedUser._id}`) My profile
.gotoTrend(style="border: 1px solid blue;")
.gotoTrend(style="border: 1px solid blue;")
p(style='color: orange;') Trending Repositories:
br
a(href=Url0, style={color:'grey'})
......
......@@ -11,23 +11,41 @@ block content
img(src="#")
.user-profile__link
a(href="#") Github
|#{' '}
a(href="#") Blog
.user-profile__column
.user-profile__info
h3 NAME
h3 GITHUB NICKNAME
h3 EMAIL
h3 SCHOOL
h3 TECH
h3 CAREER
h3 SELF-INTRODUCTION
h3(style="display: inline;") NAME:
h4(style="display: inline;")=user.name
br
h3(style="display: inline;") GITHUB NICKNAME:
h4(style="display: inline;")=user.githubName
br
h3(style="display: inline;") EMAIL:
h4(style="display: inline;")=user.email
br
h3(style="display: inline;") SCHOOL:
h4(style="display: inline;")=user.school
h3 TECH:
ul
each tech in user.tech
li=tech
h3 CAREER:
ul
each career in user.career
li=career
h3(style="display: inline;") SELF-INTRODUCTION:
h4(style="display: inline;")=user.introduction
hr
.user-status
.user-status__contributions
img(src="http://ghchart.rshah.org/lsj8706" alt="Name Your Github chart")
img(src=`http://ghchart.rshah.org/${user.githubName}` alt="Name Your Github chart")
.user-status__character
h3 Your step | commit numbers
img(src="https://preview.free3d.com/img/2019/12/2269306250288170045/1oe8ymrc-900.jpg" alt="character" style="height:200px; width:250px;")
.user-repositories
.user-repo
h3 REPO 1
\ No newline at end of file
h3 REPO 1
\ No newline at end of file
......