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..c10323f --- /dev/null +++ b/src/main/java/com/umc/DongnaeFriend/domain/scrap/service/ScrapService.java @@ -0,0 +1,53 @@ +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(); + + scrapRepository.save(scrap); + } + + // 동네정보 + else if (!(reqScrapDto.getTownInformationId() == null)) { + DongnaeBoard dongnaeBoard = scrapRepository.findByDongnaeBoardId(reqScrapDto.getTownInformationId()); + + // 스크랩 정보 저장 + Scrap scrap = Scrap.builder() + .dongnaeBoard(dongnaeBoard) + .user(user) + .build(); + + scrapRepository.save(scrap); + } + + else { + return "스크랩 실패"; + } + + return "스크랩 성공"; + } +}