правка метода копирования бара

This commit is contained in:
Kayashov.SM
2025-12-27 16:14:36 +04:00
parent 9fb5cdf87c
commit f573660325

View File

@@ -92,17 +92,24 @@ public class BarService {
return BarResponseDto.mapToDto(bar); return BarResponseDto.mapToDto(bar);
} }
@Transactional
public BarResponseDto copyBar(Long id, String name) { public BarResponseDto copyBar(Long id, String name) {
BarEntity barEntity = barEntityRepository.findById(id).orElseThrow(); BarEntity barEntity = barEntityRepository.findById(id).orElseThrow();
Long oldId = barEntity.getId();
String oldName = barEntity.getName();
barEntity.setName(name);
barEntity.setActive(false);
barEntity.setId(null);
BarEntity newBar = barEntityRepository.save(barEntity);
log.info("Бар {} - {} скопирован как {} - {}", oldId, oldName, newBar.getId(), name);
return BarResponseDto.mapToDto(newBar); BarEntity entity = new BarEntity();
entity.setName(name);
entity.setActive(false);
entity.setIngredients(new ArrayList<>());
barEntityRepository.save(entity);
List<IngredientEntity> ingredients = barEntity.getIngredients()
.stream()
.peek(i -> i.getBars().add(entity))
.toList();
entity.setIngredients(ingredients);
ingredientRepository.saveAll(ingredients);
log.info("Бар {} - {}, скопирован как {} - {}", barEntity.getId(), barEntity.getName(), entity.getId(), name);
return BarResponseDto.mapToDto(entity);
} }
private List<CocktailEntity> findAllowedCocktails(List<IngredientEntity> ingredients) { private List<CocktailEntity> findAllowedCocktails(List<IngredientEntity> ingredients) {