From 7ae3108134e0f2866c0feb2c140516588e08c66b Mon Sep 17 00:00:00 2001 From: Suanna01 Date: Sun, 30 Jul 2023 16:00:27 +0900 Subject: [PATCH] =?UTF-8?q?:sparkles:=20Feat=20#56=20:=20=EC=8A=A4?= =?UTF-8?q?=ED=81=AC=EB=9E=A9=20=EA=B8=B0=EB=8A=A5=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../scrap/controller/ScrapController.java | 23 +++++++++ .../domain/scrap/dto/ReqScrapDto.java | 13 +++++ .../scrap/repository/ScrapRepository.java | 18 +++++++ .../domain/scrap/service/ScrapService.java | 49 +++++++++++++++++++ 4 files changed, 103 insertions(+) create mode 100644 src/main/java/com/umc/DongnaeFriend/domain/scrap/controller/ScrapController.java create mode 100644 src/main/java/com/umc/DongnaeFriend/domain/scrap/dto/ReqScrapDto.java create mode 100644 src/main/java/com/umc/DongnaeFriend/domain/scrap/repository/ScrapRepository.java create mode 100644 src/main/java/com/umc/DongnaeFriend/domain/scrap/service/ScrapService.java diff --git a/src/main/java/com/umc/DongnaeFriend/domain/scrap/controller/ScrapController.java b/src/main/java/com/umc/DongnaeFriend/domain/scrap/controller/ScrapController.java new file mode 100644 index 0000000..5f673ff --- /dev/null +++ b/src/main/java/com/umc/DongnaeFriend/domain/scrap/controller/ScrapController.java @@ -0,0 +1,23 @@ +package com.umc.DongnaeFriend.domain.scrap.controller; + +import com.umc.DongnaeFriend.domain.scrap.dto.ReqScrapDto; +import com.umc.DongnaeFriend.domain.scrap.service.ScrapService; +import lombok.RequiredArgsConstructor; +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; + +@RequiredArgsConstructor +@RestController +@RequestMapping("/scrap") +public class ScrapController { + private final ScrapService scrapService; + + // 스크랩 + @PostMapping("") + public String postScrap(@RequestBody ReqScrapDto reqScrapDto) { + scrapService.newScrap(reqScrapDto); + return ""; + } +} diff --git a/src/main/java/com/umc/DongnaeFriend/domain/scrap/dto/ReqScrapDto.java b/src/main/java/com/umc/DongnaeFriend/domain/scrap/dto/ReqScrapDto.java new file mode 100644 index 0000000..2040ab9 --- /dev/null +++ b/src/main/java/com/umc/DongnaeFriend/domain/scrap/dto/ReqScrapDto.java @@ -0,0 +1,13 @@ +package com.umc.DongnaeFriend.domain.scrap.dto; + +import lombok.AllArgsConstructor; +import lombok.Getter; +import lombok.NoArgsConstructor; + +@Getter +@NoArgsConstructor +@AllArgsConstructor +public class ReqScrapDto { + Long townInformationId; + Long accountBookId; +} diff --git a/src/main/java/com/umc/DongnaeFriend/domain/scrap/repository/ScrapRepository.java b/src/main/java/com/umc/DongnaeFriend/domain/scrap/repository/ScrapRepository.java new file mode 100644 index 0000000..f4691d6 --- /dev/null +++ b/src/main/java/com/umc/DongnaeFriend/domain/scrap/repository/ScrapRepository.java @@ -0,0 +1,18 @@ +package com.umc.DongnaeFriend.domain.scrap.repository; + +import com.umc.DongnaeFriend.domain.account.sharing.entity.SharingBoard; +import com.umc.DongnaeFriend.domain.dongnae.entity.DongnaeBoard; +import com.umc.DongnaeFriend.domain.scrap.entity.Scrap; +import com.umc.DongnaeFriend.domain.user.entity.User; +import org.springframework.data.jpa.repository.JpaRepository; +import org.springframework.data.jpa.repository.Query; +import org.springframework.data.repository.query.Param; + +public interface ScrapRepository extends JpaRepository { + @Query("SELECT u FROM User u WHERE u.id = :user_id") + User findByUserId(@Param("user_id") Long user_id); + @Query("SELECT sb FROM SharingBoard sb WHERE sb.id = :sharing_board_id") + SharingBoard findBySharingBoardId(@Param("sharing_board_id") Long sharing_board_id); + @Query("SELECT db FROM DongnaeBoard db WHERE db.id = :dongnae_board_id") + DongnaeBoard findByDongnaeBoardId(@Param("dongnae_board_id") Long dongnae_board_id); +} diff --git a/src/main/java/com/umc/DongnaeFriend/domain/scrap/service/ScrapService.java b/src/main/java/com/umc/DongnaeFriend/domain/scrap/service/ScrapService.java new file mode 100644 index 0000000..32bb0d2 --- /dev/null +++ b/src/main/java/com/umc/DongnaeFriend/domain/scrap/service/ScrapService.java @@ -0,0 +1,49 @@ +package com.umc.DongnaeFriend.domain.scrap.service; + +import com.umc.DongnaeFriend.domain.account.sharing.entity.SharingBoard; +import com.umc.DongnaeFriend.domain.dongnae.entity.DongnaeBoard; +import com.umc.DongnaeFriend.domain.scrap.dto.ReqScrapDto; +import com.umc.DongnaeFriend.domain.scrap.entity.Scrap; +import com.umc.DongnaeFriend.domain.scrap.repository.ScrapRepository; +import com.umc.DongnaeFriend.domain.user.entity.User; +import lombok.RequiredArgsConstructor; +import org.springframework.stereotype.Service; + +@RequiredArgsConstructor +@Service +public class ScrapService { + private final ScrapRepository scrapRepository; + + public String newScrap(ReqScrapDto reqScrapDto) { + // !임시! 유저 가져오기 + User user = scrapRepository.findByUserId(1L); + + // 가계부 공유 + if (!(reqScrapDto.getAccountBookId() == null)) { + SharingBoard sharingBoard = scrapRepository.findBySharingBoardId(reqScrapDto.getAccountBookId()); + + // 스크랩 정보 저장 + Scrap scrap = Scrap.builder() + .sharingBoard(sharingBoard) + .user(user) + .build(); + } + + // 동네정보 + else if (!(reqScrapDto.getTownInformationId() == null)) { + DongnaeBoard dongnaeBoard = scrapRepository.findByDongnaeBoardId(reqScrapDto.getTownInformationId()); + + // 스크랩 정보 저장 + Scrap scrap = Scrap.builder() + .dongnaeBoard(dongnaeBoard) + .user(user) + .build(); + } + + else { + return "스크랩 실패"; + } + + return "스크랩 성공"; + } +}