Skip to content

Commit 8c6b2e8

Browse files
committed
Fix backend modules
1 parent a4a8d4b commit 8c6b2e8

File tree

1,708 files changed

+242305
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

1,708 files changed

+242305
-0
lines changed

backend/api_server.js

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
require('rootpath')();
2+
const express = require("express");
3+
const app = express();
4+
const bodyParser = require("body-parser");
5+
const jwt = require('./app/helper/jwt');
6+
7+
// クロスドメインリクエストの許可
8+
const cors = require('cors');
9+
app.use(cors());
10+
11+
// jsonのパーサーを実装
12+
app.use(bodyParser.json());
13+
app.use(bodyParser.urlencoded({ extended: true }))
14+
15+
/* DB設定の実装 */
16+
const database = require('./app/config/dbConfig');
17+
18+
/* テーブル存在チェック & 無い場合は作成 */
19+
database.init();
20+
21+
/* JWT Auth の実装 */
22+
app.use(jwt());
23+
24+
/* 引数がない場合は、ポート3000で起動 */
25+
const port = process.argv[2] || 3000;
26+
app.listen(port, function () {
27+
console.log("Server listening on port : " + port);
28+
});
29+
30+
app.use(bodyParser.urlencoded({extended: false}));
31+
app.use(bodyParser.json());
32+
33+
/* ルート設定 */
34+
const REST_API_ROOT = '/';
35+
app.use(REST_API_ROOT, require('./app/routes/router'));

backend/app/config/authConfig.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"secret": "1234567890asdfghjklzxcvbn"
3+
}

backend/app/config/dbConfig.js

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
let sqlite3 = require('sqlite3').verbose();
2+
3+
/*
4+
* データベース設定
5+
*/
6+
7+
/* DBをロード */
8+
let db = new sqlite3.Database('./sqlite.db');
9+
10+
/* テーブルが存在しない場合は作成 */
11+
let init = function () {
12+
// user table
13+
db.run("CREATE TABLE if not exists user (" +
14+
"id TEXT," +
15+
" username TEXT," +
16+
" password TEXT," +
17+
" firstName TEXT," +
18+
" lastName TEXT," +
19+
" age TEXT," +
20+
" imageURL TEXT," +
21+
" comment TEXT" +
22+
")");
23+
};
24+
25+
module.exports = {
26+
init: init,
27+
db: db
28+
};
29+
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
var authConfig = require('../../config/authConfig.json');
2+
var jwt = require('jsonwebtoken');
3+
4+
/**
5+
* レスポンスコマンド一覧
6+
*/
7+
class controllerCommon {
8+
9+
authSuccess(res) {
10+
return (result) => {
11+
// ここにjwtロジックを組み込む
12+
// TODO
13+
if (result) {
14+
const token = jwt.sign({ sub: result.id }, authConfig.secret);
15+
var authResult = { ...result[0], token };
16+
}
17+
res.status(200); // Found
18+
res.json(authResult);
19+
}
20+
}
21+
22+
findSuccess(res) {
23+
return (result) => {
24+
res.status(200); // Found
25+
res.json(result);
26+
}
27+
}
28+
29+
existsSuccess(res) {
30+
return (result) => {
31+
res.status(200); // Found
32+
res.json(result);
33+
}
34+
}
35+
36+
editSuccess(res) {
37+
return () => {
38+
res.status(201); // Created/Updated/Deleted
39+
res.json({});
40+
}
41+
}
42+
43+
authError(res) {
44+
return (error) => {
45+
res.status(404); // authUser Not found
46+
res.json(error);
47+
}
48+
}
49+
50+
serverError(res) {
51+
return (error) => {
52+
res.status(500);
53+
res.json(error);
54+
}
55+
}
56+
57+
findError(res) {
58+
return (error) => {
59+
res.status(404); // Not found
60+
res.json(error);
61+
}
62+
}
63+
}
64+
65+
module.exports = controllerCommon;
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
const userDao = require('../dao/userDao');
2+
3+
/* コマンド一覧を実装 */
4+
const ControllerCommon = require('./common/controllerCommon');
5+
6+
const User = require('../model/user');
7+
8+
class UserController {
9+
10+
constructor() {
11+
this.userDao = new userDao();
12+
this.common = new ControllerCommon();
13+
}
14+
15+
/**
16+
* 検索
17+
* @params req, res
18+
* @return entity
19+
*/
20+
findByAuthUser(req, res) {
21+
let username = req.body.username;
22+
let password = req.body.password;
23+
this.userDao.findByAuthUser(username, password)
24+
.then(this.common.authSuccess(res))
25+
.catch(this.common.authError(res));
26+
};
27+
28+
/**
29+
* 検索
30+
* @params req, res
31+
* @return entity
32+
*/
33+
findById(req, res) {
34+
let userId = req.params.userId;
35+
this.userDao.findById(userId)
36+
.then(this.common.findSuccess(res))
37+
.catch(this.common.findError(res));
38+
};
39+
40+
/**
41+
* 全件検索
42+
* @return all entities
43+
*/
44+
findAll(res) {
45+
this.userDao.findAll()
46+
.then(this.common.findSuccess(res))
47+
.catch(this.common.findError(res));
48+
};
49+
50+
/**
51+
* 全件合計
52+
* @return count
53+
*/
54+
countAll(res) {
55+
56+
this.userDao.countAll()
57+
.then(this.common.findSuccess(res))
58+
.catch(this.common.serverError(res));
59+
};
60+
}
61+
62+
module.exports = UserController;

