From a5771ffb51f48e787d9d95f7c6defe612db9d7c2 Mon Sep 17 00:00:00 2001 From: codewith-zach <92583144+codewith-zach@users.noreply.github.com> Date: Mon, 29 Aug 2022 13:23:00 +0100 Subject: [PATCH] search api --- api/users/user.controller.js | 25 ++++++++++++++++++++++++- api/users/user.router.js | 4 +++- api/users/user.service.js | 19 +++++++++++++++++++ 3 files changed, 46 insertions(+), 2 deletions(-) diff --git a/api/users/user.controller.js b/api/users/user.controller.js index fc0e878..c81a31a 100644 --- a/api/users/user.controller.js +++ b/api/users/user.controller.js @@ -13,7 +13,9 @@ const { getUsers, createUsers, getVisitorPurpose, - getAllVisitors + getAllVisitors, + getVisitorByFullname + } = require('./user.service'); //we called the service const { genSaltSync, hashSync, compareSync} = require('bcrypt');//importing bcrypt @@ -189,6 +191,27 @@ module.exports = { }); }); }, + getVisitorByFullname: (req, res) => { + const fullname = req.query.fullname; + console.log(fullname); + + getVisitorByFullname(fullname, (err, results) => { + if (err) { + console.log(err); + return; + } + if (!results) { + return res.json({ + success: 0, + message: "Record not Found" + }); + } + return res.json({ + success: 1, + data: results + }); + }); + }, updateUser: (req, res) => { const body = req.body; console.log(body); diff --git a/api/users/user.router.js b/api/users/user.router.js index 6121ffd..f25c9b1 100644 --- a/api/users/user.router.js +++ b/api/users/user.router.js @@ -11,7 +11,8 @@ const { //We have to import all the controllers in the router getUsers, createUsers, getVisitorPurpose, - getAllVisitors + getAllVisitors, + getVisitorByFullname } = require('./user.controller'); const router = require('express').Router(); @@ -33,5 +34,6 @@ router.post('/user/login', login); //used for login router.get('/user/getUsers', getUsers); router.post('/user/createUsers', checkToken, createUsers); router.get('/visitor/getVisitorPurpose', getVisitorPurpose); +router.get('/visitor/getVisitorByFullname', getVisitorByFullname); module.exports = router; \ No newline at end of file diff --git a/api/users/user.service.js b/api/users/user.service.js index ea7d212..030a7f9 100644 --- a/api/users/user.service.js +++ b/api/users/user.service.js @@ -164,6 +164,25 @@ module.exports = { } ); }, + getVisitorByFullname: (fullname, callBack) => { + pool.query( + ` + select v.id as id, v.fullname as fullname, v.purpose_id as purpose, v.date_added as date_added, v.address as address, + c.time_in as time_in, c.time_out as time_out,u.first_name as first_name, u.last_name as last_name + from clock_in c + join visitors v on v.id = c.visitor_id + join users u on u.id = v.user_id + where v.fullname like '%${fullname}%' + `, + [fullname], + (error, results, fields) => { + if (error) { + callBack(error); + } + return callBack(null, results[0]); + } + ); + }, updateVisitors: (data, callBack) => { pool.query( `update visitors set fullname=?, user_id=?, purpose_id=?, date_added=?, address=? where id=?`,