Skip to content

Commit 60f387b

Browse files
committed
제품 삭제 기능 추가
1 parent d24eb67 commit 60f387b

File tree

10 files changed

+76
-15
lines changed

10 files changed

+76
-15
lines changed

app.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
var express = require('express');
33
var path = require('path'); //미들웨어의 윗부분에 적혀있어야함!
44
var logger = require('morgan');
5-
var bodyParser = require('body-parser');
5+
var bodyParser = require('body-parser');//javascript 객체로 편하게 사용하기 위해 bodyparser 이용
66

77
//MongoDB 접속 express아래에 위치해야함!
88
var mongoose = require('mongoose');
@@ -32,7 +32,7 @@ app.set('view engine', 'ejs');
3232

3333

3434
//미들웨어는 항상 라우팅 오기 전에
35-
//미들웨어 셋팅(request객체에 변수를 추가)
35+
//미들웨어 셋팅(request객체에 변수를 추가), POST는 body의 데이터를 가져옴
3636
app.use(logger('dev'));
3737
app.use(bodyParser.json());
3838
app.use(bodyParser.urlencoded({ extended: false}));

models/CommentsModel.js

Whitespace-only changes.

models/contactsModel.js

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,17 @@ var autoIncrement = require('mongoose-auto-increment');
66
var ContactsSchema = new Schema({
77
id: String, //id
88
phoneNumber: Number, //연락처
9-
joined_at: Date //회원가입일
9+
joined_at: Date//회원가입일
10+
});
11+
12+
ContactsSchema.virtual('getDate').get(function(){//virtual 변수
13+
var date = new Date(this.joined_at);
14+
return {
15+
year: date.getFullYear(),
16+
month: date.getMonth()+1,
17+
day: date.getDay()
18+
19+
};
1020
});
1121

1222
//db로 보내기

routes/admin.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ router.get('/products/detail/:id', function(req, res){
4343

4444
//제품 수정 페이지
4545
router.get('/products/edit/:id',function(req,res){
46-
ProductsModel.findOne({'id': req.params.id},function(err, product){
46+
ProductsModel.findOne({'id': req.params.id},function(err, product){//
4747
res.render('admin/form',{product:product});
4848
});
4949
});
@@ -63,4 +63,11 @@ router.post('/products/edit/:id', function (req, res) {
6363
});
6464
});
6565

66+
//제품 삭제 페이지
67+
router.get('/products/delete/:id',function(req,res){
68+
ProductsModel.remove({ id: req.params.id }, function (err) {//params<-parameter
69+
res.redirect('/admin/products');//절대 경로로 써줘
70+
});
71+
});//require로 따로 뺄 수 있어
72+
6673
module.exports = router;

routes/contacts.js

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,13 @@ router.get('/',function(req,res){
1010
//contacts/이후의 url을 적는다.
1111
router.get('/list',function(req,res){
1212
ContactsModel.find(function(err,contacts){
13-
res.render('contacts/',
13+
res.render('contacts/list',
1414
{contacts : contacts}
1515
);
1616
});
1717
});
1818

19+
//작성 페이지
1920
router.get('/write',function(req,res){
2021
res.render('contacts/form',{contacts:""});
2122
});
@@ -31,4 +32,11 @@ router.post('/write',function(req,res){
3132
});
3233
});
3334

35+
//상세 페이지
36+
router.get('/detail/:id',function(req,res){
37+
ContactsModel.findOne({'id':req.params.id},function(err,contacts){
38+
res.render('contacts/contactsDetail',{contacts: contacts});
39+
});
40+
});
41+
3442
module.exports = router;

views/admin/form.ejs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<% include ../header.ejs %>
2-
<form action="" method="post">
2+
<form action="" method="post"><!--form 재사용을 위해 action을 비워둬-->
33
<table class="table table-bordered">
44
<tr>
55
<th>제품명</th>

views/admin/products.ejs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
<th>상품명</th>
66
<th>작성일</th>
77
<th>가격(원)</th>
8-
<th>삭제</th>
8+
<th></th>
99
</tr>
1010
<% products.forEach( function(product){ %>
1111
<tr>
@@ -23,7 +23,7 @@
2323
<%=product.price%>
2424
</td>
2525
<td>
26-
<a href="#" class="btn btn-danger">삭제</a>
26+
<a href="/admin/products/delete/<%=product.id%>" class="btn btn-danger" onclick="return confirm('삭제하시겠습니까?')">삭제</a>
2727
</td>
2828
</tr>
2929
<% }); %>

views/contacts/contactsDetail.ejs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<% include ../header.ejs %>
2+
3+
4+
5+
<div class="panel panel-default">
6+
<div class="panel-heading">
7+
ID: <%=contacts.id%>
8+
9+
</div>
10+
<div class="panel-body">
11+
<div style="padding-bottom: 10px">
12+
가입일 :
13+
<%=contacts.getDate.year%>
14+
<%=contacts.getDate.month%>
15+
<%=contacts.getDate.day%>
16+
<br>
17+
연락처 : <%=contacts.phoneNumber%>
18+
</div>
19+
</div>
20+
</div>
21+
22+
<a href="/contacts" class="btn btn-default">목록으로</a>
23+
<a href="/contacts/edit/<%=contacts.id%>" class="btn btn-primary">수정</a>
24+
25+
26+
27+
28+
29+
30+
<% include ../footer.ejs %>

views/contacts/form.ejs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,12 @@
66
<td><input type="text" name="id" class="form-control" value="<%=contacts.id%>"/></td>
77
</tr>
88
<tr>
9-
<th>연락처</th>
9+
<th>연락처<br>(010-0000-0000)</th>
1010
<td><input type="Number" name="phoneNumber" class="form-control" value="<%=contacts.phoneNumber%>"/></td>
1111
</tr>
1212
<tr>
1313
<th>가입일</th>
14-
<td><input type="text" name="joined_at" class="form-control" value="<%=contacts.joined_at%>"/></td>
14+
<td><input type="Date" name="joined_at" class="form-control" value="<%=contacts.joined_at%>"/></td>
1515
</tr>
1616
</table>
1717
<button class="btn btn-primary">작성하기</button>

views/contacts/list.ejs

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,22 +5,28 @@
55
<th>id</th>
66
<th>연락처</th>
77
<th>가입일</th>
8+
<th></th>
89
</tr>
9-
<% products.forEach( function(product){ %>
10+
<% contacts.forEach( function(contacts){ %>
1011
<tr>
1112
<td>
12-
아이디입니다.
13+
<a href="/contacts/detail/<%=contacts.id%>">
14+
<%=contacts.id%>
15+
</a>
1316
</td>
1417
<td>
15-
연락처입니다.
18+
<%=contacts.phoneNumber%>
1619
</td>
1720
<td>
18-
가입일 입니다.
21+
<%=contacts.getDate.year%>
22+
<%=contacts.getDate.month%>
23+
<%=contacts.getDate.day%>
1924
</td>
25+
<td><a href="/contacts/delete" class="btn btn-danger">삭제</a></td>
2026
</tr>
2127
<% }); %>
2228
</table>
2329

2430
<a href="/contacts/write" class="btn btn-default">작성하기</a>
25-
31+
2632
<% include ../footer.ejs %>

0 commit comments

Comments
 (0)