@@ -5,18 +5,17 @@ var path = require('path');
55var url = require ( 'url' ) ;
66var ff = require ( '../routes/findFile' ) ;
77
8- // get
8+ // get : read
99router . get ( '/' , function ( req , res , next ) {
1010 var resultData ;
1111 var reqPath = getFilePathByRequest ( req ) ;
1212 var filePath = addExtNameJson ( reqPath ) ;
1313
1414 try {
15- if ( fs . existsSync ( reqPath ) && fs . lstatSync ( reqPath ) . isDirectory ( ) ) {
15+ if ( isExistFile ( filePath ) ) {
16+ resultData = readData ( filePath ) ;
17+ } else if ( isDirectory ( reqPath ) ) {
1618 resultData = ff . hierarchyFiles ( ff . findFiles ( reqPath ) ) ;
17- } else if ( fs . existsSync ( filePath ) && fs . lstatSync ( filePath ) . isFile ( ) ) {
18- var fileString = fs . readFileSync ( filePath , 'utf8' ) ;
19- resultData = JSON . parse ( fileString ) ;
2019 } else {
2120 next ( ) ;
2221 }
@@ -31,21 +30,19 @@ router.get('/', function(req, res, next) {
3130 res . json ( resultData ) ;
3231} ) ;
3332
34- // post
33+ // post : create | read
3534router . post ( '/' , function ( req , res , next ) {
3635 var resultData ;
3736 var json = req . body ;
3837 var filePath = getFilePathByRequest ( req ) ;
3938 filePath = addExtNameJson ( filePath ) ;
4039
4140 try {
42- if ( fs . existsSync ( filePath ) && fs . lstatSync ( filePath ) . isFile ( ) ) {
43- var fileString = fs . readFileSync ( filePath , 'utf8' ) ;
44- resultData = JSON . parse ( fileString ) ;
41+ if ( isExistFile ( filePath ) ) {
42+ resultData = readData ( filePath ) ;
4543 } else {
46- mkdirp ( filePath ) ;
47- fs . writeFileSync ( filePath , JSON . stringify ( json ) , 'utf8' ) ;
48- resultData = json ;
44+ resultData = createData ( filePath , json ) ;
45+ res . status ( 201 ) ;
4946 }
5047 } catch ( e ) {
5148 // console.log(e);
@@ -54,49 +51,61 @@ router.post('/', function(req, res, next) {
5451 err . status = 500 ;
5552 next ( err ) ;
5653 }
54+
5755 res . json ( resultData ) ;
5856} ) ;
5957
60- // put
58+ // put : update | create
6159router . put ( '/' , function ( req , res , next ) {
6260 var resultData ;
6361 var json = req . body ;
6462 var filePath = getFilePathByRequest ( req ) ;
6563 filePath = addExtNameJson ( filePath ) ;
6664
6765 try {
68- mkdirp ( filePath ) ;
69- fs . writeFileSync ( filePath , JSON . stringify ( json ) , 'utf8' ) ;
70- resultData = json ;
66+ if ( isExistFile ( filePath ) ) {
67+ resultData = updateData ( filePath , json ) ;
68+ } else {
69+ resultData = createData ( filePath , json ) ;
70+ res . status ( 201 ) ;
71+ }
7172 } catch ( e ) {
7273 // console.log(e);
7374 var errorMessage = 'Fail Put' ;
7475 var err = new Error ( errorMessage ) ;
7576 err . status = 500 ;
7677 next ( err ) ;
7778 }
79+
7880 res . json ( resultData ) ;
7981} ) ;
8082
81- // delete
83+ // delete : delete
8284router . delete ( '/' , function ( req , res , next ) {
8385 var resultData ;
8486 var filePath = getFilePathByRequest ( req ) ;
8587 filePath = addExtNameJson ( filePath ) ;
8688
8789 try {
88- fs . unlinkSync ( filePath ) ;
89- resultData = 'Success Delete' ;
90+ if ( isExistFile ( filePath ) ) {
91+ resultData = deleteData ( filePath ) ;
92+ } else {
93+ res . status ( 204 ) ;
94+ }
9095 } catch ( e ) {
9196 // console.log(e);
9297 var errorMessage = 'Fail Delete' ;
9398 var err = new Error ( errorMessage ) ;
9499 err . status = 500 ;
95100 next ( err ) ;
96101 }
102+
97103 res . json ( resultData ) ;
98104} ) ;
99105
106+
107+ // private function
108+
100109function addExtNameJson ( urlPath ) {
101110 var resultPath ;
102111 var extname = path . extname ( urlPath ) ;
@@ -108,7 +117,7 @@ function addExtNameJson(urlPath) {
108117 resultPath = urlPath . replace ( extname , ff . extJson ) ;
109118 }
110119 } else {
111- resultPath = urlPath + ff . extJson ;
120+ resultPath = urlPath . endsWith ( ff . extJson ) ? urlPath : urlPath + ff . extJson ;
112121 }
113122
114123 return resultPath ;
@@ -123,10 +132,56 @@ function getFilePathByRequest(req) {
123132function mkdirp ( filePath ) {
124133 var dirname = path . dirname ( filePath ) ;
125134 if ( fs . existsSync ( dirname ) ) {
126- return true ;
135+ return false ;
127136 }
137+
128138 mkdirp ( dirname ) ;
129139 fs . mkdirSync ( dirname ) ;
140+
141+ return true ;
142+ }
143+
144+ function isDirectory ( reqPath ) {
145+ return fs . existsSync ( reqPath ) && fs . lstatSync ( reqPath ) . isDirectory ( ) ;
146+ }
147+
148+ function isExistFile ( filePath ) {
149+ return fs . existsSync ( filePath ) && fs . lstatSync ( filePath ) . isFile ( ) ;
150+ }
151+
152+
153+ // function : create, read, update, delete
154+
155+ function createData ( filePath , json ) {
156+ let resultData ;
157+
158+ mkdirp ( filePath ) ;
159+ fs . writeFileSync ( filePath , JSON . stringify ( json ) , 'utf8' ) ;
160+ resultData = json ;
161+
162+ return resultData ;
163+ }
164+
165+ function readData ( filePath ) {
166+ let resultData ;
167+
168+ let fileString = fs . readFileSync ( filePath , 'utf8' ) ;
169+ resultData = JSON . parse ( fileString ) ;
170+
171+ return resultData ;
172+ }
173+
174+ function updateData ( filePath , json ) {
175+ return createData ( filePath , json ) ;
176+ }
177+
178+ function deleteData ( filePath ) {
179+ let resultData ;
180+
181+ fs . unlinkSync ( filePath ) ;
182+ resultData = 'Success Delete' ;
183+
184+ return resultData ;
130185}
131186
132187module . exports = router ;
0 commit comments