const express = require('express');
const app = express();
const cors = require('cors');
const http = require('http');
const server = http.createServer(app);
const port = process.env.port || 5000;
const uuid = require('uuid');
app.use(cors());
app.use(express.json());
server.listen(port, () => {
console.log('server listen on port: ' + port);
});
const conn = require('./db');
app.post('/createuser', (req, res) => {
const user_uid = uuid.v4().substring(0, 8);
const {user_name, user_password, user_batch} = req.body;
conn.query("INSERT INTO tbl_users SET ?", {user_uid,user_name,user_password,user_batch}, (err, result) => {
if(err){
res.send({
status: false,
message: 'user not created'
})
}else{
res.send({
status: true,
message: 'user successfully created'
});
}
});
console.log("data saved...");
});
app.get('/getusers', (req, res) => {
conn.query("SELECT * FROM tbl_users", (err, result) => {
if(err){
res.send({status: false, message: 'users data not get', data: []})
}else{
res.send({
status: true,
message: 'users data',
data: result
});
}
});
});
app.post('/updateuser/:id', (req, res) => {
const { id } = req.params;
const {user_name, user_password, user_batch} = req.body;
conn.query("UPDATE tbl_users SET ? WHERE id = ?", [{user_name, user_password, user_batch},id], (err, result) => {
if(err){
res.send({
status: false,
message: 'user not update'
})
}else{
res.send({
status: true,
message: 'user successfully update'
});
}
});
console.log("data updated...");
});
app.get('/getuser/:uid', (req, res) => {
const { uid } = req.params;
conn.query(`SELECT * FROM tbl_users WHERE user_uid='${uid}'`, (err, result) => {
if(err){
res.send({status: false, message: 'user data not get', data: []})
}else{
res.send({
status: true,
message: 'single user data',
data: result
});
}
});
console.log("request get");
});
app.get('/getposts/:uid', (req, res) => {
const { uid } = req.params;
conn.query(`SELECT id,user_uid,post_title,date FROM tbl_posts WHERE user_uid='${uid}'`, (err, result) => {
if(err){
res.send({status: false, message: `DAta Not get error: ${err}`, data: []})
}else{
res.send({status: true, message: 'single user get posts list', data: result})
}
});
console.log("get posts");
});