Commit f2a2757e authored by chaiwat's avatar chaiwat

route insert getdata Garage

parents
APP=dev
APP_NAME='RepairCenter'
PORT=4000
BASE_URL='http://localhost'
\ No newline at end of file
node_modules
# .env
yarn-error.logg
\ No newline at end of file
This source diff could not be displayed because it is too large. You can view the blob instead.
{
"name": "repaicenter-api",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"start": "nodemon src/index.js"
},
"repository": {
"type": "git",
"url": "git+https://github.com/fuseSavage/RepaiCenter-api.git"
},
"author": "",
"license": "ISC",
"bugs": {
"url": "https://github.com/fuseSavage/RepaiCenter-api/issues"
},
"homepage": "https://github.com/fuseSavage/RepaiCenter-api#readme",
"dependencies": {
"axios": "^0.24.0",
"body-parser": "^1.19.0",
"cors": "^2.8.5",
"date": "^1.0.2",
"dotenv": "^10.0.0",
"express": "^4.17.1",
"jsonwebtoken": "^8.5.1",
"morgan": "^1.10.0",
"mysql": "^2.18.1",
"mysql2": "^2.3.3",
"nodemon": "^2.0.15",
"request": "^2.88.2",
"sequelize": "^6.12.0-alpha.1",
"sql-query": "^0.1.27",
"sql-query-identifier": "^2.2.4",
"yargs": "^17.2.1"
}
}
const dotenv = require('dotenv')
const { argv } = require('yargs')
dotenv.config({
path: argv.env || '.env',
})
module.exports = {
HOST: "localhost",
USER: "root",
PASSWORD: "",
DB: "repaircenter_db",
APP_NAME: process.env.APP_NAME,
PORT: process.env.PORT || 3000,
ERROR: {
NO_AUTH_CODE: 401,
}
}
module.exports = {
SUCCESS: 200,
SUCCESS_NO_CONTENT: 204,
ERROR_BAD_REQUEST: 400,
ERROR_UNAUTHORIZED: 401,
ERROR_NOT_FOUND: 404,
ERROR_API: 500,
ERROR_DB_UNKNOWN: 1000,
ERROR_DB_DUPLICATE: 1001,
ERROR_DB_DOWN: 1100,
}
\ No newline at end of file
const express = require("express");
const { registerGarage } = require("../services/database");
const {
callRegisterGarage,
callGetallGarage,
callGetGarage,
} = require("../services/funcCallback");
const responseCode = require("../configs/responseCode");
const router = express.Router();
// Insert garage
router.post("/insert", async (request, response, next) => {
try {
let data = request.body;
callRegisterGarage(data, function (err, datas, status) {
if (status == responseCode.SUCCESS) {
response.json({
code: 200,
message: "insert garage success",
data: datas,
});
} else {
response.json({
code: 204,
message: "มี UserID นี้แล้ว",
});
}
});
} catch (err) {
console.log(err);
}
});
// Get All Garage
router.get("/all", (request, response, next) => {
try {
callGetallGarage(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 All Garage
router.get("/getgarage", (request, response, next) => {
let data = request.body;
// console.log(data)
try {
callGetGarage(data, function (err, datas, status) {
if (status == responseCode.SUCCESS) {
response.json({
code: 200,
message: "get one garage success",
total: datas.length,
data: datas,
});
} else {
response.json({
code: 500,
message: "ไม่มี UsesID นี้อยู่ในตาราง",
});
}
});
} catch (err) {
console.log(err);
}
});
module.exports = router;
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
const express = require('express')
const bodyParser = require('body-parser')
const logger = require('morgan')
const cors = require("cors");
const constants = require('./configs/constants')
const responseCode = require('./configs/responseCode')
const route = require('./routes')
const appname = constants.APP_NAME
const port = constants.PORT
const app = express();
app.use(logger('combined'))
app.use(cors())
app.use(bodyParser.json())
app.use(bodyParser.urlencoded({ extended: true }))
app.use(express.static('public'))
app.use(route)
app.use((req, res) => {
return res.status(responseCode.ERROR_NOT_FOUND).send({
code: responseCode.ERROR_NOT_FOUND,
message: 'Route' + req.url + ' Not found.'
})
})
app.listen(port, () => console.log(`${appname} app listening on port ${port}!`))
\ No newline at end of file
// import express from 'express'
const express = require('express')
const router = express.Router()
const GarageController = require('../controller/GarageController')
router.use('/garage', GarageController)
module.exports = router
\ No newline at end of file
const express = require('express')
const responseCode = require('../configs/responseCode');
const router = express.Router()
const member = require('./member');
const garage = require('./garage');
router.get('/test',(req,res)=>{
const name = req.query.name
console.log(name)
let timestamp = typeof parseInt(+new Date()/1000);
res.json({ time:timestamp,code: responseCode.SUCCESS_NO_CONTENT })
console.log('test')
})
router.use(member)
router.use(garage)
module.exports = router
\ No newline at end of file
// import express from 'express'
const express = require('express')
const router = express.Router()
// import UserController from '../controllers/users/UserController'
const MemberController = require('../controller/MemberController')
router.use('/member', MemberController)
module.exports = router
\ No newline at end of file
const { query } = require("express");
const mysql = require("mysql");
const constants = require("../configs/constants");
const responseCode = require("../configs/responseCode");
const identify = require("sql-query-identifier");
var sql = require("sql-query"),
sqlQuery = sql.Query(); //for dialect: sql.Query('postgresql')
// Create a connection to the database
const db = mysql.createConnection({
host: constants.HOST,
user: constants.USER,
password: constants.PASSWORD,
database: constants.DB,
});
// Import SQL Commmand
const createTable = require("../sql/createTable");
const insertInto = require("../sql/insertInto");
// DataBase Connected
db.connect((err) => {
try {
if (err) throw err;
console.log("connected!!");
// Show All Table in DataBase
db.query("SHOW TABLES", (err, result) => {
if (err) throw err;
let allTable = [];
for (let i = 0; i < result.length; i++) {
allTable.push(result[i].Tables_in_repaircenter_db);
}
// Check Table garage in DataBase
if (allTable.includes("garage") == false) {
// Create Table
db.query(createTable.garage, (err) => {
if (err) throw err;
console.log("Created table garage successfully.");
});
} else {
console.log("database there is already this table.");
}
// Check Table member in DataBase
if (allTable.includes("member") == false) {
// Create Table
db.query(createTable.member, (err) => {
if (err) throw err;
console.log("Created table member successfully.");
});
} else {
console.log("database there is already this table.");
}
// Check Table repairDetails in DataBase
if (allTable.includes("repairdetails") == false) {
// Create Table
db.query(createTable.repairDetails, (err) => {
if (err) throw err;
console.log("Created table repairDetails successfully.");
});
} else {
console.log("database there is already this table.");
}
// Check Table reported in DataBase
if (allTable.includes("reported") == false) {
// Create Table
db.query(createTable.reported, (err) => {
if (err) throw err;
console.log("Created table reported successfully.");
});
} else {
console.log("database there is already this table.");
}
});
} catch (err) {
console.log(err);
}
});
// insert garage in RepairCenterDB
function registerGarage(data, callback) {
values = [
data.party,
data.userID,
data.password,
data.user_name,
data.garage_name,
data.email,
data.garage_type,
data.address_number,
data.moo,
data.alley,
data.road,
data.sub_district,
data.district,
data.province,
data.pos_code,
data.address_map,
new Date(),
data.on_time,
data.off_time,
data.tel,
];
try {
let sql = `SELECT userID FROM garage`;
db.query(sql, (error, result) => {
if (error) {
console.log("===>>> Error", err);
``;
} else {
// console.log(result)
let listUID = [];
result.forEach((doc) => {
listUID.push(doc.userID);
});
const checkUID = listUID.includes(data.userID);
if (checkUID === false) {
db.query(insertInto.insert_garage, [values], (err, result) => {
if (err) {
return callback(err);
} else {
callback(null, values, responseCode.SUCCESS);
// console.log("5555555");
}
});
} else {
// console.log("มี userID นี้แล้ว");
callback(null, result, responseCode.SUCCESS_NO_CONTENT);
}
}
});
} catch (err) {
console.log(err);
}
}
// get All Garage
function getAllGarage(data, callback) {
try {
db.query("SELECT * FROM garage", function (err, rows) {
if (err) return callback(err);
callback(null, rows, responseCode.SUCCESS);
});
} catch (err) {
console.log(err);
}
}
// get one Garage
function getGarage(data, callback) {
// console.log(data.userID);
let sql = `SELECT * FROM garage WHERE userID = "${data.userID}"`;
try {
db.query(sql, function (err, rows) {
if (err) return callback(err);
callback(null, rows, responseCode.SUCCESS);
});
} catch (err) {
console.log(err);
}
}
// insert Product
async function insertProduct(data) {
console.log("register ", data);
try {
db.query("SELECT * FROM repairapi", (err, result) => {
if (err) {
console.log("===>>> Error");
} else {
console.log("===>> suss", result);
}
});
return { status: responseCode.SUCCESS };
} catch (err) {
console.log(err);
}
}
// Get All Member
async function getallMember() {
try {
db.query("SELECT * FROM repairapi", (err, result) => {
if (err) {
console.log("===>>> Error");
} else {
console.log("===>> suss", result);
}
});
return { status: responseCode.SUCCESS };
} catch (err) {
console.log(err);
}
}
module.exports = {
registerGarage,
getAllGarage,
getGarage,
insertProduct,
getallMember,
};
const responseCode = require("../configs/responseCode");
const { getAllGarage, getGarage, registerGarage } = require("./database");
//fucntion callback Insert Garage
async function callRegisterGarage(data, callback) {
try {
registerGarage(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 garage
async function callGetallGarage(callback) {
try {
getAllGarage("result", function (err, result) {
if (err || !result.length) return callback("error or no results");
result = result.map((obj) => obj);
callback(null, result, responseCode.SUCCESS);
});
} catch (err) {
console.log(err);
}
}
//fucntion callback get one garage
async function callGetGarage(data, callback) {
// console.log(data)
try {
getGarage(data, function (err, result) {
if (err || !result.length) return callback("error or no results");
result = result.map((obj) => obj);
callback(null, result, responseCode.SUCCESS);
});
} catch (err) {
console.log(err);
}
}
module.exports = {
callRegisterGarage,
callGetallGarage,
callGetGarage,
};
module.exports = {
garage: `CREATE TABLE garage(
garageID int NOT NULL AUTO_INCREMENT,
party varchar(100),
userID varchar(255),
password int NOT NULL,
user_name varchar(255),
garage_name varchar(255),
email varchar(255),
garage_type varchar(255),
address_number varchar(10),
moo varchar(10),
alley varchar(100),
road varchar(100),
sub_district varchar(100),
district varchar(100),
province varchar(100),
pos_code varchar(100),
address_map varchar(255),
registration_date DATETIME,
on_time varchar(100),
off_time varchar(100),
tel varchar(30),
PRIMARY KEY (garageID)
)`,
member: `CREATE TABLE member(
memberID int NOT NULL AUTO_INCREMENT,
party varchar(100),
member_tel varchar(100),
member_name varchar(255),
member_ads varchar(255),
shop_register varchar(255),
registration_date DATETIME,
PRIMARY KEY (memberID)
)`,
repairDetails: `CREATE TABLE repairdetails(
detailsID int NOT NULL AUTO_INCREMENT,
shop_repair varchar(255),
member_tel varchar(100),
device_type varchar(255),
device varchar(255),
details varchar(255),
repair_date DATETIME,
status varchar(100),
PRIMARY KEY (detailsID)
)`,
reported: `CREATE TABLE reported(
reportID int NOT NULL AUTO_INCREMENT,
party varchar(100),
report_detail varchar(255),
report_date DATETIME,
PRIMARY KEY (reportID)
)`,
};
module.exports = {
insert_garage: `INSERT INTO garage
(
party,
userID,
password,
user_name,
garage_name,
email,
garage_type,
address_number,
moo,
alley,
road,
sub_district,
district,
province,
pos_code,
address_map,
registration_date,
on_time,
off_time,
tel
) VALUES (?)`
}
\ No newline at end of file
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