restify mysql_[菜鸟试水]关于Nodejs搭建后台API服务(Mysql-Restify)[下]
字數太多,我腰斬一下...
因為是API后臺,
沒有V只剩MC, 我們先看看Controller吧.也是簡單的處理代碼.
由require看出,我們需要在JsModule下再建一個文件夾Controll,再在里面新建GuestControll.js
var
Guest=require("../Model/Guest.js");
function
GuestControll(app,Mysql)
{
var
GuestModel=Object.create(Guest.GuestModel);
app.get('/guest/gettodayguest', GetTodayGuests);
//查詢今天客人
app.get('/guest/getroombooking',
GetRoomBooking); //查詢房間預訂信息
app.get('/guest/getguestinfobyid',
GetGuestInfoById); //根據客人id獲取信息
app.post('/guest/bookroom', NewGuest);
//預訂客房
app.post('/guest/checkin', Checkin);
//入住客房
app.post('/guest/checkout', Checkout);
//退房
app.post('/guest/cancelbooking', CancelBooking);
//取消預訂
app.post('/guest/overstay', OverStay);
//續住
function
GetTodayGuests(req, res, next) {
console.log("GetTodayGuests
query:"+JSON.stringify(req.query));
var
Today=req.params.today;
GuestModel.GetTodayGuests(Mysql,Today,function(Data){
res.charSet('utf-8');
res.send(Data);
});
}
function
NewGuest(req, res, next){
res.send('0');
}
function
Checkin(req, res, next) {
console.log("Checkin
param:"+JSON.stringify(req.params));
var
RoomId=req.params.roomid;
GuestModel.Checkin(Mysql,RoomId,function(Rst){
res.charSet('utf-8');
var
SR={iRst:Rst};
res.send(SR);
});
}
function
Checkout(req, res, next) {
console.log("Checkout
param:"+JSON.stringify(req.params));
var
RoomId=req.params.roomid;
GuestModel.Checkout(Mysql,RoomId,function(Rst){
res.charSet('utf-8');
var
SR={iRst:Rst};
res.send(SR);
});
}
function
CancelBooking(req, res, next) {
console.log("CancelBooking
param:"+JSON.stringify(req.params));
var
RoomId=req.params.roomid;
var
GuestStr=req.params.guestStr;
var
GuestArr=eval_r(GuestStr);
console.log(GuestArr);
GuestModel.CancelBooking(Mysql,RoomId,GuestArr,function(Rst){
res.charSet('utf-8');
var
SR={iRst:Rst};
res.send(SR);
});
}
function
OverStay(req, res, next) {
console.log("OverStay
param:"+JSON.stringify(req.params));
var
RoomId=req.params.roomid;
var
StayTime=req.params.staytime;
GuestModel.OverStay(Mysql,RoomId,StayTime,function(Rst){
res.charSet('utf-8');
var
SR={iRst:Rst};
res.send(SR);
});
}
function
GetRoomBooking(req, res, next)
{
console.log("GetRoomBooking
query:"+JSON.stringify(req.query));
var
RoomId=req.params.roomid;
var
NowTime=req.params.thistime;
GuestModel.GetRoomBooking(Mysql,RoomId,NowTime,function(Data){
res.charSet('utf-8');
res.send(Data);
});
}
function
GetGuestInfoById(req, res, next)
{
console.log("GetGuestInfoById
query:"+JSON.stringify(req.query));
var
UserId=req.params.uid;
GuestModel.GetGuestInfoById(Mysql,UserId,function(Data){
res.charSet('utf-8');
res.send(Data);
});
}
}
exports.GuestControll=GuestControll;
Controller都是中轉控制啦, 業務邏輯都在Module層里,
第一句就告訴我們又要再建一個Module文件夾和Guest.js 的Module.
唯一讓我覺得不夠爽的地方是注冊各個請求的接收參數名, 這個必須由后臺私下和前端事先約定, 在nodejs上無法自描述...
var
JsOper=require("../Tool/JsOper.js");
var
DataTable=require("../Tool/CDataTableName.js");
var
GuestModel={
GetTodayGuests:function(MySql,aUTCDate,aResponse)
{
var QueryCmd=['SELECT
t1.ROOM_ID,t1.GASTNR,t1.GASTNAME,t2.SEX,t2.VIP,t1.ANREISE,t1.ABREISE,t1.CHECK_FLAG
'];
QueryCmd.push(' FROM
',DataTable.TableGuestCheckIn,' t1,
',DataTable.TableGuestInfo,' t2 ' );
QueryCmd.push(' WHERE
t1.GASTNR=t2.NUMMER ?AND ((t1.ANREISE
總結
以上是生活随笔為你收集整理的restify mysql_[菜鸟试水]关于Nodejs搭建后台API服务(Mysql-Restify)[下]的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: C指针原理(43)-helloworld
- 下一篇: C指针原理(44)-汇编基础