Toggle navigation
Toggle navigation
This project
Loading...
Sign in
Jeongmin Seo
/
favorite_restaurant
Go to a project
Toggle navigation
Toggle navigation pinning
Projects
Groups
Snippets
Help
Project
Activity
Repository
Pipelines
Graphs
Issues
0
Merge Requests
0
Wiki
Snippets
Network
Create a new issue
Builds
Commits
Issue Boards
Authored by
Jeongmin Seo
2022-06-07 22:47:14 +0900
Browse Files
Options
Browse Files
Download
Email Patches
Plain Diff
Commit
434358678dd3825d1689d828e1d3c884098ec30a
43435867
1 parent
4bbe472f
Update including databases
Hide whitespace changes
Inline
Side-by-side
Showing
10 changed files
with
156 additions
and
97 deletions
login_main/.gitignore
login_main/app/app.js
login_main/app/package.json
login_main/app/src/config/db.js
login_main/app/src/models/Restaurant.js
login_main/app/src/models/RestaurantStorage.js
login_main/app/src/models/User.js
login_main/app/src/models/UserStorage.js
login_main/app/src/routes/home/home.ctrl.js
login_main/app/src/routes/home/index.js
login_main/.gitignore
View file @
4343586
...
...
@@ -117,3 +117,5 @@ dist
.yarn/install-state.gz
.pnp.*
#db
app/src/databases/*
\ No newline at end of file
...
...
login_main/app/app.js
View file @
4343586
...
...
@@ -10,15 +10,13 @@ const compression = require("compression");
const
methodOverride
=
require
(
"method-override"
);
var
cors
=
require
(
"cors"
);
const
{
logger
}
=
require
(
"./src/config/winston"
);
//app이라는 express 객체 생성
const
app
=
express
();
//라우팅
const
home
=
require
(
"./src/routes/home"
);
const
port
=
3000
;
//controller갖고있는 모듈 생성
// const index = require("../controllers/indexController");
const
jwtMiddleware
=
require
(
"./src/config/jwtMiddleware"
);
// 앱 세팅
...
...
login_main/app/package.json
View file @
4343586
...
...
@@ -26,10 +26,12 @@
"devDependencies"
:
{},
"scripts"
:
{
"start"
:
"nodemon ./bin/www.js"
,
"test"
:
"echo
\"
Error: no test specified
\"
&& exit 1"
"test"
:
"echo
\"
Error: no test specified
\"
&& exit 1"
,
"dev"
:
"NODE_ENV=development node index.js"
,
"prod"
:
"NODE_ENV=production node index.js"
},
"author"
:
""
,
"author"
:
"
Jeongmin Seo, Jumi Yang
"
,
"license"
:
"ISC"
,
"keywords"
:
[],
"description"
:
""
"description"
:
"
Node.js API Server
"
}
...
...
login_main/app/src/config/db.js
View file @
4343586
const
mysql
=
require
(
"mysql"
);
const
{
logger
}
=
require
(
"./winston"
);
const
mysql2
=
require
(
"mysql2/promise"
);
const
db
=
mysql
.
createConnection
({
host
:
process
.
env
.
DB_HOST
,
...
...
@@ -7,6 +10,61 @@ const db = mysql.createConnection({
database
:
process
.
env
.
DB_DATABASE
,
//schema
});
db
.
connect
();
const
pool
=
mysql2
.
createPool
({
host
:
process
.
env
.
DB_HOST
,
user
:
process
.
env
.
DB_USER
,
password
:
process
.
env
.
DB_PASSWORD
,
database
:
process
.
env
.
DB_DATABASE
,
//schema
});
module
.
exports
=
db
;
\ No newline at end of file
const
exampleNonTransaction
=
async
(
sql
,
params
)
=>
{
try
{
const
connection
=
await
db
.
getConnection
(
async
(
conn
)
=>
conn
);
try
{
const
[
rows
]
=
await
connection
.
query
(
sql
,
params
);
connection
.
release
();
return
rows
;
}
catch
(
err
)
{
logger
.
error
(
`example non transaction Query error\n:
${
JSON
.
stringify
(
err
)}
`
);
connection
.
release
();
return
false
;
}
}
catch
(
err
)
{
logger
.
error
(
`example non transaction DB Connection error\n:
${
JSON
.
stringify
(
err
)}
`
);
return
false
;
}
};
const
exampleTransaction
=
async
(
sql
,
params
)
=>
{
try
{
const
connection
=
await
db
.
getConnection
(
async
(
conn
)
=>
conn
);
try
{
await
connection
.
beginTransaction
();
// START TRANSACTION
const
[
rows
]
=
await
connection
.
query
(
sql
,
params
);
await
connection
.
commit
();
// COMMIT
connection
.
release
();
return
rows
;
}
catch
(
err
)
{
await
connection
.
rollback
();
// ROLLBACK
connection
.
release
();
logger
.
error
(
`example transaction Query error\n:
${
JSON
.
stringify
(
err
)}
`
);
return
false
;
}
}
catch
(
err
)
{
logger
.
error
(
`example transaction DB Connection error\n:
${
JSON
.
stringify
(
err
)}
`
);
return
false
;
}
};
db
.
connect
();
module
.
exports
=
{
db
,
pool
:
pool
,
}
\ No newline at end of file
...
...
login_main/app/src/models/Restaurant.js
View file @
4343586
...
...
@@ -2,27 +2,16 @@
//for DB manipulate
const
RestaurantStorage
=
require
(
"./RestaurantStorage"
);
const
{
db
}
=
require
(
"../config/db"
);
const
{
pool
}
=
require
(
"../config/db"
);
const
{
logger
}
=
require
(
"../config/winston"
);
const
jwt
=
require
(
"jsonwebtoken"
);
//controller가 db에 접근하기 위해 Dao folder
//controller 실행하다가 db 접근해야하는 일 있을 때 Dao들어가서 query 실행하고 그 값을 반환
// const indexDao = require("../dao/indexDao");
class
Restaurant
{
constructor
(
body
)
{
this
.
body
=
body
;
}
}
// ex. 식당 테이블 조회
exports
.
readRestaurants
=
async
function
(
req
,
res
){
exports
.
readRestaurants
=
async
function
(
req
,
res
)
{
const
{
category
}
=
req
.
query
;
if
(
category
)
{
const
validCategory
=
[
"한식"
,
"중식"
,
"일식"
,
"양식"
,
"분식"
,
"구이"
,
"회/초밥"
,
"기타"
,];
if
(
!
validCategory
.
includes
(
category
))
{
return
res
.
send
({
isSuccess
:
false
,
...
...
@@ -31,15 +20,14 @@ exports.readRestaurants = async function (req, res){
});
}
}
try
{
const
connection
=
await
pool
.
getConnection
(
async
(
conn
)
=>
conn
);
try
{
//mysql접속 관련 부분 정의하는 함수
//es6 비구조할당
//indexDao에 selectRestaurants 정의해줘야 함. 그리고 거기서 query문.
const
[
rows
]
=
await
indexDao
.
selectRestaurants
(
connection
,
category
);
const
[
rows
]
=
await
RestaurantStorage
.
selectRestaurants
(
connection
,
category
);
return
res
.
send
({
result
:
rows
,
isSuccess
:
true
,
...
...
@@ -56,5 +44,4 @@ exports.readRestaurants = async function (req, res){
logger
.
error
(
`readRestaurants DB Connection error\n:
${
JSON
.
stringify
(
err
)}
`
);
return
false
;
}
}
\ No newline at end of file
...
...
login_main/app/src/models/RestaurantStorage.js
View file @
4343586
'use strict'
;
//for DB CRUD
const
db
=
require
(
"../config/db"
);
const
pool
=
require
(
"../config/db"
);
exports
.
selectRestaurants
=
async
function
(
connection
,
category
)
{
// class RestaurantStorage {
const
selectAllRestaurantsQuery
=
`select title, address, category from restaurants where status='A';`
;
const
selectCategorizedRestaurantsQuery
=
`select title, address, category from restaurants where status='A' and category=?;`
;
const
Params
=
[
category
];
const
Query
=
category
?
selectCategorizedRestaurantsQuery
:
selectAllRestaurantsQuery
;
const
rows
=
await
connection
.
query
(
Query
,
Params
);
return
rows
;
// static getRestaurantInfo(id) {
// return new Promise((resolve, reject) => {
// const query = "SELECT * FROM users WHERE id = ?;";
// db.query(query, [id], (err, data) => {
// if (err) reject(`${err}`);
// // console.log(data[0]);
// resolve(data[0]);
// });
// });
// }
}
// static async save(userInfo) {
// return new Promise((resolve, reject) => {
// const query = "INSERT INTO users(id, name, password) VALUES(?, ?, ?);";
// db.query(
// query,
// [userInfo.id, userInfo.name, userInfo.password],
// (err, data) => {
// if (err) reject(`${err}`);
// // console.log(data[0]);
// resolve({ success: true});
// }
// );
// });
// }
// }
// module.exports = UserStorage;
\ No newline at end of file
\ No newline at end of file
...
...
login_main/app/src/models/User.js
View file @
4343586
'use strict'
;
//for DB manipulate
const
UserStorage
=
require
(
"./UserStorage"
);
class
User
{
...
...
@@ -9,21 +9,16 @@ class User {
async
login
()
{
const
client
=
this
.
body
;
try
{
const
{
id
,
password
}
=
await
UserStorage
.
getUserInfo
(
client
.
id
);
// console.log(id, password);
if
(
id
)
{
if
(
id
===
client
.
id
&&
password
===
client
.
password
)
{
return
{
success
:
true
};
}
return
{
success
:
false
,
msg
:
"비밀번호가 틀렸습니다."
};
const
{
id
,
password
}
=
await
UserStorage
.
getUserInfo
(
client
.
id
);
// console.log(id, password);
if
(
id
)
{
if
(
id
===
client
.
id
&&
password
===
client
.
password
)
{
return
{
success
:
true
};
}
return
{
success
:
false
,
msg
:
"존재하지 않는 아이디입니다."
};
}
catch
(
err
)
{
return
{
success
:
false
,
msg
:
err
};
return
{
success
:
false
,
msg
:
"비밀번호가 틀렸습니다."
};
}
return
{
success
:
false
,
msg
:
"존재하지 않는 아이디입니다."
};
}
async
register
()
{
...
...
login_main/app/src/models/UserStorage.js
View file @
4343586
'use strict'
;
//
for DB CRUD
const
db
=
require
(
"../config/db"
)
;
//
파일시스템 이용해서 파일 접근 및 DB 관리
const
fs
=
require
(
"fs"
).
promises
;
class
UserStorage
{
class
UserStorage
{
static
#
getUserInfo
(
data
,
id
)
{
const
users
=
JSON
.
parse
(
data
);
const
idx
=
users
.
id
.
indexOf
(
id
);
const
userKeys
=
Object
.
keys
(
users
);
// [id, password, name]
const
userInfo
=
userKeys
.
reduce
((
newUser
,
info
)
=>
{
newUser
[
info
]
=
users
[
info
][
idx
];
return
newUser
;
},
{});
// console.log(userInfo);
return
userInfo
;
}
static
#
getUsers
(
data
,
isAll
,
fields
)
{
const
users
=
JSON
.
parse
(
data
);
if
(
isAll
)
return
users
;
const
newUsers
=
fields
.
reduce
((
newUsers
,
field
)
=>
{
if
(
users
.
hasOwnProperty
(
field
))
{
newUsers
[
field
]
=
users
[
field
];
}
return
newUsers
;
},
{});
return
newUsers
;
}
static
getUsers
(
isAll
,
...
fields
)
{
return
fs
.
readFile
(
"./src/databases/users.json"
)
.
then
((
data
)
=>
{
return
this
.
#
getUsers
(
data
,
isAll
,
fields
);
})
.
catch
((
err
)
=>
console
.
error
);
}
static
getUserInfo
(
id
)
{
return
new
Promise
((
resolve
,
reject
)
=>
{
const
query
=
"SELECT * FROM users WHERE id = ?;"
;
db
.
query
(
query
,
[
id
],
(
err
,
data
)
=>
{
if
(
err
)
reject
(
`
${
err
}
`
);
// console.log(data[0]);
resolve
(
data
[
0
]);
});
});
return
fs
.
readFile
(
"./src/databases/users.json"
)
.
then
((
data
)
=>
{
return
this
.
#
getUserInfo
(
data
,
id
);
})
.
catch
((
err
)
=>
console
.
error
);
}
static
async
save
(
userInfo
)
{
return
new
Promise
((
resolve
,
reject
)
=>
{
const
query
=
"INSERT INTO users(id, name, password) VALUES(?, ?, ?);"
;
db
.
query
(
query
,
[
userInfo
.
id
,
userInfo
.
name
,
userInfo
.
password
],
(
err
,
data
)
=>
{
if
(
err
)
reject
(
`
${
err
}
`
);
// console.log(data[0]);
resolve
({
success
:
true
});
}
);
});
const
users
=
await
this
.
getUsers
(
true
);
//id가 없으면 회원가입 가능
if
(
users
.
id
.
includes
(
userInfo
.
id
))
{
throw
"이미 존재하는 아이디입니다."
;
}
users
.
id
.
push
(
userInfo
.
id
);
users
.
name
.
push
(
userInfo
.
name
);
users
.
password
.
push
(
userInfo
.
password
);
fs
.
writeFile
(
"./src/databases/users.json"
,
JSON
.
stringify
(
users
));
return
{
success
:
true
};
}
}
...
...
login_main/app/src/routes/home/home.ctrl.js
View file @
4343586
...
...
@@ -16,9 +16,9 @@ const output = {
res
.
render
(
"home/register"
);
},
restaurants
:
(
req
,
res
)
=>
{
res
.
render
(
"home/restaurants"
);
}
//
restaurants: (req, res) => {
//
res.render("home/restaurants");
//
}
};
const
process
=
{
...
...
login_main/app/src/routes/home/index.js
View file @
4343586
...
...
@@ -3,13 +3,16 @@
const
express
=
require
(
"express"
);
const
router
=
express
.
Router
();
// const index = require("../../controllers/indexController");
const
jwtMiddleware
=
require
(
"../../config/jwtMiddleware"
);
const
Restaurant
=
require
(
"../../models/Restaurant"
);
const
ctrl
=
require
(
"./home.ctrl"
);
router
.
get
(
"/"
,
ctrl
.
output
.
hello
);
router
.
get
(
"/login"
,
ctrl
.
output
.
login
);
router
.
get
(
"/register"
,
ctrl
.
output
.
register
);
router
.
get
(
"/restaurants"
,
ctrl
.
output
.
restaurants
);
router
.
get
(
"/restaurants"
,
Restaurant
.
readRestaurants
);
// router.get("/restaurants", ctrl.output.restaurants);
router
.
post
(
"/login"
,
ctrl
.
process
.
login
);
router
.
post
(
"/register"
,
ctrl
.
process
.
register
);
...
...
Please
register
or
login
to post a comment