-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #48 from IT-Cotato/feat/47-post
[FEATURE] 게시글 스크랩 추가/삭제 & 마이페이지 스크랩 게시글 조회
- Loading branch information
Showing
12 changed files
with
261 additions
and
20 deletions.
There are no files selected for viewing
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
36 changes: 36 additions & 0 deletions
36
src/main/java/com/cotato/kampus/domain/post/application/PostScrapUpdater.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,36 @@ | ||
package com.cotato.kampus.domain.post.application; | ||
|
||
import org.springframework.stereotype.Component; | ||
import org.springframework.transaction.annotation.Transactional; | ||
|
||
import com.cotato.kampus.domain.post.dao.PostScrapRepository; | ||
import com.cotato.kampus.domain.post.domain.PostScrap; | ||
import com.cotato.kampus.global.error.ErrorCode; | ||
import com.cotato.kampus.global.error.exception.AppException; | ||
|
||
import lombok.AccessLevel; | ||
import lombok.RequiredArgsConstructor; | ||
|
||
@Component | ||
@Transactional(readOnly = true) | ||
@RequiredArgsConstructor(access = AccessLevel.PROTECTED) | ||
public class PostScrapUpdater { | ||
|
||
private final PostScrapRepository postScrapRepository; | ||
|
||
public void append(Long postId, Long userId) { | ||
PostScrap postScrap = PostScrap.builder() | ||
.postId(postId) | ||
.userId(userId) | ||
.build(); | ||
|
||
postScrapRepository.save(postScrap); | ||
} | ||
|
||
public void delete(Long postId, Long userId) { | ||
PostScrap postScrap = postScrapRepository.findByUserIdAndPostId(userId, postId) | ||
.orElseThrow(() -> new AppException(ErrorCode.POST_SCRAP_NOT_EXIST)); | ||
|
||
postScrapRepository.delete(postScrap); | ||
} | ||
} |
33 changes: 33 additions & 0 deletions
33
src/main/java/com/cotato/kampus/domain/post/application/PostScrapValidator.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,33 @@ | ||
package com.cotato.kampus.domain.post.application; | ||
|
||
import org.springframework.stereotype.Component; | ||
import org.springframework.transaction.annotation.Transactional; | ||
|
||
import com.cotato.kampus.domain.post.dao.PostScrapRepository; | ||
import com.cotato.kampus.global.error.ErrorCode; | ||
import com.cotato.kampus.global.error.exception.AppException; | ||
|
||
import lombok.AccessLevel; | ||
import lombok.RequiredArgsConstructor; | ||
|
||
@Component | ||
@Transactional(readOnly = true) | ||
@RequiredArgsConstructor(access = AccessLevel.PROTECTED) | ||
public class PostScrapValidator { | ||
|
||
private final PostScrapRepository postScrapRepository; | ||
private final PostAuthorResolver postAuthorResolver; | ||
|
||
public void validatePostScrap(Long postId, Long userId){ | ||
Long authorId = postAuthorResolver.getAuthorId(postId); | ||
|
||
// 본인 게시글 또는 이미 스크랩한 게시글은 예외처리 | ||
if(userId.equals(authorId)){ | ||
throw new AppException(ErrorCode.POST_SCRAP_FORBIDDEN); | ||
} | ||
|
||
if(postScrapRepository.existsByUserIdAndPostId(userId, postId)){ | ||
throw new AppException(ErrorCode.POST_SCRAP_DUPLICATED); | ||
} | ||
} | ||
} |
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
18 changes: 18 additions & 0 deletions
18
src/main/java/com/cotato/kampus/domain/post/dao/PostScrapRepository.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,18 @@ | ||
package com.cotato.kampus.domain.post.dao; | ||
|
||
import java.util.Optional; | ||
|
||
import org.springframework.data.domain.Pageable; | ||
import org.springframework.data.domain.Slice; | ||
import org.springframework.data.jpa.repository.JpaRepository; | ||
|
||
import com.cotato.kampus.domain.post.domain.PostScrap; | ||
|
||
public interface PostScrapRepository extends JpaRepository<PostScrap, Long> { | ||
|
||
boolean existsByUserIdAndPostId(Long userId, Long postId); | ||
|
||
Optional<PostScrap> findByUserIdAndPostId(Long userId, Long postId); | ||
|
||
Slice<PostScrap> findAllByUserId(Long userId, Pageable pageable); | ||
} |
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
38 changes: 38 additions & 0 deletions
38
src/main/java/com/cotato/kampus/domain/post/domain/PostScrap.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,38 @@ | ||
package com.cotato.kampus.domain.post.domain; | ||
|
||
import com.cotato.kampus.domain.common.domain.BaseTimeEntity; | ||
|
||
import jakarta.persistence.Column; | ||
import jakarta.persistence.Entity; | ||
import jakarta.persistence.GeneratedValue; | ||
import jakarta.persistence.GenerationType; | ||
import jakarta.persistence.Id; | ||
import jakarta.persistence.Table; | ||
import lombok.AccessLevel; | ||
import lombok.Builder; | ||
import lombok.Getter; | ||
import lombok.NoArgsConstructor; | ||
|
||
@Entity | ||
@Table(name = "post_scrap") | ||
@Getter | ||
@NoArgsConstructor(access = AccessLevel.PROTECTED) | ||
public class PostScrap extends BaseTimeEntity { | ||
|
||
@Id | ||
@GeneratedValue(strategy = GenerationType.IDENTITY) | ||
@Column(name = "post_scrap_id") | ||
private Long id; | ||
|
||
@Column(name = "user_id", nullable = false) | ||
private Long userId; | ||
|
||
@Column(name = "post_id", nullable = false) | ||
private Long postId; | ||
|
||
@Builder | ||
public PostScrap(Long userId, Long postId) { | ||
this.userId = userId; | ||
this.postId = postId; | ||
} | ||
} |
Oops, something went wrong.