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); +// } +// +//} + 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 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()); +// } +// +//} 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); }