时间:1.6个小时左右
代码:170行左右
博客:1
学习内容:完成了电影后台管理的增删改查,电影票、影院、排片
代码:
//获取当前页影院 router.get('/api/admin/getCurrentPageCinema',function(req,res){ let { currentPage, pageSize,input } = req.query; let start = Number((currentPage-1)*pageSize); pageSize = Number(pageSize); let sqlStr = 'SELECT * FROM t_cinema WHERE cinema_name LIKE ? ORDER BY cinema_id ;'; let total; conn.query(sqlStr,["%"+input+"%"],(error,result,field)=>{ if (error){ console.log(error); } else{ result = JSON.parse(JSON.stringify(result)); total = result.length; } }); sqlStr = 'SELECT * FROM t_cinema WHERE cinema_name LIKE ? ORDER BY cinema_id LIMIT ?,?;'; conn.query(sqlStr,["%"+input+"%",start,pageSize],(error,result,field)=>{ if (error){ console.log(error); } else{ result = JSON.parse(JSON.stringify(result)); res.json({success_code:200,data:result,total:total}); } }) }); //更新影院信息 router.post('/api/admin/updateCinemaInfo',function(req,res){ let { cinemaId, cinemaName, cinemaPhone, address } = req.body; if (cinemaId){ let sqlStr = 'SELECT * from t_cinema WHERE cinema_id = ? LIMIT 1;'; conn.query(sqlStr,[cinemaId],(error,result,field)=>{ if (error){ console.log(error); } else{ sqlStr = 'SELECT * FROM t_cinema WHERE cinema_name = ? AND cinema_id <> ? LIMIT 1 ;'; conn.query(sqlStr,[cinemaName,cinemaId],(error,result,field)=>{ if (error){ console.log(error); } else{ result = JSON.parse(JSON.stringify(result)); if (result[0]){ res.json({error_code:1,message:'影院名已存在!'}); } else{ //更新数据库 let sqlStr = 'UPDATE t_cinema SET cinema_name = ?,cinema_phone = ?,specified_address = ? WHERE cinema_id = ?;'; conn.query(sqlStr,[cinemaName,cinemaPhone,address,cinemaId],(error,result,field)=>{ if (error){ res.json({error_code:1,message:'更新影院信息失败'}); console.log(error); } else{ res.json({success_code:200}) } }) } } }); } }) } }); //删除影院信息 router.post('/api/admin/deleteCinemaInfo',function(req,res){ let { cinemaId } = req.body; if (cinemaId){ let sqlStr = 'DELETE FROM t_cinema WHERE cinema_id =?'; conn.query(sqlStr,[cinemaId],(error,result,field)=>{ if (error){ console.log(error); } else { res.json({success_code:200}); } }) } }); //添加影院信息 router.post('/api/admin/addCinemaInfo',function(req,res){ let { cinemaName, cinemaPhone, address } = req.body; sqlStr = 'SELECT * FROM t_cinema WHERE cinema_name = ? LIMIT 1 ;'; conn.query(sqlStr,[cinemaName],(error,result,field)=>{ if (error){ console.log(error); } else{ result = JSON.parse(JSON.stringify(result)); if (result[0]){ res.json({error_code:1,message:'影院名已存在!'}); } else{ let sqlStr = 'INSERT INTO t_cinema(cinema_name,cinema_phone,specified_address) VALUES(?,?,?);'; conn.query(sqlStr,[cinemaName,cinemaPhone,address],(error,result,field)=>{ if (error){ console.log(error); } else{ res.json({success_code:200}); } }) } } }); });