Project/Instagram
[spring] ํด๋ก ์ฝ๋ฉ17 Follow - ํ๋ก์ฐ, ์ธํ๋ก์ฐ
yeahajeong
2020. 4. 20. 20:46
๋ฐ์ํ
Controller
FollowController
//ํ๋ก์ฐ ์์ฒญ
@PostMapping("/follow/{id}")
public String follow(@PathVariable String id, HttpSession session, Model model) throws Exception {
logger.info("/follow/" + id + " : ํ๋ก์ฐ ์์ฒญ ");
Object object = session.getAttribute("login");
UsersVO activeUser = (UsersVO)object;
UsersVO passiveUser = usersService.inquiryOfUserById(id);
FollowVO follow = new FollowVO();
follow.setActiveUser(activeUser.getUserNo());
follow.setPassiveUser(passiveUser.getUserNo());
followService.follow(follow);
return "FollowOK";
}
ํ๋ก์ฐ ์์ฒญ์ ํ๋ก์ฐํ ์ ์ ์ id๋ฅผ ๊ฐ์ง๊ณ ๋์ด๊ฐ๋ค. ๋ก๊ทธ์ธ์ธ์ ์ ๊ฐ์ ธ์์ activeUser์ ๋ด๊ณ ํ๋ก์ฐํ ์ ์ id๋ก ํด๋น ์ ์ ์ ์ ์ฒด์ ๋ณด๋ฅผ ์กฐํํด์ passiveUser์ ๋ด๋๋ค. FollowVO ๊ฐ์ฒด๋ฅผ ๋ง๋ค์ด ๊ฐ์ ์ง์ ํด ์ค๋ค DB์ ์ ์ฅํ๋ค.
//์ธํ๋ก์ฐ ์์ฒญ
@PostMapping("/unfollow/{id}")
public String unfollow(@PathVariable String id, HttpSession session, Model model) throws Exception {
logger.info("/unfollow/" + id + " : ์ธํ๋ก์ฐ ์์ฒญ ");
Object object = session.getAttribute("login");
UsersVO activeUser = (UsersVO)object;
UsersVO passiveUser = usersService.inquiryOfUserById(id);
FollowVO follow = new FollowVO();
follow.setActiveUser(activeUser.getUserNo());
follow.setPassiveUser(passiveUser.getUserNo());
followService.unfollow(follow);
return "UnFollowOK";
}
ํ๋ก์ฐ๊ธฐ๋ฅ๊ณผ ๋๊ฐ์ด ๋์ํ๋ค. DB์ชฝ์ผ๋ก ๋ฐ์ดํฐ๊ฐ ๋ค์ด๊ฐ๋ฉด์ DB์ ์ ์ฅ๋ ํ๋ก์ฐ๊ฐ ์ญ์ ๊ฐ ๋๋ค.
Service
FollowService
@Override
public void follow(FollowVO follow) {
followDAO.follow(follow);
}
@Override
public void unfollow(FollowVO follow) {
followDAO.unfollow(follow);
}
View
personal-list.jsp
$('#follow-btn').on('click', function() {
follow(true);
});
$('#unfollow-btn').on('click', function() {
follow(false);
});
function follow(check) {
if(check) {
$.ajax({
type: "POST",
url: "/myapp/follow/${user.id}",
headers: {
"Content-Type": "application/json",
"X-HTTP-Method-Override": "POST"
},
success: function(result) {
console.log("result : " + result);
if(result === "FollowOK"){
$(".follow").html('<button class="followBtn" id="unfollow-btn">์ธํ๋ก์ฐ</button>');
location.href="/myapp/post/${user.id}";
}
}
});
} else {
$.ajax({
type: "POST",
url: "/myapp/unfollow/${user.id}",
headers: {
"Content-Type": "application/json",
"X-HTTP-Method-Override": "POST"
},
success: function(result) {
console.log("result : " + result);
if(result === "UnFollowOK"){
$(".follow").html('<button class="followBtn" id="follow-btn">ํ๋ก์ฐ</button>');
location.href="/myapp/post/${user.id}";
}
}
});
}
}
๋ฐ์ํ