SPRING/chapter04_MVC
reply.js
GAWON
2023. 7. 18. 09:56
console.log("Reply Module...");
//리턴 데이터에 대한 시간 처리 함수(24시간 이내 작성 댓글은 시간으로 표기/24시간이 지난 댓글은 날짜만 표기)
function displayTime(timeValue){
var today = new Date();
var gap = today.getTime() - timeValue;
var dateObj = new Date(timeValue);
var str = "";
if(gap < (1000 * 60 * 60 * 24)){
var hh = dateObj.getHours();
var mi = dateObj.getMinutes();
var ss = dateObj.getSeconds();
return [(hh>9 ? '' : '0') + hh, ':', (mi > 9 ? '' : '0') + mi, ':', (ss > 9 ? '' : '0') + ss].join('');
}else{
var yy = dateObj.getFullYear();
var mm = dateObj.getMonth() + 1; // getMonth() is zero-based
var dd = dateObj.getDate();
return [yy, '/', (mm > 9 ? '' : '0')+mm, '/', (dd > 9 ? '' : '0') + dd].join('');
}
}
var replyService = (function(){
function add(reply, callback, error){
console.log('add reply...');
//reply : {reply : "JS TEST", replyer:"tester", bno:bnoValue}
$.ajax({
type : 'post',
url : '/replies/new',
data : JSON.stringify(reply),
contentType : 'application/json; chatset=utf-8',//전달하는 데이터 타입
success : function(result, status, xhr){
if(callback){
callback(result);
}
},
error : function(xhr, status, er){
if(error){
error(er);
}
}
});
}
function getList(param, callback, error){
console.log('get List...');
// param : {bno : bnoValue, page:1},
var bno = param.bno;
var page = param.page || 1; // param.page || 1;(param.page가 false일때 1로 대입함 js에서만)
$.ajax({
type : 'get',
url : '/replies/pages/' + bno + '/' + page + '.json',
success : function(result, status, xhr){
if(callback){
callback(result);
}
},
error : function(xhr, status, er){
if(error){
error(er);
}
}
});
}
function get(rno, callback, error){
console.log('get...');
$.ajax({
type : 'get',
url : '/replies/' + rno + '.json',
success : function(result, status, xhr){
if(callback){
callback(result);
}
},
error : function(xhr, status, er){ //요청 , 상태(성공,실패여부) ,에러
if(error){
error(er);
}
}
});
}
function remove(rno, callback, error){
console.log('remove...');
$.ajax({
type : 'delete',
url : '/replies/' + rno,
success : function(result, status, xhr){
if(callback){
callback(result);
}
},
error : function(xhr, status, er){
if(error){
error(er);
}
}
});
}
function update(reply, callback, error){
console.log('update...');
var rno = reply.rno; //reply에서 rno값을 설정해주면 데이터에서 reply를 쓸 수 있다
$.ajax({
type : 'put', //request에서 method put,patch로 받는다고 했으니까 하나 설정해줘야 함
url : '/replies/' + rno,
data : JSON.stringify(reply),
contentType : 'application/json; chatset=utf-8',
success : function(result, status, xhr){
if(callback){
callback(result);
}
},
error : function(xhr, status, er){
if(error){
error(er);
}
}
});
}
return {
add : add,
getList : getList,
get : get,
remove : remove,
update : update
}
})();