좋아요 구현
2. 좋아요 API 만들기
likes 폴더 안에 LikesRepository.java 인터페이스 생성
package com.cos.photogramstart.domain.likes;
import org.springframework.data.jpa.repository.JpaRepository;
public interface LikesRepository extends JpaRepository<Likes, Integer> {
}
Java
복사
service 폴더 안에 LikesService.java 생성
package com.cos.photogramstart.service;
import org.springframework.stereotype.Service;
import com.cos.photogramstart.domain.likes.LikesRepository;
import lombok.RequiredArgsConstructor;
@RequiredArgsConstructor
@Service
public class LikesService {
private final LikesRepository likesRepository;
}
Java
복사
좋아요 컨트롤러를 따로 생성하지 않고 ImageApiController에서 추가해서 진행한다
private final LikesService likesService;
@PostMapping("/api/image/{imageId}/likes")
public ResponseEntity<?> likes(@PathVariable int imageId, @AuthenticationPrincipal PrincipalDetails principalDetails){
likesService.좋아요(imageId, principalDetails.getUser().getId());
return null;
}
@DeleteMapping("/api/image/{imageId}/likes")
public ResponseEntity<?> unLikes(@PathVariable int imageId, @AuthenticationPrincipal PrincipalDetails principalDetails){
likesService.좋아요취소(imageId, principalDetails.getUser().getId());
return null;
}
Java
복사
LikesService.java에 좋아요랑 좋아요취소 함수도 추가해주면 컨트롤러단에서 오류가 임시적으로 사라질거다.
public void 좋아요(int imageId, int principalId) {
}
public void 좋아요취소(int imageId, int principalId) {
}
Java
복사
LikesRepository.java
@Modifying
@Query(value = "", nativeQuery = true)
void mLikes(int imageId, int principalId);
@Modifying
@Query(value = "", nativeQuery = true)
void mUnLikes(int imageId, int principalId);
Java
복사
@Modifying
@Query(value = "INSERT INTO likes(imageId , userId) VALUES(:imageId, :principalId)", nativeQuery = true)
void mLikes(int imageId, int principalId);
@Modifying
@Query(value = "DELETE FROM likes WHERE imageId = :imageId AND userId = :principalId", nativeQuery = true)
void mUnLikes(int imageId, int principalId);
Java
복사
LikesService.java
@RequiredArgsConstructor
@Service
public class LikesService {
private final LikesRepository likesRepository;
@Transactional
public void 좋아요(int imageId, int principalId) {
likesRepository.mLikes(imageId, principalId);
}
@Transactional
public void 좋아요취소(int imageId, int principalId) {
likesRepository.mUnLikes(imageId, principalId);
}
}
Java
복사
ImageApiController.java
@PostMapping("/api/image/{imageId}/likes")
public ResponseEntity<?> likes(@PathVariable int imageId, @AuthenticationPrincipal PrincipalDetails principalDetails){
likesService.좋아요(imageId, principalDetails.getUser().getId());
return new ResponseEntity<>(new CMRespDto<>(1, "좋아요 성공", null), HttpStatus.CREATED);
}
@DeleteMapping("/api/image/{imageId}/likes")
public ResponseEntity<?> unLikes(@PathVariable int imageId, @AuthenticationPrincipal PrincipalDetails principalDetails){
likesService.좋아요취소(imageId, principalDetails.getUser().getId());
return new ResponseEntity<>(new CMRespDto<>(1, "좋아요취소 성공", null), HttpStatus.OK);
}
Java
복사
postman으로 테스트를 해본다.
cos로 로그인하고
그리고 이제 좋아요 취소까지 테스트해본다
성공
근데 날짜값이 안들어오는데 이유는 insert에 날짜 부분을 추가안해줘서 그렇다.
LikesRepository.java에 추가
@Modifying
@Query(value = "INSERT INTO likes(imageId , userId, createDate) VALUES(:imageId, :principalId , now())", nativeQuery = true)
void mLikes(int imageId, int principalId);
Java
복사