본문 바로가기
Spring

[스프링] 댓글에서 추천/비추천 중복 제한 기능 구현 방법

by GoodDayDeveloper 2020. 9. 5.
반응형

 

 

오늘은 댓글에서 추천/비추천 중복으로 클릭이 제한되는 기능을 정리해보려 합니다.

이 기능은 방법이 많이 있으나, 저는 Controller에서 주로 다뤘습니다.

 

 

대락적인 로직은

  • 댓글 이력을 관리하는 테이블을 별도로 생성한 다음
  • CNT로 구분하여 0보다 클 경우에는 insert가 되지 못하도록 설정하였습니다.

 

댓글과 추천 기능이 갖춰져 있는 상태에서의 로직이니,

댓글 구현 방법과 추천 구현 방법은 전의 글을 참조하세요~

 

 

댓글 구현 방법 글

chobopark.tistory.com/53

 

추천 구현 방법 글

chobopark.tistory.com/54

 


 

 

구현 화면입니다.

 

 

DATABASE

댓글 이력을 관리하는 테이블을 생성합니다.

 

1
2
3
4
5
6
create table tbl_counsel_reply (
  rp_idx int primary key auto_increment comment '인덱스',
  faqrp_idx int comment '댓글 인덱스',
  me_id varchar(100comment '회원 아이디',
  rp_cnt_sep varchar(10comment '추천 구분'
);
cs

 

 

VO

GETTER / SETTER는 제외

 

1
2
3
4
5
6
public class tbl_reply_historyVO {
 
    private int rp_idx;                            // 인덱스
    private int faqrp_idx;                         // 댓글 인덱스
    private String me_id;                        // 회원 ID
    private String rp_cnt_sep;                    // 추천 구분
cs

 

 

JQEURY

추천과 비추천의 ajax 실패 부분에 alert로 경고창이 뜨도록 설정합니다.

 

1
2
3
.fail(function(e) {  
  alert("한개의 글에 한번만 클릭이 가능합니다.");
}) 
cs

 

 

CONTROLLER

추천과 비추천의 각각의 컨트롤러 안에 댓글 이력 VO를 넣어줍니다.(TBL_REPLY_HISTORYVO)

여기에서 댓글 인덱스와 회원 아이디,그리고 추천 구분을 Y로 고정시킵니다.

여기서 가장 중요한 부분은 IF문을 사용하기 위해서 CheckReply란 메서드를 생성합니다.

boolean pass = checkReply(replyhistory.getMe_id(), myId);

그래서 pass가 참이라면 인덱스와 회원 아이디 추천 구분을 이용하여 이 수가 0이라면 추천이 증가되게 하고, 0보다 크면 return되여 빠져나오게 만들어줍니다.

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
@RequestMapping(value = "/onlinecounsel/csfaq/likeCnt.do", method = RequestMethod.POST)
@ResponseBody
public String likeCnt(
        @RequestBody String filterJSON,
        HttpServletResponse response,
        HttpServletRequest request,
        ModelMap model ) throws Exception {
    
    LoginVO loginVO = loginService.getLoginInfo();
    
    JSONObject resMap = new JSONObject();
    
    
    
    try{
        ObjectMapper mapper = new ObjectMapper();
        
        tbl_counsel_faq_replyVO searchVO = (tbl_counsel_faq_replyVO)mapper.readValue(filterJSON,new TypeReference<tbl_counsel_faq_replyVO>(){ });
 
        
        tbl_reply_historyVO replyhistory = new tbl_reply_historyVO(); 
        
        replyhistory.setFaqrp_idx(searchVO.getFaqrp_idx());
        replyhistory.setMe_id(searchVO.getMe_id());
        replyhistory.setRp_cnt_sep("Y");
        
        String myId = "";
        
        if(loginVO != null) {
            myId = loginVO.getId();
            
        }
        
        boolean pass = checkReply(replyhistory.getMe_id(), myId);
        
        if(pass) {
            int cnt = 0;
            if(myId != null) {
                cnt = onlineCounselService.checkReply(replyhistory.getFaqrp_idx(), replyhistory.getMe_id(), replyhistory.getRp_cnt_sep());
            }
            if(cnt > 0 ) {
                pass = false;
                return null;
            }
        }
        
        onlineCounselService.getReplyHistory(replyhistory);
        
 
        onlineCounselService.updateLike(searchVO.getFaqrp_idx());
        
            
        }catch(Exception e){
        System.out.println(e.toString());
    
    }
    
    response.setContentType("text/html; charset=UTF-8");
    PrintWriter out = response.getWriter();
    out.print(resMap);
    
    return null;
}
            
        
 
@RequestMapping(value = "/onlinecounsel/csfaq/hateCnt.do", method = RequestMethod.POST)
@ResponseBody
public String hateCnt(
        @RequestBody String filterJSON,
        HttpServletResponse response,
        ModelMap model ) throws Exception {
    
    LoginVO loginVO = loginService.getLoginInfo();
    
    JSONObject resMap = new JSONObject();
    
    try{
        ObjectMapper mapper = new ObjectMapper();
        tbl_counsel_faq_replyVO searchVO = (tbl_counsel_faq_replyVO)mapper.readValue(filterJSON,new TypeReference<tbl_counsel_faq_replyVO>(){ });
        
        
        tbl_reply_historyVO replyhistory = new tbl_reply_historyVO();
        
        replyhistory.setFaqrp_idx(searchVO.getFaqrp_idx());
        replyhistory.setMe_id(searchVO.getMe_id());
        replyhistory.setRp_cnt_sep("Y");
        
        String myId = "";
        
        if(loginVO != null) {
            myId = loginVO.getId();
            
        }
        
        boolean pass = checkReply(replyhistory.getMe_id(), myId);
        
        if(pass) {
            int cnt = 0;
            if(myId != null) {
                cnt = onlineCounselService.checkReply(replyhistory.getFaqrp_idx(), replyhistory.getMe_id(), replyhistory.getRp_cnt_sep());
            }
            if(cnt > 0 ) {
                pass = false;
                return null;
            }
        }
        
        onlineCounselService.getReplyHistory(replyhistory);
        
        onlineCounselService.updateHate(searchVO.getFaqrp_idx());
        
        
    }catch(Exception e){
        System.out.println(e.toString());
    
    }
    
    response.setContentType("text/html; charset=UTF-8");
    PrintWriter out = response.getWriter();
    out.print(resMap);
    
    return null;
}
 
 
 
private boolean checkReply(String me_id, String myId) {
    boolean pass = false;
    
    try {
        String replyArr[] = me_id.split(",");
        for(String s : replyArr) {
            if(s.equals(myId)) {
                pass = true;
            }
        }
    }catch(Exception e) {}
    
    return pass;
}
cs

 

 

SERVICE

 

1
int checkReply(int faqrp_idx, String me_id, String rp_cnt_sep);
cs

 

 

SERVICEIMPL

 

1
2
3
4
@Override
public int checkReply(int faqrp_idx, String me_id, String rp_cnt_sep) {
    return dao.checkReply(faqrp_idx, me_id , rp_cnt_sep);
}
cs

 

 

DAO

DAO에서는 값이 3개이기에 Hashmap을 이용하여 cnt를 구합니다.

 

 

1
2
3
4
5
6
7
public int checkReply(int faqrp_idx, String me_id, String rp_cnt_sep) {
    Map<String,Object> map = new HashMap<String, Object>();
    map.put("faqrp_idx", faqrp_idx);
    map.put("me_id", me_id);
    map.put("rp_cnt_sep", rp_cnt_sep);
    return selectOne("checkReply", map);
}
cs

 

 

XML

 

1
2
3
4
5
6
7
8
9
10
11
12
13
<select id="checkReply" parameterType="map" resultType="int">
    select 
        count(*) 
    from 
        tbl_reply_history
    where
        faqrp_idx = #{faqrp_idx}
    and
        me_id = #{me_id}
    and    
        rp_cnt_sep = #{rp_cnt_sep}
        
</select>
cs

 

 

반응형

댓글