Compare commits
18 Commits
ba1c0e747c
...
dev
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
060188e9e0 | ||
|
|
1fa351357b | ||
|
|
a84a37c1aa | ||
|
|
74f8a0b864 | ||
|
|
b585214f58 | ||
|
|
e282681679 | ||
|
|
de96c83ddf | ||
|
|
21e44246b9 | ||
|
|
ed72511ed4 | ||
|
|
1f26015bad | ||
|
|
f573660325 | ||
|
|
9fb5cdf87c | ||
|
|
0cb15aae70 | ||
|
|
da188a65a4 | ||
|
|
1fdf084a25 | ||
|
|
d9f5a32486 | ||
|
|
8bc3501998 | ||
|
|
5abfc4d6c3 |
@@ -5,7 +5,7 @@ COPY . /build/source
|
||||
WORKDIR /build/source
|
||||
RUN mvn -am clean package
|
||||
# Создание образа my-bar
|
||||
FROM openjdk:17-jdk-slim as my-bar
|
||||
FROM openjdk:17-ea-jdk-slim as my-bar
|
||||
COPY --from=build /build/source/target/*.jar /data/app/my-bar.jar
|
||||
WORKDIR /data/app
|
||||
EXPOSE 8080
|
||||
|
||||
@@ -12,23 +12,35 @@ import Toolbar from "@mui/material/Toolbar";
|
||||
import AddCircleIcon from '@mui/icons-material/AddCircle';
|
||||
import {BarCreateModal} from "../../components/BarCreateModal";
|
||||
import PowerIcon from '@mui/icons-material/Power';
|
||||
import ContentCopyIcon from '@mui/icons-material/ContentCopy';
|
||||
import {barClient} from "../../lib/clients/BarClient";
|
||||
|
||||
export function BarChangePage() {
|
||||
const [bars, setBars] = useState([])
|
||||
const [open, setOpen] = useState(false)
|
||||
const [oldId, setOldId] = useState(null);
|
||||
const {createError, createSuccess, createWarning} = useAlert();
|
||||
|
||||
const createHandler = (name) => barClient.createBar(name, bars, createSuccess, createError, setBars, setOpen)
|
||||
const createHandler = (id, name) => {
|
||||
if (id) {
|
||||
barClient.copyBar(id, name, setBars, bars, createError, createSuccess, setOpen)
|
||||
} else {
|
||||
barClient.createBar(name, bars, createSuccess, createError, setBars, setOpen)
|
||||
}
|
||||
}
|
||||
|
||||
// eslint-disable-next-line
|
||||
useEffect(() => barClient.getBarList(setBars, createError), []);
|
||||
|
||||
return (<>
|
||||
<BarCreateModal open={open} setOpen={setOpen} create={createHandler}/>
|
||||
<BarCreateModal open={open} setOpen={setOpen} create={createHandler} id={oldId}/>
|
||||
<Paper sx={{p: 1}}>
|
||||
<Toolbar>
|
||||
<Typography variant='h6'>Списки ингредиентов (бары)</Typography>
|
||||
<IconButton edge="end" onClick={() => setOpen(true)}>
|
||||
<IconButton edge="end" onClick={() => {
|
||||
setOldId(null);
|
||||
setOpen(true);
|
||||
}}>
|
||||
<AddCircleIcon/>
|
||||
</IconButton>
|
||||
</Toolbar>
|
||||
@@ -36,18 +48,27 @@ export function BarChangePage() {
|
||||
return <Card key={b.id} sx={{m: 2, p: 2}}>
|
||||
<Stack direction='row' justifyContent={'space-between'}>
|
||||
<Typography>{b.name}</Typography>
|
||||
{b.active && <IconButton disabled>
|
||||
<PowerIcon/>
|
||||
</IconButton>}
|
||||
{!b.active && <Box>
|
||||
<IconButton onClick={() => barClient.deleteBar(b, bars, createError, createSuccess, setBars)}>
|
||||
<DeleteIcon/>
|
||||
<Box>
|
||||
<IconButton onClick={() => {
|
||||
setOldId(b.id)
|
||||
setOpen(true);
|
||||
}}>
|
||||
<ContentCopyIcon/>
|
||||
</IconButton>
|
||||
<IconButton
|
||||
onClick={() => barClient.changeBar(b.id, bars, createWarning, createSuccess, createError, setBars)}>
|
||||
<ElectricalServicesIcon/>
|
||||
</IconButton>
|
||||
</Box>}
|
||||
{b.active && <IconButton disabled>
|
||||
<PowerIcon/>
|
||||
</IconButton>}
|
||||
{!b.active && <>
|
||||
<IconButton
|
||||
onClick={() => barClient.deleteBar(b, bars, createError, createSuccess, setBars)}>
|
||||
<DeleteIcon/>
|
||||
</IconButton>
|
||||
<IconButton
|
||||
onClick={() => barClient.changeBar(b.id, bars, createWarning, createSuccess, createError, setBars)}>
|
||||
<ElectricalServicesIcon/>
|
||||
</IconButton>
|
||||
</>}
|
||||
</Box>
|
||||
</Stack>
|
||||
</Card>
|
||||
})}
|
||||
|
||||
@@ -12,7 +12,7 @@ export function CalcPage() {
|
||||
const {createError} = useAlert();
|
||||
const [cocktails, setCocktails] = React.useState([]);
|
||||
const [load, setLoad] = React.useState(false);
|
||||
const [cocktailMap, setCocktailMap] = React.useState([]);
|
||||
const [cocktailMap, setCocktailMap] = React.useState({});
|
||||
|
||||
const changeHandler = (id, value) => {
|
||||
setCocktailMap((prev) => ({
|
||||
@@ -67,6 +67,8 @@ export function CalcPage() {
|
||||
setCocktails(state);
|
||||
}
|
||||
|
||||
console.log(cocktailMap)
|
||||
|
||||
return (
|
||||
<Box padding={2}>
|
||||
<Typography variant="h4" align="center">Коктейли</Typography>
|
||||
|
||||
@@ -9,7 +9,7 @@ import {Counter} from "./Counter";
|
||||
|
||||
export function CocktailItemCalc({cocktail, deleteHandler, changeHandler}) {
|
||||
return (
|
||||
<Card sx={{mb: 1, height: '250px', display: 'relative', p: 2}}>
|
||||
<Card sx={{mb: 1, display: 'relative', p: 2}}>
|
||||
<Stack justifyContent={'start'} spacing={2}>
|
||||
<Stack direction='row' justifyContent='start' alignItems='center'>
|
||||
<Box sx={{width: '100px', height: '100px'}}>
|
||||
|
||||
@@ -3,7 +3,7 @@ import {Box, TextField, Button} from '@mui/material';
|
||||
import {styled} from '@mui/material/styles';
|
||||
|
||||
// Стилизуем контейнер счетчика
|
||||
const CounterContainer = styled(Box)`
|
||||
styled(Box)`
|
||||
display: flex;
|
||||
align-items: center;
|
||||
justify-content: center;
|
||||
|
||||
@@ -35,7 +35,8 @@ const CocktailsPageContent = () => {
|
||||
const {createError, createSuccess} = useAlert();
|
||||
const [rows, setRows] = useState([]);
|
||||
const [filter, setFilter] = useState(emptyFilter)
|
||||
const [chips, setChips] = useState([])
|
||||
// const [chips, setChips] = useState([])
|
||||
const chips = [];
|
||||
const [page, setPage] = useState(-1);
|
||||
const [load, setLoad] = useState(false);
|
||||
const [isEnd, setIsEnd] = useState(false);
|
||||
@@ -63,6 +64,7 @@ const CocktailsPageContent = () => {
|
||||
return () => window.removeEventListener('scroll', handleScroll);
|
||||
// eslint-disable-next-line
|
||||
}, [loading]);
|
||||
// eslint-disable-next-line
|
||||
useEffect(() => loading(), [filter])
|
||||
|
||||
const renderSkeleton = () => {
|
||||
@@ -122,6 +124,13 @@ const CocktailsPageContent = () => {
|
||||
|
||||
const handleSelectCocktail = (row) => selectCocktail(row.id)
|
||||
const deleteHandle = (row) => cocktailClient.deleteCocktail(row.id, rows, setRows, createSuccess, createError)
|
||||
const hideHandler = (id) => {
|
||||
cocktailClient.hiddenCocktail(id)
|
||||
.then(() => {
|
||||
createSuccess("Коктейль скрыт успешно");
|
||||
setRows(rows.filter((r) => r.id !== id))
|
||||
}).catch(() => createError("Ошибка при попытке скрыть коктейль"))
|
||||
}
|
||||
|
||||
return (
|
||||
<Box>
|
||||
@@ -156,6 +165,7 @@ const CocktailsPageContent = () => {
|
||||
handleChangeRating={handleChangeRating}
|
||||
handleSelect={handleSelectCocktail}
|
||||
deleteHandler={deleteHandle}
|
||||
hideHandler={hideHandler}
|
||||
/>
|
||||
)
|
||||
})}
|
||||
|
||||
@@ -81,6 +81,7 @@ export function EditCocktailPage() {
|
||||
// eslint-disable-next-line
|
||||
}, []);
|
||||
|
||||
// eslint-disable-next-line
|
||||
useEffect(() => cocktailClient.getOneCocktail(selected, setCocktail, getError, emptyCocktail), [selected])
|
||||
const saveHandler = () => cocktailClient.saveChangeCocktail(cocktail, createError, createSuccess)
|
||||
const deleteHandle = () => cocktailClient.deleteCocktailFromEdit(setCocktails, setCocktail, createError, cocktails, cocktail, emptyCocktail)
|
||||
|
||||
@@ -8,7 +8,7 @@ import DialogActions from "@mui/material/DialogActions";
|
||||
import Button from "@mui/material/Button";
|
||||
import TextField from "@mui/material/TextField";
|
||||
|
||||
export function BarCreateModal({open, setOpen, create}) {
|
||||
export function BarCreateModal({open, setOpen, create, id}) {
|
||||
const [value, setValue] = useState("");
|
||||
return (
|
||||
<Dialog fullWidth={true}
|
||||
@@ -26,13 +26,17 @@ export function BarCreateModal({open, setOpen, create}) {
|
||||
</DialogTitle>
|
||||
<DialogContent>
|
||||
<TextField sx={{width: '75%'}}
|
||||
label={"Название списка"} variant='outlined'
|
||||
label={<Typography pt={'4px'}>
|
||||
Название списка</Typography>} variant='outlined'
|
||||
value={!value ? "" : value}
|
||||
onChange={(e) => setValue(e.target.value)}
|
||||
/>
|
||||
</DialogContent>
|
||||
<DialogActions>
|
||||
<Button onClick={() => create(value)}>Создать</Button>
|
||||
<Button onClick={() => {
|
||||
create(id, value);
|
||||
setValue("");
|
||||
}}>Создать</Button>
|
||||
</DialogActions>
|
||||
</Dialog>
|
||||
)
|
||||
|
||||
@@ -10,6 +10,7 @@ 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 VisibilityOffIcon from '@mui/icons-material/VisibilityOff';
|
||||
import {paths} from "../../path";
|
||||
import {useAlert} from "../../hooks/useAlert";
|
||||
import {useUser} from "../../hooks/useUser";
|
||||
@@ -35,7 +36,7 @@ function renderRating(handleChangeRating, row) {
|
||||
)
|
||||
}
|
||||
|
||||
export function Cocktail({row, handleFavourite, handleChangeRating, handleSelect, deleteHandler}) {
|
||||
export function Cocktail({row, handleFavourite, handleChangeRating, handleSelect, deleteHandler, hideHandler}) {
|
||||
const {createError, createSuccess} = useAlert();
|
||||
const {user} = useUser();
|
||||
return (
|
||||
@@ -73,6 +74,9 @@ export function Cocktail({row, handleFavourite, handleChangeRating, handleSelect
|
||||
<IconButton size='small' href={`${paths.bar.cocktailEdit}?id=${row.id}`}>
|
||||
<EditIcon fontSize='small'/>
|
||||
</IconButton>
|
||||
<IconButton size='small' onClick={() => hideHandler(row.id)}>
|
||||
<VisibilityOffIcon fontSize='small'/>
|
||||
</IconButton>
|
||||
<IconButton size='small' onClick={() => deleteHandler(row)}>
|
||||
<DeleteIcon fontSize='small'/>
|
||||
</IconButton>
|
||||
|
||||
@@ -30,6 +30,7 @@ export function CocktailInfoModal({row}) {
|
||||
const [loading, setLoading] = useState(false);
|
||||
const {closeCocktail, selectIngredient, getIngredient, getOpenCocktail} = useSelect();
|
||||
|
||||
// eslint-disable-next-line
|
||||
useEffect(() => cocktailClient.getCocktailForModal(row, setLoading, setCocktail, closeCocktail, getError), [row]);
|
||||
|
||||
if (!row || !cocktail) {
|
||||
|
||||
@@ -88,10 +88,10 @@ export function FilterBlock({filter, handleFilterChange, handleClearFilter, barm
|
||||
{/*Фильтр по категории*/}
|
||||
{category.length > 0 && (
|
||||
<CheckMarks rows={category} name={"Категории"} filterValue={filter.category}
|
||||
filterName={"category"} handleChange={handleFilterChange} identity/>)}
|
||||
filterName={"category"} handleChange={handleFilterChange}/>)}
|
||||
{/*Фильтр по посуде*/}
|
||||
{glass.length > 0 && (<CheckMarks rows={glass} name={"Подача"} handleChange={handleFilterChange}
|
||||
filterValue={filter.glass} filterName={"glass"} identity/>)}
|
||||
filterValue={filter.glass} filterName={"glass"}/>)}
|
||||
{/*Фильтр по нехватке ингредиентов*/}
|
||||
{(barmen && filter.all) && (<CheckMarks rows={ingredientCount} name={"Не хватает ингредиентов"}
|
||||
handleChange={handleFilterChange}
|
||||
|
||||
@@ -58,6 +58,17 @@ class BarClient {
|
||||
setOpen(false)
|
||||
}).catch(() => createError("Ошибка создания списка"))
|
||||
}
|
||||
|
||||
copyBar(oldId, newName, setBars, bars, createError, createSuccess, setOpen) {
|
||||
api().post(requests.bar.crud + "copy/" + oldId + "/" + newName)
|
||||
.then((r) => {
|
||||
const state = bars;
|
||||
state.push(r.data)
|
||||
setBars(state);
|
||||
createSuccess("Бар скопирован")
|
||||
setOpen(false)
|
||||
}).catch(() => createError("Ошибка при копировании бара"))
|
||||
}
|
||||
}
|
||||
|
||||
export const barClient = new BarClient();
|
||||
@@ -76,8 +76,13 @@ class CocktailClient {
|
||||
return;
|
||||
}
|
||||
setCocktails(data);
|
||||
let map = [];
|
||||
map = data.map((d) => map[d.id] = 1)
|
||||
let map = {};
|
||||
data.forEach((d) => {
|
||||
map = {
|
||||
...map,
|
||||
[d.id]: 1
|
||||
}
|
||||
})
|
||||
setCocktailMap(map);
|
||||
setLoad(true);
|
||||
})
|
||||
@@ -114,6 +119,10 @@ class CocktailClient {
|
||||
.catch(() => createError("Ошибка удаления коктейля"))
|
||||
}
|
||||
|
||||
async hiddenCocktail(id) {
|
||||
return api().post(requests.cocktails.hide + id);
|
||||
}
|
||||
|
||||
saveChangeCocktail (cocktail, createError, createSuccess) {
|
||||
api().patch(requests.cocktails.basic, cocktail)
|
||||
.then((r) => {
|
||||
|
||||
@@ -70,7 +70,7 @@ class IngredientClient {
|
||||
}
|
||||
|
||||
findOne(id, selectIngredient, createError) {
|
||||
api().get(`${requests.bar.ingredient}?id=${id}`)
|
||||
api().get(`${requests.ingredient.crud}?id=${id}`)
|
||||
.then((r) => {
|
||||
selectIngredient(r.data)
|
||||
})
|
||||
|
||||
@@ -1,9 +1,9 @@
|
||||
import axios from "axios";
|
||||
import {tokenUtil} from "./TokenUtil";
|
||||
|
||||
const host = "localhost:8080"; //дебаг вместе с беком
|
||||
// const host = "localhost:8080"; //дебаг вместе с беком
|
||||
// const host = "192.168.1.100:8091"; //дебаг фронта
|
||||
// const host = "bar.kayashov.keenetic.pro"; //прод
|
||||
const host = "bar.kayashov.keenetic.pro"; //прод
|
||||
export const api = () => {
|
||||
const result = axios;
|
||||
result.defaults.baseURL = `${window.location.protocol}//${host}/`;
|
||||
|
||||
@@ -38,6 +38,7 @@ export const requests = {
|
||||
favourite: routes.cocktail + "/favourite?id=",
|
||||
rating: routes.cocktail + "/rating?id=",
|
||||
drink: routes.cocktail + "/drink",
|
||||
hide: routes.cocktail + "/hidden/",
|
||||
},
|
||||
glass: {
|
||||
list: routes.glass,
|
||||
|
||||
@@ -28,6 +28,11 @@ public class BarController {
|
||||
barService.changeActiveBar(id);
|
||||
}
|
||||
|
||||
@PostMapping("/copy/{id}/{name}")
|
||||
public BarResponseDto copyActiveBar(@PathVariable Long id, @PathVariable String name) {
|
||||
return barService.copyBar(id, name);
|
||||
}
|
||||
|
||||
@DeleteMapping("/{id}")
|
||||
public void deleteBar(@PathVariable Long id) {
|
||||
barService.deleteBar(id);
|
||||
|
||||
@@ -62,6 +62,11 @@ public class CocktailController {
|
||||
return cocktailService.savePhoto(file);
|
||||
}
|
||||
|
||||
@PostMapping("/hidden/{id}")
|
||||
public void hiddenCocktail(@PathVariable Long id) {
|
||||
cocktailService.hiddenCocktail(id);
|
||||
}
|
||||
|
||||
@GetMapping
|
||||
public CocktailForListResponseDto getOne(@RequestParam Long id) {
|
||||
return cocktailService.findById(id);
|
||||
|
||||
@@ -32,4 +32,12 @@ public class BarEntity {
|
||||
inverseJoinColumns = @JoinColumn(name = "ingredient")
|
||||
)
|
||||
private List<IngredientEntity> ingredients;
|
||||
|
||||
@ManyToMany
|
||||
@JoinTable(
|
||||
name = "hidden_cocktails",
|
||||
joinColumns = @JoinColumn(name = "bar"),
|
||||
inverseJoinColumns = @JoinColumn(name = "cocktail")
|
||||
)
|
||||
private List<CocktailEntity> hiddenCocktails;
|
||||
}
|
||||
|
||||
@@ -1,16 +1,15 @@
|
||||
package ru.kayashov.bar.model.entity;
|
||||
|
||||
import javax.persistence.Column;
|
||||
import lombok.Getter;
|
||||
import lombok.NoArgsConstructor;
|
||||
import lombok.Setter;
|
||||
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.GeneratedValue;
|
||||
import javax.persistence.GenerationType;
|
||||
import javax.persistence.Id;
|
||||
import javax.persistence.OneToMany;
|
||||
import javax.persistence.Table;
|
||||
import lombok.Getter;
|
||||
import lombok.NoArgsConstructor;
|
||||
import lombok.Setter;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@Entity
|
||||
@@ -19,17 +18,12 @@ import java.util.List;
|
||||
@Table(name = "ingredient_type")
|
||||
@NoArgsConstructor
|
||||
public class TypeEntity {
|
||||
|
||||
@Id
|
||||
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
||||
private Long id;
|
||||
private String name;
|
||||
@Column(name = "en_name")
|
||||
private String enName;
|
||||
|
||||
@OneToMany(mappedBy = "type")
|
||||
private List<IngredientEntity> ingredients;
|
||||
|
||||
public TypeEntity(String enName) {
|
||||
this.enName = enName;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,18 +1,11 @@
|
||||
package ru.kayashov.bar.repository;
|
||||
|
||||
import org.springframework.data.jpa.repository.JpaRepository;
|
||||
import org.springframework.data.jpa.repository.Query;
|
||||
import ru.kayashov.bar.model.entity.TypeEntity;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
|
||||
public interface TypeRepository extends JpaRepository<TypeEntity, Long> {
|
||||
|
||||
Optional<TypeEntity> findByEnNameIgnoreCase(String name);
|
||||
|
||||
Optional<TypeEntity> findByName(String name);
|
||||
|
||||
@Query("select i from TypeEntity i where upper(i.name) like upper(?1) or upper(i.enName) like upper(?1)")
|
||||
List<TypeEntity> findByWord(String word);
|
||||
}
|
||||
@@ -30,6 +30,7 @@ public class BarService {
|
||||
private final CocktailRepository cocktailRepository;
|
||||
private final EventService eventService;
|
||||
|
||||
@Transactional
|
||||
public void changeActiveBar(Long id) {
|
||||
Optional<BarEntity> lastBarOpt = barEntityRepository.findByActiveTrue();
|
||||
if (lastBarOpt.isPresent()) {
|
||||
@@ -55,7 +56,7 @@ public class BarService {
|
||||
.map(ingredientRepository::save)
|
||||
.toList();
|
||||
|
||||
cocktailRepository.saveAll(findAllowedCocktails(ingredients));
|
||||
cocktailRepository.saveAll(findAllowedCocktails(ingredients, barEntity.getHiddenCocktails().stream().map(CocktailEntity::getId).toList()));
|
||||
|
||||
barEntityRepository.save(barEntity);
|
||||
|
||||
@@ -92,7 +93,27 @@ public class BarService {
|
||||
return BarResponseDto.mapToDto(bar);
|
||||
}
|
||||
|
||||
private List<CocktailEntity> findAllowedCocktails(List<IngredientEntity> ingredients) {
|
||||
@Transactional
|
||||
public BarResponseDto copyBar(Long id, String name) {
|
||||
BarEntity barEntity = barEntityRepository.findById(id).orElseThrow();
|
||||
|
||||
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, List<Long> hiddenCocktails) {
|
||||
List<CocktailEntity> result = new ArrayList<>();
|
||||
for (IngredientEntity ingredient : ingredients) {
|
||||
List<CocktailEntity> cocktails = ingredient.getReceipts().stream()
|
||||
@@ -100,7 +121,8 @@ public class BarService {
|
||||
.toList();
|
||||
|
||||
for (CocktailEntity cocktail : cocktails) {
|
||||
if (cocktail.getReceipt().stream().allMatch(r -> r.getIngredient().getIsHave())) {
|
||||
if (cocktail.getReceipt().stream().allMatch(r -> r.getIngredient().getIsHave())
|
||||
&& !hiddenCocktails.contains(cocktail.getId())) {
|
||||
cocktail.setAllowed(true);
|
||||
result.add(cocktail);
|
||||
}
|
||||
|
||||
@@ -2,8 +2,6 @@ package ru.kayashov.bar.service;
|
||||
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.hibernate.Session;
|
||||
import org.hibernate.query.Query;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
@@ -18,6 +16,7 @@ import ru.kayashov.bar.controller.dto.cocktail.SortingEnum;
|
||||
import ru.kayashov.bar.controller.dto.ingredient.IngredientSimpleResponseDto;
|
||||
import ru.kayashov.bar.mapper.CocktailMapper;
|
||||
import ru.kayashov.bar.model.entity.Alcoholic;
|
||||
import ru.kayashov.bar.model.entity.BarEntity;
|
||||
import ru.kayashov.bar.model.entity.Category;
|
||||
import ru.kayashov.bar.model.entity.CocktailEntity;
|
||||
import ru.kayashov.bar.model.entity.Event;
|
||||
@@ -25,25 +24,24 @@ import ru.kayashov.bar.model.entity.EventType;
|
||||
import ru.kayashov.bar.model.entity.Glass;
|
||||
import ru.kayashov.bar.model.entity.IngredientEntity;
|
||||
import ru.kayashov.bar.model.entity.ReceiptEntity;
|
||||
import ru.kayashov.bar.repository.BarEntityRepository;
|
||||
import ru.kayashov.bar.repository.CocktailRepository;
|
||||
import ru.kayashov.bar.repository.IngredientRepository;
|
||||
import ru.kayashov.bar.repository.ReceiptRepository;
|
||||
|
||||
import javax.persistence.EntityManager;
|
||||
import javax.persistence.criteria.CriteriaBuilder;
|
||||
import javax.persistence.criteria.CriteriaQuery;
|
||||
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 java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.time.Duration;
|
||||
import java.time.Instant;
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.Comparator;
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
import java.util.Set;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
@Slf4j
|
||||
@Service
|
||||
@@ -52,6 +50,7 @@ public class CocktailService {
|
||||
|
||||
private final CocktailRepository cocktailRepository;
|
||||
private final EventService eventService;
|
||||
private final BarEntityRepository barRepository;
|
||||
@Value("${cocktail.photo.path}")
|
||||
private String photoFolder;
|
||||
|
||||
@@ -84,65 +83,6 @@ public class CocktailService {
|
||||
return mapper.cocktailsToDtoList(criteria(dto), false, true);
|
||||
}
|
||||
|
||||
private List<CocktailEntity> criteria(CocktailFilterRequestDto dto) {
|
||||
Session session = entityManager.unwrap(Session.class);
|
||||
CriteriaBuilder cb = session.getCriteriaBuilder();
|
||||
CriteriaQuery<CocktailEntity> criteriaQuery = cb.createQuery(CocktailEntity.class);
|
||||
Root<CocktailEntity> root = criteriaQuery.from(CocktailEntity.class);
|
||||
List<Predicate> predicates = new ArrayList<>();
|
||||
|
||||
criteriaQuery.distinct(true);
|
||||
if (!dto.getAll()) {
|
||||
Predicate pr = cb.isTrue(root.get("allowed"));
|
||||
predicates.add(pr);
|
||||
}
|
||||
|
||||
if (!dto.getSearch().isEmpty()) {
|
||||
String[] search = dto.getSearch().split(" ");
|
||||
List<Predicate> in = new ArrayList<>();
|
||||
Join<CocktailEntity, ReceiptEntity> receiptJoin = root.join("receipt", JoinType.LEFT);
|
||||
for (String s : search) {
|
||||
in.add(cb.like(cb.lower(root.get("name")), "%" + s.toLowerCase() + "%"));
|
||||
in.add(cb.like(cb.lower(receiptJoin.get("ingredient").get("name")), "%" + s.toLowerCase() + "%"));
|
||||
}
|
||||
predicates.add(cb.or(in.toArray(new Predicate[0])));
|
||||
}
|
||||
|
||||
if (dto.getOnlyFavourite()) {
|
||||
predicates.add(cb.isTrue(root.get("isFavorite")));
|
||||
}
|
||||
|
||||
if (dto.getGlass() != null && !dto.getGlass().isEmpty()) {
|
||||
predicates.add(root.get("glass").in(dto.getGlass().stream().map(Glass::findValue).toList()));
|
||||
}
|
||||
|
||||
if (dto.getCategory() != null && !dto.getCategory().isEmpty()) {
|
||||
predicates.add(root.get("category").in(dto.getCategory().stream().map(Category::findValue).toList()));
|
||||
}
|
||||
|
||||
if (dto.getAlcohol() != null && !dto.getAlcohol().isEmpty()) {
|
||||
predicates.add(root.get("alcoholic").in(dto.getAlcohol().stream().map(Alcoholic::findValue).toList()));
|
||||
}
|
||||
|
||||
//todo: доделать другие виды сортировки
|
||||
Order order;
|
||||
switch (dto.getSort()) {
|
||||
case NAME_ASC -> order = cb.asc(root.get("name"));
|
||||
case NAME_DESC -> order = cb.desc(root.get("name"));
|
||||
default -> order = cb.asc(root.get("name"));
|
||||
}
|
||||
|
||||
criteriaQuery.where(predicates.toArray(new Predicate[0]))
|
||||
.orderBy(order);
|
||||
Query<CocktailEntity> query = session.createQuery(criteriaQuery);
|
||||
query.setFirstResult(dto.getPage() * dto.getSize());
|
||||
query.setMaxResults(dto.getSize());
|
||||
List<CocktailEntity> cocktailEntities = query.getResultList();
|
||||
|
||||
log.info("Найдено {} коктейлей", cocktailEntities.size());
|
||||
return cocktailEntities;
|
||||
}
|
||||
|
||||
public CocktailForListResponseDto findById(Long id) {
|
||||
CocktailEntity cocktail = repository.findById(id).orElseThrow();
|
||||
return mapper.cocktailToFullDto(cocktail);
|
||||
@@ -219,11 +159,6 @@ public class CocktailService {
|
||||
repository.delete(cocktail);
|
||||
}
|
||||
|
||||
public String findInstructions(Long id) {
|
||||
CocktailEntity cocktail = repository.findById(id).orElseThrow(RuntimeException::new);
|
||||
return cocktail.getInstructions();
|
||||
}
|
||||
|
||||
public List<CocktailForIngredientModalDto> findByIngredient(Long id) {
|
||||
return ingredientRepository.findById(id).orElseThrow(RuntimeException::new)
|
||||
.getReceipts()
|
||||
@@ -234,6 +169,72 @@ public class CocktailService {
|
||||
.toList();
|
||||
}
|
||||
|
||||
public void hiddenCocktail(Long id) {
|
||||
CocktailEntity cocktail = repository.findById(id).orElseThrow(RuntimeException::new);
|
||||
|
||||
BarEntity bar = barRepository.findByActiveTrue().orElseThrow();
|
||||
bar.getHiddenCocktails().add(cocktail);
|
||||
barRepository.save(bar);
|
||||
|
||||
cocktail.setAllowed(false);
|
||||
repository.save(cocktail);
|
||||
log.info("Коктейль {} - {} был скрыт для бара {} - {}", cocktail.getId(), cocktail.getName(), bar.getId(), bar.getName());
|
||||
}
|
||||
|
||||
public void drink(Long id) {
|
||||
CocktailEntity cocktail = repository.findById(id).orElseThrow(RuntimeException::new);
|
||||
int count = cocktail.getCountDrink() + 1;
|
||||
cocktail.setCountDrink(count);
|
||||
repository.save(cocktail);
|
||||
|
||||
eventService.createEvent(Event.builder()
|
||||
.type(EventType.DRINK)
|
||||
.date(LocalDateTime.now())
|
||||
.oldState(cocktail.getName())
|
||||
.newState(String.valueOf(count))
|
||||
.build());
|
||||
}
|
||||
|
||||
//todo: написать функцию в БД, чтобы ускорить работу и сохранить читаемый код
|
||||
private List<CocktailEntity> criteria(CocktailFilterRequestDto dto) {
|
||||
Instant now = Instant.now();
|
||||
List<CocktailEntity> cocktails = repository.findAll()
|
||||
.stream()
|
||||
//todo: доделать другие виды сортировки
|
||||
.sorted(Comparator.comparing(CocktailEntity::getName))
|
||||
.filter(c -> c.getAllowed() || dto.getAll())
|
||||
.filter(c -> c.getIsFavorite() || !dto.getOnlyFavourite())
|
||||
.filter(c -> isEmpty(dto.getGlass()) || dto.getGlass().contains(c.getGlass().getName()))
|
||||
.filter(c -> isEmpty(dto.getCategory()) || dto.getCategory().contains(c.getCategory().getName()))
|
||||
.filter(c -> isEmpty(dto.getAlcohol()) || dto.getAlcohol().contains(c.getAlcoholic().getValue()))
|
||||
.filter(c -> dto.getSearch().isEmpty() || textSearchFilter(c, dto.getSearch()))
|
||||
.skip((long) dto.getPage() * dto.getSize())
|
||||
.limit(dto.getSize())
|
||||
.toList();
|
||||
log.info("Найдено {} коктейлей за {} ms", cocktails.size(), Duration.between(now, Instant.now()).toMillis());
|
||||
return cocktails;
|
||||
}
|
||||
|
||||
private boolean textSearchFilter(CocktailEntity cocktail, String founding) {
|
||||
String[] search = founding.split(" ");
|
||||
|
||||
if (Arrays.stream(search).anyMatch(s -> cocktail.getName().toLowerCase().contains(s.toLowerCase()))) {
|
||||
return true;
|
||||
}
|
||||
|
||||
Set<String> ingredientsNames = cocktail.getReceipt().stream()
|
||||
.map(ReceiptEntity::getIngredient)
|
||||
.map(IngredientEntity::getName)
|
||||
.map(String::toLowerCase)
|
||||
.collect(Collectors.toSet());
|
||||
|
||||
return Arrays.stream(search).anyMatch(s -> ingredientsNames.contains(s.toLowerCase()));
|
||||
}
|
||||
|
||||
private boolean isEmpty(List<?> value) {
|
||||
return value == null || value.isEmpty();
|
||||
}
|
||||
|
||||
//todo: попробовать отыграть эту связку каскадами
|
||||
private void editCocktailReceipts(List<ReceiptEntity> old, List<ReceiptResponseDto> actual, CocktailEntity cocktail) {
|
||||
for (ReceiptResponseDto receipt : actual) {
|
||||
@@ -299,18 +300,65 @@ public class CocktailService {
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
public void drink(Long id) {
|
||||
CocktailEntity cocktail = repository.findById(id).orElseThrow(RuntimeException::new);
|
||||
int count = cocktail.getCountDrink() + 1;
|
||||
cocktail.setCountDrink(count);
|
||||
repository.save(cocktail);
|
||||
|
||||
eventService.createEvent(Event.builder()
|
||||
.type(EventType.DRINK)
|
||||
.date(LocalDateTime.now())
|
||||
.oldState(cocktail.getName())
|
||||
.newState(String.valueOf(count))
|
||||
.build());
|
||||
//todo: оставить до написания функции в БД
|
||||
// private List<CocktailEntity> criteria(CocktailFilterRequestDto dto) {
|
||||
// Instant now = Instant.now();
|
||||
// Session session = entityManager.unwrap(Session.class);
|
||||
// CriteriaBuilder cb = session.getCriteriaBuilder();
|
||||
// CriteriaQuery<CocktailEntity> criteriaQuery = cb.createQuery(CocktailEntity.class);
|
||||
// Root<CocktailEntity> root = criteriaQuery.from(CocktailEntity.class);
|
||||
// List<Predicate> predicates = new ArrayList<>();
|
||||
//
|
||||
// criteriaQuery.distinct(true);
|
||||
// if (!dto.getAll()) {
|
||||
// Predicate pr = cb.isTrue(root.get("allowed"));
|
||||
// predicates.add(pr);
|
||||
// }
|
||||
//
|
||||
// if (!dto.getSearch().isEmpty()) {
|
||||
// String[] search = dto.getSearch().split(" ");
|
||||
// List<Predicate> in = new ArrayList<>();
|
||||
// Join<CocktailEntity, ReceiptEntity> receiptJoin = root.join("receipt", JoinType.LEFT);
|
||||
// for (String s : search) {
|
||||
// in.add(cb.like(cb.lower(root.get("name")), "%" + s.toLowerCase() + "%"));
|
||||
// in.add(cb.like(cb.lower(receiptJoin.get("ingredient").get("name")), "%" + s.toLowerCase() + "%"));
|
||||
// }
|
||||
// predicates.add(cb.or(in.toArray(new Predicate[0])));
|
||||
// }
|
||||
//
|
||||
// if (dto.getOnlyFavourite()) {
|
||||
// predicates.add(cb.isTrue(root.get("isFavorite")));
|
||||
// }
|
||||
//
|
||||
// if (dto.getGlass() != null && !dto.getGlass().isEmpty()) {
|
||||
// predicates.add(root.get("glass").in(dto.getGlass().stream().map(Glass::findValue).toList()));
|
||||
// }
|
||||
//
|
||||
// if (dto.getCategory() != null && !dto.getCategory().isEmpty()) {
|
||||
// predicates.add(root.get("category").in(dto.getCategory().stream().map(Category::findValue).toList()));
|
||||
// }
|
||||
//
|
||||
// if (dto.getAlcohol() != null && !dto.getAlcohol().isEmpty()) {
|
||||
// predicates.add(root.get("alcoholic").in(dto.getAlcohol().stream().map(Alcoholic::findValue).toList()));
|
||||
// }
|
||||
|
||||
}
|
||||
// //todo: доделать другие виды сортировки
|
||||
// Order order;
|
||||
// switch (dto.getSort()) {
|
||||
// case NAME_ASC -> order = cb.asc(root.get("name"));
|
||||
// case NAME_DESC -> order = cb.desc(root.get("name"));
|
||||
// default -> order = cb.asc(root.get("name"));
|
||||
// }
|
||||
//
|
||||
// criteriaQuery.where(predicates.toArray(new Predicate[0]))
|
||||
// .orderBy(order);
|
||||
// Query<CocktailEntity> query = session.createQuery(criteriaQuery);
|
||||
// query.setFirstResult(dto.getPage() * dto.getSize());
|
||||
// query.setMaxResults(dto.getSize());
|
||||
// List<CocktailEntity> cocktailEntities = query.getResultList();
|
||||
//
|
||||
// log.info("Найдено {} коктейлей за {} ms", cocktailEntities.size(), Duration.between(now, Instant.now()).toMillis());
|
||||
// return cocktailEntities;
|
||||
// }
|
||||
}
|
||||
|
||||
@@ -3,9 +3,9 @@ spring.application.name=myBar
|
||||
cocktail.photo.path=${COCKTAIL_PHOTO_PATH:/mnt/sdb1/my-bar-front/build/assets/cocktails}
|
||||
|
||||
spring.datasource.driver-class-name=org.postgresql.Driver
|
||||
spring.datasource.url=${DB_URL:jdbc:postgresql://192.168.1.100:5432/drinks}
|
||||
spring.datasource.username=${DB_NAME:nextcloud}
|
||||
spring.datasource.password=${DB_PASSWORD:kayash73}
|
||||
spring.datasource.url=${DB_URL:jdbc:postgresql://localhost:5433/drink}
|
||||
spring.datasource.username=${DB_NAME:postgres}
|
||||
spring.datasource.password=${DB_PASSWORD:pgpass}
|
||||
spring.datasource.hikari.minimum-idle=15
|
||||
spring.datasource.hikari.maximum-pool-size=50
|
||||
spring.datasource.hikari.idle-timeout=30000
|
||||
@@ -15,4 +15,6 @@ spring.jpa.generate-ddl=true
|
||||
|
||||
token.signing.key=${SIGNING_KEY:ThisIsKayashovBarSecretKey-1.0.0Version}
|
||||
|
||||
spring.jpa.show-sql=false
|
||||
spring.jpa.show-sql=false
|
||||
|
||||
#server.port=8081
|
||||
Reference in New Issue
Block a user