Skip to content

Commit

Permalink
merge
Browse files Browse the repository at this point in the history
  • Loading branch information
EthanKIMHG committed Nov 4, 2022
2 parents 59bacfe + 96a8407 commit dfc410e
Show file tree
Hide file tree
Showing 12 changed files with 119 additions and 42 deletions.
5 changes: 5 additions & 0 deletions client/.env
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
MONGODB='mongodb+srv://takoyaki-cluster.aovdheg.mongodb.net/Takoyaki-DB?authSource=%24external&authMechanism=MONGODB-X509&retryWrites=true&w=majority'
SECRET=aae08e313ca6ee1dcdbe191db59c4af17b242b21c2b23e4bcba8239ae4de9206
WEB3KEY=5c093988cddb4e77a7d78369e7b2e384
CONTRACT=0x04a49E94d26B1D51E0bDe72B193882eCa7EB4722
PRIVATEKEY=61e4dbfbf9fc8ac2dec5b43d1d04dbc0e33ca17d296d2c144b3096e5d142fbe1
5 changes: 4 additions & 1 deletion client/public/style.css
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,7 @@ body {
margin-right: 0;
margin-bottom: 0;
background-color: rgb(252, 250, 234);
}

font-family: 'Poppins';
}

5 changes: 3 additions & 2 deletions client/src/components/Article.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import Avatar from '@mui/material/Avatar';
import Typography from '@mui/material/Typography';
import '../utils/Article.css';
import '../utils/Font.css';
import { Box } from '@mui/material';
import { Box, Link } from '@mui/material';

const Article = (props) => {
const context = useContext(AppContext);
Expand All @@ -23,7 +23,8 @@ const Article = (props) => {
sx={{ borderRadius: '10px' }}
>
<CardHeader
sx={{bgcolor: "background.header"}}
// sx={{bgcolor: "#ffd2c9"}}
sx={{bgcolor: "#fde1e1"}}
avatar={
<Avatar src={imgFile} aria-label='recipe' />
}
Expand Down
17 changes: 14 additions & 3 deletions client/src/components/Comments.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import React, { useState, useContext } from 'react'
import React, { useState, useContext,useEffect } from 'react'
import { AppContext } from "../AppContext";
import { ThemeProvider } from '@mui/material/styles';
import { Stack,Button, fabClasses } from '@mui/material';
import { Stack,Button, fabClasses, Chip } from '@mui/material';
import Typography from '@mui/material/Typography';
import Avatar from '@mui/material/Avatar';
import "../utils/Comments.css"
import { Box } from '@mui/system';
import HighlightOffIcon from '@mui/icons-material/HighlightOff';
import axios from 'axios';

export const Comments = () => {
const context = useContext(AppContext);
Expand All @@ -30,6 +32,11 @@ export const Comments = () => {
}
}

const deleteComment = (targetId) => {
const deleteList = commentLists.filter((comment) => comment.id !== targetId);
setCommentLists(deleteList);
}

return (
<ThemeProvider theme={context.state.theme}>
<Stack>
Expand Down Expand Up @@ -62,7 +69,11 @@ export const Comments = () => {
<Stack spacing={1} className="users-comments" direction="row" sx={{marginY:"5px", color:"text.secondary" }} >
<Avatar sx={{width: "30px", height: "30px"}} src={context.state.imgSrc} />
<Typography component="span" sx={{textAlign:"center"}}>{userId}</Typography>
<Typography component="p">{comment.content}</Typography>
<Typography component="p">{comment.content}
<Button onClick={(id) => deleteComment(comment.id)}>
<HighlightOffIcon />
</Button>
</Typography>
</Stack>
)
})}
Expand Down
1 change: 1 addition & 0 deletions client/src/components/Like.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ export const Like = () => {
const [isLike, setIsLike] = useState(false);
const [count, setCount] = useState(1);


return (
<div>
{isLike
Expand Down
9 changes: 5 additions & 4 deletions client/src/utils/Article.css
Original file line number Diff line number Diff line change
@@ -1,21 +1,22 @@
.contents-container {
max-width: 600px;
background-color: rgba(47, 83, 239);
background-color:white;
margin-bottom: 30px;
border-radius: 10px;
}

.css-1qbkelo-MuiCardHeader-content > span {
color: #ffffff;
font-weight: 600;
color: black;
font-weight: 550;
font-size: 19px;

}

.contents-container > footer {
background-color: #a9def9;
background-color: rgb(239,204,202);
padding-bottom: 20px;
color: white;
font-size: 15px;
font-weight: 600;
}

6 changes: 3 additions & 3 deletions client/src/utils/MainPage.css
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
}

.nav-list {
background-color: white;
background-color:white;
border: none;
border-top-left-radius: 5px;
border-top-right-radius: 5px;
Expand Down Expand Up @@ -46,11 +46,11 @@
}

#flight {
color: #a9def9;
color: rgba(231,127,112);
}

#flag {
color: #a9def9;
color: rgba(113,122,133);
}


Expand Down
47 changes: 39 additions & 8 deletions server/controller/articles.js
Original file line number Diff line number Diff line change
Expand Up @@ -117,34 +117,65 @@ const write = async (req, res) => {
res.status(401).send('transaction err');
}
} catch (error) {
res.status(400).json(error);
const msg = {};
msg[`${error.name}`] = `${error.message}`;
console.error(`${error.name} : ${error.message}`);
res.status(400).json(msg);
}
};

