Skip to content

Commit

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

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.application.team.dto.request.CreateHomeTeamReq;
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 +31,7 @@
public class CreateBaseballTeamController {

private final CreateBaseballTeamUsecase createBaseballTeamUsecase;
private final CreateHomeTeamUsecase createHomeTeamUsecase;

@PostMapping("/baseball-teams")
@ResponseStatus(HttpStatus.CREATED)
Expand All @@ -34,4 +40,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 @Valid CreateHomeTeamReq req) {
createHomeTeamUsecase.createHomeTeams(stadiumId, req.teamIds());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package org.depromeet.spot.application.team.dto.request;

import java.util.Set;

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

public record CreateHomeTeamReq(@NotEmpty @NotNull Set<@Positive @NotNull Long> teamIds) {}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ public static class BaseballTeamNotFoundException extends TeamException {
public BaseballTeamNotFoundException() {
super(TeamErrorCode.BASEBALL_TEAM_NOT_FOUND);
}

public BaseballTeamNotFoundException(Long id) {
super(TeamErrorCode.BASEBALL_TEAM_NOT_FOUND.appended(" : " + id));
}
}

public static class InvalidBaseballTeamNameException extends TeamException {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package org.depromeet.spot.domain.team;

import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;

@Getter
@Builder
@AllArgsConstructor
public class StadiumHomeTeam {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,6 @@

public interface BaseballTeamJpaRepository extends JpaRepository<BaseballTeamEntity, Long> {
boolean existsByNameIn(List<String> names);

boolean existsById(Long teamId);
}
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,39 +32,18 @@ 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: 홈 팀 등록할 때 구현 예정
public boolean existsByNameIn(List<String> names) {
return baseballTeamJpaRepository.existsByNameIn(names);
}

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

import org.depromeet.spot.jpa.team.entity.StadiumHomeTeamEntity;
import org.springframework.data.jpa.repository.JpaRepository;

public interface HomeTeamJpaRepository extends JpaRepository<StadiumHomeTeamEntity, Long> {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
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.domain.team.StadiumHomeTeam;
import org.depromeet.spot.jpa.stadium.entity.StadiumEntity;
import org.depromeet.spot.jpa.team.entity.BaseballTeamEntity;
import org.depromeet.spot.jpa.team.entity.StadiumHomeTeamEntity;
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;
private final HomeTeamJpaRepository homeTeamJpaRepository;

@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<StadiumHomeTeam> homeTeams) {
List<StadiumHomeTeamEntity> entities =
homeTeams.stream().map(StadiumHomeTeamEntity::from).toList();
homeTeamJpaRepository.saveAll(entities);
}
}
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.Set;

public interface CreateHomeTeamUsecase {

void createHomeTeams(Long stadiumId, Set<Long> teamIds);
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
package org.depromeet.spot.usecase.port.in.team;

import java.util.List;
import java.util.Set;

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

public interface ReadBaseballTeamUsecase {

BaseballTeam findById(Long id);

List<BaseballTeam> findAll();

void areAllTeamIdsExist(Set<Long> teamIds);
}
Original file line number Diff line number Diff line change
@@ -1,23 +1,17 @@
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);

boolean existsById(Long id);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
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;
import org.depromeet.spot.domain.team.StadiumHomeTeam;

public interface HomeTeamRepository {

List<BaseballTeam> findAllHomeTeamByStadium(Long stadiumId);

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

void saveAll(List<StadiumHomeTeam> homeTeams);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package org.depromeet.spot.usecase.service.team;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

import org.depromeet.spot.domain.team.StadiumHomeTeam;
import org.depromeet.spot.usecase.port.in.stadium.StadiumReadUsecase;
import org.depromeet.spot.usecase.port.in.team.CreateHomeTeamUsecase;
import org.depromeet.spot.usecase.port.in.team.ReadBaseballTeamUsecase;
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;
private final StadiumReadUsecase stadiumReadUsecase;
private final ReadBaseballTeamUsecase readBaseballTeamUsecase;

@Override
public void createHomeTeams(final Long stadiumId, Set<Long> teamIds) {
stadiumReadUsecase.checkIsExistsBy(stadiumId);
readBaseballTeamUsecase.areAllTeamIdsExist(teamIds);
List<StadiumHomeTeam> homeTeams = new ArrayList<>();
teamIds.forEach(
teamId ->
homeTeams.add(
StadiumHomeTeam.builder()
.stadiumId(stadiumId)
.teamId(teamId)
.build()));
homeTeamRepository.saveAll(homeTeams);
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package org.depromeet.spot.usecase.service.team;

import java.util.List;
import java.util.Set;

import org.depromeet.spot.common.exception.team.TeamException.BaseballTeamNotFoundException;
import org.depromeet.spot.domain.team.BaseballTeam;
import org.depromeet.spot.usecase.port.in.team.ReadBaseballTeamUsecase;
import org.depromeet.spot.usecase.port.out.team.BaseballTeamRepository;
Expand All @@ -17,8 +19,22 @@ public class ReadBaseballTeamService implements ReadBaseballTeamUsecase {

private final BaseballTeamRepository baseballTeamRepository;

@Override
public BaseballTeam findById(final Long id) {
return baseballTeamRepository.findById(id);
}

@Override
public List<BaseballTeam> findAll() {
return baseballTeamRepository.findAll();
}

@Override
public void areAllTeamIdsExist(Set<Long> teamIds) {
for (Long teamId : teamIds) {
if (!baseballTeamRepository.existsById(teamId)) {
throw new BaseballTeamNotFoundException(teamId);
}
}
}
}
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();
}
}
Loading

0 comments on commit c2ca87f

Please sign in to comment.