-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
152 additions
and
2 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
64 changes: 64 additions & 0 deletions
64
src/main/java/cotato/growingpain/post/controller/PostSaveController.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,64 @@ | ||
package cotato.growingpain.post.controller; | ||
|
||
import cotato.growingpain.common.Response; | ||
import cotato.growingpain.post.domain.entity.Post; | ||
import cotato.growingpain.post.service.PostSaveService; | ||
import io.swagger.v3.oas.annotations.Operation; | ||
import io.swagger.v3.oas.annotations.media.Content; | ||
import io.swagger.v3.oas.annotations.media.Schema; | ||
import io.swagger.v3.oas.annotations.responses.ApiResponse; | ||
import io.swagger.v3.oas.annotations.tags.Tag; | ||
import java.util.List; | ||
import lombok.RequiredArgsConstructor; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.http.HttpStatus; | ||
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.RequestMapping; | ||
import org.springframework.web.bind.annotation.ResponseStatus; | ||
import org.springframework.web.bind.annotation.RestController; | ||
|
||
@Tag(name = "게시글 저장", description = "게시글 저장 관련된 api") | ||
@RestController | ||
@RequiredArgsConstructor | ||
@RequestMapping("/api/post/saves") | ||
@Slf4j | ||
public class PostSaveController { | ||
|
||
private final PostSaveService postSaveService; | ||
|
||
@Operation(summary = "게시글 저장", description = "게시글을 저장하기 위한 메소드") | ||
@ApiResponse(content = @Content(schema = @Schema(implementation = Response.class))) | ||
@PostMapping("/{post-id}") | ||
@ResponseStatus(HttpStatus.OK) | ||
public Response<?> savePost(@PathVariable("post-id") Long postId, | ||
@AuthenticationPrincipal Long memberId) { | ||
log.info("게시글 {} 저장한 memberId: {}", postId, memberId); | ||
postSaveService.savePost(postId, memberId); | ||
return Response.createSuccessWithNoData("게시글 저장 완료"); | ||
} | ||
|
||
@Operation(summary = "게시글 저장 취소", description = "게시글 저장을 취소하기 위한 메소드") | ||
@ApiResponse(content = @Content(schema = @Schema(implementation = Response.class))) | ||
@DeleteMapping("/{post-id}") | ||
@ResponseStatus(HttpStatus.OK) | ||
public Response<?> deleteSavePost(@PathVariable("post-id") Long postId, | ||
@AuthenticationPrincipal Long memberId) { | ||
log.info("게시글 {} 저장 취소한 memberId: {}", postId, memberId); | ||
postSaveService.deleteSavePost(postId, memberId); | ||
return Response.createSuccessWithNoData("게시글 저장 취소 완료"); | ||
} | ||
|
||
@Operation(summary = "저장한 게시글 목록 조회", description = "사용자가 저장한 게시글의 목록을 조회하기 위한 메소드") | ||
@ApiResponse(content = @Content(schema = @Schema(implementation = Response.class))) | ||
@GetMapping("/list/{member-id}") | ||
@ResponseStatus(HttpStatus.OK) | ||
public Response<List<Post>> getSavedPosts(@AuthenticationPrincipal Long memberId) { | ||
log.info("사용자가 저장한 게시글 목록 요청: memberId {}", memberId); | ||
List<Post> savedPosts = postSaveService.getSavedPosts(memberId); | ||
return Response.createSuccess("저장한 게시글 목록 조회 완료", savedPosts); | ||
} | ||
} |
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/cotato/growingpain/post/repository/PostSaveRepository.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 cotato.growingpain.post.repository; | ||
|
||
import cotato.growingpain.post.domain.entity.PostSave; | ||
import java.util.List; | ||
import org.springframework.data.jpa.repository.JpaRepository; | ||
|
||
public interface PostSaveRepository extends JpaRepository<PostSave, Long> { | ||
|
||
boolean existsById(Long postSaveId); | ||
boolean existsByMemberIdAndPostId(Long memberId, Long postId); | ||
void deleteByMemberIdAndPostId(Long memberId, Long postId); | ||
|
||
List<PostSave> findByMemberId(Long memberId); | ||
} |
60 changes: 60 additions & 0 deletions
60
src/main/java/cotato/growingpain/post/service/PostSaveService.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,60 @@ | ||
package cotato.growingpain.post.service; | ||
|
||
import cotato.growingpain.common.exception.AppException; | ||
import cotato.growingpain.common.exception.ErrorCode; | ||
import cotato.growingpain.member.domain.entity.Member; | ||
import cotato.growingpain.member.repository.MemberRepository; | ||
import cotato.growingpain.post.domain.entity.Post; | ||
import cotato.growingpain.post.domain.entity.PostSave; | ||
import cotato.growingpain.post.repository.PostRepository; | ||
import cotato.growingpain.post.repository.PostSaveRepository; | ||
import java.util.List; | ||
import java.util.stream.Collectors; | ||
import lombok.RequiredArgsConstructor; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.stereotype.Service; | ||
import org.springframework.transaction.annotation.Transactional; | ||
|
||
@Slf4j | ||
@Service | ||
@RequiredArgsConstructor | ||
public class PostSaveService { | ||
|
||
private final MemberRepository memberRepository; | ||
private final PostRepository postRepository; | ||
private final PostSaveRepository postSaveRepository; | ||
|
||
@Transactional | ||
public void savePost(Long postId, Long memberId) { | ||
Member member = memberRepository.findById(memberId) | ||
.orElseThrow(() -> new AppException(ErrorCode.MEMBER_NOT_FOUND)); | ||
Post post = postRepository.findById(postId) | ||
.orElseThrow(() -> new AppException(ErrorCode.POST_NOT_FOUND)); | ||
|
||
if (postSaveRepository.existsByMemberIdAndPostId(memberId, postId)) { | ||
throw new AppException(ErrorCode.ALREADY_SAVED); | ||
} | ||
|
||
PostSave postSave = PostSave.createPostSave(member, post); | ||
postSaveRepository.save(postSave); | ||
} | ||
|
||
@Transactional | ||
public void deleteSavePost(Long postId, Long memberId) { | ||
|
||
if (!postSaveRepository.existsByMemberIdAndPostId(postId, memberId)) { | ||
throw new AppException(ErrorCode.POST_NOT_FOUND); | ||
} | ||
|
||
postSaveRepository.deleteByMemberIdAndPostId(memberId, postId); | ||
} | ||
|
||
@Transactional | ||
public List<Post> getSavedPosts(Long memberId) { | ||
List<PostSave> postSaves = postSaveRepository.findByMemberId(memberId); | ||
|
||
return postSaves.stream() | ||
.map(PostSave::getPost) | ||
.collect(Collectors.toList()); | ||
} | ||
} |