diff --git a/BE/src/main/java/com/example/baseball/controller/GameListController.java b/BE/src/main/java/com/example/baseball/controller/GameListController.java new file mode 100644 index 000000000..a8b62ae67 --- /dev/null +++ b/BE/src/main/java/com/example/baseball/controller/GameListController.java @@ -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 findGameList() { + List games = gameListService.findGameList(); + return games; + } + +} diff --git a/BE/src/main/java/com/example/baseball/dto/MatchedTeamDTO.java b/BE/src/main/java/com/example/baseball/dto/MatchedTeamDTO.java new file mode 100644 index 000000000..522caf8fa --- /dev/null +++ b/BE/src/main/java/com/example/baseball/dto/MatchedTeamDTO.java @@ -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 { + @Id + @JsonIgnore + private Long id; + + private String homeTeam; + private String awayTeam; + + 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()); + } + +} diff --git a/BE/src/main/java/com/example/baseball/repository/GameRepository.java b/BE/src/main/java/com/example/baseball/repository/GameRepository.java new file mode 100644 index 000000000..201126e34 --- /dev/null +++ b/BE/src/main/java/com/example/baseball/repository/GameRepository.java @@ -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 { + @Query("SELECT `GAME`.`ID` AS `ID`,`GAME`.`HOME_TEAM` AS `HOME_TEAM`, `GAME`.`AWAY_TEAM` AS `AWAY_TEAM` FROM `GAME` ") + @Override + List findAll(); + + @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 findById(Long id); +} diff --git a/BE/src/main/java/com/example/baseball/service/GameListService.java b/BE/src/main/java/com/example/baseball/service/GameListService.java new file mode 100644 index 000000000..0cbd59a71 --- /dev/null +++ b/BE/src/main/java/com/example/baseball/service/GameListService.java @@ -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 findGameList() { + return gameRepository.findAll() + .stream() + .map(game -> findGame(game.getId())) + .collect(Collectors.toList()); + } + +}