-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
* add: 좋아요 도메인 - likes 테이블 생성 - userId와 postId FK * add: 좋아요 생성 및 취소 기능 * add: 게시물 좋아요 개수 증가/감소 기능 - JPQL UPDATE 사용을 위해 @Modifying * add: Like 관련 에러 메시지 및 테스트용 entity 생성 * add: 좋아요 생성 및 취소 API, LikeRequestDto * add: testUtils 수정으로 인한 Post 데이터 값 변경 * add: 좋아요 리포지토리와 서비스 테스트 - repository : 추가한 findByUserAndPost 메서드만 진행 - service : BDDMockito 테스트 * add: 좋아요 컨트롤러 테스트 및 Rest docs 생성 * add: build를 위한 test Lombok 제거 * add: 좋아요 생성 API 수정 및 LikeDto userId 변경 - 컨트롤러 API에 postId 추가 - @AuthenticationPrincipal 처리
- Loading branch information
1 parent
9071e53
commit 08dfa72
Showing
20 changed files
with
650 additions
and
58 deletions.
There are no files selected for viewing
47 changes: 47 additions & 0 deletions
47
src/main/java/com/prgrms/prolog/domain/like/api/LikeController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package com.prgrms.prolog.domain.like.api; | ||
|
||
import java.net.URI; | ||
|
||
import javax.validation.Valid; | ||
|
||
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.PathVariable; | ||
import org.springframework.web.bind.annotation.PostMapping; | ||
import org.springframework.web.bind.annotation.RequestBody; | ||
import org.springframework.web.bind.annotation.RequestMapping; | ||
import org.springframework.web.bind.annotation.RestController; | ||
import org.springframework.web.util.UriComponentsBuilder; | ||
|
||
import com.prgrms.prolog.domain.like.dto.LikeDto; | ||
import com.prgrms.prolog.domain.like.dto.LikeDto.likeRequest; | ||
import com.prgrms.prolog.domain.like.service.LikeServiceImpl; | ||
import com.prgrms.prolog.global.jwt.JwtAuthentication; | ||
|
||
import lombok.RequiredArgsConstructor; | ||
|
||
@RestController | ||
@RequiredArgsConstructor | ||
@RequestMapping("/api/v1/like") | ||
public class LikeController { | ||
|
||
private final LikeServiceImpl likeService; | ||
|
||
@PostMapping(value = "/{postId}") | ||
public ResponseEntity<Long> insert( | ||
@PathVariable Long postId, | ||
@AuthenticationPrincipal JwtAuthentication user | ||
) { | ||
LikeDto.likeRequest request = new likeRequest(user.id(), postId); | ||
Long likeId = likeService.save(request); | ||
URI location = UriComponentsBuilder.fromUriString("/api/v1/like/" + likeId).build().toUri(); | ||
return ResponseEntity.created(location).build(); | ||
} | ||
|
||
@DeleteMapping | ||
public ResponseEntity<Void> delete(@RequestBody @Valid likeRequest likeRequest) { | ||
likeService.cancel(likeRequest); | ||
return ResponseEntity.noContent().build(); | ||
} | ||
} |
10 changes: 10 additions & 0 deletions
10
src/main/java/com/prgrms/prolog/domain/like/dto/LikeDto.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package com.prgrms.prolog.domain.like.dto; | ||
|
||
import javax.validation.constraints.NotNull; | ||
|
||
public class LikeDto { | ||
|
||
public record likeRequest(@NotNull Long userId, | ||
@NotNull Long postId) { | ||
} | ||
} |
44 changes: 44 additions & 0 deletions
44
src/main/java/com/prgrms/prolog/domain/like/model/Like.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
package com.prgrms.prolog.domain.like.model; | ||
|
||
import static javax.persistence.FetchType.*; | ||
import static javax.persistence.GenerationType.*; | ||
import static lombok.AccessLevel.*; | ||
|
||
import javax.persistence.Entity; | ||
import javax.persistence.GeneratedValue; | ||
import javax.persistence.Id; | ||
import javax.persistence.JoinColumn; | ||
import javax.persistence.ManyToOne; | ||
import javax.persistence.Table; | ||
|
||
import com.prgrms.prolog.domain.post.model.Post; | ||
import com.prgrms.prolog.domain.user.model.User; | ||
|
||
import lombok.Builder; | ||
import lombok.Getter; | ||
import lombok.NoArgsConstructor; | ||
|
||
@Table(name = "likes") | ||
@Entity | ||
@Getter | ||
@NoArgsConstructor(access = PROTECTED) | ||
public class Like { | ||
|
||
@Id | ||
@GeneratedValue(strategy = IDENTITY) | ||
private Long id; | ||
|
||
@ManyToOne(fetch = LAZY) | ||
@JoinColumn(name = "user_id") | ||
private User user; | ||
|
||
@ManyToOne(fetch = LAZY) | ||
@JoinColumn(name = "post_id") | ||
private Post post; | ||
|
||
@Builder | ||
public Like(User user, Post post) { | ||
this.user = user; | ||
this.post = post; | ||
} | ||
} |
15 changes: 15 additions & 0 deletions
15
src/main/java/com/prgrms/prolog/domain/like/repository/LikeRepository.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package com.prgrms.prolog.domain.like.repository; | ||
|
||
import java.util.Optional; | ||
|
||
import org.springframework.data.jpa.repository.JpaRepository; | ||
import org.springframework.stereotype.Repository; | ||
|
||
import com.prgrms.prolog.domain.like.model.Like; | ||
import com.prgrms.prolog.domain.post.model.Post; | ||
import com.prgrms.prolog.domain.user.model.User; | ||
|
||
@Repository | ||
public interface LikeRepository extends JpaRepository<Like, Long> { | ||
Optional<Like> findByUserAndPost(User user, Post post); | ||
} |
9 changes: 9 additions & 0 deletions
9
src/main/java/com/prgrms/prolog/domain/like/service/LikeService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package com.prgrms.prolog.domain.like.service; | ||
|
||
import com.prgrms.prolog.domain.like.dto.LikeDto; | ||
|
||
public interface LikeService { | ||
Long save(LikeDto.likeRequest likeRequest); | ||
|
||
void cancel(LikeDto.likeRequest likeRequest); | ||
} |
73 changes: 73 additions & 0 deletions
73
src/main/java/com/prgrms/prolog/domain/like/service/LikeServiceImpl.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
package com.prgrms.prolog.domain.like.service; | ||
|
||
import javax.persistence.EntityNotFoundException; | ||
|
||
import org.springframework.stereotype.Service; | ||
import org.springframework.transaction.annotation.Transactional; | ||
|
||
import com.prgrms.prolog.domain.like.dto.LikeDto.likeRequest; | ||
import com.prgrms.prolog.domain.like.model.Like; | ||
import com.prgrms.prolog.domain.like.repository.LikeRepository; | ||
import com.prgrms.prolog.domain.post.model.Post; | ||
import com.prgrms.prolog.domain.post.repository.PostRepository; | ||
import com.prgrms.prolog.domain.user.model.User; | ||
import com.prgrms.prolog.domain.user.repository.UserRepository; | ||
|
||
import lombok.RequiredArgsConstructor; | ||
|
||
@RequiredArgsConstructor | ||
@Transactional | ||
@Service | ||
public class LikeServiceImpl implements LikeService { | ||
|
||
private final LikeRepository likeRepository; | ||
private final UserRepository userRepository; | ||
private final PostRepository postRepository; | ||
|
||
@Override | ||
public Long save(likeRequest likeRequest) { | ||
|
||
User user = getFindUserBy(likeRequest.userId()); | ||
Post post = getFindPostBy(likeRequest.postId()); | ||
|
||
//TODO 이미 좋아요 되어있으면 에러 반환 -> 409 Conflict 오류로 변환 | ||
if (likeRepository.findByUserAndPost(user, post).isPresent()) { | ||
throw new EntityNotFoundException("exception.like.alreadyExist"); | ||
} | ||
|
||
Like like = likeRepository.save(saveLike(user, post)); | ||
|
||
postRepository.addLikeCount(post.getId()); | ||
return like.getId(); | ||
} | ||
|
||
@Override | ||
public void cancel(likeRequest likeRequest) { | ||
|
||
User user = getFindUserBy(likeRequest.userId()); | ||
Post post = getFindPostBy(likeRequest.postId()); | ||
|
||
Like like = likeRepository.findByUserAndPost(user, post) | ||
.orElseThrow(() -> new EntityNotFoundException("exception.like.notExist")); | ||
|
||
likeRepository.delete(like); | ||
postRepository.subLikeCount(post.getId()); | ||
} | ||
|
||
private Like saveLike(User user, Post post) { | ||
return Like.builder() | ||
.user(user) | ||
.post(post) | ||
.build(); | ||
} | ||
|
||
private User getFindUserBy(Long userId) { | ||
return userRepository.findById(userId) | ||
.orElseThrow(() -> new IllegalArgumentException("exception.user.notExists")); | ||
} | ||
|
||
private Post getFindPostBy(Long postId) { | ||
return postRepository.findById(postId) | ||
.orElseThrow(() -> new IllegalArgumentException("exception.post.notExists")); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
DROP TABLE IF EXISTS likes; | ||
|
||
CREATE TABLE likes | ||
( | ||
id bigint NOT NULL PRIMARY KEY AUTO_INCREMENT, | ||
user_id bigint NOT NULL, | ||
post_id bigint NOT NULL, | ||
FOREIGN KEY fk_likes_user_id (user_id) REFERENCES users (id), | ||
FOREIGN KEY fk_likes_post_id (post_id) REFERENCES post (id) | ||
); | ||
|
||
ALTER TABLE post ADD like_count INT DEFAULT 0; |
Oops, something went wrong.