Skip to content

Commit

Permalink
add : get userinfo function, point system, test (#144)
Browse files Browse the repository at this point in the history
  • Loading branch information
yjjjwww authored Sep 21, 2022
1 parent 2f9a766 commit 61de141
Show file tree
Hide file tree
Showing 10 changed files with 3,394 additions and 42 deletions.
12 changes: 10 additions & 2 deletions server/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,24 +6,32 @@
"start": "node dist/app.js",
"build": "tsc -p .",
"dev": "nodemon --watch \"src/**/*.ts\" --exec \"ts-node\" src/app.ts",
"postinstall": "prisma generate"
"postinstall": "prisma generate",
"test": "yarn mocha ./test/test.ts -r ts-node/register"
},
"license": "MIT",
"devDependencies": {
"@types/chai": "^4.3.3",
"@types/cors": "^2.8.12",
"@types/express": "^4.17.13",
"@types/mocha": "^9.1.1",
"@types/node": "^18.7.14",
"chai": "^4.3.6",
"express": "^4.18.1",
"nodemon": "^2.0.19",
"prisma": "^4.3.1",
"ts-mocha": "^10.0.0",
"ts-node": "^10.9.1",
"typescript": "^4.8.2"
},
"dependencies": {
"@prisma/client": "^4.3.1",
"@types/express-session": "^1.17.5",
"axios": "^0.27.2",
"caver-js": "^1.9.0",
"cors": "^2.8.5",
"dotenv": "^16.0.2",
"express-session": "^1.17.3"
"express-session": "^1.17.3",
"mocha": "^10.0.0"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
-- AlterTable
ALTER TABLE "User" ADD COLUMN "user_point" INTEGER NOT NULL DEFAULT 0;
1 change: 1 addition & 0 deletions server/prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ model User {
user_network String
user_nickname String
user_introduction String
user_point Int @default(0)
articles Article[]
lectures Lecture[]
comments Comment[]
Expand Down
59 changes: 36 additions & 23 deletions server/src/app.ts
Original file line number Diff line number Diff line change
@@ -1,41 +1,54 @@
import express, { Request, Response, NextFunction } from "express";
import dotenv from "dotenv";
import cors from "cors";
import session from "express-session";
import express, { Request, Response, NextFunction } from 'express';
import dotenv from 'dotenv';
import cors from 'cors';
import session from 'express-session';

const Caver = require('caver-js');
const caver = new Caver('https://api.baobab.klaytn.net:8651/%27');

dotenv.config();

const app = express();
const PORT = process.env.PORT;
const articleRouter = require("./router/article");
const commentRouter = require("./router/comment");
const likeRouter = require("./router/like");
const lectureRouter = require("./router/lecture");
const userLectureRouter = require("./router/user_lecture");
const userRouter = require("./router/users");
const profileRouter = require("./router/profile");

app.use(cors({ origin: "*" }));
const articleRouter = require('./router/article');
const commentRouter = require('./router/comment');
const likeRouter = require('./router/like');
const lectureRouter = require('./router/lecture');
const userLectureRouter = require('./router/user_lecture');
const userRouter = require('./router/users');
const profileRouter = require('./router/profile');

app.use(cors({ origin: '*' }));
app.use(express.json());
app.use(express.urlencoded({ extended: false }));
app.use(
session({
secret: "vote",
secret: 'vote',
resave: false,
saveUninitialized: true,
})
);

app.use("/article", articleRouter);
app.use("/comment", commentRouter);
app.use("/like", likeRouter);
app.use("/lecture", lectureRouter);
app.use("/userlecture", userLectureRouter);
app.use("/user", userRouter);
app.use("/profile", profileRouter);
app.use('/article', articleRouter);
app.use('/comment', commentRouter);
app.use('/like', likeRouter);
app.use('/lecture', lectureRouter);
app.use('/userlecture', userLectureRouter);
app.use('/user', userRouter);
app.use('/profile', profileRouter);

app.get('/', (req: Request, res: Response, next: NextFunction) => {
res.status(200).send('Havruta DAO');
});

app.get("/", (req: Request, res: Response, next: NextFunction) => {
res.status(200).send("Havruta DAO");
app.post('/getbalance', async (req, res) => {
// const balance = await caver.klay.getBalance(req.body.address);
// const convert = caver.utils.convertFromPeb(balance, 'KLAY');
const token = new caver.klay.KIP7(
'0x0ef92ccf7313a4677e0d15fdf01fbb1b5a0a4f05'
);
const balance = await token.balanceOf(req.body.address);
res.status(201).json(balance);
});

app.listen(PORT, () => {
Expand Down
8 changes: 8 additions & 0 deletions server/src/controller/article_controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,14 @@ module.exports = {
});
const allArticle = await prisma.Article.findMany({});
console.dir(allArticle, { depth: null });
await prisma.User.update({
where: {
user_id: user_id,
},
data: {
user_point: { increment: 5 },
},
});
};
postArticleHandler(user_id, article_content)
.then(async () => {
Expand Down
8 changes: 8 additions & 0 deletions server/src/controller/comment_controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,14 @@ module.exports = {
where: { article_id: article_id },
data: { comment_count: { increment: 1 } },
});
await prisma.User.update({
where: {
user_id: user_id,
},
data: {
user_point: { increment: 3 },
},
});
const allComment = await prisma.Comment.findMany({});
console.dir(allComment, { depth: null });
};
Expand Down
26 changes: 26 additions & 0 deletions server/src/controller/userinfo_controller.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { Request, Response } from 'express';
const prisma = require('../db/index');

module.exports = {
userInfo: (req: Request, res: Response) => {
const user_id = Number(req.query.user_id as String);
const userInfoHandler = async (user_id: Number) => {
const userInfo = await prisma.User.findMany({
where: {
user_id: user_id,
},
});
return userInfo;
};
userInfoHandler(user_id)
.then(async (result) => {
await prisma.$disconnect();
res.status(201).json(result);
})
.catch(async (e) => {
console.error(e);
await prisma.$disconnect();
res.status(500).send('Server Error');
});
},
};
14 changes: 7 additions & 7 deletions server/src/router/users.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { Router } from "express";
import { Router } from 'express';

const router = Router();
const { user_login} = require("../controller/login_controller");
const { user_logout } = require("../controller/logout_controller");
const { user_login } = require('../controller/login_controller');
const { user_logout } = require('../controller/logout_controller');
const { userInfo } = require('../controller/userinfo_controller');

router.post("/login", user_login);
router.post("/logout", user_logout);
router.post('/login', user_login);
router.post('/logout', user_logout);
router.get('/userinfo', userInfo);

module.exports = router;


104 changes: 104 additions & 0 deletions server/test/test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
import { expect } from 'chai';
import { describe } from 'mocha';
import axios from 'axios';

describe('์ปค๋ฎค๋‹ˆํ‹ฐ ํ™œ๋™์œผ๋กœ ํฌ์ธํŠธ ํš๋“', () => {
it('๊ธ€ ์ž‘์„ฑ์‹œ ํฌ์ธํŠธ ํš๋“', async () => {
const userInfo = await axios.get(
'http://localhost:8000/user/userinfo?user_id=6'
);
const beforePoint: Number = Number(userInfo.data[0].user_point);

const postArticle = async () => {
await axios.post(`http://localhost:8000/article`, {
user_id: 6,
article_content: 'test article',
});
const userInfo = await axios.get(
'http://localhost:8000/user/userinfo?user_id=6'
);
const afterPoint = Number(userInfo.data[0].user_point);
return afterPoint;
};

const afterPoint = postArticle();

expect(Number(afterPoint) - Number(beforePoint)).to.equal(5);
});

it('๋Œ“๊ธ€ ์ž‘์„ฑ์‹œ ํฌ์ธํŠธ ํš๋“', async () => {
const userInfo = await axios.get(
'http://localhost:8000/user/userinfo?user_id=6'
);
const beforePoint: Number = Number(userInfo.data[0].user_point);

const postComment = async () => {
await axios.post(`http://localhost:8000/comment`, {
user_id: 6,
article_id: 51,
comment_content: 'test comment',
});
const userInfo = await axios.get(
'http://localhost:8000/user/userinfo?user_id=6'
);
const afterPoint = Number(userInfo.data[0].user_point);
return afterPoint;
};

const afterPoint = postComment();

expect(Number(afterPoint) - Number(beforePoint)).to.equal(3);
});
});

describe('ํฌ์ธํŠธ๋ฅผ ํ† ํฐ์œผ๋กœ ์ „ํ™˜', () => {
it('์‹ ์ฒญํ–ˆ์„ ๋•Œ ํฌ์ธํŠธ ๋น„๊ต', async () => {
const userInfo = await axios.get(
'http://localhost:8000/user/userinfo?user_id=6'
);
const beforePoint: Number = Number(userInfo.data[0].user_point);

const exchangeToken = async () => {
await axios.post('http://localhost:8000/exchange', {
user_id: 6,
user_point: 5,
});
const userInfo = await axios.get(
'http://localhost:8000/user/userinfo?user_id=6'
);
const afterPoint = Number(userInfo.data[0].user_point);
return afterPoint;
};

const afterPoint = exchangeToken();

expect(Number(beforePoint) - Number(afterPoint)).to.equal(5);
});
it('์‹ ์ฒญํ–ˆ์„ ๋•Œ ํ† ํฐ ๋น„๊ต', async () => {
const beforeToken = await axios.post('http://localhost:8000/getbalance', {
address: '0xE8Cf73e4e60c7fbC60A7d5931009aBBAE21B8465',
});
const numberBeforeToken = Number(beforeToken.data);

const exchangeToken = async () => {
await axios.post('http://localhost:8000/exchange', {
user_id: 6,
user_point: 5,
});
const afterToken = await axios.post('http://localhost:8000/getbalance', {
address: '0xE8Cf73e4e60c7fbC60A7d5931009aBBAE21B8465',
});
const numberAfterToken = Number(afterToken.data);
return numberAfterToken;
};

const numberAfterToken = exchangeToken();

expect(Number(numberBeforeToken) - Number(numberAfterToken)).to.equal(1);
});
});

describe('nft', () => {
it('ํ† ํฐ์œผ๋กœ nft ๋ฏผํŒ…', () => {});
it('nft๋กœ ๋ชจ๋“  ๊ฐ•์˜ ์ˆ˜๊ฐ•', () => {});
});
Loading

0 comments on commit 61de141

Please sign in to comment.