const comment = async (req, res) => {
try {
if (!req.auth) throw new Error('Unauthorized to write an article. Please sign in.');
const { content, postId } = req.body;
const author = await User.findOne({ userId: req.auth.userId }, '_id');
const token = req.cookies.token;
const data = jwt.verify(token, process.env.SECRET);
const author = await User.findOne({ userId: data.userId }, '_id account');
const { content, articleId } = req.body;

const comment = new Comment({
content,
postId,
articleId,
userId: author._id
});

const newDocument = await comment.save();
res.status(201).send(newDocument);
} catch (error) {
res.status(400).json(error);
const msg = {};
msg[`${error.name}`] = `${error.message}`;
console.error(`${error.name} : ${error.message}`);
res.status(400).json(msg);
}
};

const like = async (req, res) => {
try {
res.status(200).send();
const token = req.cookies.token;
const data = jwt.verify(token, process.env.SECRET);
const user = await User.findOne({ userId: data.userId }, '_id userId');

const { articleId } = req.body;
const article = await Article.findById(articleId);

// 중복 아이디 검사 조건문
const _loverIdx = article.like.findIndex(_lover => _lover.userId === user.userId);
const articleLover = article.like[_loverIdx];

// 만약 like를 누른 사람이 없다면
// like 목록에 추가하고
// 있을 경우 지운다
if (!articleLover) {
article.like.push(user);
} else {
article.like.splice(_loverIdx, 1);
}

const updatedDocument = await article.save();
res.status(201).json({ updatedDocument, likeCount: updatedDocument.like.length });
} catch (error) {
res.status(400).json(error);
const msg = {};
msg[`${error.name}`] = `${error.message}`;
console.error(`${error.name} : ${error.message}`);
res.status(400).json(msg);
}
};

Expand Down
46 changes: 28 additions & 18 deletions server/controller/users.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ require('dotenv').config();
const User = require('../model/user');
const reward = require('../utility/reward');
const jwt = require('jsonwebtoken');
const newAccount = require('../utility/createAccount');
const { main, transferFrom } = require('./web3');

const find = async (req, res) => {
const _queries = req.query;
console.log(req.query)
const user = await User.aggregate([
{
$lookup: {
Expand Down Expand Up @@ -47,22 +47,25 @@ const find = async (req, res) => {
};

const signup = async (req, res) => {
const { userId, password } = req.body;
try {
const { userId, password } = req.body;

// 여기 있는 account 데이터는 추후 이더리움 노드와 연동되면 채워질 부분입니다.
const user = new User({
userId,
password
});
// 여기 있는 account 데이터는 추후 이더리움 노드와 연동되면 채워질 부분입니다.
const user = new User({
userId,
password
});

// user 모델에서 mongoose-unique-validator 플러그인을 적용한 채로
// User Model에서 userID에 unique 옵션을 설정했기 때문에
// validate() 함수에서 유효성 조사하는 과정 중 이중 아이디를 검출가능
try {
// user 모델에서 mongoose-unique-validator 플러그인을 적용한 채로
// User Model에서 userID에 unique 옵션을 설정했기 때문에
// validate() 함수에서 유효성 조사하는 과정 중 이중 아이디를 검출가능
const newDocument = await user.save();
res.status(201).send(newDocument);
} catch (error) {
res.status(400).json(error);
const msg = {};
msg[`${error.name}`] = `${error.message}`;
console.error(`${error.name} : ${error.message}`);
res.status(401).json(msg);
}
};

Expand All @@ -83,7 +86,10 @@ const login = async (req, res) => {
res.status(200).json({ user, token });
}
} catch (error) {
res.status(401).json(error);
const msg = {};
msg[`${error.name}`] = `${error.message}`;
console.error(`${error.name} : ${error.message}`);
res.status(401).json(msg);
}
};

Expand All @@ -95,22 +101,26 @@ const uploadProfile = async (req, res) => {
const user = await User.updateOne({ userId: userId }, { profile_image: profileImage });
res.status(200).json({ user });
} catch (error) {
res.status(400).json(error);
const msg = {};
msg[`${error.name}`] = `${error.message}`;
console.error(`${error.name} : ${error.message}`);
res.status(400).json(msg);
}
};

const refresh = async (req, res) => {
// console.log(req)
const token = req.cookies.token;

try {
const token = req.cookies.token;
const data = jwt.verify(token, process.env.SECRET);
const userId = data.userId;
const user = await User.findOne({ userId }, '_id userId profileImage createdAt address');

res.status(200).json({ user, token });
} catch (error) {
res.status(401).json(error);
const msg = {};
msg[`${error.name}`] = `${error.message}`;
console.error(`${error.name} : ${error.message}`);
res.status(401).json(msg);
}
};

Expand Down
14 changes: 13 additions & 1 deletion server/model/article.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const mongoose = require('mongoose');
const uniqueValidator = require('mongoose-unique-validator');

const schema = new mongoose.Schema({
title: {
Expand All @@ -16,12 +17,23 @@ const schema = new mongoose.Schema({
city: {
type: mongoose.ObjectId
},
like: Array,
like: {
type: [
new mongoose.Schema({
userId: {
type: String,
required: true
}
})
]
},
// imgFile 불러오기.
imgFile: String,
createdAt: Date
});

schema.plugin(uniqueValidator);

// save 직전 작동하는 pre hook 함수
schema.pre('save', function (next) {
// 생성 날짜를 자동 기입함
Expand Down
4 changes: 3 additions & 1 deletion server/model/user.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,9 @@ schema.pre('save', function (next) {

// 1. address가 없으면 address를 생성.
// 2. if 로직이 필요없을거같고, web3 transfer 함수를 보내면 될거같고.
this.address = newAccount.address;

const _account = newAccount();
this.address = _account.address;

next();
});
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const web3 = require('./web3provider');
const { web3 } = require('./web3provider');
const newAccount = () => {
return web3.eth.accounts.create(web3.utils.randomHex(32));
};
Expand Down

0 comments on commit dfc410e

Please sign in to comment.