Skip to content

Commit

Permalink
보드게임 로직 개선
Browse files Browse the repository at this point in the history
  • Loading branch information
enbraining committed Dec 31, 2023
1 parent 10431b5 commit bda727d
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,12 @@ public class Boardgame {

public ToBoardgameDto toDto(Boardgame boardgame){
List<NumberNameWithId> players = new ArrayList<>();
boardgame.getMembers().forEach(member -> {
boardgame.getMembers().forEach(member ->
players.add(new NumberNameWithId(
member.getNumber() + " " + member.getName(),
member.getId().toString()
));
});
))
);

return new ToBoardgameDto(
boardgame.getId().toString(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,12 @@
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;

import java.util.Optional;
import java.util.UUID;

@Repository
public interface BoardgameRepository extends JpaRepository<Boardgame, Long> {
boolean existsById(UUID id);

Boardgame findBoardgameById(UUID id);
Optional<Boardgame> findBoardgameById(UUID id);
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import com.gapple.weeingback.domain.member.entity.Member;
import com.gapple.weeingback.domain.member.repository.MemberRepository;
import com.gapple.weeingback.global.exception.BoardgameExistsException;
import com.gapple.weeingback.global.exception.BoardgameNotFoundException;
import com.gapple.weeingback.global.exception.SameCreatorException;
import lombok.RequiredArgsConstructor;
import org.springframework.security.core.context.SecurityContextHolder;
Expand Down Expand Up @@ -62,7 +63,7 @@ public void joinBoardgame(UUID id) {
String memberId = SecurityContextHolder.getContext().getAuthentication().getName();
Member member = memberRepository.findMemberById(UUID.fromString(memberId));

Boardgame boardgame = boardgameRepository.findBoardgameById(id);
Boardgame boardgame = boardgameRepository.findBoardgameById(id).orElseThrow(BoardgameNotFoundException::new);

if(boardgame.getCreator().getId().toString().equals(memberId)){
throw new SameCreatorException();
Expand All @@ -75,8 +76,7 @@ public void joinBoardgame(UUID id) {

@Override
public void doneBoardgame(UUID id) {
Boardgame boardgame =
boardgameRepository.findBoardgameById(id);
Boardgame boardgame = boardgameRepository.findBoardgameById(id).orElseThrow(BoardgameNotFoundException::new);

boardgameRepository.delete(boardgame);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package com.gapple.weeingback.global.exception;

public class BoardgameNotFoundException extends RuntimeException {
}
Original file line number Diff line number Diff line change
Expand Up @@ -68,4 +68,9 @@ public ResponseEntity<HttpStatus> handleMethodArgumentNotValidException(){
public ResponseEntity<HttpStatus> consultingNotFoundException(){
return new ResponseEntity<>(HttpStatus.NOT_FOUND);
}

@ExceptionHandler(BoardgameNotFoundException.class)
public ResponseEntity<HttpStatus> baordgameNotFoundException(){
return new ResponseEntity<>(HttpStatus.NOT_FOUND);
}
}

0 comments on commit bda727d

Please sign in to comment.