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) {
}
}
//fucntion callback Update Garage
async function callUpdateGarage(data, callback) {
// console.log(data)
try {
updateGarage(data, function (err, result, status) {
// console.log('test', result)
callback(null, result, status);
});
} catch (err) {
console.log(err);
}
}
//fucntion callback Delete Garage
async function callDeleteGarage(data, callback) {
// console.log(data)
try {
deleteGarage(data, function (err, result, status) {
// console.log('test', result)
callback(null, result, status);
});
} catch (err) {
console.log(err);
}
}
//fucntion callback get all member
async function callGetallMember(callback) {
try {
getAllMember("result", function (err, result, status) {
if (err || !result.length) return callback("error or no results");
result = result.map((obj) => obj);
// console.log(result);
callback(null, result, status);
});
} catch (err) {
console.log(err);
}
}
//fucntion callback Insert Member
async function callRegisterMember(data, callback) {
try {
registerMember(data, function (err, result, status) {
if (err || !result.length) return callback("error or no results");
result = result.map((obj) => obj);
callback(null, result, status);
});
} catch (err) {
console.log(err);
}
}
//fucntion callback get one Member
async function callGetMember(data, callback) {
// console.log(data)
try {
getMember(data, function (err, result, status) {
if (err || !result.length) return callback("error or no results");
result = result.map((obj) => obj);
callback(null, result, status);
});
} catch (err) {
console.log(err);
}
}
//fucntion callback Delete Member
async function callDeleteMember(data, callback) {
// console.log(data)
try {
deleteMember(data, function (err, result, status) {
// console.log('test', result)
callback(null, result, status);
});
} catch (err) {
console.log(err);
}
}
//fucntion callback Insert Member
async function callRepairDetail(data, callback) {
try {
repairDetail(data, function (err, result, status) {
// if (err || !result.length) return callback("error or no results");
// result = result.map((obj) => obj);
callback(null, result, status);
});
} catch (err) {
console.log(err);
}
}
//fucntion callback get all member
async function callGetallDetail(callback) {
try {
getAllDetail("result", function (err, result, status) {
if (err || !result.length) return callback("error or no results");
result = result.map((obj) => obj);
callback(null, result, status);
});
} catch (err) {
console.log(err);
}
}
//fucntion callback get by member
async function callGetByMember(data, callback) {
try {
getByMember(data , function (err, result, status) {
if (err || !result.length) return callback("error or no results");
result = result.map((obj) => obj);
callback(null, result, status);
});
} catch (err) {
console.log(err);
}
}
//fucntion callback get by garage
async function callGetByGarage(data, callback) {
try {
getByGarage(data , function (err, result, status) {
if (err || !result.length) return callback("error or no results");
result = result.map((obj) => obj);
// console.log(result)
callback(null, result, status);
});
} catch (err) {
console.log(err);
}
}
//fucntion callback Insert report
async function callInsertReport(data, callback) {
try {
insertReport(data, function (err, result, status) {
// if (err || !result.length) return callback("error or no results");
// result = result.map((obj) => obj);
callback(null, result, status);
});
} catch (err) {
console.log(err);
}
}
//fucntion callback get all member
async function callGetReport(callback) {
try {
getReport("result", function (err, result, status) {
if (err || !result.length) return callback("error or no results");
result = result.map((obj) => obj);
// console.log(result);
callback(null, result, status);
});
} catch (err) {
console.log(err);
}
}
module.exports = {
callRegisterGarage,
callGetallGarage,
callGetGarage,
callUpdateGarage,
callDeleteGarage,
callGetallMember,
callRegisterMember,
callGetMember,
callDeleteMember,
callRepairDetail,
callGetallDetail,
callGetByMember,
callGetByGarage,
callInsertReport,
callGetReport,
callLoginUser
};
module.exports = {
garage: `CREATE TABLE garage(
garageID int NOT NULL AUTO_INCREMENT,
garageID varchar(255),
party varchar(100),
userID varchar(255),
password int NOT NULL,
password varchar(100),
user_name varchar(255),
garage_name varchar(255),
email varchar(255),
......@@ -17,7 +16,7 @@ module.exports = {
province varchar(100),
pos_code varchar(100),
address_map varchar(255),
registration_date DATETIME,
registration_date varchar(100),
on_time varchar(100),
off_time varchar(100),
tel varchar(30),
......@@ -26,36 +25,40 @@ module.exports = {
)`,
member: `CREATE TABLE member(
memberID int NOT NULL AUTO_INCREMENT,
member_tel varchar(100),
party varchar(100),
member_tel varchar(100),
member_name varchar(255),
member_ads varchar(255),
shop_register varchar(255),
registration_date DATETIME,
registration_date varchar(100),
PRIMARY KEY (memberID)
PRIMARY KEY (member_tel)
)`,
repairDetails: `CREATE TABLE repairdetails(
detailsID int NOT NULL AUTO_INCREMENT,
shop_repair varchar(255),
garageID varchar(255),
member_tel varchar(100),
device_type varchar(255),
device varchar(255),
details varchar(255),
repair_date DATETIME,
repair_date varchar(255),
status varchar(100),
price int(10),
PRIMARY KEY (detailsID)
PRIMARY KEY (detailsID),
FOREIGN KEY (member_tel) REFERENCES member(member_tel),
FOREIGN KEY (garageID) REFERENCES garage(garageID)
)`,
reported: `CREATE TABLE reported(
reportID int NOT NULL AUTO_INCREMENT,
party varchar(100),
user_report varchar(255),
name varchar(255),
report_detail varchar(255),
report_date DATETIME,
report_tel varchar(50),
report_date varchar(100),
PRIMARY KEY (reportID)
)`,
......
module.exports = {
insert_garage: `INSERT INTO garage
insert_garage: `INSERT INTO garage
(
garageID,
party,
userID,
password,
user_name,
garage_name,
......@@ -21,5 +21,37 @@ module.exports = {
on_time,
off_time,
tel
) VALUES (?)`
}
\ No newline at end of file
) VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)`,
insert_member: `INSERT INTO member
(
party,
member_tel,
member_name,
member_ads,
shop_register,
registration_date
) VALUES (?)`,
insert_detail: `INSERT INTO repairdetails
(
garageID,
member_tel,
device_type,
device,
details,
repair_date,
status,
price
) VALUES (?,?,?,?,?,?,?,?)`,
insert_reported: `INSERT INTO reported
(
party,
user_report,
name,
report_detail,
report_tel,
report_date
) VALUES (?)`
};
This diff is collapsed.
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment