Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
T
TestNodeJS
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
dangdoan
TestNodeJS
Commits
25995fc8
Commit
25995fc8
authored
2 years ago
by
dangdoan
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
fix readme
parent
167417f7
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
120 additions
and
1 deletion
+120
-1
README.md
README.md
+1
-1
products.js
app/controller/products.js
+119
-0
No files found.
README.md
View file @
25995fc8
## Project use nodejs version 15.0.1 and mysql in XAMPP##
## Project use nodejs version 15.0.1 and mysql in XAMPP##
## Generate data base ##
## Generate data base ##
####
####
execute query in file mysql.sql
execute query in file mysql.sql
.
####
####
## Install package ##
## Install package ##
```
npm install```
```
npm install```
...
...
This diff is collapsed.
Click to expand it.
app/controller/products.js
0 → 100644
View file @
25995fc8
const
ProductProvider
=
require
(
'../provider/productProvider'
);
const
{
sendOK
,
sendError
}
=
require
(
'../helper/response'
);
class
product
{
constructor
()
{
this
.
producProvider
=
new
ProductProvider
()
}
init
(
router
)
{
const
self
=
this
;
router
.
route
(
""
)
.
get
(
async
function
(
req
,
res
,
next
)
{
await
self
.
getProducts
(
req
,
res
,
next
);
});
router
.
route
(
"/:code"
)
.
get
(
async
function
(
req
,
res
,
next
)
{
await
self
.
getByCode
(
req
,
res
,
next
);
});
router
.
route
(
""
)
.
post
(
async
function
(
req
,
res
,
next
)
{
await
self
.
createProduct
(
req
,
res
,
next
);
});
router
.
route
(
"/:code"
)
.
put
(
async
function
(
req
,
res
,
next
)
{
await
self
.
updateProduct
(
req
,
res
,
next
);
});
router
.
route
(
"/:code"
)
.
delete
(
async
function
(
req
,
res
,
next
)
{
await
self
.
deleteProduct
(
req
,
res
,
next
);
});
}
async
getProducts
(
req
,
res
,
next
)
{
const
self
=
this
;
try
{
const
currentPage
=
parseInt
(
req
.
query
.
page
)
||
1
const
pageSize
=
parseInt
(
req
.
query
.
size
)
||
10
// sort & dir
let
sort
=
req
.
query
.
sort
||
"id"
let
dir
=
req
.
query
.
dir
||
"asc"
const
lstResult
=
await
self
.
producProvider
.
getAll
(
currentPage
,
pageSize
,
sort
,
dir
);
lstResult
[
'totalPages'
]
=
Math
.
ceil
(
lstResult
[
'count'
]
/
pageSize
);
lstResult
[
'currentPage'
]
=
currentPage
return
res
.
status
(
200
).
json
(
sendOK
(
lstResult
));
}
catch
(
error
)
{
res
.
status
(
500
).
json
(
sendError
(
error
.
message
))
}
}
async
getByCode
(
req
,
res
,
next
)
{
const
self
=
this
;
try
{
const
result
=
await
self
.
producProvider
.
getByCode
(
req
.
params
);
return
res
.
status
(
200
).
json
(
sendOK
(
result
));
}
catch
(
error
)
{
res
.
status
(
500
).
json
(
sendError
(
error
.
message
))
}
}
async
createProduct
(
req
,
res
,
next
)
{
const
self
=
this
;
try
{
let
body
=
req
.
body
;
body
.
createdAt
=
new
Date
;
const
result
=
await
self
.
producProvider
.
post
(
body
);
return
res
.
status
(
200
).
json
(
sendOK
(
result
));
}
catch
(
error
)
{
res
.
status
(
500
).
json
(
sendError
(
error
.
message
))
}
}
async
updateProduct
(
req
,
res
,
next
)
{
const
self
=
this
;
try
{
let
body
=
req
.
body
;
body
.
updatedAt
=
new
Date
;
const
result
=
await
self
.
producProvider
.
put
(
body
,
req
.
params
.
code
);
if
(
result
==
1
)
return
res
.
status
(
200
).
json
(
sendOK
(
result
));
else
res
.
status
(
500
).
json
(
sendError
(
"update fail"
));
}
catch
(
error
)
{
res
.
status
(
500
).
json
(
sendError
(
error
.
message
))
}
}
async
deleteProduct
(
req
,
res
,
next
)
{
const
self
=
this
;
try
{
const
result
=
await
self
.
producProvider
.
delete
(
req
.
params
.
code
);
if
(
result
==
1
)
return
res
.
status
(
200
).
json
(
sendOK
(
result
));
else
res
.
status
(
500
).
json
(
sendError
(
"delete fail"
))
}
catch
(
error
)
{
res
.
status
(
500
).
json
(
sendError
(
error
.
message
));
}
}
}
module
.
exports
=
product
;
\ No newline at end of file
This diff is collapsed.
Click to expand it.
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment