Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

modify: [공감하기 & 취소 API 수정] #27

Open
wants to merge 1 commit into
base: bori
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions src/main/java/com/sesac/gmd/src/song/SongController.java
Original file line number Diff line number Diff line change
Expand Up @@ -125,17 +125,17 @@ public BaseResponse<List<GetPinsRes>> getPins(@RequestParam int radius, @Request
@ApiImplicitParam(name = "X-ACCESS-TOKEN", required = true, dataType = "string", paramType = "header"),
})
@ResponseBody
@PostMapping("/like/{userIdx}/{pinIdx}")
public BaseResponse<PostLikeRes> likeSong(PostLikeReq postLikeReq, @PathVariable int userIdx, @PathVariable int pinIdx){
@PostMapping("/like")
public BaseResponse<String> likeSong(@RequestBody PostLikeReq postLikeReq){
try{
int userIdxJwt = jwtService.getUserIdx();
//useridx로 접근한 유저가 같은 유저인지 확인하기
if(postLikeReq.getUserIdx() != userIdxJwt) {
return new BaseResponse<>(BaseResponseStatus.INVALID_USER_JWT);
}
//유저 아이디랑 핀 아이디 넘겨주기
PostLikeRes postLikeRes = songService.likeSong(userIdx, pinIdx);
return new BaseResponse<>(postLikeRes);
String result = songService.likeSong(postLikeReq);
return new BaseResponse<>(result);
} catch(BaseException exception){
return new BaseResponse<>((exception.getStatus()));
}
Expand Down
18 changes: 9 additions & 9 deletions src/main/java/com/sesac/gmd/src/song/SongDao.java
Original file line number Diff line number Diff line change
Expand Up @@ -122,30 +122,30 @@ public List<GetPinsRes> getPins(GetPinsReq getPinsReq) {
}

/* 핀 공감 & 공감 취소 API */
public PostLikeRes likeSong(int userIdx, int pinIdx) {
String getLikeQuery = "select * from pin_like_tbl where pinIdx=? and userIdx=?";
List<Map<String, Object>> rows = this.jdbcTemplate.queryForList(getLikeQuery, userIdx, pinIdx);
public String likeSong(PostLikeReq postLikeReq) {
String getLikeQuery = "select * from pin_like_tbl where userIdx=? and pinIdx=?";
List<Map<String, Object>> rows = this.jdbcTemplate.queryForList(getLikeQuery, postLikeReq.getUserIdx(), postLikeReq.getPinIdx());
// 해당 사용자가 좋아요를 눌렀는지 확인.
// member_idx와 product_idx를 뽑아 rows에 저장해서 rows의 길이가 0인 경우, 즉 좋아요 누른 게시글이 없는 경우 like 테이블에 member_idx와 product_idx를 저장하자.
if (rows.size() == 0) {
String createProductQuery = "insert into pin_like_tbl (pinIdx, userIdx) VALUES (?, ?)";
String createProductQuery = "insert into pin_like_tbl (userIdx, pinIdx) VALUES (?, ?)";

Object[] createProductParams = new Object[]{userIdx, pinIdx};
Object[] createProductParams = new Object[]{postLikeReq.getUserIdx(), postLikeReq.getPinIdx()};

this.jdbcTemplate.update(createProductQuery, createProductParams);

String getLastInsertIdxQuery = "select last_insert_id()";
int lastInsertIdx = this.jdbcTemplate.queryForObject(getLastInsertIdxQuery, int.class);

return new PostLikeRes(userIdx, pinIdx);
return "핀에 공감하였습니다.";
} else { // 그럼 그 반대의 경우는 rows의 길이가 0이 아닌, 이미 좋아요를 누른 게시글이다. DELETE 쿼리로 좋아요 취소를 구현한다.
String createProductQuery = "DELETE FROM pin_like_tbl WHERE pinIdx=? and userIdx=?";
String createProductQuery = "DELETE FROM pin_like_tbl WHERE userIdx=? and pinIdx=?";

Object[] createProductParams = new Object[]{userIdx, pinIdx};
Object[] createProductParams = new Object[]{postLikeReq.getUserIdx(), postLikeReq.getPinIdx()};

this.jdbcTemplate.update(createProductQuery, createProductParams);

return new PostLikeRes(userIdx, pinIdx);
return "핀 공감을 취소하였습니다.";

}

Expand Down
10 changes: 7 additions & 3 deletions src/main/java/com/sesac/gmd/src/song/SongService.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
import com.sesac.gmd.src.song.model.PostPinRes;
import com.sesac.gmd.src.song.model.PostCommentReq;
import com.sesac.gmd.src.song.model.PostLikeReq;
import com.sesac.gmd.src.song.model.PostLikeRes;
import com.sesac.gmd.utils.JwtService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
Expand Down Expand Up @@ -44,8 +43,13 @@ public PostPinRes createPin(PostPinReq postPinReq) throws BaseException {
}

/* 핀 공감 & 공감 취소 API */
public PostLikeRes likeSong(int useridx, int pinidx){
return songDao.likeSong(useridx, pinidx);
public String likeSong(PostLikeReq postLikeReq) throws BaseException{
try{
return songDao.likeSong(postLikeReq);
}catch(Exception exception){
throw new BaseException(DATABASE_ERROR);
}

}

/* 댓글 작성 API */
Expand Down
2 changes: 2 additions & 0 deletions src/main/java/com/sesac/gmd/src/song/model/PostLikeReq.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@

import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;

@Getter
@Setter
@AllArgsConstructor
@NoArgsConstructor
public class PostLikeReq {
private int userIdx;
private int pinIdx;
Expand Down
13 changes: 0 additions & 13 deletions src/main/java/com/sesac/gmd/src/song/model/PostLikeRes.java

This file was deleted.