-
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 branch 'develop' of https://github.com/Codiary-UMC-6th/Backend-โฆ
โฆCodiary into develop
- Loading branch information
Showing
7 changed files
with
188 additions
and
23 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
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
14 changes: 14 additions & 0 deletions
14
src/main/java/com/codiary/backend/domain/post/repository/BookmarkRepository.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,14 @@ | ||
package com.codiary.backend.domain.post.repository; | ||
|
||
import com.codiary.backend.domain.member.entity.Member; | ||
import com.codiary.backend.domain.post.entity.Bookmark; | ||
import com.codiary.backend.domain.post.entity.Post; | ||
import java.util.Optional; | ||
import org.springframework.data.jpa.repository.JpaRepository; | ||
|
||
public interface BookmarkRepository extends JpaRepository<Bookmark, Long> { | ||
|
||
Boolean existsByMemberAndPost(Member member, Post post); | ||
|
||
Optional<Bookmark> findByMemberAndPost(Member member, Post post); | ||
} |
81 changes: 81 additions & 0 deletions
81
src/main/java/com/codiary/backend/domain/post/service/BookmarkService.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,81 @@ | ||
package com.codiary.backend.domain.post.service; | ||
|
||
import com.codiary.backend.domain.member.entity.Member; | ||
import com.codiary.backend.domain.member.repository.MemberRepository; | ||
import com.codiary.backend.domain.post.entity.Bookmark; | ||
import com.codiary.backend.domain.post.entity.Post; | ||
import com.codiary.backend.domain.post.enumerate.PostAccess; | ||
import com.codiary.backend.domain.post.repository.BookmarkRepository; | ||
import com.codiary.backend.domain.post.repository.PostRepository; | ||
import com.codiary.backend.domain.team.entity.Team; | ||
import com.codiary.backend.domain.team.repository.TeamRepository; | ||
import com.codiary.backend.global.apiPayload.code.status.ErrorStatus; | ||
import com.codiary.backend.global.apiPayload.exception.GeneralException; | ||
import com.codiary.backend.global.apiPayload.exception.handler.BookmarkHandler; | ||
import com.codiary.backend.global.apiPayload.exception.handler.MemberHandler; | ||
import com.codiary.backend.global.apiPayload.exception.handler.PostHandler; | ||
import com.codiary.backend.global.apiPayload.exception.handler.TeamHandler; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.stereotype.Service; | ||
import org.springframework.transaction.annotation.Transactional; | ||
|
||
@Service | ||
@Transactional | ||
@RequiredArgsConstructor | ||
public class BookmarkService { | ||
|
||
private final MemberRepository memberRepository; | ||
private final PostRepository postRepository; | ||
private final BookmarkRepository bookmarkRepository; | ||
private final TeamRepository teamRepository; | ||
|
||
public Bookmark bookmarkPost(Long memberId, Long postId) { | ||
// validation: member, post ์ฌ๋ถ | ||
Member member = memberRepository.findById(memberId) | ||
.orElseThrow(() -> new MemberHandler(ErrorStatus.MEMBER_NOT_FOUND)); | ||
Post post = postRepository.findById(postId).orElseThrow(() -> new PostHandler(ErrorStatus.POST_NOT_FOUND)); | ||
|
||
// validation: ์ด๋ฏธ ๋ถ๋งํฌํ ์ํ | ||
if (bookmarkRepository.existsByMemberAndPost(member, post)) { | ||
throw new BookmarkHandler(ErrorStatus.BOOKMARK_ALREADY_EXIST); | ||
} | ||
|
||
// validation: post ๊ถํ ํ์ธ | ||
if (post.getPostAccess().equals(PostAccess.MEMBER) && post.getMember() != member) { | ||
throw new GeneralException(ErrorStatus.BOOKMARK_CREATE_UNAUTHORIZED); | ||
} else if (post.getPostAccess().equals(PostAccess.TEAM)) { | ||
Team teamOfPost = teamRepository.findByIdWithTeamMemberList(post.getTeam().getTeamId()) | ||
.orElseThrow(() -> new TeamHandler(ErrorStatus.TEAM_NOT_FOUND)); | ||
if (!teamRepository.isTeamMember(teamOfPost, member)) { | ||
throw new GeneralException((ErrorStatus.BOOKMARK_CREATE_UNAUTHORIZED)); | ||
} | ||
} | ||
|
||
// business logic | ||
Bookmark bookmark = Bookmark.builder() | ||
.member(member) | ||
.post(post) | ||
.build(); | ||
Bookmark savedBookmark = bookmarkRepository.save(bookmark); | ||
|
||
// response | ||
return savedBookmark; | ||
} | ||
|
||
public String cancelBookmark(Long memberId, Long postId) { | ||
// validation: member, post, bookmark ์ฌ๋ถ | ||
Member member = memberRepository.findById(memberId) | ||
.orElseThrow(() -> new MemberHandler(ErrorStatus.MEMBER_NOT_FOUND)); | ||
Post post = postRepository.findById(postId).orElseThrow(() -> new PostHandler(ErrorStatus.POST_NOT_FOUND)); | ||
Bookmark bookmark = bookmarkRepository.findByMemberAndPost(member, post) | ||
.orElseThrow(() -> new BookmarkHandler(ErrorStatus.BOOKMARK_ALREADY_CANCELED)); | ||
|
||
// business logic | ||
member.getBookmarkList().remove(bookmark); | ||
post.getBookmarkList().remove(bookmark); | ||
bookmarkRepository.deleteById(bookmark.getId()); | ||
|
||
// response | ||
return "๋ถ๋งํฌ๊ฐ ์ทจ์๋์์ต๋๋ค."; | ||
} | ||
} |
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