Skip to content

Commit

Permalink
Merge pull request #61 from DongnaeFriend/feature/#56
Browse files Browse the repository at this point in the history
Feature/#56
  • Loading branch information
Suanna01 authored Jul 31, 2023
2 parents 0fda1d8 + 1619464 commit ed5e0a6
Show file tree
Hide file tree
Showing 4 changed files with 107 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -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 "";
}
}
Original file line number Diff line number Diff line change
@@ -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;
}
Original file line number Diff line number Diff line change
@@ -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<Scrap, Long> {
@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);
}
Original file line number Diff line number Diff line change
@@ -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 "스크랩 성공";
}
}

0 comments on commit ed5e0a6

Please sign in to comment.