diff --git a/src/main/java/com/umc/DongnaeFriend/domain/account/sharing/controller/accountBookSharingController.java b/src/main/java/com/umc/DongnaeFriend/domain/account/sharing/controller/accountBookSharingController.java index bb9ac15..788c35c 100644 --- a/src/main/java/com/umc/DongnaeFriend/domain/account/sharing/controller/accountBookSharingController.java +++ b/src/main/java/com/umc/DongnaeFriend/domain/account/sharing/controller/accountBookSharingController.java @@ -48,11 +48,19 @@ public ResponseEntity searchAll(@RequestParam("keyword") String keyword, @Req * @param RequestDto */ + @PostMapping("") public ResponseEntity createPost(@RequestBody SharingDto.Request req) { accountBookSharingService.createPost(req); return new ResponseEntity<>(HttpStatus.OK); } - - + /* + * [가계부 공유] 게시글 상세조회 + * + * @PathVariable accountBookId + */ + @GetMapping("/{accountBookId}") + public ResponseEntity getPost(@PathVariable("accountBookId") int board_id) { + return ResponseEntity.ok(accountBookSharingService.getBoard(board_id)); + } } diff --git a/src/main/java/com/umc/DongnaeFriend/domain/account/sharing/dto/SharingDto.java b/src/main/java/com/umc/DongnaeFriend/domain/account/sharing/dto/SharingDto.java index e2d0fc2..dcacacd 100644 --- a/src/main/java/com/umc/DongnaeFriend/domain/account/sharing/dto/SharingDto.java +++ b/src/main/java/com/umc/DongnaeFriend/domain/account/sharing/dto/SharingDto.java @@ -1,5 +1,11 @@ package com.umc.DongnaeFriend.domain.account.sharing.dto; +import com.umc.DongnaeFriend.domain.account.sharing.entity.SharingBoard; +import com.umc.DongnaeFriend.domain.dongnae.entity.Dongnae; +import com.umc.DongnaeFriend.domain.dongnae.entity.DongnaeBoard; +import com.umc.DongnaeFriend.domain.type.DongnaeBoardCategory; +import com.umc.DongnaeFriend.domain.type.SharingCategory; +import com.umc.DongnaeFriend.domain.user.entity.User; import lombok.AllArgsConstructor; import lombok.Builder; import lombok.Getter; @@ -21,6 +27,34 @@ public static class Request { private String content; private List images; + + public SharingBoard toEntity(User user) { + return SharingBoard.builder() + .user(user) + .category(SharingCategory.valueOf(category)) + .title(title) + .content(content) + .view(0) + .build(); + } + } + + @Getter + @Builder + @AllArgsConstructor + @NoArgsConstructor + public static class Response{ + String profileImage; + String nickname; + int category; + String title; + String content; + List images; + String createdAt; + boolean isWriter; + boolean likeOrNot; + boolean scrapOrNot; + int view; } diff --git a/src/main/java/com/umc/DongnaeFriend/domain/account/sharing/repository/SharingImgRepository.java b/src/main/java/com/umc/DongnaeFriend/domain/account/sharing/repository/SharingImgRepository.java index 2ce7801..d3f924d 100644 --- a/src/main/java/com/umc/DongnaeFriend/domain/account/sharing/repository/SharingImgRepository.java +++ b/src/main/java/com/umc/DongnaeFriend/domain/account/sharing/repository/SharingImgRepository.java @@ -1,10 +1,12 @@ package com.umc.DongnaeFriend.domain.account.sharing.repository; import com.umc.DongnaeFriend.domain.account.sharing.entity.SharingImg; +import com.umc.DongnaeFriend.domain.dongnae.entity.DongnaeImg; import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.data.jpa.repository.Query; import org.springframework.stereotype.Repository; +import java.util.List; import java.util.Optional; @Repository @@ -12,4 +14,6 @@ public interface SharingImgRepository extends JpaRepository { @Query(value = "SELECT * FROM sharing_img WHERE sharing_board_id = ?1 ORDER BY created_at ASC LIMIT 1", nativeQuery = true) Optional findFirst(long sharing_board_id); + + List findAllBySharingBoard_Id(long id); } diff --git a/src/main/java/com/umc/DongnaeFriend/domain/account/sharing/service/AccountBookSharingService.java b/src/main/java/com/umc/DongnaeFriend/domain/account/sharing/service/AccountBookSharingService.java index 1f1d50b..ba3711d 100644 --- a/src/main/java/com/umc/DongnaeFriend/domain/account/sharing/service/AccountBookSharingService.java +++ b/src/main/java/com/umc/DongnaeFriend/domain/account/sharing/service/AccountBookSharingService.java @@ -10,4 +10,9 @@ public interface AccountBookSharingService { List searchByKeyword(String keyword, int category, Pageable pageable); void createPost(SharingDto.Request req); + + /* + * [가계부 공유] 게시글 상세 조회 + */ + SharingDto.Response getBoard(long board_id); } diff --git a/src/main/java/com/umc/DongnaeFriend/domain/account/sharing/service/AccountBookSharingServiceImpl.java b/src/main/java/com/umc/DongnaeFriend/domain/account/sharing/service/AccountBookSharingServiceImpl.java index fad6545..53c2a69 100644 --- a/src/main/java/com/umc/DongnaeFriend/domain/account/sharing/service/AccountBookSharingServiceImpl.java +++ b/src/main/java/com/umc/DongnaeFriend/domain/account/sharing/service/AccountBookSharingServiceImpl.java @@ -21,6 +21,8 @@ import org.springframework.stereotype.Service; import java.util.List; +import java.util.Objects; +import java.util.Optional; import java.util.stream.Collectors; import static com.umc.DongnaeFriend.global.util.TimeUtil.getTime; @@ -32,7 +34,7 @@ public class AccountBookSharingServiceImpl implements AccountBookSharingService //임시 유저 Dongnae dongnae = Dongnae.builder().id(1L).gu("서울구").dong("서울동").city("서울시").townName("무슨마을").build(); - User user = User.builder().id(1L).age(Age.AGE10).email("email").dongnae(dongnae).gender(Gender.FEMALE).infoCert(YesNo.NO).townCert(YesNo.NO).townCertCnt(10).id(1L).kakaoId(90L).nickname("nickname").refreshToken("refreshToken").build(); + User user = User.builder().profileImage("profileImage").id(1L).age(Age.AGE10).email("email").dongnae(dongnae).gender(Gender.FEMALE).infoCert(YesNo.NO).townCert(YesNo.NO).townCertCnt(10).id(1L).kakaoId(90L).nickname("nickname").refreshToken("refreshToken").build(); @Autowired private SharingBoardRepository sharingBoardRepository; @@ -66,16 +68,49 @@ public List searchByKeyword(String keyword, int categor */ @Override public void createPost(SharingDto.Request req) { - sharingBoardRepository.save(SharingBoard.builder() - .category(SharingCategory.valueOf(req.getCategory())) - .title(req.getTitle()) - .content(req.getContent()) - .build() - ); + sharingBoardRepository.save(req.toEntity(user)); //TODO : Img 파일 업로드 } + /* + * [가계부 공유] 게시글 상세 조회 + */ + @Override + public SharingDto.Response getBoard(long board_id) { + //TODO : User 식별자 필요. + Optional board = sharingBoardRepository.findById(board_id); + if (board.isEmpty()) { + throw new CustomException(ErrorCode.NO_CONTENT_FOUND); + } + + //Get Images + List images = sharingImgRepository.findAllBySharingBoard_Id(board_id); + + //Writer인지 검사 + boolean isWriter = Objects.equals(board.get().getUser().getId(), user.getId()); + + //LikeOrNot 검사 + boolean likeOrNot = !sharingSympathyRepository.findByUser_Id(user.getId()).isEmpty(); + + //TODO: ScrapRepository 필요 + //scrapOrNot 검사 + boolean scrapOrNot = false; + + return SharingDto.Response.builder() + .profileImage(user.getProfileImage()) + .nickname(user.getNickname()) + .category(board.get().getCategory().getValue()) + .title(board.get().getTitle()) + .content(board.get().getContent()) + .images(images.stream().map(SharingImg::getImageUrl).collect(Collectors.toList())) + .createdAt(getTime(board.get().getCreatedAt())) + .isWriter(isWriter) + .likeOrNot(likeOrNot) + .scrapOrNot(scrapOrNot) + .view(board.get().getView()).build(); + } + diff --git a/src/main/java/com/umc/DongnaeFriend/domain/dongnae/dto/DongnaeBoardDto.java b/src/main/java/com/umc/DongnaeFriend/domain/dongnae/dto/DongnaeBoardDto.java index 0dc62ea..f639334 100644 --- a/src/main/java/com/umc/DongnaeFriend/domain/dongnae/dto/DongnaeBoardDto.java +++ b/src/main/java/com/umc/DongnaeFriend/domain/dongnae/dto/DongnaeBoardDto.java @@ -104,7 +104,7 @@ public static class Response { private boolean likeOrNot; - private boolean ScrapOrNot; + private boolean scrapOrNot; private int view; diff --git a/src/main/java/com/umc/DongnaeFriend/domain/dongnae/service/DongnaeBoardServiceImpl.java b/src/main/java/com/umc/DongnaeFriend/domain/dongnae/service/DongnaeBoardServiceImpl.java index e75a0d7..8ae14e8 100644 --- a/src/main/java/com/umc/DongnaeFriend/domain/dongnae/service/DongnaeBoardServiceImpl.java +++ b/src/main/java/com/umc/DongnaeFriend/domain/dongnae/service/DongnaeBoardServiceImpl.java @@ -130,7 +130,6 @@ public void createBoard(DongnaeBoardDto.Request req) { @Transactional(propagation = Propagation.REQUIRED) public DongnaeBoardDto.Response getBoard(long board_id) { //TODO : User 식별자 필요. - int user_id = 1; Optional board = dongnaeBoardRepository.findById(board_id); if (board.isEmpty()) { throw new RuntimeException(); @@ -140,10 +139,10 @@ public DongnaeBoardDto.Response getBoard(long board_id) { List images = dongnaeImgRepository.findAllByDongnaeBoard_Id(board_id); //Writer인지 검사 - boolean isWriter = board.get().getUser().getId() == user_id; + boolean isWriter = Objects.equals(board.get().getUser().getId(), user.getId()); //LikeOrNot 검사 - boolean likeOrNot = !dongnaeSympathyRepository.findByUser_Id(user_id).isEmpty(); + boolean likeOrNot = !dongnaeSympathyRepository.findByUser_Id(user.getId()).isEmpty(); //TODO: ScrapRepository 필요 //scrapOrNot 검사 @@ -163,7 +162,7 @@ public DongnaeBoardDto.Response getBoard(long board_id) { .townCertification(user.getTownCertCnt()) .isWriter(isWriter) .likeOrNot(likeOrNot) - .ScrapOrNot(scrapOrNot) + .scrapOrNot(scrapOrNot) .view(board.get().getView()).build(); }