Skip to content

Commit

Permalink
Merge pull request #434 from woowacourse-teams/develop
Browse files Browse the repository at this point in the history
Release: v1.2.0
  • Loading branch information
n0eyes authored Sep 21, 2023
2 parents 461d4c1 + 664dac4 commit c21e8c3
Show file tree
Hide file tree
Showing 168 changed files with 3,065 additions and 747 deletions.
2 changes: 1 addition & 1 deletion backend/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ dependencies {
runtimeOnly 'com.h2database:h2'

implementation 'software.amazon.awssdk:s3:2.20.121'
compileOnly 'software.amazon.awssdk:url-connection-client:2.20.121'
implementation 'software.amazon.awssdk:url-connection-client:2.20.121'

testImplementation 'org.springframework.boot:spring-boot-starter-test'
testImplementation 'io.rest-assured:rest-assured:5.3.1'
Expand Down
2 changes: 1 addition & 1 deletion backend/docker/docker-compose.dev.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ version: '3'
services:
mysql:
container_name: zipgo
image: mysql/mysql-server:5.7
image: mysql/mysql-server
environment:
MYSQL_DATABASE: zipgo-mysql
MYSQL_ROOT_HOST: '%'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,25 @@
import org.springframework.transaction.annotation.Transactional;
import zipgo.admin.dto.BrandSelectResponse;
import zipgo.admin.dto.FunctionalitySelectResponse;
import zipgo.admin.dto.PetFoodReadResponse;
import zipgo.admin.dto.PetFoodUpdateRequest;
import zipgo.admin.dto.PrimaryIngredientSelectResponse;
import zipgo.brand.domain.Brand;
import zipgo.brand.domain.repository.BrandRepository;
import zipgo.petfood.domain.Functionality;
import zipgo.petfood.domain.PetFood;
import zipgo.petfood.domain.PrimaryIngredient;
import zipgo.petfood.domain.repository.FunctionalityRepository;
import zipgo.petfood.domain.repository.PetFoodRepository;
import zipgo.petfood.domain.repository.PrimaryIngredientRepository;
import zipgo.admin.dto.PrimaryIngredientSelectResponse;

@Service
@RequiredArgsConstructor
@Transactional(readOnly = true)
public class AdminQueryService {

private final BrandRepository brandRepository;
private final PetFoodRepository petFoodRepository;
private final FunctionalityRepository functionalityRepository;
private final PrimaryIngredientRepository primaryIngredientRepository;

Expand All @@ -30,17 +36,27 @@ public List<BrandSelectResponse> getBrands() {
}

public List<FunctionalitySelectResponse> getFunctionalities() {
List<Functionality> distinctFunctionalities = functionalityRepository.findDistinctFunctionalities();
List<Functionality> distinctFunctionalities = functionalityRepository.findAll();
return distinctFunctionalities.stream()
.map(functionality -> FunctionalitySelectResponse.of(functionality.getId(), functionality.getName()))
.collect(Collectors.toList());
}

public List<PrimaryIngredientSelectResponse> getPrimaryIngredients() {
List<PrimaryIngredient> primaryIngredients = primaryIngredientRepository.findDistinctPrimaryIngredients();
List<PrimaryIngredient> primaryIngredients = primaryIngredientRepository.findAll();
return primaryIngredients.stream()
.map(primaryIngredient -> PrimaryIngredientSelectResponse.of(primaryIngredient.getId(), primaryIngredient.getName()))
.map(primaryIngredient -> PrimaryIngredientSelectResponse.of(primaryIngredient.getId(),
primaryIngredient.getName()))
.collect(Collectors.toList());
}

public List<PetFood> getPetFoods() {
return petFoodRepository.findAll();
}

public PetFoodReadResponse getPetFoodById(Long petFoodId) {
PetFood petFood = petFoodRepository.getById(petFoodId);
return PetFoodReadResponse.from(petFood);
}

}
87 changes: 82 additions & 5 deletions backend/src/main/java/zipgo/admin/application/AdminService.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import zipgo.admin.dto.BrandCreateRequest;
import zipgo.admin.dto.FunctionalityCreateRequest;
import zipgo.admin.dto.PetFoodCreateRequest;
import zipgo.admin.dto.PetFoodUpdateRequest;
import zipgo.admin.dto.PrimaryIngredientCreateRequest;
import zipgo.brand.domain.Brand;
import zipgo.brand.domain.repository.BrandRepository;
import zipgo.petfood.domain.Functionality;
Expand All @@ -13,17 +17,16 @@
import zipgo.petfood.domain.PetFoodPrimaryIngredient;
import zipgo.petfood.domain.PrimaryIngredient;
import zipgo.petfood.domain.repository.FunctionalityRepository;
import zipgo.admin.dto.FunctionalityCreateRequest;
import zipgo.petfood.domain.repository.PetFoodRepository;
import zipgo.petfood.domain.repository.PrimaryIngredientRepository;
import zipgo.admin.dto.PrimaryIngredientCreateRequest;
import zipgo.admin.dto.PetFoodCreateRequest;

@Service
@Transactional
@RequiredArgsConstructor
public class AdminService {

private static final int EMPTY_STRING_CHECK_INDEX = 0;

private final BrandRepository brandRepository;
private final FunctionalityRepository functionalityRepository;
private final PrimaryIngredientRepository primaryIngredientRepository;
Expand Down Expand Up @@ -59,7 +62,8 @@ private void addPrimaryIngredients(PetFoodCreateRequest request, PetFood petFood
.map(primaryIngredientRepository::getById)
.toList();
for (PrimaryIngredient primaryIngredient : primaryIngredients) {
petFood.addPetFoodPrimaryIngredient(PetFoodPrimaryIngredient.builder().petFood(petFood).primaryIngredient(primaryIngredient).build());
petFood.addPetFoodPrimaryIngredient(
PetFoodPrimaryIngredient.builder().petFood(petFood).primaryIngredient(primaryIngredient).build());
}
}

Expand All @@ -68,8 +72,81 @@ private void addFunctionalities(PetFoodCreateRequest request, PetFood petFood) {
.map(functionalityRepository::getById)
.toList();
for (Functionality functionality : functionalities) {
petFood.addPetFoodFunctionality(PetFoodFunctionality.builder().petFood(petFood).functionality(functionality).build());
petFood.addPetFoodFunctionality(
PetFoodFunctionality.builder().petFood(petFood).functionality(functionality).build());
}
}

public void updatePetFood(Long petFoodId, PetFoodUpdateRequest request) {
PetFood petFood = petFoodRepository.getById(petFoodId);
petFood.updatePetFood(request.petFoodName(), request.euStandard(), request.usStandard(), request.imageUrl());
Brand brand = brandRepository.getByName(request.brandName());
petFood.updateBrand(brand);

updateFunctionalities(request, petFood);
updatePrimaryIngredients(request, petFood);
}

private void updateFunctionalities(PetFoodUpdateRequest request, PetFood petFood) {
petFood.initPetFoodFunctionalities();
List<String> functionalitiesName = request.functionalities();
if (functionalityNameIsEmpty(functionalitiesName)) {
return;
}
List<Functionality> functionalities = request.functionalities().stream()
.map(functionalityName -> functionalityRepository.getByName(functionalityName))
.toList();
changeFunctionalityRelations(functionalities, petFood);
}

private boolean functionalityNameIsEmpty(List<String> functionalitiesName) {
if (functionalitiesName.get(EMPTY_STRING_CHECK_INDEX).equals("")) {
return true;
}
return false;
}

private void changeFunctionalityRelations(List<Functionality> functionalities, PetFood petFood) {
for (Functionality functionality : functionalities) {
PetFoodFunctionality petFoodFunctionality = PetFoodFunctionality.builder()
.functionality(functionality)
.petFood(petFood)
.build();
petFoodFunctionality.changeRelations(petFood, functionality);
}
}

private void updatePrimaryIngredients(PetFoodUpdateRequest request, PetFood petFood) {
petFood.initPetFoodPrimaryIngredients();
List<String> primaryIngredientsName = request.primaryIngredients();
if (primaryIngredientsNameIsEmpty(primaryIngredientsName)) {
return;
}
List<PrimaryIngredient> primaryIngredients = request.primaryIngredients().stream()
.map(functionalityName -> primaryIngredientRepository.getByName(functionalityName))
.toList();
changePrimaryIngredientRelations(primaryIngredients, petFood);
}

private static boolean primaryIngredientsNameIsEmpty(List<String> primaryIngredientsName) {
if (primaryIngredientsName.get(EMPTY_STRING_CHECK_INDEX).equals("")) {
return true;
}
return false;
}

private void changePrimaryIngredientRelations(List<PrimaryIngredient> primaryIngredients, PetFood petFood) {
for (PrimaryIngredient primaryIngredient : primaryIngredients) {
PetFoodPrimaryIngredient petFoodPrimaryIngredient = PetFoodPrimaryIngredient.builder()
.primaryIngredient(primaryIngredient)
.petFood(petFood)
.build();
petFoodPrimaryIngredient.changeRelations(petFood, primaryIngredient);
}
}

public void deletePetFood(Long petFoodId) {
petFoodRepository.deleteById(petFoodId);
}

}
34 changes: 34 additions & 0 deletions backend/src/main/java/zipgo/admin/dto/PetFoodReadResponse.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package zipgo.admin.dto;

import java.util.List;
import zipgo.petfood.domain.PetFood;

public record PetFoodReadResponse(
Long id,
String brandName,
String foodName,
String imageUrl,
boolean euStandard,
boolean usStandard,
List<String> functionalities,
List<String> primaryIngredients
) {

public static PetFoodReadResponse from(PetFood petFood) {
return new PetFoodReadResponse(
petFood.getId(),
petFood.getBrand().getName(),
petFood.getName(),
petFood.getImageUrl(),
petFood.getHasStandard().getEurope(),
petFood.getHasStandard().getUnitedStates(),
petFood.getPetFoodFunctionalities().stream()
.map(petFoodFunctionality -> petFoodFunctionality.getFunctionality().getName())
.toList(),
petFood.getPetFoodPrimaryIngredients().stream()
.map(petFoodPrimaryIngredient -> petFoodPrimaryIngredient.getPrimaryIngredient().getName())
.toList()
);
}

}
20 changes: 20 additions & 0 deletions backend/src/main/java/zipgo/admin/dto/PetFoodUpdateRequest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package zipgo.admin.dto;

import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.databind.PropertyNamingStrategies;
import com.fasterxml.jackson.databind.annotation.JsonNaming;
import java.util.List;

@JsonNaming(PropertyNamingStrategies.SnakeCaseStrategy.class)
public record PetFoodUpdateRequest (
String petFoodName,
String brandName,
boolean euStandard,
boolean usStandard,
String imageUrl,
List<String> functionalities,
List<String> primaryIngredients
) {

}

Loading

0 comments on commit c21e8c3

Please sign in to comment.