Skip to content

Commit

Permalink
Merge branch 'be/develop' of https://github.com/woowacourse-teams/202…
Browse files Browse the repository at this point in the history
…4-pokerogue-helper into feat/#364-data-test
  • Loading branch information
jinchiim committed Oct 23, 2024
2 parents 0ace12d + a7b8f4f commit 278deee
Show file tree
Hide file tree
Showing 28 changed files with 533 additions and 48 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,6 @@ jobs:
token: ${{ secrets.ACTION_TOKEN }}
submodules: true

- name: Copy docker-compose.yml to home directory
working-directory: ./backend/pokerogue/src/main/resources
run: sudo cp ./docker-compose-prod.yml /home/ubuntu/docker-compose.yml

- name: Set up JDK 17
uses: actions/setup-java@v3
with:
Expand Down Expand Up @@ -71,5 +67,7 @@ jobs:
docker pull ${{ secrets.DOCKER_SERVER_IMAGE }}
docker-compose -f docker-compose.yml up -d server
docker image prune -f
docker run -d -p 80:8080 --name server \
-e JAVA_OPTS="-XX:InitialRAMPercentage=70.0 -XX:MaxRAMPercentage=70.0" \
-e TZ=Asia/Seoul \
${{ secrets.DOCKER_SERVER_IMAGE }}
73 changes: 73 additions & 0 deletions .github/workflows/Backend-CD-Prod-B.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
name: Backend Production Server CD

on:
push:
branches: [ "be/release" ]
tags:
- 'v*'

permissions:
contents: read

jobs:
test:
uses: ./.github/workflows/Backend-CI.yml
secrets: inherit


build:
needs: test
runs-on: [cd, app-b]
steps:

- name: Checkout
uses: actions/checkout@v3
with:
token: ${{ secrets.ACTION_TOKEN }}
submodules: true

- name: Set up JDK 17
uses: actions/setup-java@v3
with:
java-version: '17'
distribution: 'temurin'

- name: Build with Gradle
run: ./gradlew bootJar
working-directory: ./backend/pokerogue

- name: Docker build and push
run: |
docker login -u ${{ secrets.DOCKER_USERNAME }} -p ${{ secrets.DOCKER_PASSWORD }}
docker build -t ${{ secrets.DOCKER_SERVER_IMAGE }} -f ./backend/pokerogue/docker/Dockerfile ./backend/pokerogue
docker push ${{ secrets.DOCKER_SERVER_IMAGE }}

deploy:
needs: build
runs-on: [cd, app-b]
steps:

- name: Change permission
run: |
sudo chown -R ubuntu:ubuntu /home/ubuntu/actions-runner/_work/2024-pokerogue-helper
- name: Deploy
run: |
cd /home/ubuntu
sudo chmod 666 /var/run/docker.sock
if [ "$(docker ps -qa -f name=server)" ]; then
docker rm -f server
else
echo "No container named 'server' to remove."
fi
docker pull ${{ secrets.DOCKER_SERVER_IMAGE }}
docker run -d -p 80:8080 --name server \
-e JAVA_OPTS="-XX:InitialRAMPercentage=70.0 -XX:MaxRAMPercentage=70.0" \
-e TZ=Asia/Seoul \
${{ secrets.DOCKER_SERVER_IMAGE }}
2 changes: 1 addition & 1 deletion .github/workflows/Backend-CI.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ jobs:
- name: Remove Containers
run: |
docker ps -aq | xargs -r docker rm -vf
docker builder prune
docker system prune -a -f
- name: Set up Test MongoDB
working-directory: ./backend/pokerogue/src/main/resources/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,12 @@

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.data.mongodb.repository.config.EnableMongoRepositories;

@SpringBootApplication
@EnableMongoRepositories
public class PokerogueApplication {

public static void main(String[] args) {
SpringApplication.run(PokerogueApplication.class, args);
}
public static void main(String[] args) {
SpringApplication.run(PokerogueApplication.class, args);
}

}
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
package com.pokerogue.helper.biome.controller;

import com.pokerogue.helper.biome.dto.BiomeResponse;
import com.pokerogue.helper.biome.dto.BiomeDetailResponse;
import com.pokerogue.helper.biome.dto.BiomeResponse;
import com.pokerogue.helper.biome.service.BiomeService;
import com.pokerogue.helper.global.constant.SortingCriteria;
import com.pokerogue.helper.util.dto.ApiResponse;
import java.util.List;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@Slf4j
Expand All @@ -24,14 +26,17 @@ public ApiResponse<List<BiomeResponse>> biomeList() {
}

