-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package com.example.baseball.controller; | ||
|
||
import com.example.baseball.dto.MatchedTeamDTO; | ||
import com.example.baseball.service.GameListService; | ||
import org.springframework.web.bind.annotation.GetMapping; | ||
import org.springframework.web.bind.annotation.PathVariable; | ||
import org.springframework.web.bind.annotation.RestController; | ||
|
||
import java.util.List; | ||
|
||
@RestController | ||
public class GameListController { | ||
private final GameListService gameListService; | ||
|
||
public GameListController (GameListService gameListService) { | ||
this.gameListService = gameListService; | ||
} | ||
|
||
@GetMapping("/matchTeam/{id}") | ||
public MatchedTeamDTO findGame(@PathVariable Long id) { | ||
return gameListService.findGame(id); | ||
} | ||
|
||
@GetMapping("/helloWorld") | ||
public List<MatchedTeamDTO> findGameList() { | ||
List<MatchedTeamDTO> games = gameListService.findGameList(); | ||
return games; | ||
} | ||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package com.example.baseball.dto; | ||
|
||
import com.example.baseball.entity.Game; | ||
import com.fasterxml.jackson.annotation.JsonIgnore; | ||
import org.springframework.data.annotation.Id; | ||
|
||
public class MatchedTeamDTO { | ||
This comment has been minimized.
Sorry, something went wrong. |
||
@Id | ||
@JsonIgnore | ||
private Long id; | ||
|
||
private String homeTeam; | ||
private String awayTeam; | ||
This comment has been minimized.
Sorry, something went wrong. |
||
|
||
public MatchedTeamDTO(Long id, String homeTeam, String awayTeam) { | ||
this.id = id; | ||
this.homeTeam = homeTeam; | ||
this.awayTeam = awayTeam; | ||
} | ||
|
||
public Long getId() { | ||
return id; | ||
} | ||
|
||
public String getHomeTeam() { | ||
return homeTeam; | ||
} | ||
|
||
public String getAwayTeam() { | ||
return awayTeam; | ||
} | ||
|
||
public static MatchedTeamDTO of(Game game) { | ||
return new MatchedTeamDTO(game.getId(), game.getHomeTeam(), game.getAwayTeam()); | ||
} | ||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package com.example.baseball.repository; | ||
|
||
import com.example.baseball.entity.Game; | ||
import org.springframework.data.jdbc.repository.query.Query; | ||
import org.springframework.data.repository.CrudRepository; | ||
|
||
import java.util.List; | ||
import java.util.Optional; | ||
|
||
public interface GameRepository extends CrudRepository<Game, Long> { | ||
@Query("SELECT `GAME`.`ID` AS `ID`,`GAME`.`HOME_TEAM` AS `HOME_TEAM`, `GAME`.`AWAY_TEAM` AS `AWAY_TEAM` FROM `GAME` ") | ||
@Override | ||
List<Game> findAll(); | ||
This comment has been minimized.
Sorry, something went wrong.
kyupid
Owner
|
||
|
||
@Query("SELECT `GAME`.`ID` AS `ID`, `GAME`.`HOME_TEAM` AS `HOME_TEAM`, `GAME`.`AWAY_TEAM` AS `AWAY_TEAM` FROM `GAME` WHERE `GAME`.`ID` = :id") | ||
Optional<Game> findById(Long id); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package com.example.baseball.service; | ||
|
||
import com.example.baseball.entity.Game; | ||
import com.example.baseball.dto.MatchedTeamDTO; | ||
import com.example.baseball.repository.GameRepository; | ||
import org.springframework.stereotype.Service; | ||
|
||
import java.util.List; | ||
import java.util.stream.Collectors; | ||
|
||
@Service | ||
public class GameListService { | ||
|
||
private final GameRepository gameRepository; | ||
|
||
private GameListService(GameRepository gameRepository) { | ||
this.gameRepository = gameRepository; | ||
} | ||
|
||
public MatchedTeamDTO findGame(Long id) { | ||
Game game = gameRepository.findById(id).orElseThrow(IllegalArgumentException::new); | ||
System.out.println(game.getId()); | ||
return MatchedTeamDTO.of(game); | ||
} | ||
|
||
public List<MatchedTeamDTO> findGameList() { | ||
This comment has been minimized.
Sorry, something went wrong.
kyupid
Owner
|
||
return gameRepository.findAll() | ||
.stream() | ||
.map(game -> findGame(game.getId())) | ||
.collect(Collectors.toList()); | ||
} | ||
|
||
} |
MatchedTeam이 서로 맞붙는 팀이라는 의미에서 작성하신건가요?
아니면 서로 매칭되는 팀이라는 의미일까요?