временные изменения
This commit is contained in:
@@ -191,23 +191,13 @@ const CocktailsPageContent = () => {
|
||||
const handleSelectCocktail = (row) => {
|
||||
selectCocktail(row.id)
|
||||
}
|
||||
const handleEditMenu = (row, value) => {
|
||||
const newState = rows.map((r) => {
|
||||
if (r.id !== row.id) {
|
||||
return r;
|
||||
}
|
||||
if (filter.all) {
|
||||
return {
|
||||
...r,
|
||||
inMenu: value
|
||||
}
|
||||
}
|
||||
return null
|
||||
}).filter((r) => r !== null);
|
||||
|
||||
api().post(`${requests.cocktails.menu}?id=${row.id}&value=${value}`)
|
||||
.then(() => setRows(newState))
|
||||
.catch(() => createError("Ошибка сохранения данных"))
|
||||
const deleteHandle = (row) => {
|
||||
api().delete(requests.cocktails.cocktail + row.id)
|
||||
.then(() => {
|
||||
setRows(rows.filter((r) => r.id !== row.id))
|
||||
createSuccess("Коктейль удален")
|
||||
})
|
||||
.catch(() => createError("Ошибка удаления коктейля"))
|
||||
}
|
||||
|
||||
return (
|
||||
@@ -242,6 +232,7 @@ const CocktailsPageContent = () => {
|
||||
<Cocktail key={row.id} row={row} handleFavourite={handleFavourite}
|
||||
handleChangeRating={handleChangeRating}
|
||||
handleSelect={handleSelectCocktail}
|
||||
deleteHandler={deleteHandle}
|
||||
/>
|
||||
)
|
||||
})}
|
||||
|
||||
@@ -1,22 +1,22 @@
|
||||
import {CardActions, CardContent, CardMedia, Rating} from "@mui/material";
|
||||
import {useAlert} from "../../hooks/useAlert";
|
||||
import Typography from "@mui/material/Typography";
|
||||
import Button from "@mui/material/Button";
|
||||
import Grid from "@mui/material/Grid";
|
||||
import {requests} from "../../requests";
|
||||
import {CocktailItemStyled} from "./CocktailItemStyled";
|
||||
import IconButton from "@mui/material/IconButton";
|
||||
import FavoriteBorderIcon from '@mui/icons-material/FavoriteTwoTone';
|
||||
import FavoriteIcon from '@mui/icons-material/Favorite';
|
||||
import {api} from "../../lib/clients/api";
|
||||
import Box from "@mui/material/Box";
|
||||
import {useUser} from "../../hooks/useUser";
|
||||
import {CocktailDescription} from "./CocktailDescription";
|
||||
import EditIcon from '@mui/icons-material/Edit';
|
||||
import DeleteIcon from '@mui/icons-material/Delete';
|
||||
import LocalBarIcon from '@mui/icons-material/LocalBar';
|
||||
import {paths} from "../../path";
|
||||
|
||||
function renderFavouriteBadge(handleFavourite, row) {
|
||||
const childIcon = row.rating.favourite ? <FavoriteIcon color='error'/> : <FavoriteBorderIcon color={'warning'}/>;
|
||||
const childIcon = row.rating.favourite ? <FavoriteIcon/> : <FavoriteBorderIcon/>;
|
||||
return (
|
||||
<IconButton sx={{position: 'absolute', top: "15px", right: "15px"}} onClick={() => handleFavourite(row)}>
|
||||
<IconButton size={'small'}
|
||||
onClick={() => handleFavourite(row)}>
|
||||
{childIcon}
|
||||
</IconButton>
|
||||
)
|
||||
@@ -25,16 +25,14 @@ function renderFavouriteBadge(handleFavourite, row) {
|
||||
function renderRating(handleChangeRating, row) {
|
||||
return (
|
||||
<Rating
|
||||
sx={{position: 'absolute', top: '310px', right: '85px'}}
|
||||
name="simple-controlled"
|
||||
size="large"
|
||||
value={row.rating.rating}
|
||||
onChange={(event, newValue) => handleChangeRating(row, newValue)}
|
||||
/>
|
||||
)
|
||||
}
|
||||
|
||||
export function Cocktail({row, handleFavourite, handleChangeRating, handleSelect}) {
|
||||
export function Cocktail({row, handleFavourite, handleChangeRating, handleSelect, deleteHandler}) {
|
||||
return (
|
||||
<Grid item sx={{pr: 2}}>
|
||||
<CocktailItemStyled>
|
||||
@@ -56,15 +54,25 @@ export function Cocktail({row, handleFavourite, handleChangeRating, handleSelect
|
||||
|
||||
image={row.image.includes("thecocktaildb") ? (row.image + "/preview") : row.image}
|
||||
/>
|
||||
{renderFavouriteBadge(handleFavourite, row)}
|
||||
{renderRating(handleChangeRating, row)}
|
||||
<CardContent sx={{pb: '4px', pl: 2}}>
|
||||
<CardActions>
|
||||
<IconButton sx={{m: 0}} size='small'>
|
||||
<LocalBarIcon fontSize='small'/>
|
||||
</IconButton>
|
||||
{renderFavouriteBadge(handleFavourite, row)}
|
||||
{renderRating(handleChangeRating, row)}
|
||||
|
||||
<IconButton size={'small'} href={`${paths.bar.cocktailEdit}?id=${row.id}`}>
|
||||
<EditIcon fontSize={'small'}/>
|
||||
</IconButton>
|
||||
<IconButton size={'small'} onClick={() => deleteHandler(row)}>
|
||||
<DeleteIcon fontSize={'small'}/>
|
||||
</IconButton>
|
||||
</CardActions>
|
||||
<CardContent sx={{pb: 0, pl: 2, pt: 0}}>
|
||||
<Typography variant="h5" minHeight={'50px'} mt={2}>{row.name} </Typography>
|
||||
<CocktailDescription row={row}/>
|
||||
</CardContent>
|
||||
<CardActions>
|
||||
<Button variant={'contained'} color={"error"}>Удалить</Button>
|
||||
</CardActions>
|
||||
|
||||
</Box>
|
||||
</CocktailItemStyled>
|
||||
</Grid>
|
||||
|
||||
@@ -19,36 +19,9 @@ export function FilterBlock({filter, handleFilterChange, handleClearFilter, barm
|
||||
const {createError} = useAlert();
|
||||
const [glass, setGlass] = useState([]);
|
||||
const [category, setCategory] = useState([]);
|
||||
const alcohol = [
|
||||
{
|
||||
name: "Алкогольный",
|
||||
id: "alcohol1"
|
||||
},
|
||||
{
|
||||
name: "Безалкогольный",
|
||||
id: "alcohol2"
|
||||
}];
|
||||
const ingredientCount = [
|
||||
{
|
||||
id: "1IngredientCount",
|
||||
name: 1
|
||||
},
|
||||
{
|
||||
id: "2IngredientCount",
|
||||
name: 2
|
||||
},
|
||||
{
|
||||
id: "3IngredientCount",
|
||||
name: 3
|
||||
},
|
||||
{
|
||||
id: "4IngredientCount",
|
||||
name: 4
|
||||
},
|
||||
{
|
||||
id: "5IngredientCount",
|
||||
name: 5
|
||||
}]
|
||||
const alcohol = ['Алкогольный', 'Безалкогольный'];
|
||||
const ingredientCount = [1,2,3,4,5];
|
||||
const sort = ['Название по возрастанию', 'Название по убыванию'];
|
||||
|
||||
useEffect(() => {
|
||||
api().get(requests.bar.category)
|
||||
@@ -88,8 +61,8 @@ export function FilterBlock({filter, handleFilterChange, handleClearFilter, barm
|
||||
<Box hidden={filter.hidden}>
|
||||
<Grid container>
|
||||
{/*Фильтр по алкогольности*/}
|
||||
<CheckMarks rows={sortList} name={"Сортировать по..."} handleChange={handleFilterChange}
|
||||
filterValue={filter.sorting} filterName={"sorting"} nonMulti/>
|
||||
<CheckMarks rows={sort} name={"Сортировать по..."} handleChange={handleFilterChange}
|
||||
filterValue={filter.sorting} filterName={"sorting"} nonMulti identity/>
|
||||
</Grid>
|
||||
</Box>
|
||||
{/*Блок фильтров*/}
|
||||
@@ -117,18 +90,18 @@ export function FilterBlock({filter, handleFilterChange, handleClearFilter, barm
|
||||
/>
|
||||
{/*Фильтр по алкогольности*/}
|
||||
<CheckMarks rows={alcohol} name={"Алкогольность"} handleChange={handleFilterChange}
|
||||
filterValue={filter.alcohol} filterName={"alcohol"}/>
|
||||
filterValue={filter.alcohol} filterName={"alcohol"} identity/>
|
||||
{/*Фильтр по категории*/}
|
||||
{category.length > 0 && (
|
||||
<CheckMarks rows={category} name={"Категории"} filterValue={filter.category}
|
||||
filterName={"category"} handleChange={handleFilterChange}/>)}
|
||||
filterName={"category"} handleChange={handleFilterChange} identity/>)}
|
||||
{/*Фильтр по посуде*/}
|
||||
{glass.length > 0 && (<CheckMarks rows={glass} name={"Подача"} handleChange={handleFilterChange}
|
||||
filterValue={filter.glass} filterName={"glass"}/>)}
|
||||
filterValue={filter.glass} filterName={"glass"} identity/>)}
|
||||
{/*Фильтр по нехватке ингредиентов*/}
|
||||
{(barmen && filter.all) && (<CheckMarks rows={ingredientCount} name={"Не хватает ингредиентов"}
|
||||
handleChange={handleFilterChange}
|
||||
nonMulti nullValue
|
||||
nonMulti nullValue identity
|
||||
filterValue={filter.iCount} filterName={"iCount"}/>)}
|
||||
<Button onClick={() => handleClearFilter()}>Сбросить</Button>
|
||||
</Grid>
|
||||
|
||||
@@ -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