Commit 32670d9a authored by chaiwat's avatar chaiwat

add

parent f2a2757e
......@@ -19,6 +19,8 @@
"homepage": "https://github.com/fuseSavage/RepaiCenter-api#readme",
"dependencies": {
"axios": "^0.24.0",
"bcrypt": "^5.0.1",
"bcryptjs": "^2.4.3",
"body-parser": "^1.19.0",
"cors": "^2.8.5",
"date": "^1.0.2",
......
......@@ -9,6 +9,10 @@ dotenv.config({
module.exports = {
JWT_SECRET: 'secretRepairJWT',
EXPIRE_TOKEN: 604800,
EXPIRE_TOKEN_MEMBER: 604800,
HOST: "localhost",
USER: "root",
PASSWORD: "",
......
const express = require("express");
const moment = require("moment");
const jwt = require("jsonwebtoken");
const constants = require("../configs/constants");
const responseCode = require("../configs/responseCode");
const router = express.Router();
const {
callRegisterGarage,
callLoginUser,
} = require("../services/funcCallback");
// router.post('/accesstoken', Validate(createAccesstokenSchema), async (request, response, next) => {
router.post("/accesstoken", async (request, response, next) => {
try {
const data = request.body;
// console.log("data 00000000 ", data);
callRegisterGarage(data, function (err, datas, status) {
// console.log("status ", status);
if (status == responseCode.SUCCESS) {
const expireAt = moment().add(constants.EXPIRE_TOKEN, "seconds");
const tokenData = {
userId: data.garageID,
userName: data.user_name,
garageName: data.garage_name,
email: data.email,
status: status,
};
const token = jwt.sign(tokenData, constants.JWT_SECRET, {
expiresIn: constants.EXPIRE_TOKEN,
});
response.json({
code: 200,
message: "insert garage success",
data: {
token,
expired_at: expireAt,
},
userData: tokenData,
});
} else {
response.json({
code: 204,
message: "มี UserID นี้แล้ว",
});
}
});
} catch (error) {
console.log(error);
}
});
//User Login
router.post("/login", async (request, response, next) => {
try {
//get user input
const data = request.body;
callLoginUser(data, function (err, datas, status) {
// console.log(status);
if (status == responseCode.SUCCESS) {
const expireAt = moment().add(constants.EXPIRE_TOKEN, "seconds");
const tokenData = {
userId: datas.garageID,
userName: datas.user_name,
garageName: datas.garage_name,
email: datas.email,
status: status,
};
const token = jwt.sign(tokenData, constants.JWT_SECRET, {
expiresIn: constants.EXPIRE_TOKEN,
});
response.json({
code: 200,
message: "login success",
data: {
token,
expired_at: expireAt,
},
userData: tokenData,
});
//seve user token
// user.token = token
} else if (status == responseCode.SUCCESS_NO_CONTENT) {
response.json({
code: 204,
message: "Incorrect Email and/or Password!"
});
}
else {
response.json({
code: 204,
message: "login is not success"
});
}
});
// response.status(400).send("Invalid Credentials")
} catch (error) {
console.log(error);
}
});
router.delete("/accesstoken", async (request, response) => {
response.json({
code: 200,
message: "Destroy accesstoken success",
data: response.session,
});
});
module.exports = router;
const express = require("express");
const { registerGarage } = require("../services/database");
const {
callRegisterGarage,
callGetallGarage,
callGetGarage,
callUpdateGarage,
callDeleteGarage,
} = require("../services/funcCallback");
const responseCode = require("../configs/responseCode");
......@@ -14,6 +15,7 @@ const router = express.Router();
router.post("/insert", async (request, response, next) => {
try {
let data = request.body;
// console.log(request.body)
callRegisterGarage(data, function (err, datas, status) {
if (status == responseCode.SUCCESS) {
......@@ -53,7 +55,7 @@ router.get("/all", (request, response, next) => {
}
});
// Get All Garage
// Get One Garage
router.get("/getgarage", (request, response, next) => {
let data = request.body;
// console.log(data)
......@@ -78,4 +80,56 @@ router.get("/getgarage", (request, response, next) => {
}
});
// Update Garage
router.post("/update", (request, response, next) => {
let data = request.body;
// console.log(data)
try {
callUpdateGarage(data, function (err, datas, status) {
// console.log(status)
if (status == responseCode.SUCCESS) {
response.json({
code: 200,
message: "Update garage success",
total: datas.length,
data: datas,
});
} else {
response.json({
code: 400,
message: "Update garage not success",
});
}
});
} catch (err) {
console.log(err);
}
});
// Delete Garage
router.delete("/delete", (request, response, next) => {
let data = request.body;
try {
callDeleteGarage(data, function (err, datas, status) {
// console.log(status)
if (status == responseCode.SUCCESS) {
response.json({
code: 200,
message: "Delete garage success",
total: datas.length,
data: datas,
});
} else {
response.json({
code: 400,
message:
"Delete garage not success มี garageID นี้ อยู่ในประวัติการซ่อมอยู่",
});
}
});
} catch (err) {
console.log(err);
}
});
module.exports = router;
const express = require('express')
const express = require("express");
const {
getallMember,
} = require('../services/database')
const responseCode = require('../configs/responseCode')
const router = express.Router()
router.get('/all', (request, response, next) => {
console.log(['hello'])
try {
getallMember().then(res => {
if (res.status == responseCode.SUCCESS) {
response.json({
code: 200,
message: 'get all success',
})
}
})
} catch (err) {
console.log(err)
}
})
module.exports = router
\ No newline at end of file
callGetallMember,
callRegisterMember,
callGetMember,
callDeleteMember,
} = require("../services/funcCallback");
const responseCode = require("../configs/responseCode");
const router = express.Router();
// Get All Member
router.get("/all", (request, response, next) => {
try {
callGetallMember(function (err, datas, status) {
// console.log(status);
if (status == responseCode.SUCCESS) {
response.json({
code: 200,
message: "get all success",
total: datas.length,
data: datas,
});
} else {
response.json({
code: 400,
message: "nodata",
});
}
});
} catch (err) {
console.log(err);
}
});
// Insert Member
router.post("/insert", async (request, response, next) => {
try {
let data = request.body;
callRegisterMember(data, function (err, datas, status) {
if (status == responseCode.SUCCESS) {
response.json({
code: 200,
message: "insert member success",
data: datas,
});
} else {
response.json({
code: 204,
message: "มี member นี้แล้ว",
});
}
});
} catch (err) {
console.log(err);
}
});
// Get One Member
router.get("/getmember", (request, response, next) => {
let data = request.body;
// console.log(data)
try {
callGetMember(data, function (err, datas, status) {
if (status == responseCode.SUCCESS) {
response.json({
code: 200,
message: "get data success",
total: datas.length,
data: datas,
});
} else {
response.json({
code: 500,
message: "ไม่มี UsesID นี้อยู่ในตาราง",
});
}
});
} catch (err) {
console.log(err);
}
});
// Delete Member
router.delete("/delete", (request, response, next) => {
let data = request.body;
try {
callDeleteMember(data, function (err, datas, status) {
// console.log(status)
if (status == responseCode.SUCCESS) {
response.json({
code: 200,
message: "Delete success",
total: datas.length,
data: datas,
});
} else {
response.json({
code: 400,
message: "ลบไม่ได้ เนื่องจากมี member นี้ในประวัติการซ่อม",
});
}
});
} catch (err) {
console.log(err);
}
});
module.exports = router;
const express = require("express");
const {
callRepairDetail,
callGetallDetail,
callGetByMember,
callGetByGarage,
} = require("../services/funcCallback");
const responseCode = require("../configs/responseCode");
const router = express.Router();
// Insert Member
router.post("/insert", async (request, response, next) => {
try {
let data = request.body;
callRepairDetail(data, function (err, datas, status) {
if (status == responseCode.SUCCESS) {
response.json({
code: 200,
message: "insert success",
data: datas,
});
} else {
response.json({
code: 204,
message: "sql is not working!!",
});
}
});
} catch (err) {
console.log(err);
}
});
// Get All Detail
router.get("/all", (request, response, next) => {
try {
callGetallDetail( function (err, datas, status) {
// console.log(status);
if (status == responseCode.SUCCESS) {
response.json({
code: 200,
message: "get all success",
total: datas.length,
data: datas,
});
}
});
} catch (err) {
console.log(err);
}
});
// Get By Member
router.get("/getbymember", (request, response, next) => {
let data = request.body;
// console.log(data)
try {
callGetByMember(data, function (err, datas, status) {
if (status == responseCode.SUCCESS) {
response.json({
code: 200,
message: "get data success",
total: datas.length,
data: datas,
});
} else {
response.json({
code: 500,
message: "ไม่มี member นี้อยู่ในตาราง",
});
}
});
} catch (err) {
console.log(err);
}
});
// Get By Member
router.get("/getbygarage", (request, response, next) => {
let data = request.body;
// console.log(data)
try {
callGetByGarage(data, function (err, datas, status) {
if (status == responseCode.SUCCESS) {
response.json({
code: 200,
message: "get data success",
total: datas.length,
data: datas,
});
} else {
response.json({
code: 500,
message: "ไม่มี garage นี้อยู่ในตาราง",
});
}
});
} catch (err) {
console.log(err);
}
});
module.exports = router;
const express = require("express");
const { callInsertReport, callGetReport } = require("../services/funcCallback");
const responseCode = require("../configs/responseCode");
const router = express.Router();
// Insert Member
router.post("/insert", async (request, response, next) => {
try {
let data = request.body;
callInsertReport(data, function (err, datas, status) {
if (status == responseCode.SUCCESS) {
response.json({
code: 200,
message: "insert success",
data: datas,
});
} else {
response.json({
code: 204,
message: "sql is not working!!",
});
}
});
} catch (err) {
console.log(err);
}
});
// Get All Report
router.get("/all", (request, response, next) => {
try {
callGetReport(function (err, datas, status) {
// console.log(status);
if (status == responseCode.SUCCESS) {
response.json({
code: 200,
message: "get all success",
total: datas.length,
data: datas,
});
}
});
} catch (err) {
console.log(err);
}
});
module.exports = router;
const jwt = require("jsonwebtoken");
const constants = require("../configs/constants");
const responseCode = require("../configs/responseCode");
const { ERROR_UNAUTHORIZED } = responseCode;
const auth = (req, res, next) => {
try {
if (!req.headers)
return res.status(ERROR_UNAUTHORIZED).send({
code: ERROR_UNAUTHORIZED,
message: "No headers provided.",
});
const token = req.body.token || req.query.token || req.headers['x-access-token'] || req.headers.authorization;
jwt.verify(token, constants.JWT_SECRET, (err, decoded) => {
if (err) {
return res.status(ERROR_UNAUTHORIZED).send({
code: ERROR_UNAUTHORIZED,
message: "Failed to authenticate token.",
});
}
const { userId, userName, garageName, email, status } = decoded;
console.log(
"userId, status, imageUrl, userName => ",
userId,
userName,
garageName,
email,
status
);
req.session = {
userId,
userName,
garageName,
email,
status,
};
console.log("correct!!!!!!! token");
next();
});
} catch (err) {
return res.status(ERROR_UNAUTHORIZED).send({
code: ERROR_UNAUTHORIZED,
message: 'Failed to authenticate token.'
})
}
};
module.exports = auth;
const express = require('express')
const router = express.Router()
const AuthenticationController = require('../controller/AuthenticationController')
router.use('/authentication', AuthenticationController)
module.exports = router
......@@ -2,8 +2,13 @@ const express = require('express')
const responseCode = require('../configs/responseCode');
const router = express.Router()
const authentication = require('./authentication')
const member = require('./member');
const garage = require('./garage');
const repairdetail = require('./repairdetail')
const report = require('./report')
const middleware = require('../middlewares/authentication')
router.get('/test',(req,res)=>{
......@@ -14,7 +19,10 @@ router.get('/test',(req,res)=>{
console.log('test')
})
router.use(member)
router.use(garage)
router.use(authentication)
router.use(middleware, member)
router.use(middleware, garage)
router.use(middleware, repairdetail)
router.use(middleware, report)
module.exports = router
\ No newline at end of file
// import express from 'express'
const express = require("express");
const router = express.Router();
const RepairDetailController = require("../controller/RepairDetailController");
router.use("/repairdetail", RepairDetailController);
module.exports = router;
// import express from 'express'
const express = require('express')
const router = express.Router()
// import UserController from '../controllers/users/UserController'
const ReportController = require('../controller/ReportController')
router.use('/report', ReportController)
module.exports = router
\ No newline at end of file
This diff is collapsed.
const responseCode = require("../configs/responseCode");
const { getAllGarage, getGarage, registerGarage } = require("./database");
const {
getAllGarage,
getGarage,
registerGarage,
updateGarage,
deleteGarage,
getAllMember,
registerMember,
getMember,
deleteMember,
repairDetail,
getAllDetail,
getByMember,
getByGarage,
insertReport,
getReport,
loginUser
} = require("./database");
//fucntion callback Insert Garage
async function callRegisterGarage(data, callback) {
try {
// console.log(data)
registerGarage(data, function (err, result, status) {
if (err || !result.length) return callback("error or no results");
if (err || !result.length) {
// callback("error or no results");
console.log('55555')
}
result = result.map((obj) => obj);
// console.log(result)
callback(null, result, status);
});
} catch (err) {
console.log(err);
}
}
//fucntion callback Login User Garage
async function callLoginUser(data, callback) {
try {
// console.log(data)
loginUser(data, function (err, result, status) {
callback(null, result, status);
});
} catch (err) {
console.log(err);
}
}
//fucntion callback get all garage
async function callGetallGarage(callback) {
......@@ -35,7 +62,6 @@ async function callGetallGarage(callback) {
callback(null, result, responseCode.SUCCESS);
});
} catch (err) {
console.log(err);
}
......@@ -57,9 +83,202 @@ async function callGetGarage(data, callback) {
}
}