Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

투자자가 투자한 게시글 구분 #45

Merged
merged 4 commits into from
Nov 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
116 changes: 70 additions & 46 deletions src/main/java/univ/yesummit/domain/board/api/InvestmentController.java
Original file line number Diff line number Diff line change
Expand Up @@ -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.*;
Expand All @@ -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<String> 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<String> 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<String> 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<String> 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<List<InvestmentResDto>> getAllInvestment(@User LoginUser loginUser, @RequestParam Long boardId) {
List<InvestmentResDto> investments = investmentService.getAllInvestment(loginUser.getMemberId(), boardId);
@Operation(summary = "내가 투자한 게시글 조회", description = "특정 사용자가 투자한 게시글 리스트를 조회합니다.")
@GetMapping("/my-investments")
public ResponseEntity<List<InvestmentResDto>> getMyInvestments(@User LoginUser loginUser) {
List<InvestmentResDto> 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<String> 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<String> 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<List<InvestmentResDto>> getAllInvestment(@User LoginUser loginUser, @RequestParam Long boardId) {
// List<InvestmentResDto> investments = investmentService.getAllInvestment(loginUser.getMemberId(), boardId);
// return ResponseEntity.ok(investments);
// }
//
//}

Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import java.util.List;
import java.util.stream.Collectors;


@Service
@RequiredArgsConstructor
@Transactional(readOnly = true)
Expand All @@ -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("이미 투자 제안 중입니다.");
}

Expand All @@ -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<InvestmentResDto> getAllInvestment(Long memberId, Long boardId) {
public List<InvestmentResDto> getInvestedBoardsWithDetailsByMember(Long memberId) {
Member member = memberRepository.findById(memberId)
.orElseThrow(() -> new IllegalArgumentException("회원이 존재하지 않습니다."));

Board board = boardRepository.findById(boardId)
.orElseThrow(() -> new IllegalArgumentException("게시글이 존재하지 않습니다."));

// 해당 게시글에 대한 모든 투자 제안 조회
List<Investment> investments = investmentRepository.findByBoard(board);
List<Investment> 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<InvestmentResDto> getAllInvestment(Long memberId, Long boardId) {
// Member member = memberRepository.findById(memberId)
// .orElseThrow(() -> new IllegalArgumentException("회원이 존재하지 않습니다."));
//
// Board board = boardRepository.findById(boardId)
// .orElseThrow(() -> new IllegalArgumentException("게시글이 존재하지 않습니다."));
//
// // 해당 게시글에 대한 모든 투자 제안 조회
// List<Investment> 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());
// }
//
//}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
public interface InvestmentRepository extends JpaRepository<Investment, Long> {

boolean existsByBoardAndMember(Board board, Member member);

Optional<Investment> findByBoardAndMember(Board board, Member member);
List<Investment> findByBoard(Board board);

List<Investment> findByMember(Member member);
}
Loading