From 04cb6f328ccdc41ef4204efff82780554c8e9567 Mon Sep 17 00:00:00 2001 From: sonsumin Date: Sat, 23 Nov 2024 23:56:55 +0900 Subject: [PATCH 1/4] =?UTF-8?q?[#40]=E2=9C=A8Feat:=20=ED=88=AC=EC=9E=90?= =?UTF-8?q?=EC=9E=90=20=EC=9D=91=EB=8B=B5=20dto=20=EC=9E=91=EC=84=B1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../api/dto/response/InvestmentResDto.java | 30 +++++++++++++++++-- 1 file changed, 28 insertions(+), 2 deletions(-) diff --git a/src/main/java/univ/yesummit/domain/board/api/dto/response/InvestmentResDto.java b/src/main/java/univ/yesummit/domain/board/api/dto/response/InvestmentResDto.java index 8b706ef..c22e0b9 100644 --- a/src/main/java/univ/yesummit/domain/board/api/dto/response/InvestmentResDto.java +++ b/src/main/java/univ/yesummit/domain/board/api/dto/response/InvestmentResDto.java @@ -6,6 +6,32 @@ public record InvestmentResDto( String position, String phoneNumber, String email, - Long boardId + Long boardId, + String boardTitle, + String boardContent, + int investmentCount ) { -} + public static InvestmentResDto + of(Long myMemberId, + String username, + String position, + String phoneNumber, + String email, + Long boardId, + String boardTitle, + String boardContent, + int investmentCount) + + { + return new InvestmentResDto( + myMemberId, + username, + position, + phoneNumber, + email, + boardId, + boardTitle, + boardContent, + investmentCount); + } +} \ No newline at end of file From 3e499f2317ba2b708ed34334c7ece128d61c3f5b Mon Sep 17 00:00:00 2001 From: sonsumin Date: Sat, 23 Nov 2024 23:57:32 +0900 Subject: [PATCH 2/4] =?UTF-8?q?[#40]=E2=9C=A8Feat:=20=ED=88=AC=EC=9E=90?= =?UTF-8?q?=EC=9E=90=20=EA=B2=8C=EC=8B=9C=EA=B8=80=20api=20=EC=9E=91?= =?UTF-8?q?=EC=84=B1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../board/application/InvestmentService.java | 129 +++++++++++++----- 1 file changed, 97 insertions(+), 32 deletions(-) diff --git a/src/main/java/univ/yesummit/domain/board/application/InvestmentService.java b/src/main/java/univ/yesummit/domain/board/application/InvestmentService.java index 719fdc1..19b4803 100644 --- a/src/main/java/univ/yesummit/domain/board/application/InvestmentService.java +++ b/src/main/java/univ/yesummit/domain/board/application/InvestmentService.java @@ -14,6 +14,7 @@ import java.util.List; import java.util.stream.Collectors; + @Service @RequiredArgsConstructor @Transactional(readOnly = true) @@ -23,17 +24,14 @@ public class InvestmentService { private final BoardRepository boardRepository; private final InvestmentRepository investmentRepository; - - // 투자하기 버튼 클릭 @Transactional - public void addInvestment(Long memberId, Board boardId) { + public void addInvestment(Long memberId, Long boardId) { Member member = memberRepository.findById(memberId) .orElseThrow(() -> new IllegalArgumentException("회원이 존재하지 않습니다.")); - - Board board = boardRepository.findById(boardId.getBoardId()) + Board board = boardRepository.findById(boardId) .orElseThrow(() -> new IllegalArgumentException("게시글이 존재하지 않습니다.")); - if (!investmentRepository.existsByBoardAndMember(board, member)) { + if (investmentRepository.existsByBoardAndMember(board, member)) { throw new IllegalStateException("이미 투자 제안 중입니다."); } @@ -44,50 +42,117 @@ public void addInvestment(Long memberId, Board boardId) { .build()); } - // 투자하기 취소 @Transactional - public void cancelInvestment(Long memberId, Board boardId) { - + public void cancelInvestment(Long memberId, Long boardId) { Member member = memberRepository.findById(memberId) .orElseThrow(() -> new IllegalArgumentException("회원이 존재하지 않습니다.")); - - Board board = boardRepository.findById(boardId.getBoardId()) + Board board = boardRepository.findById(boardId) .orElseThrow(() -> new IllegalArgumentException("게시글이 존재하지 않습니다.")); - if (!investmentRepository.existsByBoardAndMember(board, member)) { - throw new IllegalStateException("투자 제안 중이 아닙니다."); - } - Investment investment = investmentRepository.findByBoardAndMember(board, member) - .orElseThrow(() -> new IllegalArgumentException("좋아요 정보가 없습니다.")); + .orElseThrow(() -> new IllegalArgumentException("투자 기록이 존재하지 않습니다.")); board.cancelInvestmentCount(); investmentRepository.delete(investment); } - @Transactional - public List getAllInvestment(Long memberId, Long boardId) { + public List getInvestedBoardsWithDetailsByMember(Long memberId) { Member member = memberRepository.findById(memberId) .orElseThrow(() -> new IllegalArgumentException("회원이 존재하지 않습니다.")); - Board board = boardRepository.findById(boardId) - .orElseThrow(() -> new IllegalArgumentException("게시글이 존재하지 않습니다.")); - - // 해당 게시글에 대한 모든 투자 제안 조회 - List investments = investmentRepository.findByBoard(board); + List investments = investmentRepository.findByMember(member); return investments.stream() .map(investment -> { - Member investor = investment.getMember(); // 투자자 정보 가져오기 - return new InvestmentResDto( - investor.getId(), - investor.getUsername(), - investor.getPosition(), - investor.getPhoneNumber(), - investor.getEmail(), - boardId + Board board = investment.getBoard(); + return InvestmentResDto.of( + member.getId(), + member.getUsername(), + member.getPosition(), + member.getPhoneNumber(), + member.getEmail(), + board.getBoardId(), + board.getTitle(), + board.getContent(), + board.getInvestmentCount() ); }) .collect(Collectors.toList()); } - } +//@Service +//@RequiredArgsConstructor +//@Transactional(readOnly = true) +//public class InvestmentService { +// +// private final MemberRepository memberRepository; +// private final BoardRepository boardRepository; +// private final InvestmentRepository investmentRepository; +// +// +// // 투자하기 버튼 클릭 +// @Transactional +// public void addInvestment(Long memberId, Board boardId) { +// Member member = memberRepository.findById(memberId) +// .orElseThrow(() -> new IllegalArgumentException("회원이 존재하지 않습니다.")); +// +// Board board = boardRepository.findById(boardId.getBoardId()) +// .orElseThrow(() -> new IllegalArgumentException("게시글이 존재하지 않습니다.")); +// +// if (!investmentRepository.existsByBoardAndMember(board, member)) { +// throw new IllegalStateException("이미 투자 제안 중입니다."); +// } +// +// board.updateInvestmentCount(); +// investmentRepository.save(Investment.builder() +// .board(board) +// .member(member) +// .build()); +// } +// +// // 투자하기 취소 +// @Transactional +// public void cancelInvestment(Long memberId, Board boardId) { +// +// Member member = memberRepository.findById(memberId) +// .orElseThrow(() -> new IllegalArgumentException("회원이 존재하지 않습니다.")); +// +// Board board = boardRepository.findById(boardId.getBoardId()) +// .orElseThrow(() -> new IllegalArgumentException("게시글이 존재하지 않습니다.")); +// +// if (!investmentRepository.existsByBoardAndMember(board, member)) { +// throw new IllegalStateException("투자 제안 중이 아닙니다."); +// } +// +// Investment investment = investmentRepository.findByBoardAndMember(board, member) +// .orElseThrow(() -> new IllegalArgumentException("좋아요 정보가 없습니다.")); +// +// board.cancelInvestmentCount(); +// investmentRepository.delete(investment); +// } +// +// @Transactional +// public List getAllInvestment(Long memberId, Long boardId) { +// Member member = memberRepository.findById(memberId) +// .orElseThrow(() -> new IllegalArgumentException("회원이 존재하지 않습니다.")); +// +// Board board = boardRepository.findById(boardId) +// .orElseThrow(() -> new IllegalArgumentException("게시글이 존재하지 않습니다.")); +// +// // 해당 게시글에 대한 모든 투자 제안 조회 +// List investments = investmentRepository.findByBoard(board); +// return investments.stream() +// .map(investment -> { +// Member investor = investment.getMember(); // 투자자 정보 가져오기 +// return new InvestmentResDto( +// investor.getId(), +// investor.getUsername(), +// investor.getPosition(), +// investor.getPhoneNumber(), +// investor.getEmail(), +// boardId +// ); +// }) +// .collect(Collectors.toList()); +// } +// +//} From 00162d37c1569c731b1dfcc88ad60b537e9bc30c Mon Sep 17 00:00:00 2001 From: sonsumin Date: Sat, 23 Nov 2024 23:57:41 +0900 Subject: [PATCH 3/4] =?UTF-8?q?[#40]=E2=9C=A8Feat:=20=ED=88=AC=EC=9E=90?= =?UTF-8?q?=EC=9E=90=20=EA=B5=AC=EB=B6=84=20=EB=A0=88=ED=8F=AC=20=EC=B6=94?= =?UTF-8?q?=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../domain/board/domain/repository/InvestmentRepository.java | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/main/java/univ/yesummit/domain/board/domain/repository/InvestmentRepository.java b/src/main/java/univ/yesummit/domain/board/domain/repository/InvestmentRepository.java index d6f8b33..dce07ff 100644 --- a/src/main/java/univ/yesummit/domain/board/domain/repository/InvestmentRepository.java +++ b/src/main/java/univ/yesummit/domain/board/domain/repository/InvestmentRepository.java @@ -11,6 +11,8 @@ public interface InvestmentRepository extends JpaRepository { boolean existsByBoardAndMember(Board board, Member member); + Optional findByBoardAndMember(Board board, Member member); - List findByBoard(Board board); + + List findByMember(Member member); } From 4f73abef850c5c9de7714096861eab5114cecad6 Mon Sep 17 00:00:00 2001 From: sonsumin Date: Sat, 23 Nov 2024 23:57:56 +0900 Subject: [PATCH 4/4] =?UTF-8?q?[#40]=E2=9C=A8Feat:=20=ED=88=AC=EC=9E=90?= =?UTF-8?q?=EC=9E=90=20=EA=B5=AC=EB=B6=84=20=EC=97=94=EB=93=9C=ED=8F=AC?= =?UTF-8?q?=EC=9D=B8=ED=8A=B8=20=EC=A7=80=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../board/api/InvestmentController.java | 116 +++++++++++------- 1 file changed, 70 insertions(+), 46 deletions(-) diff --git a/src/main/java/univ/yesummit/domain/board/api/InvestmentController.java b/src/main/java/univ/yesummit/domain/board/api/InvestmentController.java index 81e22a4..266e5e6 100644 --- a/src/main/java/univ/yesummit/domain/board/api/InvestmentController.java +++ b/src/main/java/univ/yesummit/domain/board/api/InvestmentController.java @@ -5,6 +5,7 @@ import io.swagger.v3.oas.annotations.media.Schema; import io.swagger.v3.oas.annotations.responses.ApiResponse; import io.swagger.v3.oas.annotations.responses.ApiResponses; +import lombok.RequiredArgsConstructor; import org.springframework.http.MediaType; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.*; @@ -21,62 +22,85 @@ @RestController @RequestMapping("/v1/api/board/investment") +@RequiredArgsConstructor public class InvestmentController { - private final BoardRepository boardRepository; - private final MemberRepository memberRepository; + private final InvestmentService investmentService; - public InvestmentController(BoardRepository boardRepository, MemberRepository memberRepository, InvestmentService investmentService) { - this.boardRepository = boardRepository; - this.memberRepository = memberRepository; - this.investmentService = investmentService; - } - @Operation(summary = "투자 제안하기", description = "투자 제안을 등록합니다.") - @ApiResponses(value = { - @ApiResponse(responseCode = "200", description = "투자 제안하기 성공"), - @ApiResponse(responseCode = "401", description = "인증실패", content = @Content(schema = @Schema(example = "INVALID_HEADER or INVALID_TOKEN"))), - @ApiResponse(responseCode = "404", description = "게시글 또는 회원을 찾을 수 없음", content = @Content(schema = @Schema(example = "BOARD_NOT_FOUND or MEMBER_NOT_FOUND"))), - }) - @PostMapping(value = "/invest", produces = MediaType.APPLICATION_JSON_UTF8_VALUE) - public ResponseEntity addBoardLike(@User LoginUser loginUser, @RequestParam Long boardId) { - Member member = memberRepository.findById(loginUser.getMemberId()) - .orElseThrow(() -> new IllegalArgumentException("사용자를 찾을 수 없습니다.")); - Board board = boardRepository.findById(boardId) - .orElseThrow(() -> new IllegalArgumentException("게시글을 찾을 수 없습니다.")); + @Operation(summary = "투자 제안하기", description = "특정 게시글에 투자 제안을 등록합니다.") + @PostMapping("/{boardId}") + public ResponseEntity addInvestment( + @User LoginUser loginUser, + @PathVariable Long boardId) { - investmentService.addInvestment(loginUser.getMemberId(), board); - return ResponseEntity.ok("투자 제안하기"); + investmentService.addInvestment(loginUser.getMemberId(), boardId); + return ResponseEntity.ok("investment ok"); } - @Operation(summary = "투자 제안하기 취소", description = "투자 제안을 취소합니다.") - @ApiResponses(value = { - @ApiResponse(responseCode = "200", description = "투자 제안하기 취소 성공"), - @ApiResponse(responseCode = "401", description = "인증실패", content = @Content(schema = @Schema(example = "INVALID_HEADER or INVALID_TOKEN"))), - @ApiResponse(responseCode = "404", description = "게시글 또는 회원을 찾을 수 없음", content = @Content(schema = @Schema(example = "BOARD_NOT_FOUND or MEMBER_NOT_FOUND"))), - }) - @PostMapping(value = "/cancel", produces = MediaType.APPLICATION_JSON_UTF8_VALUE) - public ResponseEntity cancelBoardLike(@User LoginUser loginUser, @RequestParam Long boardId) { - Member member = memberRepository.findById(loginUser.getMemberId()) - .orElseThrow(() -> new IllegalArgumentException("사용자를 찾을 수 없습니다.")); - Board board = boardRepository.findById(boardId) - .orElseThrow(() -> new IllegalArgumentException("게시글을 찾을 수 없습니다.")); + @Operation(summary = "투자 제안 취소", description = "특정 게시글의 투자 제안을 취소합니다.") + @DeleteMapping("/{boardId}") + public ResponseEntity cancelInvestment( + @User LoginUser loginUser, + @PathVariable Long boardId) { - investmentService.cancelInvestment(loginUser.getMemberId(), board); - return ResponseEntity.ok("투자 제안하기 취소"); + investmentService.cancelInvestment(loginUser.getMemberId(), boardId); + return ResponseEntity.ok("cancel investment ok"); } - @Operation(summary = "내가 받은 투자 제안 조회", description = "내가 받은 투자 제안을 조회합니다.") - @ApiResponses(value = { - @ApiResponse(responseCode = "200", description = "내가 받은 투자 제안 조회 성공"), - @ApiResponse(responseCode = "401", description = "인증실패", content = @Content(schema = @Schema(example = "INVALID_HEADER or INVALID_TOKEN"))), - @ApiResponse(responseCode = "404", description = "게시글 또는 회원을 찾을 수 없음", content = @Content(schema = @Schema(example = "BOARD_NOT_FOUND or MEMBER_NOT_FOUND"))), - }) - @GetMapping("/get-investment") - public ResponseEntity> getAllInvestment(@User LoginUser loginUser, @RequestParam Long boardId) { - List investments = investmentService.getAllInvestment(loginUser.getMemberId(), boardId); + @Operation(summary = "내가 투자한 게시글 조회", description = "특정 사용자가 투자한 게시글 리스트를 조회합니다.") + @GetMapping("/my-investments") + public ResponseEntity> getMyInvestments(@User LoginUser loginUser) { + List investments = investmentService.getInvestedBoardsWithDetailsByMember(loginUser.getMemberId()); return ResponseEntity.ok(investments); } - - } +// +// @Operation(summary = "투자 제안하기", description = "투자 제안을 등록합니다.") +// @ApiResponses(value = { +// @ApiResponse(responseCode = "200", description = "투자 제안하기 성공"), +// @ApiResponse(responseCode = "401", description = "인증실패", content = @Content(schema = @Schema(example = "INVALID_HEADER or INVALID_TOKEN"))), +// @ApiResponse(responseCode = "404", description = "게시글 또는 회원을 찾을 수 없음", content = @Content(schema = @Schema(example = "BOARD_NOT_FOUND or MEMBER_NOT_FOUND"))), +// }) +// @PostMapping(value = "/invest", produces = MediaType.APPLICATION_JSON_UTF8_VALUE) +// public ResponseEntity addBoardLike(@User LoginUser loginUser, @RequestParam Long boardId) { +// Member member = memberRepository.findById(loginUser.getMemberId()) +// .orElseThrow(() -> new IllegalArgumentException("사용자를 찾을 수 없습니다.")); +// Board board = boardRepository.findById(boardId) +// .orElseThrow(() -> new IllegalArgumentException("게시글을 찾을 수 없습니다.")); +// +// investmentService.addInvestment(loginUser.getMemberId(), board); +// return ResponseEntity.ok("투자 제안하기"); +// } +// +// @Operation(summary = "투자 제안하기 취소", description = "투자 제안을 취소합니다.") +// @ApiResponses(value = { +// @ApiResponse(responseCode = "200", description = "투자 제안하기 취소 성공"), +// @ApiResponse(responseCode = "401", description = "인증실패", content = @Content(schema = @Schema(example = "INVALID_HEADER or INVALID_TOKEN"))), +// @ApiResponse(responseCode = "404", description = "게시글 또는 회원을 찾을 수 없음", content = @Content(schema = @Schema(example = "BOARD_NOT_FOUND or MEMBER_NOT_FOUND"))), +// }) +// @PostMapping(value = "/cancel", produces = MediaType.APPLICATION_JSON_UTF8_VALUE) +// public ResponseEntity cancelBoardLike(@User LoginUser loginUser, @RequestParam Long boardId) { +// Member member = memberRepository.findById(loginUser.getMemberId()) +// .orElseThrow(() -> new IllegalArgumentException("사용자를 찾을 수 없습니다.")); +// Board board = boardRepository.findById(boardId) +// .orElseThrow(() -> new IllegalArgumentException("게시글을 찾을 수 없습니다.")); +// +// investmentService.cancelInvestment(loginUser.getMemberId(), board); +// return ResponseEntity.ok("투자 제안하기 취소"); +// } +// +// @Operation(summary = "내가 받은 투자 제안 조회", description = "내가 받은 투자 제안을 조회합니다.") +// @ApiResponses(value = { +// @ApiResponse(responseCode = "200", description = "내가 받은 투자 제안 조회 성공"), +// @ApiResponse(responseCode = "401", description = "인증실패", content = @Content(schema = @Schema(example = "INVALID_HEADER or INVALID_TOKEN"))), +// @ApiResponse(responseCode = "404", description = "게시글 또는 회원을 찾을 수 없음", content = @Content(schema = @Schema(example = "BOARD_NOT_FOUND or MEMBER_NOT_FOUND"))), +// }) +// @GetMapping("/get-investment") +// public ResponseEntity> getAllInvestment(@User LoginUser loginUser, @RequestParam Long boardId) { +// List investments = investmentService.getAllInvestment(loginUser.getMemberId(), boardId); +// return ResponseEntity.ok(investments); +// } +// +//} +