Wanna be Brilliant Full-Stack Developer
2/16 SpringBoot 좋아요 API 만들기 본문
LikesRepository도 만들고 LikesService도 만들고 있는데 LikesApi컨트롤러를 만드는것이아니라 ImageApi컨트롤러를 만들것이다 .
그이유가 주소가 @PostMapping("/api/image/{imageId}/likes") 이렇게 나오는데
주소가 시작이 이미지가 된다, 이미지를 좋아하는것이 아니라 어떤 유저를 좋아하는거라면 /api/user/{userId}/likes 만들겠지만 우리는 이미지를 좋아하는것이기떄문에 @PostMapping("/api/image/{imageId}/likes") 이렇게 만들기 위해 이미지API 컨트롤러에서 만든다!
package com.cos.photogramstart.web.api;
import java.util.List;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.web.PageableDefault;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import com.cos.photogramstart.config.auth.PrincipalDetails;
import com.cos.photogramstart.domain.image.Image;
import com.cos.photogramstart.service.ImageService;
import com.cos.photogramstart.service.LikesService;
import com.cos.photogramstart.web.dto.CMRespDto;
import lombok.RequiredArgsConstructor;
@RequiredArgsConstructor
@RestController
public class ImageApiController {
private final ImageService imageService;
private final LikesService likesService;
@GetMapping("/api/image")
public ResponseEntity<?> imageStory(@AuthenticationPrincipal PrincipalDetails principalDetails,
@PageableDefault(size=3) Pageable pageable){
Page<Image> images = imageService.이미지스토리(principalDetails.getUser().getId(), pageable);
return new ResponseEntity<>(new CMRespDto<>(1,"성공",images), HttpStatus.OK);
}
@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;
}
}
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;
public void 좋아요(int imageId, int principalId) {
}
public void 좋아요취소(int imageId, int principalId) {
}
}
데이터베이스 보면 어떤 이미지를 어떤 유저가 좋아한다 데이터가 들어올것이다.
그러면 이것을 먼저 서비스에서 구현해야한다.
package com.cos.photogramstart.service;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.cos.photogramstart.domain.likes.LikesRepository;
import lombok.RequiredArgsConstructor;
@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);
}
}
package com.cos.photogramstart.domain.likes;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
public interface LikesRepository extends JpaRepository<Likes, Integer>{
@Modifying
@Query(value ="INSERT INTO likes(imageId,userId, createDate) VALUES(:imageId, :principalId,now())", nativeQuery = true)
int mLikes(int imageId, int principalId);
@Modifying
@Query(value ="DELETE FROM likes WHERE imageID =:imageId AND userId =:principalId", nativeQuery = true)
int mUnLikes(int imageId, int principalId);
}
package com.cos.photogramstart.web.api;
import java.util.List;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.web.PageableDefault;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import com.cos.photogramstart.config.auth.PrincipalDetails;
import com.cos.photogramstart.domain.image.Image;
import com.cos.photogramstart.service.ImageService;
import com.cos.photogramstart.service.LikesService;
import com.cos.photogramstart.web.dto.CMRespDto;
import lombok.RequiredArgsConstructor;
@RequiredArgsConstructor
@RestController
public class ImageApiController {
private final ImageService imageService;
private final LikesService likesService;
@GetMapping("/api/image")
public ResponseEntity<?> imageStory(@AuthenticationPrincipal PrincipalDetails principalDetails,
@PageableDefault(size=3) Pageable pageable){
Page<Image> images = imageService.이미지스토리(principalDetails.getUser().getId(), pageable);
return new ResponseEntity<>(new CMRespDto<>(1,"성공",images), HttpStatus.OK);
}
@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);
}
}
좋아요 및 좋아요 취소 API 구현 성공!!
'Back-End > Spring Boot' 카테고리의 다른 글
2/16 Spring OOP관점에서 모델링이란??? (0) | 2022.02.17 |
---|---|
2/16 SPRING with JPA 영속성 컨텍스트는 무엇인가? (0) | 2022.02.17 |
2/16 SpringBoot Likes 모델 만들기 (0) | 2022.02.16 |
2/16 Spring Boot 스토리 스크롤 페이징하기! (0) | 2022.02.16 |
2/16 SpringBoot 포토리스트 뷰 렌더링하기 (0) | 2022.02.16 |