@GetMapping("/api/v1/biome/{id}")
public ApiResponse<BiomeDetailResponse> biomeDetails(@PathVariable("id") String id) {
public ApiResponse<BiomeDetailResponse> biomeDetails(@PathVariable("id") String id,
@RequestParam(value = "boss", defaultValue = "desc") SortingCriteria bossPokemonOrder,
@RequestParam(value = "wild", defaultValue = "asc") SortingCriteria wildPokemonOrder) {
log.info(
"---- URI : {}, Param : {}, ThreadName : {}",
"/api/v1/biome/{id}",
id,
Thread.currentThread().getName()
);

return new ApiResponse<>("바이옴 정보 불러오기에 성공했습니다.", biomeService.findBiome(id));
return new ApiResponse<>("바이옴 정보 불러오기에 성공했습니다.",
biomeService.findBiome(id, bossPokemonOrder, wildPokemonOrder));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.pokerogue.helper.biome.converter;

import com.pokerogue.helper.global.constant.SortingCriteria;
import org.springframework.core.convert.converter.Converter;

public class SortingCriteriaRequestConverter implements Converter<String, SortingCriteria> {

@Override
public SortingCriteria convert(String sortingCriteriaValue) {
return SortingCriteria.convertFrom(sortingCriteriaValue);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,16 @@

import java.util.List;
import lombok.AccessLevel;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import org.springframework.data.mongodb.core.mapping.Field;

@Getter
@AllArgsConstructor
@NoArgsConstructor(access = AccessLevel.PROTECTED)
public class NativePokemon {

private static final String BOSS = "보스";

@Field("tier")
private Tier tier;

Expand All @@ -25,4 +25,8 @@ public boolean isWild() {
public boolean isBoss() {
return tier.isBoss();
}

public int getRarity() {
return this.tier.getRarity();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,21 +8,23 @@
@Getter
public enum Tier {

COMMON("보통"),
UNCOMMON("드묾"),
RARE("레어"),
SUPER_RARE("슈퍼 레어"),
ULTRA_RARE("울트라 레어"),
BOSS("보스"),
BOSS_RARE("레어 보스"),
BOSS_SUPER_RARE("슈퍼 레어 보스"),
BOSS_ULTRA_RARE("슈퍼 울트라 레어 보스"),
COMMON("보통", 1),
UNCOMMON("드묾", 2),
RARE("레어", 3),
SUPER_RARE("슈퍼 레어", 4),
ULTRA_RARE("울트라 레어", 5),
BOSS("보스", 6),
BOSS_RARE("레어 보스", 7),
BOSS_SUPER_RARE("슈퍼 레어 보스", 8),
BOSS_ULTRA_RARE("슈퍼 울트라 레어 보스", 9),
;

private final String name;
private final int rarity;

Tier(String name) {
Tier(String name, int rarity) {
this.name = name;
this.rarity = rarity;
}

public boolean isWild() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import com.pokerogue.helper.biome.dto.NextBiomeResponse;
import com.pokerogue.helper.biome.dto.TrainerPokemonResponse;
import com.pokerogue.helper.biome.repository.BiomeRepository;
import com.pokerogue.helper.global.constant.SortingCriteria;
import com.pokerogue.helper.global.exception.ErrorMessage;
import com.pokerogue.helper.global.exception.GlobalCustomException;
import com.pokerogue.helper.pokemon.repository.PokemonRepository;
Expand Down Expand Up @@ -39,32 +40,34 @@ public List<BiomeResponse> findBiomes() {
.toList();
}

public BiomeDetailResponse findBiome(String id) {
public BiomeDetailResponse findBiome(String id, SortingCriteria bossPokemonOrder, SortingCriteria wildPokemonOrder) {
Biome biome = biomeRepository.findById(id)
.orElseThrow(() -> new GlobalCustomException(ErrorMessage.BIOME_NOT_FOUND));

return BiomeDetailResponse.of(
biome,
s3Service.getBiomeImageFromS3(biome.getId()),
getWildPokemons(biome.getNativePokemons()),
getBossPokemons(biome.getNativePokemons()),
getWildPokemons(biome.getNativePokemons(), wildPokemonOrder),
getBossPokemons(biome.getNativePokemons(), bossPokemonOrder),
getTrainerPokemons(biome),
getNextBiomes(biome)
);
}

private List<BiomeAllPokemonResponse> getWildPokemons(List<NativePokemon> nativePokemons) {
private List<BiomeAllPokemonResponse> getWildPokemons(List<NativePokemon> nativePokemons, SortingCriteria wildPokemonOrder) {
return nativePokemons.stream()
.filter(NativePokemon::isWild)
.sorted(NativePokemonComparator.of(wildPokemonOrder))
.map(nativePokemon -> BiomeAllPokemonResponse.of(
nativePokemon,
getBiomePokemons(nativePokemon.getPokemonIds())))
.toList();
}

private List<BiomeAllPokemonResponse> getBossPokemons(List<NativePokemon> nativePokemons) {
private List<BiomeAllPokemonResponse> getBossPokemons(List<NativePokemon> nativePokemons, SortingCriteria bossPokemonOrder) {
return nativePokemons.stream()
.filter(NativePokemon::isBoss)
.sorted(NativePokemonComparator.of(bossPokemonOrder))
.map(nativePokemon -> BiomeAllPokemonResponse.of(
nativePokemon,
getBiomePokemons(nativePokemon.getPokemonIds())))
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package com.pokerogue.helper.biome.service;

import static com.pokerogue.helper.global.constant.SortingCriteria.ASCENDING;
import static com.pokerogue.helper.global.constant.SortingCriteria.DESCENDING;

import com.pokerogue.helper.biome.data.NativePokemon;
import com.pokerogue.helper.global.constant.SortingCriteria;
import java.util.Comparator;

public class NativePokemonComparator implements Comparator<NativePokemon> {

private static final NativePokemonComparator ASCENDING_COMPARATOR = new NativePokemonComparator(ASCENDING);
private static final NativePokemonComparator DESCENDING_COMPARATOR = new NativePokemonComparator(DESCENDING);

private final SortingCriteria criteria;

private NativePokemonComparator(SortingCriteria criteria) {
this.criteria = criteria;
}

public static NativePokemonComparator of(SortingCriteria criteria) {
if (criteria.equals(ASCENDING)) {
return ASCENDING_COMPARATOR;
}

return DESCENDING_COMPARATOR;
}

@Override
public int compare(NativePokemon firstPokemon, NativePokemon secondPokemon) {
if (this.criteria.equals(ASCENDING)) {
return Integer.compare(firstPokemon.getRarity(), secondPokemon.getRarity());
}

return Integer.compare(secondPokemon.getRarity(), firstPokemon.getRarity());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,11 @@
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.mongodb.core.convert.MongoCustomConversions;
import org.springframework.data.mongodb.repository.config.EnableMongoRepositories;

@Configuration
public class ConverterConfig {
@EnableMongoRepositories(basePackages = {"com.pokerogue"})
public class DataMongoDbConfig {

@Bean
public MongoCustomConversions customConversions() {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.pokerogue.helper.global.config;

import com.pokerogue.helper.biome.converter.SortingCriteriaRequestConverter;
import org.springframework.context.annotation.Configuration;
import org.springframework.format.FormatterRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@Configuration
public class WebConfig implements WebMvcConfigurer {

@Override
public void addFormatters(FormatterRegistry registry) {
registry.addConverter(new SortingCriteriaRequestConverter());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package com.pokerogue.helper.global.constant;

import com.pokerogue.helper.global.exception.ErrorMessage;
import com.pokerogue.helper.global.exception.GlobalCustomException;
import java.util.Arrays;
import lombok.Getter;

@Getter
public enum SortingCriteria {

ASCENDING("asc"),
DESCENDING("desc"),
;

private final String value;

SortingCriteria(String value) {
this.value = value;
}

public static SortingCriteria convertFrom(String value) {
return Arrays.stream(values())
.filter(criteria -> criteria.value.equals(value))
.findAny()
.orElseThrow(() -> new GlobalCustomException(ErrorMessage.INVALID_SORTING_CRITERIA));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,7 @@ public enum ErrorMessage {
POKEMON_TYPE_DUPLICATION(HttpStatus.INTERNAL_SERVER_ERROR,"포켓몬의 타입은 중복될 수 없습니다."),
POKEMON_EVOLUTION_ID_MISMATCH(HttpStatus.INTERNAL_SERVER_ERROR, "포켓몬과 진화 포켓몬 아이디가 서로 일치하지 않습니다"),




INVALID_SORTING_CRITERIA(HttpStatus.BAD_REQUEST, "지원되지 않는 정렬 기준입니다."),
FILE_ACCESS_FAILED(HttpStatus.BAD_REQUEST, "파일 정보 접근에 실패했습니다."),
FILE_EXTENSION_NOT_APPLY(HttpStatus.BAD_REQUEST, "지원하지 않는 파일 형식입니다."),
;
Expand Down
Loading

0 comments on commit 278deee

Please sign in to comment.