временные изменения
This commit is contained in:
@@ -10,10 +10,12 @@ import ru.kayashov.bar.controller.dto.cocktail.CocktailModalDto;
|
||||
import ru.kayashov.bar.controller.dto.cocktail.RatingResponseDto;
|
||||
import ru.kayashov.bar.controller.dto.cocktail.ReceiptResponseDto;
|
||||
import ru.kayashov.bar.controller.dto.ingredient.IngredientSimpleResponseDto;
|
||||
import ru.kayashov.bar.model.entity.BarEntity;
|
||||
import ru.kayashov.bar.model.entity.CocktailEntity;
|
||||
import ru.kayashov.bar.model.entity.IngredientEntity;
|
||||
import ru.kayashov.bar.model.entity.ReceiptEntity;
|
||||
import ru.kayashov.bar.model.entity.Visitor;
|
||||
import ru.kayashov.bar.repository.BarEntityRepository;
|
||||
import ru.kayashov.bar.repository.VisitorsRepository;
|
||||
|
||||
import java.util.ArrayList;
|
||||
@@ -25,6 +27,7 @@ import java.util.stream.Collectors;
|
||||
public class CocktailMapper {
|
||||
|
||||
private final VisitorsRepository visitorsRepository;
|
||||
private final BarEntityRepository barRepository;
|
||||
|
||||
@Transactional
|
||||
public List<CocktailForListResponseDto> cocktailsToDtoList(List<CocktailEntity> cocktails, Boolean all) {
|
||||
@@ -33,7 +36,7 @@ public class CocktailMapper {
|
||||
// return new ArrayList<>();
|
||||
// }
|
||||
List<Long> barStopList = new ArrayList<>();
|
||||
List<Long> allowedIngredients = getAllowedIngredients(visitor);
|
||||
List<Long> allowedIngredients = getAllowedIngredients();
|
||||
return cocktails.stream()
|
||||
.map(c -> cocktailToDto(c, visitor, allowedIngredients, barStopList))
|
||||
// .filter(c -> all || c.getIsAllowed())
|
||||
@@ -42,7 +45,7 @@ public class CocktailMapper {
|
||||
}
|
||||
|
||||
public CocktailForListResponseDto cocktailToFullDto(CocktailEntity e) {
|
||||
List<Long> allowed = getAllowedIngredients(getCurrentVisitor());
|
||||
List<Long> allowed = getAllowedIngredients();
|
||||
return CocktailForListResponseDto.builder()
|
||||
.id(e.getId())
|
||||
.name(e.getName())
|
||||
@@ -107,8 +110,13 @@ public class CocktailMapper {
|
||||
// d.setReceipt(e.getReceipt().stream().map(ReceiptResponseDto::mapToDto).toList());
|
||||
}
|
||||
|
||||
private List<Long> getAllowedIngredients(Visitor visitor) {
|
||||
return new ArrayList<>();
|
||||
private List<Long> getAllowedIngredients() {
|
||||
return barRepository.findByActiveTrue()
|
||||
.map(BarEntity::getIngredients)
|
||||
.orElseThrow()
|
||||
.stream()
|
||||
.map(IngredientEntity::getId)
|
||||
.toList();
|
||||
// return visitor.getResidents().stream()
|
||||
// .filter(BarResident::getActive)
|
||||
// .map(BarResident::getBar)
|
||||
@@ -145,8 +153,7 @@ public class CocktailMapper {
|
||||
}
|
||||
|
||||
public CocktailModalDto cocktailToModalDto(CocktailEntity e) {
|
||||
Visitor visitor = getCurrentVisitor();
|
||||
List<Long> allowedIngredients = getAllowedIngredients(visitor);
|
||||
List<Long> allowedIngredients = getAllowedIngredients();
|
||||
return CocktailModalDto.builder()
|
||||
.id(e.getId())
|
||||
.name(e.getName())
|
||||
|
||||
@@ -4,8 +4,9 @@ import lombok.RequiredArgsConstructor;
|
||||
import org.springframework.stereotype.Component;
|
||||
import ru.kayashov.bar.controller.dto.ingredient.IngredientResponseDto;
|
||||
import ru.kayashov.bar.controller.dto.ingredient.IngredientSimpleResponseDto;
|
||||
import ru.kayashov.bar.model.entity.BarEntity;
|
||||
import ru.kayashov.bar.model.entity.IngredientEntity;
|
||||
import ru.kayashov.bar.service.VisitorService;
|
||||
import ru.kayashov.bar.repository.BarEntityRepository;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@@ -13,15 +14,24 @@ import java.util.List;
|
||||
@RequiredArgsConstructor
|
||||
public class IngredientMapper {
|
||||
|
||||
private final VisitorService visitorService;
|
||||
private final BarEntityRepository barRepository;
|
||||
|
||||
public List<IngredientResponseDto> mapIngredientsToDtoList(List<IngredientEntity> ingredients) {
|
||||
List<Long> allowedIngredients = visitorService.getAllowedIngredients();
|
||||
List<Long> allowedIngredients = getAllowedIngredients();
|
||||
return ingredients.stream()
|
||||
.map(i -> mapIngredientToDto(i, allowedIngredients))
|
||||
.toList();
|
||||
}
|
||||
|
||||
private List<Long> getAllowedIngredients() {
|
||||
return barRepository.findByActiveTrue()
|
||||
.map(BarEntity::getIngredients)
|
||||
.orElseThrow()
|
||||
.stream()
|
||||
.map(IngredientEntity::getId)
|
||||
.toList();
|
||||
}
|
||||
|
||||
private IngredientResponseDto mapIngredientToDto(IngredientEntity i, List<Long> allowedIngredients) {
|
||||
return IngredientResponseDto
|
||||
.builder()
|
||||
@@ -37,7 +47,7 @@ public class IngredientMapper {
|
||||
}
|
||||
|
||||
public List<IngredientSimpleResponseDto> mapIngredientsToSimpleDtoList(List<IngredientEntity> ingredients) {
|
||||
List<Long> allowedIngredients = visitorService.getAllowedIngredients();
|
||||
List<Long> allowedIngredients = getAllowedIngredients();
|
||||
return ingredients.stream()
|
||||
.map(i -> mapIngredientToSimpleDto(i, allowedIngredients))
|
||||
.toList();
|
||||
|
||||
@@ -7,6 +7,9 @@ import javax.persistence.Entity;
|
||||
import javax.persistence.GeneratedValue;
|
||||
import javax.persistence.GenerationType;
|
||||
import javax.persistence.Id;
|
||||
import javax.persistence.JoinColumn;
|
||||
import javax.persistence.JoinTable;
|
||||
import javax.persistence.ManyToMany;
|
||||
import javax.persistence.OneToMany;
|
||||
import javax.persistence.Table;
|
||||
import java.util.List;
|
||||
@@ -20,7 +23,13 @@ public class BarEntity {
|
||||
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
||||
private Long id;
|
||||
private String name;
|
||||
private Boolean active;
|
||||
|
||||
@OneToMany(mappedBy = "bar")
|
||||
private List<BarIngredientStorage> ingredients;
|
||||
@ManyToMany
|
||||
@JoinTable(
|
||||
name = "bar_ingredient",
|
||||
joinColumns = @JoinColumn(name = "bar"),
|
||||
inverseJoinColumns = @JoinColumn(name = "ingredient")
|
||||
)
|
||||
private List<IngredientEntity> ingredients;
|
||||
}
|
||||
|
||||
@@ -1,25 +0,0 @@
|
||||
package ru.kayashov.bar.model.entity;
|
||||
|
||||
import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.GeneratedValue;
|
||||
import javax.persistence.GenerationType;
|
||||
import javax.persistence.Id;
|
||||
import javax.persistence.ManyToOne;
|
||||
|
||||
@Entity
|
||||
@Getter
|
||||
@Setter
|
||||
public class BarIngredientStorage {
|
||||
@Id
|
||||
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
||||
private Long id;
|
||||
|
||||
@ManyToOne
|
||||
private IngredientEntity ingredient;
|
||||
|
||||
@ManyToOne
|
||||
private BarEntity bar;
|
||||
}
|
||||
@@ -10,6 +10,8 @@ import javax.persistence.Column;
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.Id;
|
||||
import javax.persistence.JoinColumn;
|
||||
import javax.persistence.JoinTable;
|
||||
import javax.persistence.ManyToMany;
|
||||
import javax.persistence.ManyToOne;
|
||||
import javax.persistence.OneToMany;
|
||||
import javax.persistence.Table;
|
||||
@@ -40,6 +42,11 @@ public class IngredientEntity {
|
||||
@JoinColumn(name = "type")
|
||||
private TypeEntity type;
|
||||
|
||||
@OneToMany(mappedBy = "ingredient")
|
||||
private List<BarIngredientStorage> barIngredients;
|
||||
@ManyToMany
|
||||
@JoinTable(
|
||||
name = "bar_ingredient",
|
||||
joinColumns = @JoinColumn(name = "ingredient"),
|
||||
inverseJoinColumns = @JoinColumn(name = "bar")
|
||||
)
|
||||
private List<BarEntity> bars;
|
||||
}
|
||||
|
||||
@@ -3,5 +3,9 @@ package ru.kayashov.bar.repository;
|
||||
import org.springframework.data.jpa.repository.JpaRepository;
|
||||
import ru.kayashov.bar.model.entity.BarEntity;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
|
||||
public interface BarEntityRepository extends JpaRepository<BarEntity, Long> {
|
||||
Optional<BarEntity> findByActiveTrue();
|
||||
}
|
||||
@@ -1,7 +0,0 @@
|
||||
package ru.kayashov.bar.repository;
|
||||
|
||||
import org.springframework.data.jpa.repository.JpaRepository;
|
||||
import ru.kayashov.bar.model.entity.BarIngredientStorage;
|
||||
|
||||
public interface BarIngredientStorageRepository extends JpaRepository<BarIngredientStorage, Long> {
|
||||
}
|
||||
@@ -15,8 +15,8 @@ import ru.kayashov.bar.controller.dto.cocktail.CocktailModalDto;
|
||||
import ru.kayashov.bar.controller.dto.cocktail.CocktailSimpleResponseDto;
|
||||
import ru.kayashov.bar.controller.dto.cocktail.ReceiptResponseDto;
|
||||
import ru.kayashov.bar.mapper.CocktailMapper;
|
||||
import ru.kayashov.bar.model.Ingredient;
|
||||
import ru.kayashov.bar.model.entity.Alcoholic;
|
||||
import ru.kayashov.bar.model.entity.BarIngredientStorage;
|
||||
import ru.kayashov.bar.model.entity.Category;
|
||||
import ru.kayashov.bar.model.entity.CocktailEntity;
|
||||
import ru.kayashov.bar.model.entity.Glass;
|
||||
@@ -30,12 +30,14 @@ import ru.kayashov.bar.repository.ReceiptRepository;
|
||||
import javax.persistence.EntityManager;
|
||||
import javax.persistence.criteria.CriteriaBuilder;
|
||||
import javax.persistence.criteria.CriteriaQuery;
|
||||
import javax.persistence.criteria.Expression;
|
||||
import javax.persistence.criteria.Join;
|
||||
import javax.persistence.criteria.JoinType;
|
||||
import javax.persistence.criteria.Order;
|
||||
import javax.persistence.criteria.Predicate;
|
||||
import javax.persistence.criteria.Root;
|
||||
import javax.persistence.criteria.Subquery;
|
||||
import javax.print.attribute.standard.MediaSize;
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.util.ArrayList;
|
||||
@@ -72,8 +74,6 @@ public class CocktailService {
|
||||
}
|
||||
|
||||
private List<CocktailEntity> criteria(CocktailFilterRequestDto dto) {
|
||||
Visitor visitor = visitorService.getCurrentVisitor();
|
||||
|
||||
Session session = entityManager.unwrap(Session.class);
|
||||
CriteriaBuilder cb = session.getCriteriaBuilder();
|
||||
CriteriaQuery<CocktailEntity> criteriaQuery = cb.createQuery(CocktailEntity.class);
|
||||
@@ -82,14 +82,7 @@ public class CocktailService {
|
||||
|
||||
criteriaQuery.distinct(true);
|
||||
if (!dto.getAll()) {
|
||||
// Long barId = visitor.getResidents().stream()
|
||||
// .filter(BarResident::getActive)
|
||||
// .map(BarResident::getBar)
|
||||
// .map(BarEntity::getId)
|
||||
// .toList()
|
||||
// .get(0);
|
||||
Long barId = 1L;
|
||||
List<Long> cocktailIds = getAllowedCocktailIds(barId);
|
||||
List<Long> cocktailIds = findICountCocktailIds(0, new ArrayList<>());
|
||||
Predicate pr = root.get("id").in(cocktailIds);
|
||||
predicates.add(pr);
|
||||
}
|
||||
@@ -110,19 +103,19 @@ public class CocktailService {
|
||||
}
|
||||
|
||||
if (dto.getGlass() != null && !dto.getGlass().isEmpty()) {
|
||||
predicates.add(root.get("glassEntity").get("name").in(dto.getGlass()));
|
||||
predicates.add(root.get("glass").in(dto.getGlass().stream().map(Glass::findValue).toList()));
|
||||
}
|
||||
|
||||
if (dto.getCategory() != null && !dto.getCategory().isEmpty()) {
|
||||
predicates.add(root.get("categoryEntity").get("name").in(dto.getCategory()));
|
||||
predicates.add(root.get("category").in(dto.getCategory().stream().map(Category::findValue).toList()));
|
||||
}
|
||||
|
||||
if (dto.getAlcohol() != null && !dto.getAlcohol().isEmpty()) {
|
||||
predicates.add(root.get("alcoholicEntity").get("name").in(dto.getAlcohol()));
|
||||
predicates.add(root.get("alcoholic").in(dto.getAlcohol().stream().map(Alcoholic::findValue).toList()));
|
||||
}
|
||||
|
||||
if (dto.getNotHaveCount() != null) {
|
||||
List<Long> approveCocktail = findICountCocktailIds(dto.getNotHaveCount(), visitor, dto.getIngredient());
|
||||
List<Long> approveCocktail = findICountCocktailIds(dto.getNotHaveCount(), dto.getIngredient());
|
||||
predicates.add(root.get("id").in(approveCocktail));
|
||||
}
|
||||
//
|
||||
@@ -152,8 +145,8 @@ public class CocktailService {
|
||||
return cocktailEntities;
|
||||
}
|
||||
|
||||
private List<Long> findICountCocktailIds(Integer iCount, Visitor visitor, List<String> ingredientFilter) {
|
||||
List<Long> allowedIngredient = new ArrayList<>();
|
||||
private List<Long> findICountCocktailIds(Integer iCount, List<String> ingredientFilter) {
|
||||
List<Long> allowedIngredient = visitorService.getAllowedIngredients();
|
||||
|
||||
Stream<List<ReceiptEntity>> receiptStream = receiptRepository.findAll()
|
||||
.stream()
|
||||
@@ -182,46 +175,85 @@ public class CocktailService {
|
||||
.count());
|
||||
}
|
||||
|
||||
private List<Long> getAllowedCocktailIds(Long barId) {
|
||||
// private List<Long> getAllowedCocktailIds(Long barId) {
|
||||
// CriteriaBuilder cb = entityManager.getCriteriaBuilder();
|
||||
// CriteriaQuery<Long> query = cb.createQuery(Long.class);
|
||||
//
|
||||
// Root<ReceiptEntity> receiptRoot = query.from(ReceiptEntity.class);
|
||||
// Join<ReceiptEntity, IngredientEntity> ingredientJoin = receiptRoot.join("ingredient", JoinType.LEFT);
|
||||
// Join<IngredientEntity, BarEntity> barIngredientStorageJoin = ingredientJoin.join("bars", JoinType.LEFT);
|
||||
//
|
||||
// // Внешний подзапрос с NOT EXISTS
|
||||
// Subquery<Long> subquery = query.subquery(Long.class);
|
||||
// Root<ReceiptEntity> receiptSubRoot = subquery.from(ReceiptEntity.class);
|
||||
// Join<ReceiptEntity, IngredientEntity> ingredientSubJoin = receiptSubRoot.join("ingredient", JoinType.LEFT);
|
||||
//
|
||||
// // Внутренний подзапрос с NOT EXISTS
|
||||
// Subquery<Long> innerSubquery = subquery.subquery(Long.class);
|
||||
// Root<BarIngredientStorage> barIngredientStorageInnerRoot = innerSubquery.from(BarIngredientStorage.class);
|
||||
//
|
||||
// // Условия внутреннего подзапроса
|
||||
// innerSubquery.select(barIngredientStorageInnerRoot.get("id"))
|
||||
// .where(
|
||||
// cb.equal(barIngredientStorageInnerRoot.get("ingredient"), ingredientSubJoin.get("id")),
|
||||
// cb.equal(barIngredientStorageInnerRoot.get("bar").get("id"), barId)
|
||||
// );
|
||||
//
|
||||
// // Условия внешнего подзапроса
|
||||
// subquery.select(receiptSubRoot.get("id"))
|
||||
// .where(
|
||||
// cb.equal(receiptSubRoot.get("cocktail").get("id"), receiptRoot.get("cocktail").get("id")),
|
||||
// cb.not(cb.exists(innerSubquery))
|
||||
// );
|
||||
//
|
||||
// // Основной запрос
|
||||
// query.select(receiptRoot.get("cocktail").get("id"))
|
||||
// .distinct(true)
|
||||
// .where(
|
||||
// cb.equal(barIngredientStorageJoin.get("bar").get("id"), barId),
|
||||
// cb.not(cb.exists(subquery))
|
||||
// );
|
||||
//
|
||||
// return entityManager.createQuery(query).getResultList();
|
||||
// }
|
||||
|
||||
/*
|
||||
select cifc.cocktail_id
|
||||
from (select r.cocktail_id, COUNT(CASE WHEN i.is_have THEN 0 END) as false_count
|
||||
from receipt r
|
||||
left join public.ingredient i on i.id = r.ingredient_id
|
||||
group by r.cocktail_id) as cifc
|
||||
where false_count = 0
|
||||
*/
|
||||
private List<Long> findCocktailByCountNotHaveIngredient(Integer notHaveCount, List<Long> currentIngredient) {
|
||||
CriteriaBuilder cb = entityManager.getCriteriaBuilder();
|
||||
CriteriaQuery<Long> query = cb.createQuery(Long.class);
|
||||
List<Predicate> predicates = new ArrayList<>();
|
||||
CriteriaQuery<Long> mainQuery = cb.createQuery(Long.class);
|
||||
|
||||
Root<ReceiptEntity> receiptRoot = query.from(ReceiptEntity.class);
|
||||
Join<ReceiptEntity, IngredientEntity> ingredientJoin = receiptRoot.join("ingredient", JoinType.LEFT);
|
||||
Join<IngredientEntity, BarIngredientStorage> barIngredientStorageJoin = ingredientJoin.join("barIngredients", JoinType.LEFT);
|
||||
// Создаем корневые сущности
|
||||
Subquery<Long> sq = mainQuery.subquery(Long.class);
|
||||
Root<ReceiptEntity> rsq = sq.from(ReceiptEntity.class);
|
||||
Join<ReceiptEntity, IngredientEntity> ingredientJoin = rsq.join("ingredient", JoinType.LEFT);
|
||||
// Создаем подзапрос
|
||||
sq.select(cb.count(cb.selectCase(cb.isFalse(ingredientJoin.get("isHave")))))
|
||||
.groupBy(rsq.get("cocktail"))
|
||||
.having(cb.equal(sq.getSelection(), notHaveCount));
|
||||
|
||||
// Внешний подзапрос с NOT EXISTS
|
||||
Subquery<Long> subquery = query.subquery(Long.class);
|
||||
Root<ReceiptEntity> receiptSubRoot = subquery.from(ReceiptEntity.class);
|
||||
Join<ReceiptEntity, IngredientEntity> ingredientSubJoin = receiptSubRoot.join("ingredient", JoinType.LEFT);
|
||||
// Создаем внешний запрос
|
||||
mainQuery.select(rsq.get("cocktail"));
|
||||
Predicate predicate = cb.exists(sq);
|
||||
predicates.add(predicate);
|
||||
// mainQuery.where(cb.exists(sq));
|
||||
|
||||
// Внутренний подзапрос с NOT EXISTS
|
||||
Subquery<Long> innerSubquery = subquery.subquery(Long.class);
|
||||
Root<BarIngredientStorage> barIngredientStorageInnerRoot = innerSubquery.from(BarIngredientStorage.class);
|
||||
if(!currentIngredient.isEmpty()) {
|
||||
predicates.add(rsq.get("ingredient").in(currentIngredient));
|
||||
}
|
||||
|
||||
// Условия внутреннего подзапроса
|
||||
innerSubquery.select(barIngredientStorageInnerRoot.get("id"))
|
||||
.where(
|
||||
cb.equal(barIngredientStorageInnerRoot.get("ingredient"), ingredientSubJoin.get("id")),
|
||||
cb.equal(barIngredientStorageInnerRoot.get("bar").get("id"), barId)
|
||||
);
|
||||
mainQuery.where(predicates.toArray(new Predicate[0]));
|
||||
|
||||
// Условия внешнего подзапроса
|
||||
subquery.select(receiptSubRoot.get("id"))
|
||||
.where(
|
||||
cb.equal(receiptSubRoot.get("cocktail").get("id"), receiptRoot.get("cocktail").get("id")),
|
||||
cb.not(cb.exists(innerSubquery))
|
||||
);
|
||||
|
||||
// Основной запрос
|
||||
query.select(receiptRoot.get("cocktail").get("id"))
|
||||
.distinct(true)
|
||||
.where(
|
||||
cb.equal(barIngredientStorageJoin.get("bar").get("id"), barId),
|
||||
cb.not(cb.exists(subquery))
|
||||
);
|
||||
|
||||
return entityManager.createQuery(query).getResultList();
|
||||
// Выполняем запрос
|
||||
return entityManager.createQuery(mainQuery)
|
||||
.getResultList();
|
||||
}
|
||||
|
||||
public CocktailForListResponseDto findById(Long id) {
|
||||
@@ -307,7 +339,6 @@ public class CocktailService {
|
||||
receiptEntity.setIngredient(ingredient);
|
||||
receiptEntity.setCount(receipt.getCount());
|
||||
receiptEntity.setUnit(receipt.getUnit());
|
||||
// receiptEntity.setMeasure(receipt.getMeasure());
|
||||
receiptEntity.setCocktail(cocktail);
|
||||
receiptRepository.save(receiptEntity);
|
||||
}
|
||||
|
||||
@@ -6,12 +6,14 @@ import org.springframework.stereotype.Service;
|
||||
import ru.kayashov.bar.controller.dto.ingredient.IngredientResponseDto;
|
||||
import ru.kayashov.bar.controller.dto.ingredient.IngredientSimpleResponseDto;
|
||||
import ru.kayashov.bar.mapper.IngredientMapper;
|
||||
import ru.kayashov.bar.repository.BarIngredientStorageRepository;
|
||||
import ru.kayashov.bar.model.entity.BarEntity;
|
||||
import ru.kayashov.bar.model.entity.IngredientEntity;
|
||||
import ru.kayashov.bar.model.entity.TypeEntity;
|
||||
import ru.kayashov.bar.repository.BarEntityRepository;
|
||||
import ru.kayashov.bar.repository.IngredientRepository;
|
||||
import ru.kayashov.bar.repository.TypeRepository;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
@Slf4j
|
||||
@@ -19,16 +21,10 @@ import java.util.List;
|
||||
@RequiredArgsConstructor
|
||||
public class IngredientService {
|
||||
|
||||
private final VisitorService visitorService;
|
||||
private final TypeRepository typeRepository;
|
||||
private final IngredientRepository repository;
|
||||
private final IngredientMapper mapper;
|
||||
private final BarIngredientStorageRepository barIngredientStorageRepository;
|
||||
|
||||
public IngredientEntity findIngredientByName(String name) {
|
||||
return repository.findByEnNameIgnoreCase(name)
|
||||
.orElseThrow(() -> new RuntimeException("Не удалось найти ингредиент с названием " + name));
|
||||
}
|
||||
private final BarEntityRepository barEntityRepository;
|
||||
|
||||
private TypeEntity findTypeByName(String name) {
|
||||
return typeRepository.findByName(name)
|
||||
@@ -53,20 +49,15 @@ public class IngredientService {
|
||||
}
|
||||
|
||||
public void changeBarIngredient(Long id, boolean isHave) {
|
||||
// Visitor visitor = visitorService.getCurrentVisitor();
|
||||
// List<BarIngredientStorage> storage = bar.getIngredients();
|
||||
// IngredientEntity ingredientEntity = getIngredientById(id);
|
||||
// if (isHave) {
|
||||
// BarIngredientStorage entity = new BarIngredientStorage();
|
||||
// entity.setBar(bar);
|
||||
// entity.setIngredient(ingredientEntity);
|
||||
// entity = barIngredientStorageRepository.save(entity);
|
||||
// storage.add(entity);
|
||||
// } else {
|
||||
// storage.stream()
|
||||
// .filter(s -> Objects.equals(s.getIngredient().getId(), ingredientEntity.getId()))
|
||||
// .forEach(s -> barIngredientStorageRepository.deleteById(s.getId()));
|
||||
// }
|
||||
BarEntity bar = barEntityRepository.findByActiveTrue().orElseThrow();
|
||||
IngredientEntity ingredientEntity = getIngredientById(id);
|
||||
|
||||
if (isHave) {
|
||||
bar.getIngredients().add(ingredientEntity);
|
||||
} else {
|
||||
bar.getIngredients().remove(ingredientEntity);
|
||||
}
|
||||
barEntityRepository.save(bar);
|
||||
}
|
||||
|
||||
public boolean saveChange(IngredientResponseDto dto) {
|
||||
@@ -81,8 +72,6 @@ public class IngredientService {
|
||||
|
||||
entity.setName(dto.getName());
|
||||
entity.setDescription(dto.getDescription());
|
||||
// entity.setEnName(dto.getEnName());
|
||||
// entity.setIsHave(dto.isHave());
|
||||
entity.setAbv(dto.getAbv());
|
||||
entity.setAlcohol(dto.getAlcohol());
|
||||
|
||||
|
||||
@@ -3,7 +3,9 @@ package ru.kayashov.bar.service;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.springframework.security.core.context.SecurityContextHolder;
|
||||
import org.springframework.stereotype.Service;
|
||||
import ru.kayashov.bar.model.entity.IngredientEntity;
|
||||
import ru.kayashov.bar.model.entity.Visitor;
|
||||
import ru.kayashov.bar.repository.BarEntityRepository;
|
||||
import ru.kayashov.bar.repository.VisitorsRepository;
|
||||
|
||||
import java.util.ArrayList;
|
||||
@@ -14,6 +16,7 @@ import java.util.List;
|
||||
public class VisitorService {
|
||||
|
||||
private final VisitorsRepository visitorsRepository;
|
||||
private final BarEntityRepository barRepository;
|
||||
|
||||
public Visitor getCurrentVisitor() {
|
||||
Long id = ((Visitor) SecurityContextHolder.getContext().getAuthentication().getPrincipal()).getId();
|
||||
@@ -25,15 +28,10 @@ public class VisitorService {
|
||||
}
|
||||
|
||||
public List<Long> getAllowedIngredients() {
|
||||
return new ArrayList<>();
|
||||
// return getCurrentVisitor().getResidents()
|
||||
// .stream()
|
||||
// .filter(BarResident::getActive)
|
||||
// .map(BarResident::getBar)
|
||||
// .map(BarEntity::getIngredients)
|
||||
// .flatMap(List::stream)
|
||||
// .map(BarIngredientStorage::getIngredient)
|
||||
// .map(IngredientEntity::getId)
|
||||
// .toList();
|
||||
return barRepository.findByActiveTrue().orElseThrow()
|
||||
.getIngredients()
|
||||
.stream()
|
||||
.map(IngredientEntity::getId)
|
||||
.toList();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user