Skip to content

Commit

Permalink
Merge 033099b into 8ec3561
Browse files Browse the repository at this point in the history
  • Loading branch information
EunjiShin authored Jul 17, 2024
2 parents 8ec3561 + 033099b commit 8b1ac8e
Show file tree
Hide file tree
Showing 11 changed files with 135 additions and 78 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,15 @@

import jakarta.validation.Valid;
import jakarta.validation.constraints.NotEmpty;
import jakarta.validation.constraints.NotNull;
import jakarta.validation.constraints.Positive;

import org.depromeet.spot.application.team.dto.request.CreateBaseballTeamReq;
import org.depromeet.spot.domain.team.BaseballTeam;
import org.depromeet.spot.usecase.port.in.team.CreateBaseballTeamUsecase;
import org.depromeet.spot.usecase.port.in.team.CreateHomeTeamUsecase;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
Expand All @@ -26,6 +30,7 @@
public class CreateBaseballTeamController {

private final CreateBaseballTeamUsecase createBaseballTeamUsecase;
private final CreateHomeTeamUsecase createHomeTeamUsecase;

@PostMapping("/baseball-teams")
@ResponseStatus(HttpStatus.CREATED)
Expand All @@ -34,4 +39,13 @@ public void create(@RequestBody @Valid @NotEmpty List<CreateBaseballTeamReq> req
List<BaseballTeam> teams = requests.stream().map(CreateBaseballTeamReq::toDomain).toList();
createBaseballTeamUsecase.saveAll(teams);
}

@PostMapping("/stadiums/{stadiumId}/baseball-teams")
@ResponseStatus(HttpStatus.CREATED)
@Operation(summary = "특정 경기장에 홈 팀을 등록한다.")
public void createHomeTeam(
@PathVariable @Positive @NotNull final Long stadiumId,
@RequestBody @NotEmpty List<@Positive @NotNull Long> teamIds) {
createHomeTeamUsecase.createHomeTeam(stadiumId, teamIds);
}
}
Original file line number Diff line number Diff line change
@@ -1,13 +1,9 @@
package org.depromeet.spot.jpa.team.repository;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import org.depromeet.spot.common.exception.team.TeamException.BaseballTeamNotFoundException;
import org.depromeet.spot.domain.stadium.Stadium;
import org.depromeet.spot.domain.team.BaseballTeam;
import org.depromeet.spot.jpa.stadium.entity.StadiumEntity;
import org.depromeet.spot.jpa.team.entity.BaseballTeamEntity;
import org.depromeet.spot.usecase.port.out.team.BaseballTeamRepository;
import org.springframework.stereotype.Repository;
Expand All @@ -18,7 +14,6 @@
@RequiredArgsConstructor
public class BaseballTeamRepositoryImpl implements BaseballTeamRepository {

private final StadiumHomeTeamCustomRepository stadiumHomeTeamCustomRepository;
private final BaseballTeamJpaRepository baseballTeamJpaRepository;
private final BaseballTeamJdbcRepository baseballTeamJdbcRepository;

Expand All @@ -37,37 +32,11 @@ public List<BaseballTeam> findAll() {
return entities.stream().map(BaseballTeamEntity::toDomain).toList();
}

@Override
public List<BaseballTeam> findAllHomeTeamByStadium(final Long stadiumId) {
List<BaseballTeamEntity> homeTeamEntities =
stadiumHomeTeamCustomRepository.findAllHomeTeamByStadium(stadiumId);
return homeTeamEntities.stream().map(BaseballTeamEntity::toDomain).toList();
}

@Override
public Map<Stadium, List<BaseballTeam>> findAllStadiumHomeTeam() {
Map<StadiumEntity, List<BaseballTeamEntity>> stadiumHomeTeamMap =
stadiumHomeTeamCustomRepository.findAllStadiumHomeTeam();
return stadiumHomeTeamMap.entrySet().stream()
.collect(
Collectors.toMap(
entry -> entry.getKey().toDomain(),
entry ->
entry.getValue().stream()
.map(BaseballTeamEntity::toDomain)
.toList()));
}

@Override
public void saveAll(List<BaseballTeam> teams) {
baseballTeamJdbcRepository.createBaseballTeams(teams);
}

@Override
public void createHomeTeam(Long stadiumId, List<Long> teamIds) {
// TODO: 홈 팀 등록할 때 구현 예정
}

@Override
public boolean existsByNameIn(List<String> names) {
return baseballTeamJpaRepository.existsByNameIn(names);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package org.depromeet.spot.jpa.team.repository.hometeam;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import org.depromeet.spot.domain.stadium.Stadium;
import org.depromeet.spot.domain.team.BaseballTeam;
import org.depromeet.spot.jpa.stadium.entity.StadiumEntity;
import org.depromeet.spot.jpa.team.entity.BaseballTeamEntity;
import org.depromeet.spot.usecase.port.out.team.HomeTeamRepository;
import org.springframework.stereotype.Repository;

import lombok.RequiredArgsConstructor;

@Repository
@RequiredArgsConstructor
public class HomeTeamRepositoryImpl implements HomeTeamRepository {

private final StadiumHomeTeamCustomRepository stadiumHomeTeamCustomRepository;

@Override
public List<BaseballTeam> findAllHomeTeamByStadium(final Long stadiumId) {
List<BaseballTeamEntity> homeTeamEntities =
stadiumHomeTeamCustomRepository.findAllHomeTeamByStadium(stadiumId);
return homeTeamEntities.stream().map(BaseballTeamEntity::toDomain).toList();
}

@Override
public Map<Stadium, List<BaseballTeam>> findAllStadiumHomeTeam() {
Map<StadiumEntity, List<BaseballTeamEntity>> stadiumHomeTeamMap =
stadiumHomeTeamCustomRepository.findAllStadiumHomeTeam();
return stadiumHomeTeamMap.entrySet().stream()
.collect(
Collectors.toMap(
entry -> entry.getKey().toDomain(),
entry ->
entry.getValue().stream()
.map(BaseballTeamEntity::toDomain)
.toList()));
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package org.depromeet.spot.jpa.team.repository;
package org.depromeet.spot.jpa.team.repository.hometeam;

import static com.querydsl.core.group.GroupBy.groupBy;
import static com.querydsl.core.group.GroupBy.list;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package org.depromeet.spot.usecase.port.in.team;

import java.util.List;

public interface CreateHomeTeamUsecase {

void createHomeTeam(Long stadiumId, List<Long> teamIds);
}
Original file line number Diff line number Diff line change
@@ -1,23 +1,15 @@
package org.depromeet.spot.usecase.port.out.team;

import java.util.List;
import java.util.Map;

import org.depromeet.spot.domain.stadium.Stadium;
import org.depromeet.spot.domain.team.BaseballTeam;

public interface BaseballTeamRepository {
BaseballTeam findById(Long id);

List<BaseballTeam> findAll();

List<BaseballTeam> findAllHomeTeamByStadium(Long stadiumId);

Map<Stadium, List<BaseballTeam>> findAllStadiumHomeTeam();

void saveAll(List<BaseballTeam> teams);

void createHomeTeam(Long stadiumId, List<Long> teamIds);

boolean existsByNameIn(List<String> names);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package org.depromeet.spot.usecase.port.out.team;

import java.util.List;
import java.util.Map;

import org.depromeet.spot.domain.stadium.Stadium;
import org.depromeet.spot.domain.team.BaseballTeam;

public interface HomeTeamRepository {

List<BaseballTeam> findAllHomeTeamByStadium(Long stadiumId);

Map<Stadium, List<BaseballTeam>> findAllStadiumHomeTeam();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package org.depromeet.spot.usecase.service.team;

import java.util.List;

import org.depromeet.spot.usecase.port.in.team.CreateHomeTeamUsecase;
import org.depromeet.spot.usecase.port.out.team.HomeTeamRepository;
import org.springframework.stereotype.Service;

import lombok.RequiredArgsConstructor;

@Service
@RequiredArgsConstructor
public class CreateHomeTeamService implements CreateHomeTeamUsecase {

private final HomeTeamRepository homeTeamRepository;

@Override
public void createHomeTeam(Long stadiumId, List<Long> teamIds) {}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import org.depromeet.spot.domain.stadium.Stadium;
import org.depromeet.spot.domain.team.BaseballTeam;
import org.depromeet.spot.usecase.port.in.team.ReadStadiumHomeTeamUsecase;
import org.depromeet.spot.usecase.port.out.team.BaseballTeamRepository;
import org.depromeet.spot.usecase.port.out.team.HomeTeamRepository;
import org.springframework.stereotype.Service;

import lombok.RequiredArgsConstructor;
Expand All @@ -15,18 +15,18 @@
@RequiredArgsConstructor
public class ReadStadiumHomeTeamService implements ReadStadiumHomeTeamUsecase {

private final BaseballTeamRepository baseballTeamRepository;
private final HomeTeamRepository homeTeamRepository;

@Override
public List<HomeTeamInfo> findByStadium(final Long stadiumId) {
List<BaseballTeam> teams = baseballTeamRepository.findAllHomeTeamByStadium(stadiumId);
List<BaseballTeam> teams = homeTeamRepository.findAllHomeTeamByStadium(stadiumId);
return teams.stream()
.map(t -> new HomeTeamInfo(t.getId(), t.getAlias(), t.getLabelRgbCode()))
.toList();
}

@Override
public Map<Stadium, List<BaseballTeam>> findAllStadiumHomeTeam() {
return baseballTeamRepository.findAllStadiumHomeTeam();
return homeTeamRepository.findAllStadiumHomeTeam();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,19 @@

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.concurrent.atomic.AtomicLong;
import java.util.stream.Collectors;

import org.depromeet.spot.common.exception.team.TeamException.BaseballTeamNotFoundException;
import org.depromeet.spot.domain.stadium.Stadium;
import org.depromeet.spot.domain.team.BaseballTeam;
import org.depromeet.spot.usecase.port.out.team.BaseballTeamRepository;

public class FakeBaseballTeamRepository implements BaseballTeamRepository {

private final AtomicLong autoGeneratedId = new AtomicLong(0);
private final List<BaseballTeam> data = Collections.synchronizedList(new ArrayList<>());
private final Map<Stadium, List<BaseballTeam>> homeTeamMap =
Collections.synchronizedMap(new HashMap<>());

@Override
public BaseballTeam findById(Long id) {
Expand All @@ -36,19 +30,6 @@ public List<BaseballTeam> findAll() {
return data;
}

@Override
public List<BaseballTeam> findAllHomeTeamByStadium(Long stadiumId) {
return homeTeamMap.entrySet().stream()
.filter(entry -> stadiumId.equals(entry.getKey().getId()))
.flatMap(entry -> entry.getValue().stream())
.collect(Collectors.toList());
}

@Override
public Map<Stadium, List<BaseballTeam>> findAllStadiumHomeTeam() {
return homeTeamMap;
}

public BaseballTeam save(BaseballTeam team) {
if (team.getId() == null || team.getId() == 0) {
BaseballTeam newTeam =
Expand All @@ -73,21 +54,6 @@ public void saveAll(List<BaseballTeam> teams) {
teams.forEach(this::save);
}

@Override
public void createHomeTeam(Long stadiumId, List<Long> teamIds) {
Stadium stadium = Stadium.builder().id(stadiumId).build();
List<BaseballTeam> newTeams =
teamIds.stream().map(t -> BaseballTeam.builder().id(t).build()).toList();

homeTeamMap.merge(
stadium,
newTeams,
(existingTeams, teams) -> {
existingTeams.addAll(teams);
return existingTeams;
});
}

@Override
public boolean existsByNameIn(List<String> names) {
return data.stream().map(BaseballTeam::getName).anyMatch(names::contains);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package org.depromeet.spot.usecase.service.fake;

import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.atomic.AtomicLong;
import java.util.stream.Collectors;

import org.depromeet.spot.domain.stadium.Stadium;
import org.depromeet.spot.domain.team.BaseballTeam;
import org.depromeet.spot.usecase.port.out.team.HomeTeamRepository;

public class FakeHomeTeamRepository implements HomeTeamRepository {

private final AtomicLong autoGeneratedId = new AtomicLong(0);

private final Map<Stadium, List<BaseballTeam>> homeTeamMap =
Collections.synchronizedMap(new HashMap<>());

@Override
public List<BaseballTeam> findAllHomeTeamByStadium(Long stadiumId) {
return homeTeamMap.entrySet().stream()
.filter(entry -> stadiumId.equals(entry.getKey().getId()))
.flatMap(entry -> entry.getValue().stream())
.collect(Collectors.toList());
}

@Override
public Map<Stadium, List<BaseballTeam>> findAllStadiumHomeTeam() {
return homeTeamMap;
}
}

0 comments on commit 8b1ac8e

Please sign in to comment.