backend/app/dao/commons/daoCommon.js

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
/** データベースのロードと
2+
* データベースコンフィグの適用
3+
*/
4+
const database = require('../../config/dbConfig');
5+
6+
/* 非同期処理にBlueBirdを実装 */
7+
const Promise = require('bluebird');
8+
9+
/* Daoエラーの設定 */
10+
const DaoError = require('./daoError');
11+
12+
/**
13+
* DAOコマンド一覧
14+
*/
15+
class Common {
16+
17+
findAll(sqlRequest) {
18+
return new Promise(function (resolve, reject) {
19+
database.db.all(sqlRequest, function (err, rows) {
20+
if (err) {
21+
reject(
22+
new DaoError(20, "Internal server error")
23+
);
24+
} else if (rows === null || rows.length === 0) {
25+
reject(
26+
new DaoError(21, "Entity not found")
27+
);
28+
} else {
29+
resolve(rows);
30+
}
31+
})
32+
});
33+
}
34+
35+
findOne(sqlRequest, sqlParams) {
36+
return new Promise(function (resolve, reject) {
37+
let stmt = database.db.prepare(sqlRequest);
38+
stmt.all(sqlParams, function (err, rows) {
39+
if (err) {
40+
reject(
41+
new DaoError(11, "Invalid arguments")
42+
);
43+
} else if (rows === null || rows.length === 0) {
44+
reject(
45+
new DaoError(21, "Entity not found")
46+
);
47+
} else {
48+
resolve(rows);
49+
}
50+
})
51+
});
52+
}
53+
54+
existsOne(sqlRequest, sqlParams) {
55+
return new Promise(function (resolve, reject) {
56+
let stmt = database.db.prepare(sqlRequest);
57+
stmt.each(sqlParams, function (err, row) {
58+
if (err) {
59+
reject(
60+
new DaoError(20, "Internal server error")
61+
);
62+
} else if (row && row.found === 1) {
63+
resolve(true);
64+
} else {
65+
reject(
66+
new DaoError(21, "Entity not found")
67+
);
68+
}
69+
})
70+
});
71+
}
72+
73+
run(sqlRequest, sqlParams) {
74+
return new Promise(function (resolve, reject) {
75+
let stmt = database.db.prepare(sqlRequest);
76+
stmt.run(sqlParams, function (err) {
77+
if (this.changes === 1) {
78+
resolve(true);
79+
} else if (this.changes === 0) {
80+
reject(
81+
new DaoError(21, "Entity not found")
82+
)
83+
} else {
84+
console.log(err);
85+
reject(
86+
new DaoError(11, "Invalid arguments")
87+
)
88+
}
89+
})
90+
});
91+
}
92+
}
93+
94+
module.exports = Common;

backend/app/dao/commons/daoError.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
/**
2+
* Dao エラー
3+
*/
4+
5+
class DaoError {
6+
constructor(errorCode, message) {
7+
this.errorCode = errorCode;
8+
this.message = message;
9+
}
10+
}
11+
12+
module.exports = DaoError;

backend/app/dao/userDao.js

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
const User = require('../model/user');
2+
3+
const daoCommon = require('./commons/daoCommon');
4+
5+
class UserDao {
6+
7+
constructor() {
8+
this.common = new daoCommon();
9+
}
10+
11+
/**
12+
* 存在チェック
13+
* @params username, password
14+
* returns true or false
15+
*/
16+
findByAuthUser(username, password) {
17+
let sqlRequest = "SELECT id, username, password, firstName, lastName, age, imageURL, comment FROM User WHERE username=$username and password=$password";
18+
let sqlParams = {
19+
$username: username,
20+
$password: password
21+
};
22+
console.log(sqlParams)
23+
return this.common.findOne(sqlRequest, sqlParams).then(rows =>{
24+
let user = [];
25+
for (const row of rows) {
26+
user.push(new User(row.id, row.username, row.password, row.firstName, row.lastName, row.age, row.imageURL, row.comment));
27+
}
28+
return user;
29+
});
30+
}
31+
32+
/**
33+
* 検索
34+
* @params userId
35+
* @return entity
36+
*/
37+
findById(userId) {
38+
let sqlRequest = "SELECT id, username, password, firstName, lastName, age, imageURL, comment FROM User WHERE id like '%'||$userId||'%' ";
39+
let sqlParams = {$userId: userId};
40+
return this.common.findOne(sqlRequest, sqlParams).then(rows =>{
41+
let users = [];
42+
for (const row of rows) {
43+
users.push(new User(row.id, row.username, row.password, row.firstName, row.lastName, row.age, row.imageURL, row.comment));
44+
}
45+
return users;
46+
});
47+
};
48+
49+
/**
50+
* 検索(全件)
51+
* @return all entities
52+
*/
53+
findAll() {
54+
let sqlRequest = "SELECT * FROM User";
55+
return this.common.findAll(sqlRequest).then(rows => {
56+
let users = [];
57+
for (const row of rows) {
58+
users.push(new User(row.id, row.username, row.password, row.firstName, row.lastName, row.age, row.imageURL, row.comment));
59+
}
60+
return users;
61+
});
62+
};
63+
64+
/**
65+
* 件数(全件)
66+
* @return count
67+
*/
68+
countAll() {
69+
let sqlRequest = "SELECT COUNT(*) AS count FROM User";
70+
return this.common.findOne(sqlRequest);
71+
};
72+
73+
}
74+
75+
module.exports = UserDao;

0 commit comments

Comments
 (0)