удалены основные сущности бек
This commit is contained in:
@@ -1,39 +0,0 @@
|
||||
package ru.kayashov.bar.config;
|
||||
|
||||
import org.apache.kafka.clients.producer.ProducerConfig;
|
||||
import org.apache.kafka.common.serialization.StringSerializer;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.kafka.core.DefaultKafkaProducerFactory;
|
||||
import org.springframework.kafka.core.KafkaTemplate;
|
||||
import org.springframework.kafka.core.ProducerFactory;
|
||||
import ru.kayashov.bar.model.AbstractSendMessage;
|
||||
import ru.kayashov.bar.service.AbstractMessageSerializer;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
@Configuration
|
||||
public class KafkaConfig {
|
||||
|
||||
@Value("${spring.kafka.bootstrap-servers}")
|
||||
private String bootstrapServers;
|
||||
|
||||
@Bean
|
||||
public ProducerFactory<String, AbstractSendMessage> producerFactory() {
|
||||
Map<String, Object> configProps = new HashMap<>();
|
||||
configProps.put(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG, bootstrapServers);
|
||||
configProps.put(ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG, StringSerializer.class);
|
||||
configProps.put(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG, AbstractMessageSerializer.class);
|
||||
configProps.put(ProducerConfig.MAX_REQUEST_SIZE_CONFIG, 31457280);
|
||||
return new DefaultKafkaProducerFactory<>(configProps);
|
||||
}
|
||||
|
||||
@Bean
|
||||
public KafkaTemplate<String, AbstractSendMessage> kafkaTemplate(ProducerFactory<String, AbstractSendMessage> factory) {
|
||||
KafkaTemplate<String, AbstractSendMessage> broker = new KafkaTemplate<>(factory);
|
||||
broker.flush();
|
||||
return broker;
|
||||
}
|
||||
}
|
||||
@@ -2,33 +2,23 @@ package ru.kayashov.bar.controller;
|
||||
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.hibernate.Session;
|
||||
import org.springframework.web.bind.annotation.CrossOrigin;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.PatchMapping;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
import org.springframework.web.bind.annotation.RequestBody;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RequestParam;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
import ru.kayashov.bar.controller.dto.SessionResponseDto;
|
||||
import ru.kayashov.bar.controller.dto.VisitorResponseDto;
|
||||
import ru.kayashov.bar.controller.dto.bar.BarResponseDto;
|
||||
import ru.kayashov.bar.controller.dto.bar.CategoryResponseDto;
|
||||
import ru.kayashov.bar.controller.dto.bar.GlassResponseDto;
|
||||
import ru.kayashov.bar.controller.dto.bar.TagResponseDto;
|
||||
import ru.kayashov.bar.controller.dto.cocktail.ReceiptResponseDto;
|
||||
import ru.kayashov.bar.model.entity.BarResident;
|
||||
import ru.kayashov.bar.model.entity.SessionEntity;
|
||||
import ru.kayashov.bar.model.entity.Category;
|
||||
import ru.kayashov.bar.model.entity.Glass;
|
||||
import ru.kayashov.bar.model.entity.Unit;
|
||||
import ru.kayashov.bar.model.entity.UnitRepository;
|
||||
import ru.kayashov.bar.model.entity.UserRole;
|
||||
import ru.kayashov.bar.model.entity.Visitor;
|
||||
import ru.kayashov.bar.repository.SessionRepository;
|
||||
import ru.kayashov.bar.repository.TagRepository;
|
||||
import ru.kayashov.bar.repository.UnitRepository;
|
||||
import ru.kayashov.bar.service.SessionService;
|
||||
import ru.kayashov.bar.service.VisitorService;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
@Slf4j
|
||||
@@ -39,51 +29,22 @@ import java.util.List;
|
||||
public class BarController {
|
||||
|
||||
private final SessionService sessionService;
|
||||
private final TagRepository tagRepository;
|
||||
private final SessionRepository sessionRepository;
|
||||
private final VisitorService visitorService;
|
||||
private final UnitRepository unitRepository;
|
||||
|
||||
@GetMapping("/list")
|
||||
public List<BarResponseDto> getBarList(@RequestParam Boolean my) {
|
||||
return sessionService.getBarList(my);
|
||||
}
|
||||
|
||||
@GetMapping("/units")
|
||||
public List<Unit> getUnitList() {
|
||||
return unitRepository.findAll();
|
||||
}
|
||||
|
||||
@PostMapping("/addToMyList")
|
||||
public void addToMyList(@RequestBody BarResponseDto dto) {
|
||||
sessionService.addToMyList(dto);
|
||||
}
|
||||
|
||||
@PatchMapping("/enter")
|
||||
public void enterChange(@RequestParam Long id, @RequestParam Boolean value) {
|
||||
sessionService.enterChange(id, value);
|
||||
}
|
||||
|
||||
@GetMapping("tags")
|
||||
public List<TagResponseDto> getTags() {
|
||||
return tagRepository.findAll()
|
||||
.stream()
|
||||
.map(TagResponseDto::mapToDto)
|
||||
.toList();
|
||||
}
|
||||
|
||||
@GetMapping("glass")
|
||||
public List<GlassResponseDto> getGlass() {
|
||||
return sessionService.getGlassList().stream()
|
||||
.map(GlassResponseDto::mapToDto)
|
||||
.toList();
|
||||
public List<String> getGlass() {
|
||||
return Arrays.stream(Glass.values()).map(Glass::getName).toList();
|
||||
}
|
||||
|
||||
@GetMapping("category")
|
||||
public List<CategoryResponseDto> getCategory() {
|
||||
return sessionService.getCategoryList().stream()
|
||||
.map(CategoryResponseDto::mapToDto)
|
||||
.toList();
|
||||
public List<String> getCategory() {
|
||||
return Arrays.stream(Category.values()).map(Category::getName).toList();
|
||||
}
|
||||
|
||||
@GetMapping("receipt")
|
||||
@@ -91,53 +52,21 @@ public class BarController {
|
||||
return sessionService.getReceiptList(id);
|
||||
}
|
||||
|
||||
@GetMapping("session")
|
||||
public Long getSession() {
|
||||
return sessionService.findActiveSession().getId();
|
||||
}
|
||||
|
||||
@GetMapping("session/info")
|
||||
public SessionResponseDto getSessionInfo() {
|
||||
SessionEntity session = sessionService.findActiveSession();
|
||||
return SessionResponseDto.mapToDto(session);
|
||||
}
|
||||
|
||||
@PostMapping("session")
|
||||
public void changeSessionStatus(@RequestParam Boolean value) {
|
||||
if (value) {
|
||||
SessionEntity entity = sessionService.createEmptySession();
|
||||
log.info("Открыта смена {}", entity.getId());
|
||||
return;
|
||||
}
|
||||
SessionEntity session = sessionService.findActiveSession();
|
||||
session.setIsActive(false);
|
||||
sessionRepository.save(session);
|
||||
log.info("Закрыта смена {}", session.getId());
|
||||
}
|
||||
|
||||
@GetMapping("/getMe")
|
||||
public VisitorResponseDto getMe() {
|
||||
Visitor visitor = visitorService.getCurrentVisitor();
|
||||
BarResident resident = visitor.getResidents().stream().filter(BarResident::getActive).findFirst().orElse(null);
|
||||
String role;
|
||||
Boolean invited;
|
||||
boolean active;
|
||||
if(resident != null) {
|
||||
role = resident.getRole().toString();
|
||||
invited = resident.getInvited();
|
||||
active = resident.getBar().getSessions().stream().anyMatch(SessionEntity::getIsActive);
|
||||
} else {
|
||||
role = UserRole.USER.toString();
|
||||
invited = false;
|
||||
active = false;
|
||||
}
|
||||
role = UserRole.ADMIN.toString();
|
||||
invited = true;
|
||||
active = true;
|
||||
VisitorResponseDto dto = VisitorResponseDto.mapToDto(visitor, invited, role, active);
|
||||
log.info("Запрос информации о пользователе: {}-{} {}, {}вошел в бар{},в роли {}",
|
||||
log.info("Запрос информации о пользователе: {}-{} {}, вошел в бар{},в роли {}",
|
||||
dto.getId(),
|
||||
dto.getName().strip(),
|
||||
dto.getLastName() != null ? dto.getLastName().strip() : "",
|
||||
invited ? "" : "не ",
|
||||
resident != null ? " " + resident.getBar().getId() + "-" + resident.getBar().getName() : "",
|
||||
role);
|
||||
return dto;
|
||||
}
|
||||
|
||||
@@ -77,16 +77,6 @@ public class CocktailController {
|
||||
return cocktailService.getReceipts(id);
|
||||
}
|
||||
|
||||
@PostMapping("menuEdit")
|
||||
public ResponseEntity<ErrorDto> inMenuEdit(@RequestParam Long id, @RequestParam Boolean value) {
|
||||
try {
|
||||
cocktailService.inMenuEdit(id, value);
|
||||
return ResponseEntity.ok(new ErrorDto(null));
|
||||
} catch (Exception e) {
|
||||
return ResponseEntity.ok(new ErrorDto(e.getMessage()));
|
||||
}
|
||||
}
|
||||
|
||||
@GetMapping("/modal")
|
||||
public CocktailModalDto getForModal(@RequestParam Long id) {
|
||||
return cocktailService.getForModal(id);
|
||||
@@ -110,16 +100,12 @@ public class CocktailController {
|
||||
|
||||
@PutMapping("/favourite")
|
||||
public void addInFavourite(@RequestParam("id") Long id) {
|
||||
Long visitorId = ((Visitor) SecurityContextHolder.getContext().getAuthentication().getPrincipal())
|
||||
.getId();
|
||||
cocktailService.editFavourite(id, visitorId, true);
|
||||
cocktailService.editFavourite(id, true);
|
||||
}
|
||||
|
||||
@DeleteMapping("/favourite")
|
||||
public void deleteFromFavourite(@RequestParam("id") Long id) {
|
||||
Long visitorId = ((Visitor) SecurityContextHolder.getContext().getAuthentication().getPrincipal())
|
||||
.getId();
|
||||
cocktailService.editFavourite(id, visitorId, false);
|
||||
cocktailService.editFavourite(id, false);
|
||||
}
|
||||
|
||||
@PostMapping("/rating")
|
||||
|
||||
@@ -1,86 +0,0 @@
|
||||
package ru.kayashov.bar.controller;
|
||||
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.security.core.context.SecurityContextHolder;
|
||||
import org.springframework.web.bind.annotation.CrossOrigin;
|
||||
import org.springframework.web.bind.annotation.DeleteMapping;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
import org.springframework.web.bind.annotation.PutMapping;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RequestParam;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
import ru.kayashov.bar.controller.dto.OrderResponseDto;
|
||||
import ru.kayashov.bar.model.AbstractSendMessage;
|
||||
import ru.kayashov.bar.model.entity.BarEntity;
|
||||
import ru.kayashov.bar.model.entity.BarResident;
|
||||
import ru.kayashov.bar.model.entity.SessionEntity;
|
||||
import ru.kayashov.bar.model.entity.Visitor;
|
||||
import ru.kayashov.bar.service.KafkaSender;
|
||||
import ru.kayashov.bar.service.OrderService;
|
||||
import ru.kayashov.bar.service.VisitorService;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
import java.util.concurrent.ExecutorService;
|
||||
|
||||
@Slf4j
|
||||
@CrossOrigin(origins = {"*"})
|
||||
@RestController
|
||||
@RequestMapping("/api/order")
|
||||
@RequiredArgsConstructor
|
||||
public class OrderController {
|
||||
|
||||
@Value("${kafka-topic}")
|
||||
private String topic;
|
||||
|
||||
private final ExecutorService executor;
|
||||
private final OrderService orderService;
|
||||
private final KafkaSender kafkaSender;
|
||||
private final VisitorService visitorService;
|
||||
|
||||
@PostMapping
|
||||
public void pay(@RequestParam Long cocktail) {
|
||||
Long id = ((Visitor) SecurityContextHolder.getContext().getAuthentication().getPrincipal()).getId();
|
||||
List<AbstractSendMessage> messages = orderService.createOrder(id, cocktail);
|
||||
executor.submit(() -> messages.forEach(m -> kafkaSender.send(topic, m)));
|
||||
}
|
||||
|
||||
@DeleteMapping
|
||||
public void cancelOrder(@RequestParam Long id) {
|
||||
AbstractSendMessage message = orderService.updateOrder(false, id);
|
||||
executor.submit(() -> kafkaSender.send(topic, message));
|
||||
}
|
||||
|
||||
@PutMapping
|
||||
public void doneOrder(@RequestParam Long id) {
|
||||
AbstractSendMessage message = orderService.updateOrder(true, id);
|
||||
executor.submit(() -> kafkaSender.send(topic, message));
|
||||
}
|
||||
|
||||
@GetMapping
|
||||
public List<OrderResponseDto> getOrders() {
|
||||
return orderService.getOrders()
|
||||
.stream()
|
||||
.map(OrderResponseDto::new)
|
||||
.toList();
|
||||
}
|
||||
|
||||
@GetMapping("/my")
|
||||
public List<OrderResponseDto> getMyOrders() {
|
||||
Visitor visitor = visitorService.getCurrentVisitor();
|
||||
return visitor.getResidents().stream()
|
||||
.filter(BarResident::getActive)
|
||||
.map(BarResident::getBar)
|
||||
.map(BarEntity::getSessions)
|
||||
.flatMap(List::stream)
|
||||
.filter(SessionEntity::getIsActive)
|
||||
.map(SessionEntity::getOrders)
|
||||
.flatMap(List::stream)
|
||||
.filter(o -> Objects.equals(o.getVisitor().getId(), visitor.getId()))
|
||||
.map(OrderResponseDto::new)
|
||||
.toList();
|
||||
}
|
||||
}
|
||||
@@ -1,36 +0,0 @@
|
||||
package ru.kayashov.bar.controller;
|
||||
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.web.bind.annotation.CrossOrigin;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RequestParam;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
import ru.kayashov.bar.controller.dto.VisitorResponseDto;
|
||||
import ru.kayashov.bar.repository.VisitorsRepository;
|
||||
import ru.kayashov.bar.service.VisitorService;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
@Slf4j
|
||||
@CrossOrigin(origins = {"*"})
|
||||
@RestController
|
||||
@RequestMapping("/api/visitors")
|
||||
@RequiredArgsConstructor
|
||||
public class VisitorController {
|
||||
|
||||
private final VisitorService visitorService;
|
||||
|
||||
@GetMapping
|
||||
public List<VisitorResponseDto> getVisitors() {
|
||||
return visitorService.findAll();
|
||||
}
|
||||
|
||||
@PostMapping("/invite")
|
||||
public void invite(@RequestParam Boolean value, @RequestParam Long id) {
|
||||
visitorService.invited(value, id);
|
||||
}
|
||||
}
|
||||
@@ -1,24 +0,0 @@
|
||||
package ru.kayashov.bar.controller.dto;
|
||||
|
||||
import lombok.Getter;
|
||||
import lombok.NoArgsConstructor;
|
||||
import lombok.Setter;
|
||||
import ru.kayashov.bar.controller.dto.cocktail.CocktailSimpleResponseDto;
|
||||
import ru.kayashov.bar.model.entity.Pay;
|
||||
|
||||
@Getter
|
||||
@Setter
|
||||
@NoArgsConstructor
|
||||
public class OrderResponseDto {
|
||||
private Long id;
|
||||
private CocktailSimpleResponseDto cocktail;
|
||||
private VisitorResponseDto visitor;
|
||||
private String status;
|
||||
|
||||
public OrderResponseDto(Pay pay) {
|
||||
id = pay.getId();
|
||||
cocktail = CocktailSimpleResponseDto.mapToDto(pay.getCocktail());
|
||||
visitor = VisitorResponseDto.mapToDto(pay.getVisitor());
|
||||
status = pay.getStatus().toString();
|
||||
}
|
||||
}
|
||||
@@ -1,21 +0,0 @@
|
||||
package ru.kayashov.bar.controller.dto;
|
||||
|
||||
import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
import ru.kayashov.bar.model.entity.SessionEntity;
|
||||
|
||||
@Getter
|
||||
@Setter
|
||||
public class SessionResponseDto {
|
||||
|
||||
private Long id;
|
||||
private Boolean isActive;
|
||||
|
||||
|
||||
public static SessionResponseDto mapToDto(SessionEntity session) {
|
||||
SessionResponseDto dto = new SessionResponseDto();
|
||||
dto.setId(session.getId());
|
||||
dto.setIsActive(session.getIsActive());
|
||||
return dto;
|
||||
}
|
||||
}
|
||||
@@ -1,19 +0,0 @@
|
||||
package ru.kayashov.bar.controller.dto.bar;
|
||||
|
||||
import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
import ru.kayashov.bar.model.entity.CategoryEntity;
|
||||
|
||||
@Getter
|
||||
@Setter
|
||||
public class CategoryResponseDto {
|
||||
private int id;
|
||||
private String name;
|
||||
|
||||
public static CategoryResponseDto mapToDto(CategoryEntity entity) {
|
||||
CategoryResponseDto dto = new CategoryResponseDto();
|
||||
dto.setId(entity.getId());
|
||||
dto.setName(entity.getName());
|
||||
return dto;
|
||||
}
|
||||
}
|
||||
@@ -1,19 +0,0 @@
|
||||
package ru.kayashov.bar.controller.dto.bar;
|
||||
|
||||
import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
import ru.kayashov.bar.model.entity.GlassEntity;
|
||||
|
||||
@Getter
|
||||
@Setter
|
||||
public class GlassResponseDto {
|
||||
private int id;
|
||||
private String name;
|
||||
|
||||
public static GlassResponseDto mapToDto(GlassEntity entity) {
|
||||
GlassResponseDto dto = new GlassResponseDto();
|
||||
dto.setId(entity.getId());
|
||||
dto.setName(entity.getName());
|
||||
return dto;
|
||||
}
|
||||
}
|
||||
@@ -1,19 +0,0 @@
|
||||
package ru.kayashov.bar.controller.dto.bar;
|
||||
|
||||
import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
import ru.kayashov.bar.model.entity.TagEntity;
|
||||
|
||||
@Getter
|
||||
@Setter
|
||||
public class TagResponseDto {
|
||||
private Long id;
|
||||
private String name;
|
||||
|
||||
public static TagResponseDto mapToDto(TagEntity e) {
|
||||
TagResponseDto dto = new TagResponseDto();
|
||||
dto.setId(e.getId());
|
||||
dto.setName(e.getName());
|
||||
return dto;
|
||||
}
|
||||
}
|
||||
@@ -6,8 +6,6 @@ import lombok.Getter;
|
||||
import lombok.NoArgsConstructor;
|
||||
import lombok.Setter;
|
||||
import ru.kayashov.bar.model.entity.CocktailEntity;
|
||||
import ru.kayashov.bar.model.entity.Rating;
|
||||
import ru.kayashov.bar.model.entity.TagEntity;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
@@ -48,43 +46,17 @@ public class CocktailForListResponseDto {
|
||||
d.setVideo(e.getVideo());
|
||||
d.setInstructions(e.getInstructions());
|
||||
|
||||
d.setCategory(e.getCategoryEntity().getName());
|
||||
d.setAlcoholic(e.getAlcoholicEntity().getName());
|
||||
d.setGlass(e.getGlassEntity().getName());
|
||||
|
||||
// d.setIsAllowed(e.getIsAllowed());
|
||||
|
||||
d.setTags(e.getTags().stream().map(TagEntity::getName).collect(Collectors.joining(",")));
|
||||
d.setCategory(e.getCategory().getName());
|
||||
d.setAlcoholic(e.getAlcoholic().getValue());
|
||||
d.setGlass(e.getGlass().getName());
|
||||
|
||||
String components = e.getReceipt().stream().map(r -> r.getIngredient().getName()).collect(Collectors.joining(", "));
|
||||
d.setComponents(components);
|
||||
d.setReceipt(e.getReceipt().stream().map(ReceiptResponseDto::mapToDto).toList());
|
||||
|
||||
RatingResponseDto rating = new RatingResponseDto();
|
||||
int sum = 0;
|
||||
int count = 0;
|
||||
for (Rating current : e.getRating()) {
|
||||
//если у данного пользователя есть рейтинг для этого коктейля
|
||||
if (current.getVisitor().getId().equals(visitorId)) {
|
||||
//присваиваем избранное для конкретного пользователя
|
||||
rating.setFavourite(current.isFavorite());
|
||||
//проверяем, ставил данный пользователь свою оценку этому коктейлю, если да, берем ее и возвращаем рейтинг
|
||||
if (current.getRating() != 0) {
|
||||
rating.setRating(current.getRating());
|
||||
d.setRating(rating);
|
||||
return d;
|
||||
}
|
||||
}
|
||||
//если пользователь ставил оценку, добавляем ее к сумме остальных оценок и увеличиваем количество оценивших
|
||||
if (current.getRating() > 0) {
|
||||
sum += current.getRating();
|
||||
count++;
|
||||
}
|
||||
}
|
||||
//если после всех итераций есть какая-то сумма оценок - вычисляем среднюю
|
||||
if (sum > 0) {
|
||||
rating.setRating(sum / count);
|
||||
}
|
||||
rating.setFavourite(e.getIsFavorite());
|
||||
rating.setRating(e.getRating());
|
||||
d.setRating(rating);
|
||||
return d;
|
||||
}
|
||||
|
||||
@@ -5,20 +5,14 @@ import org.springframework.security.core.context.SecurityContextHolder;
|
||||
import org.springframework.stereotype.Component;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
import ru.kayashov.bar.controller.dto.cocktail.CocktailForIngredientModalDto;
|
||||
import ru.kayashov.bar.controller.dto.cocktail.CocktailModalDto;
|
||||
import ru.kayashov.bar.controller.dto.cocktail.CocktailForListResponseDto;
|
||||
import ru.kayashov.bar.controller.dto.ingredient.IngredientSimpleResponseDto;
|
||||
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.model.entity.BarEntity;
|
||||
import ru.kayashov.bar.model.entity.BarIngredientStorage;
|
||||
import ru.kayashov.bar.model.entity.BarResident;
|
||||
import ru.kayashov.bar.controller.dto.ingredient.IngredientSimpleResponseDto;
|
||||
import ru.kayashov.bar.model.entity.CocktailEntity;
|
||||
import ru.kayashov.bar.model.entity.IngredientEntity;
|
||||
import ru.kayashov.bar.model.entity.Rating;
|
||||
import ru.kayashov.bar.model.entity.ReceiptEntity;
|
||||
import ru.kayashov.bar.model.entity.StopList;
|
||||
import ru.kayashov.bar.model.entity.TagEntity;
|
||||
import ru.kayashov.bar.model.entity.Visitor;
|
||||
import ru.kayashov.bar.repository.VisitorsRepository;
|
||||
|
||||
@@ -38,7 +32,7 @@ public class CocktailMapper {
|
||||
// if(checkUserNotInBar(visitor)) {
|
||||
// return new ArrayList<>();
|
||||
// }
|
||||
List<Long> barStopList = getStopList(visitor);
|
||||
List<Long> barStopList = new ArrayList<>();
|
||||
List<Long> allowedIngredients = getAllowedIngredients(visitor);
|
||||
return cocktails.stream()
|
||||
.map(c -> cocktailToDto(c, visitor, allowedIngredients, barStopList))
|
||||
@@ -53,55 +47,46 @@ public class CocktailMapper {
|
||||
.id(e.getId())
|
||||
.name(e.getName())
|
||||
.image(e.getImage())
|
||||
.category(e.getCategoryEntity().getName())
|
||||
.alcoholic(e.getAlcoholicEntity().getName())
|
||||
.glass(e.getGlassEntity().getName())
|
||||
.tags(containCocktailTags(e.getTags()))
|
||||
.category(e.getCategory().getName())
|
||||
.alcoholic(e.getAlcoholic().getValue())
|
||||
.glass(e.getGlass().getName())
|
||||
.instructions(e.getInstructions())
|
||||
.video(e.getVideo())
|
||||
.receipt(createReceiptDtoList(e.getReceipt(), allowed))
|
||||
.build();
|
||||
}
|
||||
|
||||
private boolean checkUserNotInBar(Visitor visitor) {
|
||||
return visitor.getResidents().stream()
|
||||
.filter(BarResident::getActive)
|
||||
.map(BarResident::getBar)
|
||||
.toList()
|
||||
.isEmpty();
|
||||
}
|
||||
|
||||
private CocktailForListResponseDto cocktailToDto(CocktailEntity e, Visitor visitor, List<Long> allowedIngredients, List<Long> barStopList) {
|
||||
boolean hasError = false;
|
||||
int volume = 0;
|
||||
Float abv = 0f;
|
||||
Integer p = null;
|
||||
for(ReceiptEntity receipt : e.getReceipt()) {
|
||||
if(receipt.getCount() == null) {
|
||||
for (ReceiptEntity receipt : e.getReceipt()) {
|
||||
if (receipt.getCount() == null) {
|
||||
hasError = true;
|
||||
break;
|
||||
}
|
||||
if(receipt.getUnit() == null) {
|
||||
if (receipt.getUnit() == null) {
|
||||
hasError = true;
|
||||
break;
|
||||
}
|
||||
if(!receipt.getUnit().getId().equals(74L) && abv != null) {
|
||||
if (!receipt.getUnit().getId().equals(74L) && abv != null) {
|
||||
abv = null;
|
||||
}
|
||||
if(abv != null) {
|
||||
if (abv != null) {
|
||||
IngredientEntity ingredient = receipt.getIngredient();
|
||||
if(ingredient.getAlcohol() && ingredient.getAbv() == null) {
|
||||
if (ingredient.getAlcohol() && ingredient.getAbv() == null) {
|
||||
hasError = true;
|
||||
break;
|
||||
}
|
||||
if(ingredient.getAlcohol()) {
|
||||
if (ingredient.getAlcohol()) {
|
||||
abv += ingredient.getAbv() * receipt.getCount();
|
||||
}
|
||||
volume += receipt.getCount();
|
||||
}
|
||||
}
|
||||
|
||||
if(!hasError && abv != null) {
|
||||
if (!hasError && abv != null) {
|
||||
p = Math.round(abv / volume);
|
||||
}
|
||||
|
||||
@@ -111,38 +96,27 @@ public class CocktailMapper {
|
||||
.image(e.getImage())
|
||||
.hasError(hasError)
|
||||
.volume(p)
|
||||
.category(e.getCategoryEntity().getName())
|
||||
.alcoholic(e.getAlcoholicEntity().getName())
|
||||
.glass(e.getGlassEntity().getName())
|
||||
.tags(containCocktailTags(e.getTags()))
|
||||
.category(e.getCategory().getName())
|
||||
.alcoholic(e.getAlcoholic().getValue())
|
||||
.glass(e.getGlass().getName())
|
||||
.components(containCocktailComponents(e.getReceipt()))
|
||||
.rating(createRatingDto(e.getRating(), visitor))
|
||||
.rating(createRatingDto(e))
|
||||
.isAllowed(calculateAllowed(e.getReceipt(), allowedIngredients))
|
||||
.inMenu(!barStopList.contains(e.getId()))
|
||||
.build();
|
||||
// d.setReceipt(e.getReceipt().stream().map(ReceiptResponseDto::mapToDto).toList());
|
||||
}
|
||||
|
||||
private List<Long> getStopList(Visitor visitor) {
|
||||
return visitor.getResidents().stream()
|
||||
.filter(BarResident::getActive)
|
||||
.map(BarResident::getBar)
|
||||
.map(BarEntity::getStops)
|
||||
.flatMap(List::stream)
|
||||
.map(StopList::getCocktail)
|
||||
.map(CocktailEntity::getId)
|
||||
.toList();
|
||||
}
|
||||
|
||||
private List<Long> getAllowedIngredients(Visitor visitor) {
|
||||
return visitor.getResidents().stream()
|
||||
.filter(BarResident::getActive)
|
||||
.map(BarResident::getBar)
|
||||
.map(BarEntity::getIngredients)
|
||||
.flatMap(List::stream)
|
||||
.map(BarIngredientStorage::getIngredient)
|
||||
.map(IngredientEntity::getId)
|
||||
.toList();
|
||||
return new ArrayList<>();
|
||||
// return visitor.getResidents().stream()
|
||||
// .filter(BarResident::getActive)
|
||||
// .map(BarResident::getBar)
|
||||
// .map(BarEntity::getIngredients)
|
||||
// .flatMap(List::stream)
|
||||
// .map(BarIngredientStorage::getIngredient)
|
||||
// .map(IngredientEntity::getId)
|
||||
// .toList();
|
||||
}
|
||||
|
||||
private Boolean calculateAllowed(List<ReceiptEntity> e, List<Long> allowedIngredients) {
|
||||
@@ -151,25 +125,10 @@ public class CocktailMapper {
|
||||
.allMatch(i -> allowedIngredients.contains(i.getId()));
|
||||
}
|
||||
|
||||
private RatingResponseDto createRatingDto(List<Rating> rating, Visitor visitor) {
|
||||
private RatingResponseDto createRatingDto(CocktailEntity entity) {
|
||||
RatingResponseDto result = new RatingResponseDto();
|
||||
int sum = 0;
|
||||
int count = 0;
|
||||
for (Rating current : rating) {
|
||||
if (current.getVisitor().getId().equals(visitor.getId())) {
|
||||
result.setFavourite(current.isFavorite());
|
||||
if (current.getRating() != 0) {
|
||||
result.setRating(current.getRating());
|
||||
return result;
|
||||
}
|
||||
} else if (current.getRating() > 0) {
|
||||
sum += current.getRating();
|
||||
count++;
|
||||
}
|
||||
}
|
||||
if (sum > 0) {
|
||||
result.setRating(sum / count);
|
||||
}
|
||||
result.setRating(entity.getRating());
|
||||
result.setFavourite(entity.getIsFavorite());
|
||||
return result;
|
||||
}
|
||||
|
||||
@@ -180,12 +139,6 @@ public class CocktailMapper {
|
||||
.collect(Collectors.joining(", "));
|
||||
}
|
||||
|
||||
private String containCocktailTags(List<TagEntity> tags) {
|
||||
return tags.stream()
|
||||
.map(TagEntity::getName)
|
||||
.collect(Collectors.joining(","));
|
||||
}
|
||||
|
||||
private Visitor getCurrentVisitor() {
|
||||
Long visitorId = ((Visitor) SecurityContextHolder.getContext().getAuthentication().getPrincipal()).getId();
|
||||
return visitorsRepository.findById(visitorId).orElseThrow(RuntimeException::new);
|
||||
@@ -199,7 +152,7 @@ public class CocktailMapper {
|
||||
.name(e.getName())
|
||||
.image(e.getImage())
|
||||
.instructions(e.getInstructions())
|
||||
.rating(createRatingDto(e.getRating(), visitor))
|
||||
.rating(createRatingDto(e))
|
||||
.receipt(createReceiptDtoList(e.getReceipt(), allowedIngredients))
|
||||
.build();
|
||||
}
|
||||
@@ -226,12 +179,11 @@ public class CocktailMapper {
|
||||
|
||||
|
||||
public CocktailForIngredientModalDto cocktailToIngredientDtoList(CocktailEntity e) {
|
||||
Visitor visitor = getCurrentVisitor();
|
||||
return CocktailForIngredientModalDto.builder()
|
||||
.id(e.getId())
|
||||
.name(e.getName())
|
||||
.image(e.getImage())
|
||||
.rating(createRatingDto(e.getRating(), visitor))
|
||||
.rating(createRatingDto(e))
|
||||
.build();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,11 +0,0 @@
|
||||
package ru.kayashov.bar.model;
|
||||
|
||||
import lombok.Builder;
|
||||
import lombok.Getter;
|
||||
|
||||
@Builder
|
||||
@Getter
|
||||
public class AbstractSendMessage {
|
||||
private Long chatId;
|
||||
private String message;
|
||||
}
|
||||
24
src/main/java/ru/kayashov/bar/model/entity/Alcoholic.java
Normal file
24
src/main/java/ru/kayashov/bar/model/entity/Alcoholic.java
Normal file
@@ -0,0 +1,24 @@
|
||||
package ru.kayashov.bar.model.entity;
|
||||
|
||||
|
||||
import lombok.Getter;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
|
||||
@Getter
|
||||
@RequiredArgsConstructor
|
||||
public enum Alcoholic {
|
||||
ALCOHOLIC("Алкогольный"),
|
||||
NON_ALCOHOLIC("Безалкогольный");
|
||||
|
||||
private final String value;
|
||||
|
||||
public static Alcoholic findValue(String value) {
|
||||
for (Alcoholic alcoholic : Alcoholic.values()) {
|
||||
if (alcoholic.getValue().equals(value)) {
|
||||
return alcoholic;
|
||||
}
|
||||
}
|
||||
throw new RuntimeException("Не существует значения " + value);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,27 +0,0 @@
|
||||
package ru.kayashov.bar.model.entity;
|
||||
|
||||
import javax.persistence.Column;
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.Id;
|
||||
import javax.persistence.OneToMany;
|
||||
import javax.persistence.Table;
|
||||
import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@Getter
|
||||
@Setter
|
||||
@Entity
|
||||
@Table(name = "alcoholic")
|
||||
public class AlcoholicEntity {
|
||||
@Id
|
||||
private Integer id;
|
||||
@Column(name = "en_name")
|
||||
private String enName;
|
||||
private String name;
|
||||
|
||||
@OneToMany(mappedBy = "alcoholicEntity")
|
||||
private List<CocktailEntity> cocktails;
|
||||
|
||||
}
|
||||
@@ -21,15 +21,6 @@ public class BarEntity {
|
||||
private Long id;
|
||||
private String name;
|
||||
|
||||
@OneToMany(mappedBy = "bar")
|
||||
private List<SessionEntity> sessions;
|
||||
|
||||
@OneToMany(mappedBy = "bar")
|
||||
private List<BarIngredientStorage> ingredients;
|
||||
|
||||
@OneToMany(mappedBy = "bar")
|
||||
private List<BarResident> visitors;
|
||||
|
||||
@OneToMany(mappedBy = "bar")
|
||||
private List<StopList> stops;
|
||||
}
|
||||
|
||||
@@ -22,7 +22,4 @@ public class BarIngredientStorage {
|
||||
|
||||
@ManyToOne
|
||||
private BarEntity bar;
|
||||
|
||||
//todo: с заделом на будущее
|
||||
private String measure;
|
||||
}
|
||||
|
||||
@@ -1,36 +0,0 @@
|
||||
package ru.kayashov.bar.model.entity;
|
||||
|
||||
import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.EnumType;
|
||||
import javax.persistence.Enumerated;
|
||||
import javax.persistence.GeneratedValue;
|
||||
import javax.persistence.GenerationType;
|
||||
import javax.persistence.Id;
|
||||
import javax.persistence.ManyToOne;
|
||||
|
||||
import static ru.kayashov.bar.model.entity.UserRole.USER;
|
||||
|
||||
@Entity
|
||||
@Getter
|
||||
@Setter
|
||||
public class BarResident {
|
||||
|
||||
@Id
|
||||
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
||||
private Long id;
|
||||
|
||||
@ManyToOne
|
||||
private Visitor visitor;
|
||||
|
||||
@ManyToOne
|
||||
private BarEntity bar;
|
||||
|
||||
private Boolean invited = true;
|
||||
private Boolean active = true;
|
||||
|
||||
@Enumerated(value = EnumType.STRING)
|
||||
private UserRole role = USER;
|
||||
}
|
||||
31
src/main/java/ru/kayashov/bar/model/entity/Category.java
Normal file
31
src/main/java/ru/kayashov/bar/model/entity/Category.java
Normal file
@@ -0,0 +1,31 @@
|
||||
package ru.kayashov.bar.model.entity;
|
||||
|
||||
import lombok.Getter;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
|
||||
@Getter
|
||||
@RequiredArgsConstructor
|
||||
public enum Category {
|
||||
COCKTAIL("Коктейль"),
|
||||
PUNCH("Пунш"),
|
||||
SHAKE("Шейк"),
|
||||
OTHER("Другие"),
|
||||
BEER("Пиво"),
|
||||
HOMEMADE_LIQUEUR("Домашний ликер"),
|
||||
SHOT("Шот"),
|
||||
COCOA("Какао"),
|
||||
COFFEE_TEA("Кофе-Чай"),
|
||||
SOFT("Безалкогольный напиток"),
|
||||
ORDINARY("Обычный напиток");
|
||||
|
||||
private final String name;
|
||||
|
||||
public static Category findValue(String name) {
|
||||
for (Category category : Category.values()) {
|
||||
if (category.getName().equals(name)) {
|
||||
return category;
|
||||
}
|
||||
}
|
||||
throw new IllegalArgumentException();
|
||||
}
|
||||
}
|
||||
@@ -1,26 +0,0 @@
|
||||
package ru.kayashov.bar.model.entity;
|
||||
|
||||
import javax.persistence.Column;
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.Id;
|
||||
import javax.persistence.OneToMany;
|
||||
import javax.persistence.Table;
|
||||
import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@Getter
|
||||
@Setter
|
||||
@Entity
|
||||
@Table(name = "category")
|
||||
public class CategoryEntity {
|
||||
@Id
|
||||
private Integer id;
|
||||
@Column(name = "en_name")
|
||||
private String enName;
|
||||
private String name;
|
||||
|
||||
@OneToMany(mappedBy = "categoryEntity")
|
||||
private List<CocktailEntity> cocktails;
|
||||
}
|
||||
@@ -9,12 +9,11 @@ import lombok.Setter;
|
||||
import javax.persistence.CascadeType;
|
||||
import javax.persistence.Column;
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.EnumType;
|
||||
import javax.persistence.Enumerated;
|
||||
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.ManyToOne;
|
||||
import javax.persistence.OneToMany;
|
||||
import javax.persistence.Table;
|
||||
@@ -35,35 +34,28 @@ public class CocktailEntity {
|
||||
private String image;
|
||||
private String video;
|
||||
|
||||
@OneToMany(mappedBy = "cocktail", cascade = CascadeType.REMOVE)
|
||||
private List<StopList> stopLists;
|
||||
|
||||
@Column(columnDefinition = "text")
|
||||
private String instructions;
|
||||
|
||||
@ManyToOne
|
||||
private CategoryEntity categoryEntity;
|
||||
@ManyToOne
|
||||
private AlcoholicEntity alcoholicEntity;
|
||||
@ManyToOne
|
||||
private GlassEntity glassEntity;
|
||||
@Enumerated(EnumType.STRING)
|
||||
private Category category;
|
||||
|
||||
@Enumerated(EnumType.STRING)
|
||||
private Alcoholic alcoholic;
|
||||
|
||||
@Enumerated(EnumType.STRING)
|
||||
private Glass glass;
|
||||
|
||||
private Integer rating;
|
||||
|
||||
@Column(name = "favourite")
|
||||
private Boolean isFavorite;
|
||||
|
||||
@OneToMany(mappedBy = "cocktail", cascade = CascadeType.REMOVE)
|
||||
private List<ReceiptEntity> receipt;
|
||||
|
||||
@ManyToMany
|
||||
@JoinTable(
|
||||
name = "cocktail_tags",
|
||||
joinColumns = @JoinColumn(name = "cocktail_id"),
|
||||
inverseJoinColumns = @JoinColumn(name = "tag_id")
|
||||
)
|
||||
private List<TagEntity> tags;
|
||||
|
||||
@OneToMany(mappedBy = "cocktail", cascade = {CascadeType.PERSIST, CascadeType.REMOVE})
|
||||
private List<Rating> rating;
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return id + "-" + name + "-" + alcoholicEntity.getId();
|
||||
return id + "-" + name + "-" + alcoholic.getValue();
|
||||
}
|
||||
}
|
||||
|
||||
36
src/main/java/ru/kayashov/bar/model/entity/Glass.java
Normal file
36
src/main/java/ru/kayashov/bar/model/entity/Glass.java
Normal file
@@ -0,0 +1,36 @@
|
||||
package ru.kayashov.bar.model.entity;
|
||||
|
||||
import lombok.Getter;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
|
||||
@Getter
|
||||
@RequiredArgsConstructor
|
||||
public enum Glass {
|
||||
HIGHBALL("Хайболл"),
|
||||
COCKTAIL("Коктейльный бокал"),
|
||||
COLLINS("Стакан"),
|
||||
POUSSE("Рюмка на ножке"),
|
||||
CHAMPAGNE("Бокал флюте"),
|
||||
BRANDY("Коньячный бокал"),
|
||||
HURRICANE("Ураган"),
|
||||
COFFEE("Кофейная кружка"),
|
||||
SHOT("Рюмка"),
|
||||
JAR("Банка"),
|
||||
PITCHER("Кувшин"),
|
||||
CORDIAL("Ликерная рюмка"),
|
||||
BEER("Пивной бокал"),
|
||||
WINE("Бокал для вина"),
|
||||
MARGARITA("Бокал Маргарита");
|
||||
|
||||
private final String name;
|
||||
|
||||
public static Glass findValue(String name) {
|
||||
for (Glass glass : Glass.values()) {
|
||||
if (glass.name.equals(name)) {
|
||||
return glass;
|
||||
}
|
||||
}
|
||||
throw new RuntimeException("Не существует значение " + name);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,29 +0,0 @@
|
||||
package ru.kayashov.bar.model.entity;
|
||||
|
||||
import javax.persistence.Column;
|
||||
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.Setter;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@Getter
|
||||
@Setter
|
||||
@Entity
|
||||
@Table(name = "glass")
|
||||
public class GlassEntity {
|
||||
@Id
|
||||
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
||||
private Integer id;
|
||||
@Column(name = "en_name", nullable = false, length = Integer.MAX_VALUE)
|
||||
private String enName;
|
||||
private String name;
|
||||
|
||||
@OneToMany(mappedBy = "glassEntity")
|
||||
private List<CocktailEntity> cocktails;
|
||||
}
|
||||
@@ -1,5 +0,0 @@
|
||||
package ru.kayashov.bar.model.entity;
|
||||
|
||||
public enum OrderStatus {
|
||||
NEW, PROCESS, DONE, CANCEL
|
||||
}
|
||||
@@ -1,37 +0,0 @@
|
||||
package ru.kayashov.bar.model.entity;
|
||||
|
||||
import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.EnumType;
|
||||
import javax.persistence.Enumerated;
|
||||
import javax.persistence.GeneratedValue;
|
||||
import javax.persistence.GenerationType;
|
||||
import javax.persistence.Id;
|
||||
import javax.persistence.ManyToOne;
|
||||
import java.time.LocalDateTime;
|
||||
|
||||
@Entity
|
||||
@Getter
|
||||
@Setter
|
||||
public class Pay {
|
||||
@Id
|
||||
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
||||
private Long id;
|
||||
|
||||
@ManyToOne
|
||||
private CocktailEntity cocktail;
|
||||
|
||||
@ManyToOne
|
||||
private Visitor visitor;
|
||||
|
||||
@Enumerated(EnumType.STRING)
|
||||
private OrderStatus status;
|
||||
|
||||
@ManyToOne
|
||||
private SessionEntity session;
|
||||
|
||||
private LocalDateTime createdAt;
|
||||
private LocalDateTime closedAt;
|
||||
}
|
||||
@@ -1,30 +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 Rating {
|
||||
|
||||
@Id
|
||||
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
||||
private Long id;
|
||||
|
||||
private int rating;
|
||||
|
||||
private boolean isFavorite;
|
||||
|
||||
@ManyToOne
|
||||
private CocktailEntity cocktail;
|
||||
|
||||
@ManyToOne
|
||||
private Visitor visitor;
|
||||
}
|
||||
@@ -1,30 +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;
|
||||
import javax.persistence.OneToMany;
|
||||
import javax.persistence.Table;
|
||||
import java.util.List;
|
||||
|
||||
@Entity
|
||||
@Getter
|
||||
@Setter
|
||||
@Table(name = "session")
|
||||
public class SessionEntity {
|
||||
@Id
|
||||
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
||||
private Long id;
|
||||
private Boolean isActive;
|
||||
|
||||
@ManyToOne
|
||||
private BarEntity bar;
|
||||
|
||||
@OneToMany(mappedBy = "session")
|
||||
private List<Pay> orders;
|
||||
}
|
||||
@@ -1,26 +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 StopList {
|
||||
|
||||
@Id
|
||||
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
||||
private Long id;
|
||||
|
||||
@ManyToOne
|
||||
private BarEntity bar;
|
||||
|
||||
@ManyToOne
|
||||
private CocktailEntity cocktail;
|
||||
}
|
||||
@@ -1,38 +0,0 @@
|
||||
package ru.kayashov.bar.model.entity;
|
||||
|
||||
import javax.persistence.Column;
|
||||
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.Table;
|
||||
import lombok.Getter;
|
||||
import lombok.NoArgsConstructor;
|
||||
import lombok.Setter;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@Entity
|
||||
@Table(name = "tag")
|
||||
@Getter
|
||||
@Setter
|
||||
@NoArgsConstructor
|
||||
public class TagEntity {
|
||||
@Id
|
||||
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
||||
private Long id;
|
||||
private String name;
|
||||
@Column(name = "en_name")
|
||||
private String enName;
|
||||
|
||||
@ManyToMany()
|
||||
@JoinTable(
|
||||
name = "cocktail_tags",
|
||||
inverseJoinColumns = @JoinColumn(name = "cocktail_id"),
|
||||
joinColumns = @JoinColumn(name = "tag_id")
|
||||
)
|
||||
private List<CocktailEntity> cocktails;
|
||||
}
|
||||
@@ -12,7 +12,6 @@ import javax.persistence.OneToMany;
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
@Entity
|
||||
@Getter
|
||||
@@ -27,20 +26,15 @@ public class Visitor implements UserDetails {
|
||||
private String password;
|
||||
private LocalDateTime expired;
|
||||
|
||||
@OneToMany(cascade = CascadeType.ALL, mappedBy = "visitor")
|
||||
private List<Rating> rating;
|
||||
|
||||
@OneToMany(mappedBy = "visitor")
|
||||
private List<BarResident> residents;
|
||||
|
||||
@Override
|
||||
public Collection<? extends GrantedAuthority> getAuthorities() {
|
||||
return residents.stream()
|
||||
.filter(BarResident::getActive)
|
||||
.map(BarResident::getRole)
|
||||
.map(UserRole::getAuthorities)
|
||||
.flatMap(Collection::stream)
|
||||
.collect(Collectors.toSet());
|
||||
return UserRole.ADMIN.getAuthorities();
|
||||
// return residents.stream()
|
||||
// .filter(BarResident::getActive)
|
||||
// .map(BarResident::getRole)
|
||||
// .map(UserRole::getAuthorities)
|
||||
// .flatMap(Collection::stream)
|
||||
// .collect(Collectors.toSet());
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -1,11 +0,0 @@
|
||||
package ru.kayashov.bar.repository;
|
||||
|
||||
import org.springframework.data.jpa.repository.JpaRepository;
|
||||
import ru.kayashov.bar.model.entity.AlcoholicEntity;
|
||||
|
||||
import java.util.Optional;
|
||||
|
||||
public interface AlcoholicRepository extends JpaRepository<AlcoholicEntity, Integer> {
|
||||
|
||||
Optional<AlcoholicEntity> findByName(String name);
|
||||
}
|
||||
@@ -1,7 +0,0 @@
|
||||
package ru.kayashov.bar.repository;
|
||||
|
||||
import org.springframework.data.jpa.repository.JpaRepository;
|
||||
import ru.kayashov.bar.model.entity.BarResident;
|
||||
|
||||
public interface BarResidentRepository extends JpaRepository<BarResident, Long> {
|
||||
}
|
||||
@@ -1,11 +0,0 @@
|
||||
package ru.kayashov.bar.repository;
|
||||
|
||||
import org.springframework.data.jpa.repository.JpaRepository;
|
||||
import ru.kayashov.bar.model.entity.CategoryEntity;
|
||||
|
||||
import java.util.Optional;
|
||||
|
||||
public interface CategoryRepository extends JpaRepository<CategoryEntity, Integer> {
|
||||
|
||||
Optional<CategoryEntity> findByNameIgnoreCase(String name);
|
||||
}
|
||||
@@ -1,7 +1,20 @@
|
||||
package ru.kayashov.bar.repository;
|
||||
|
||||
import org.springframework.data.jpa.repository.JpaRepository;
|
||||
import org.springframework.data.jpa.repository.Modifying;
|
||||
import org.springframework.data.jpa.repository.Query;
|
||||
import org.springframework.data.repository.query.Param;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
import ru.kayashov.bar.model.entity.CocktailEntity;
|
||||
|
||||
public interface CocktailRepository extends JpaRepository<CocktailEntity, Long> {
|
||||
@Modifying
|
||||
@Transactional
|
||||
@Query("UPDATE CocktailEntity c SET c.isFavorite = :favorite WHERE c.id = :id")
|
||||
void updateFavouriteById(@Param("id") Long id, @Param("favorite") Boolean favorite);
|
||||
|
||||
@Modifying
|
||||
@Transactional
|
||||
@Query("UPDATE CocktailEntity c SET c.rating = :rating WHERE c.id = :id")
|
||||
void updateRatingById(@Param("id") Long id, @Param("rating") Integer rating);
|
||||
}
|
||||
@@ -1,11 +0,0 @@
|
||||
package ru.kayashov.bar.repository;
|
||||
|
||||
import org.springframework.data.jpa.repository.JpaRepository;
|
||||
import ru.kayashov.bar.model.entity.GlassEntity;
|
||||
|
||||
import java.util.Optional;
|
||||
|
||||
public interface GlassRepository extends JpaRepository<GlassEntity, Integer> {
|
||||
|
||||
Optional<GlassEntity> findByNameIgnoreCase(String enName);
|
||||
}
|
||||
@@ -1,7 +0,0 @@
|
||||
package ru.kayashov.bar.repository;
|
||||
|
||||
import org.springframework.data.jpa.repository.JpaRepository;
|
||||
import ru.kayashov.bar.model.entity.Pay;
|
||||
|
||||
public interface OrdersRepository extends JpaRepository<Pay, Long> {
|
||||
}
|
||||
@@ -1,11 +0,0 @@
|
||||
package ru.kayashov.bar.repository;
|
||||
|
||||
import org.springframework.data.jpa.repository.JpaRepository;
|
||||
import ru.kayashov.bar.model.entity.Rating;
|
||||
|
||||
import java.util.Optional;
|
||||
|
||||
public interface RatingRepository extends JpaRepository<Rating, Long> {
|
||||
|
||||
Optional<Rating> findRatingByCocktailIdAndVisitorId(Long cocktailId, Long visitorId);
|
||||
}
|
||||
@@ -1,9 +0,0 @@
|
||||
package ru.kayashov.bar.repository;
|
||||
|
||||
import org.springframework.data.jpa.repository.JpaRepository;
|
||||
import ru.kayashov.bar.model.entity.SessionEntity;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public interface SessionRepository extends JpaRepository<SessionEntity, Long> {
|
||||
}
|
||||
@@ -1,7 +0,0 @@
|
||||
package ru.kayashov.bar.repository;
|
||||
|
||||
import org.springframework.data.jpa.repository.JpaRepository;
|
||||
import ru.kayashov.bar.model.entity.StopList;
|
||||
|
||||
public interface StopListRepository extends JpaRepository<StopList, Long> {
|
||||
}
|
||||
@@ -1,11 +0,0 @@
|
||||
package ru.kayashov.bar.repository;
|
||||
|
||||
import org.springframework.data.jpa.repository.JpaRepository;
|
||||
import ru.kayashov.bar.model.entity.TagEntity;
|
||||
|
||||
import java.util.Optional;
|
||||
|
||||
public interface TagRepository extends JpaRepository<TagEntity, Long> {
|
||||
|
||||
Optional<TagEntity> findByName(String names);
|
||||
}
|
||||
@@ -1,8 +1,7 @@
|
||||
package ru.kayashov.bar.model.entity;
|
||||
package ru.kayashov.bar.repository;
|
||||
|
||||
import org.springframework.data.jpa.repository.JpaRepository;
|
||||
|
||||
import java.util.Optional;
|
||||
import ru.kayashov.bar.model.entity.Unit;
|
||||
|
||||
public interface UnitRepository extends JpaRepository<Unit, Long> {
|
||||
}
|
||||
@@ -7,14 +7,10 @@ import org.springframework.security.core.userdetails.UserDetailsService;
|
||||
import org.springframework.security.core.userdetails.UsernameNotFoundException;
|
||||
import org.springframework.stereotype.Component;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
import ru.kayashov.bar.model.entity.BarResident;
|
||||
import ru.kayashov.bar.model.entity.UserRole;
|
||||
import ru.kayashov.bar.model.entity.Visitor;
|
||||
import ru.kayashov.bar.repository.VisitorsRepository;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.Set;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
@Component
|
||||
@RequiredArgsConstructor
|
||||
@@ -29,12 +25,13 @@ public class CustomUserDetailService implements UserDetailsService {
|
||||
|
||||
@Transactional
|
||||
public Set<GrantedAuthority> getAuthorities(long userId) {
|
||||
Visitor visitor = repository.findById(userId).orElseThrow(RuntimeException::new);
|
||||
return visitor.getResidents().stream()
|
||||
.filter(BarResident::getActive)
|
||||
.map(BarResident::getRole)
|
||||
.map(UserRole::getAuthorities)
|
||||
.flatMap(Collection::stream)
|
||||
.collect(Collectors.toSet());
|
||||
// Visitor visitor = repository.findById(userId).orElseThrow(RuntimeException::new);
|
||||
return UserRole.ADMIN.getAuthorities();
|
||||
// return visitor.getResidents().stream()
|
||||
// .filter(BarResident::getActive)
|
||||
// .map(BarResident::getRole)
|
||||
// .map(UserRole::getAuthorities)
|
||||
// .flatMap(Collection::stream)
|
||||
// .collect(Collectors.toSet());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,42 +0,0 @@
|
||||
package ru.kayashov.bar.service;
|
||||
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.apache.kafka.common.errors.SerializationException;
|
||||
import org.apache.kafka.common.header.Headers;
|
||||
import org.apache.kafka.common.serialization.Serializer;
|
||||
import ru.kayashov.bar.model.AbstractSendMessage;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
@Slf4j
|
||||
public class AbstractMessageSerializer implements Serializer<AbstractSendMessage> {
|
||||
|
||||
private final ObjectMapper objectMapper = new ObjectMapper();
|
||||
|
||||
@Override
|
||||
public void configure(Map<String, ?> configs, boolean isKey) {
|
||||
}
|
||||
|
||||
@Override
|
||||
public byte[] serialize(String topic, AbstractSendMessage data) {
|
||||
try {
|
||||
if (data == null){
|
||||
log.warn("Отсутствует объект для отправки сообщения");
|
||||
return null;
|
||||
}
|
||||
return objectMapper.writeValueAsBytes(data);
|
||||
} catch (Exception e) {
|
||||
throw new SerializationException("Ошибка сериализации объекта");
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public byte[] serialize(String topic, Headers headers, AbstractSendMessage data) {
|
||||
return Serializer.super.serialize(topic, headers, data);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void close() {
|
||||
}
|
||||
}
|
||||
@@ -5,7 +5,6 @@ import lombok.extern.slf4j.Slf4j;
|
||||
import org.hibernate.Session;
|
||||
import org.hibernate.query.Query;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.security.core.context.SecurityContextHolder;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
@@ -16,29 +15,17 @@ 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.entity.AlcoholicEntity;
|
||||
import ru.kayashov.bar.model.entity.BarEntity;
|
||||
import ru.kayashov.bar.model.entity.Alcoholic;
|
||||
import ru.kayashov.bar.model.entity.BarIngredientStorage;
|
||||
import ru.kayashov.bar.model.entity.BarResident;
|
||||
import ru.kayashov.bar.model.entity.CategoryEntity;
|
||||
import ru.kayashov.bar.model.entity.Category;
|
||||
import ru.kayashov.bar.model.entity.CocktailEntity;
|
||||
import ru.kayashov.bar.model.entity.GlassEntity;
|
||||
import ru.kayashov.bar.model.entity.Glass;
|
||||
import ru.kayashov.bar.model.entity.IngredientEntity;
|
||||
import ru.kayashov.bar.model.entity.Rating;
|
||||
import ru.kayashov.bar.model.entity.ReceiptEntity;
|
||||
import ru.kayashov.bar.model.entity.StopList;
|
||||
import ru.kayashov.bar.model.entity.TagEntity;
|
||||
import ru.kayashov.bar.model.entity.Visitor;
|
||||
import ru.kayashov.bar.repository.AlcoholicRepository;
|
||||
import ru.kayashov.bar.repository.CategoryRepository;
|
||||
import ru.kayashov.bar.repository.CocktailRepository;
|
||||
import ru.kayashov.bar.repository.GlassRepository;
|
||||
import ru.kayashov.bar.repository.IngredientRepository;
|
||||
import ru.kayashov.bar.repository.RatingRepository;
|
||||
import ru.kayashov.bar.repository.ReceiptRepository;
|
||||
import ru.kayashov.bar.repository.StopListRepository;
|
||||
import ru.kayashov.bar.repository.TagRepository;
|
||||
import ru.kayashov.bar.repository.VisitorsRepository;
|
||||
|
||||
import javax.persistence.EntityManager;
|
||||
import javax.persistence.criteria.CriteriaBuilder;
|
||||
@@ -55,7 +42,6 @@ import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
import java.util.Optional;
|
||||
import java.util.stream.Collectors;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
@@ -64,23 +50,15 @@ import java.util.stream.Stream;
|
||||
@RequiredArgsConstructor
|
||||
public class CocktailService {
|
||||
|
||||
private final CocktailRepository cocktailRepository;
|
||||
@Value("${cocktail.photo.path}")
|
||||
private String photoFolder;
|
||||
|
||||
private final StopListRepository stopListRepository;
|
||||
|
||||
private final CocktailMapper mapper;
|
||||
|
||||
private final CocktailRepository repository;
|
||||
private final AlcoholicRepository alcoholicRepository;
|
||||
private final CategoryRepository categoryRepository;
|
||||
private final GlassRepository glassRepository;
|
||||
private final TagRepository tagRepository;
|
||||
|
||||
private final ReceiptRepository receiptRepository;
|
||||
private final IngredientRepository ingredientRepository;
|
||||
private final VisitorsRepository visitorsRepository;
|
||||
private final RatingRepository ratingRepository;
|
||||
private final VisitorService visitorService;
|
||||
|
||||
private final EntityManager entityManager;
|
||||
@@ -104,18 +82,16 @@ 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 = visitor.getResidents().stream()
|
||||
// .filter(BarResident::getActive)
|
||||
// .map(BarResident::getBar)
|
||||
// .map(BarEntity::getId)
|
||||
// .toList()
|
||||
// .get(0);
|
||||
Long barId = 1L;
|
||||
List<Long> cocktailIds = getAllowedCocktailIds(barId);
|
||||
Predicate pr = root.get("id").in(cocktailIds);
|
||||
predicates.add(pr);
|
||||
|
||||
List<Long> stopListIds = getStopListIds(visitor);
|
||||
predicates.add(cb.not(root.get("id").in(stopListIds)));
|
||||
}
|
||||
|
||||
if (!dto.getSearch().isEmpty()) {
|
||||
@@ -130,12 +106,7 @@ public class CocktailService {
|
||||
}
|
||||
|
||||
if (dto.getOnlyFavourite()) {
|
||||
List<Long> favouriteCocktailsId = visitor.getRating().stream()
|
||||
.filter(Rating::isFavorite)
|
||||
.map(Rating::getCocktail)
|
||||
.map(CocktailEntity::getId)
|
||||
.toList();
|
||||
predicates.add(root.get("id").in(favouriteCocktailsId));
|
||||
predicates.add(cb.isTrue(root.get("favourite")));
|
||||
}
|
||||
|
||||
if (dto.getGlass() != null && !dto.getGlass().isEmpty()) {
|
||||
@@ -150,19 +121,6 @@ public class CocktailService {
|
||||
predicates.add(root.get("alcoholicEntity").get("name").in(dto.getAlcohol()));
|
||||
}
|
||||
|
||||
if (!dto.getTags().isEmpty()) {
|
||||
Join<CocktailEntity, TagEntity> tagJoin = root.join("tags", JoinType.LEFT);
|
||||
predicates.add(tagJoin.get("name").in(dto.getTags()));
|
||||
}
|
||||
|
||||
if (dto.getInMenu() != null) {
|
||||
List<Long> stopListIds = getStopListIds(visitor);
|
||||
switch (dto.getInMenu()) {
|
||||
case "Есть в меню" -> predicates.add(cb.not(root.get("id").in(stopListIds)));
|
||||
case "Нет в меню" -> predicates.add(root.get("id").in(stopListIds));
|
||||
}
|
||||
}
|
||||
|
||||
if (dto.getNotHaveCount() != null) {
|
||||
List<Long> approveCocktail = findICountCocktailIds(dto.getNotHaveCount(), visitor, dto.getIngredient());
|
||||
predicates.add(root.get("id").in(approveCocktail));
|
||||
@@ -195,14 +153,7 @@ public class CocktailService {
|
||||
}
|
||||
|
||||
private List<Long> findICountCocktailIds(Integer iCount, Visitor visitor, List<String> ingredientFilter) {
|
||||
List<Long> allowedIngredient = visitor.getResidents().stream()
|
||||
.filter(BarResident::getActive)
|
||||
.map(BarResident::getBar)
|
||||
.map(BarEntity::getIngredients)
|
||||
.flatMap(List::stream)
|
||||
.map(BarIngredientStorage::getIngredient)
|
||||
.map(IngredientEntity::getId)
|
||||
.toList();
|
||||
List<Long> allowedIngredient = new ArrayList<>();
|
||||
|
||||
Stream<List<ReceiptEntity>> receiptStream = receiptRepository.findAll()
|
||||
.stream()
|
||||
@@ -231,18 +182,6 @@ public class CocktailService {
|
||||
.count());
|
||||
}
|
||||
|
||||
private List<Long> getStopListIds(Visitor visitor) {
|
||||
return visitor.getResidents()
|
||||
.stream()
|
||||
.filter(BarResident::getActive)
|
||||
.map(BarResident::getBar)
|
||||
.map(BarEntity::getStops)
|
||||
.flatMap(List::stream)
|
||||
.map(StopList::getCocktail)
|
||||
.map(CocktailEntity::getId)
|
||||
.toList();
|
||||
}
|
||||
|
||||
private List<Long> getAllowedCocktailIds(Long barId) {
|
||||
CriteriaBuilder cb = entityManager.getCriteriaBuilder();
|
||||
CriteriaQuery<Long> query = cb.createQuery(Long.class);
|
||||
@@ -303,72 +242,26 @@ public class CocktailService {
|
||||
cocktail = repository.findById(dto.getId())
|
||||
.orElseThrow(() -> new RuntimeException("Не удалось найти коктейль с id " + dto.getId()));
|
||||
}
|
||||
CategoryEntity category = categoryRepository.findByNameIgnoreCase(dto.getCategory())
|
||||
.orElseThrow(() -> new RuntimeException("Не удалось найти категорию " + dto.getCategory()));
|
||||
GlassEntity glassEntity = glassRepository.findByNameIgnoreCase(dto.getGlass())
|
||||
.orElseThrow(() -> new RuntimeException("Не удалось найти посуду" + dto.getGlass()));
|
||||
AlcoholicEntity alcoholicEntity = alcoholicRepository.findByName(dto.getAlcoholic())
|
||||
.orElseThrow(() -> new RuntimeException("Не удалось найти алкогольность" + dto.getAlcoholic()));
|
||||
|
||||
cocktail.setName(dto.getName());
|
||||
cocktail.setInstructions(dto.getInstructions());
|
||||
cocktail.setImage(dto.getImage());
|
||||
cocktail.setVideo(cocktail.getVideo());
|
||||
cocktail.setCategoryEntity(category);
|
||||
cocktail.setGlassEntity(glassEntity);
|
||||
cocktail.setAlcoholicEntity(alcoholicEntity);
|
||||
cocktail.setTags(findTags(dto.getTags()));
|
||||
cocktail.setCategory(Category.findValue(dto.getCategory()));
|
||||
cocktail.setGlass(Glass.findValue(dto.getGlass()));
|
||||
cocktail.setAlcoholic(Alcoholic.findValue(dto.getAlcoholic()));
|
||||
cocktail.setRating(cocktail.getRating());
|
||||
repository.save(cocktail);
|
||||
|
||||
editCocktailReceipts(cocktail.getReceipt(), dto.getReceipt(), cocktail);
|
||||
}
|
||||
|
||||
public void editFavourite(Long cocktailId, Long visitorId, boolean put) {
|
||||
Visitor visitor = visitorsRepository.findById(visitorId).orElseThrow();
|
||||
|
||||
Optional<Rating> ratingOpt = ratingRepository.findRatingByCocktailIdAndVisitorId(cocktailId, visitorId);
|
||||
|
||||
Rating rating;
|
||||
if (put) {
|
||||
CocktailEntity cocktail = repository.findById(cocktailId)
|
||||
.orElseThrow();
|
||||
if (ratingOpt.isEmpty()) {
|
||||
rating = new Rating();
|
||||
rating.setCocktail(cocktail);
|
||||
rating.setVisitor(visitor);
|
||||
} else {
|
||||
rating = ratingOpt.get();
|
||||
}
|
||||
rating.setFavorite(true);
|
||||
ratingRepository.save(rating);
|
||||
} else {
|
||||
if (ratingOpt.isPresent()) {
|
||||
rating = ratingOpt.get();
|
||||
rating.setFavorite(false);
|
||||
ratingRepository.save(rating);
|
||||
}
|
||||
}
|
||||
public void editFavourite(Long cocktailId, boolean put) {
|
||||
cocktailRepository.updateFavouriteById(cocktailId, put);
|
||||
}
|
||||
|
||||
public void setRating(Long cocktailId, Integer rating) {
|
||||
Long id = ((Visitor) SecurityContextHolder.getContext().getAuthentication().getPrincipal()).getId();
|
||||
Visitor visitor = visitorsRepository.findById(id)
|
||||
.orElseThrow();
|
||||
|
||||
Rating rate;
|
||||
Optional<Rating> rateOpt = ratingRepository.findRatingByCocktailIdAndVisitorId(cocktailId, id);
|
||||
if (rateOpt.isEmpty()) {
|
||||
rate = new Rating();
|
||||
rate.setVisitor(visitor);
|
||||
rate.setCocktail(repository.findById(cocktailId).orElseThrow());
|
||||
rate.setFavorite(false);
|
||||
} else {
|
||||
rate = rateOpt.get();
|
||||
}
|
||||
|
||||
rate.setRating(rating);
|
||||
ratingRepository.save(rate);
|
||||
cocktailRepository.updateRatingById(cocktailId, rating);
|
||||
}
|
||||
|
||||
private void editCocktailReceipts(List<ReceiptEntity> old, List<ReceiptResponseDto> actual, CocktailEntity cocktail) {
|
||||
@@ -419,50 +312,6 @@ public class CocktailService {
|
||||
receiptRepository.save(receiptEntity);
|
||||
}
|
||||
|
||||
private List<TagEntity> findTags(String tagString) {
|
||||
if (tagString == null || tagString.isEmpty()) {
|
||||
return new ArrayList<>();
|
||||
}
|
||||
List<TagEntity> tags = new ArrayList<>();
|
||||
for (String name : tagString.split(",")) {
|
||||
if (name.isEmpty()) {
|
||||
continue;
|
||||
}
|
||||
TagEntity tagEntity = tagRepository.findByName(name)
|
||||
.orElseThrow(() -> new RuntimeException("Не удалось найти тег " + name));
|
||||
tags.add(tagEntity);
|
||||
}
|
||||
return tags;
|
||||
}
|
||||
|
||||
public void inMenuEdit(Long id, Boolean value) {
|
||||
Visitor visitor = visitorService.getCurrentVisitor();
|
||||
CocktailEntity entity = repository.findById(id)
|
||||
.orElseThrow(() -> new RuntimeException("Не удалось найти коктейль с id " + id));
|
||||
|
||||
BarEntity bar = visitor.getResidents().stream()
|
||||
.filter(BarResident::getActive)
|
||||
.map(BarResident::getBar)
|
||||
.toList()
|
||||
.get(0);
|
||||
|
||||
Optional<StopList> stop = bar.getStops().stream()
|
||||
.filter(s -> Objects.equals(s.getCocktail().getId(), id))
|
||||
.findFirst();
|
||||
|
||||
if (value && stop.isPresent()) {
|
||||
stopListRepository.deleteById(stop.get().getId());
|
||||
return;
|
||||
}
|
||||
|
||||
if (!value && stop.isEmpty()) {
|
||||
StopList stopList = new StopList();
|
||||
stopList.setCocktail(entity);
|
||||
stopList.setBar(bar);
|
||||
stopListRepository.save(stopList);
|
||||
}
|
||||
}
|
||||
|
||||
public CocktailModalDto getForModal(Long id) {
|
||||
CocktailEntity cocktail = repository.findById(id).orElseThrow(RuntimeException::new);
|
||||
return mapper.cocktailToModalDto(cocktail);
|
||||
|
||||
@@ -6,18 +6,13 @@ 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.model.entity.BarEntity;
|
||||
import ru.kayashov.bar.model.entity.BarIngredientStorage;
|
||||
import ru.kayashov.bar.repository.BarIngredientStorageRepository;
|
||||
import ru.kayashov.bar.model.entity.BarResident;
|
||||
import ru.kayashov.bar.model.entity.IngredientEntity;
|
||||
import ru.kayashov.bar.model.entity.TypeEntity;
|
||||
import ru.kayashov.bar.model.entity.Visitor;
|
||||
import ru.kayashov.bar.repository.IngredientRepository;
|
||||
import ru.kayashov.bar.repository.TypeRepository;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
|
||||
@Slf4j
|
||||
@Service
|
||||
@@ -58,27 +53,20 @@ public class IngredientService {
|
||||
}
|
||||
|
||||
public void changeBarIngredient(Long id, boolean isHave) {
|
||||
Visitor visitor = visitorService.getCurrentVisitor();
|
||||
BarEntity bar = visitor.getResidents()
|
||||
.stream()
|
||||
.filter(BarResident::getActive)
|
||||
.map(BarResident::getBar)
|
||||
.toList()
|
||||
.get(0);
|
||||
List<BarIngredientStorage> storage = bar.getIngredients();
|
||||
IngredientEntity ingredientEntity = getIngredientById(id);
|
||||
if (isHave) {
|
||||
BarIngredientStorage entity = new BarIngredientStorage();
|
||||
entity.setBar(bar);
|
||||
entity.setIngredient(ingredientEntity);
|
||||
//todo: прикрутить позже entity.setMeasure();
|
||||
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()));
|
||||
}
|
||||
// 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()));
|
||||
// }
|
||||
}
|
||||
|
||||
public boolean saveChange(IngredientResponseDto dto) {
|
||||
|
||||
@@ -1,28 +0,0 @@
|
||||
package ru.kayashov.bar.service;
|
||||
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.kafka.core.KafkaTemplate;
|
||||
import org.springframework.stereotype.Component;
|
||||
import ru.kayashov.bar.model.AbstractSendMessage;
|
||||
|
||||
import java.util.Arrays;
|
||||
|
||||
@Slf4j
|
||||
@Component
|
||||
@RequiredArgsConstructor
|
||||
public class KafkaSender {
|
||||
|
||||
private final KafkaTemplate<String, AbstractSendMessage> kafkaTemplate;
|
||||
|
||||
public String send(String topic, AbstractSendMessage message) {
|
||||
try {
|
||||
kafkaTemplate.send(topic, message);
|
||||
log.info("отправлено сообщение в топик {}, сообщение {}", topic, message.getMessage());
|
||||
return null;
|
||||
} catch (Exception e) {
|
||||
log.error(e.getMessage(), Arrays.toString(e.getStackTrace()));
|
||||
return e.getMessage();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,103 +0,0 @@
|
||||
package ru.kayashov.bar.service;
|
||||
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.springframework.stereotype.Service;
|
||||
import ru.kayashov.bar.model.AbstractSendMessage;
|
||||
import ru.kayashov.bar.model.entity.BarEntity;
|
||||
import ru.kayashov.bar.model.entity.BarResident;
|
||||
import ru.kayashov.bar.model.entity.CocktailEntity;
|
||||
import ru.kayashov.bar.model.entity.Pay;
|
||||
import ru.kayashov.bar.model.entity.SessionEntity;
|
||||
import ru.kayashov.bar.model.entity.Visitor;
|
||||
import ru.kayashov.bar.repository.CocktailRepository;
|
||||
import ru.kayashov.bar.repository.OrdersRepository;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.List;
|
||||
|
||||
import static ru.kayashov.bar.model.entity.OrderStatus.CANCEL;
|
||||
import static ru.kayashov.bar.model.entity.OrderStatus.DONE;
|
||||
import static ru.kayashov.bar.model.entity.OrderStatus.NEW;
|
||||
import static ru.kayashov.bar.model.entity.UserRole.ADMIN;
|
||||
import static ru.kayashov.bar.model.entity.UserRole.BARMEN;
|
||||
|
||||
@Service
|
||||
@RequiredArgsConstructor
|
||||
public class OrderService {
|
||||
|
||||
private final VisitorService visitorService;
|
||||
private final CocktailRepository cocktailRepository;
|
||||
private final OrdersRepository ordersRepository;
|
||||
|
||||
/**
|
||||
* Новый заказ
|
||||
*/
|
||||
public List<AbstractSendMessage> createOrder(Long visitorId, Long cocktailId) {
|
||||
Visitor visitor = visitorService.findById(visitorId);
|
||||
BarEntity bar = visitor.getResidents().stream()
|
||||
.filter(BarResident::getActive)
|
||||
.map(BarResident::getBar)
|
||||
.toList()
|
||||
.get(0);
|
||||
SessionEntity session = bar.getSessions().stream()
|
||||
.filter(SessionEntity::getIsActive)
|
||||
.toList()
|
||||
.get(0);
|
||||
|
||||
CocktailEntity cocktail = cocktailRepository.findById(cocktailId).orElseThrow();
|
||||
|
||||
Pay pay = new Pay();
|
||||
pay.setStatus(NEW);
|
||||
pay.setVisitor(visitor);
|
||||
pay.setCocktail(cocktail);
|
||||
pay.setSession(session);
|
||||
pay.setCreatedAt(LocalDateTime.now());
|
||||
ordersRepository.save(pay);
|
||||
|
||||
return bar.getVisitors().stream()
|
||||
.filter(BarResident::getActive)
|
||||
.filter(BarResident::getInvited)
|
||||
.filter(b -> b.getRole() == ADMIN || b.getRole() == BARMEN)
|
||||
.map(BarResident::getVisitor)
|
||||
.map(admin -> createOrderMessage(admin, visitor, cocktail))
|
||||
.toList();
|
||||
}
|
||||
|
||||
private AbstractSendMessage createOrderMessage(Visitor admin, Visitor client, CocktailEntity cocktail) {
|
||||
return AbstractSendMessage.builder()
|
||||
.chatId(admin.getId())
|
||||
.message(client.getName() + " " + client.getLastName() + " заказал(а) коктейль " + cocktail.getName())
|
||||
.build();
|
||||
}
|
||||
|
||||
/**
|
||||
* Обновить статус заказа
|
||||
*/
|
||||
public AbstractSendMessage updateOrder(boolean check, long l) {
|
||||
Pay pay = ordersRepository.findById(l).orElseThrow();
|
||||
pay.setStatus(check ? DONE : CANCEL);
|
||||
pay.setClosedAt(LocalDateTime.now());
|
||||
ordersRepository.save(pay);
|
||||
|
||||
return AbstractSendMessage.builder()
|
||||
.chatId(pay.getVisitor().getId())
|
||||
.message("Коктейль " + pay.getCocktail().getName() + (check ? " готов" : " отменен"))
|
||||
.build();
|
||||
}
|
||||
|
||||
/**
|
||||
* Получить заказ из очереди
|
||||
*/
|
||||
public List<Pay> getOrders() {
|
||||
Visitor visitor = visitorService.getCurrentVisitor();
|
||||
return visitor.getResidents().stream()
|
||||
.filter(BarResident::getActive)
|
||||
.map(BarResident::getBar)
|
||||
.map(BarEntity::getSessions)
|
||||
.flatMap(List::stream)
|
||||
.filter(SessionEntity::getIsActive)
|
||||
.map(SessionEntity::getOrders)
|
||||
.flatMap(List::stream)
|
||||
.toList();
|
||||
}
|
||||
}
|
||||
@@ -4,27 +4,13 @@ import lombok.RequiredArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.security.core.context.SecurityContextHolder;
|
||||
import org.springframework.stereotype.Service;
|
||||
import ru.kayashov.bar.controller.dto.bar.BarResponseDto;
|
||||
import ru.kayashov.bar.controller.dto.ingredient.IngredientSimpleResponseDto;
|
||||
import ru.kayashov.bar.controller.dto.cocktail.ReceiptResponseDto;
|
||||
import ru.kayashov.bar.model.entity.BarEntity;
|
||||
import ru.kayashov.bar.model.entity.BarResident;
|
||||
import ru.kayashov.bar.repository.BarResidentRepository;
|
||||
import ru.kayashov.bar.model.entity.CategoryEntity;
|
||||
import ru.kayashov.bar.controller.dto.ingredient.IngredientSimpleResponseDto;
|
||||
import ru.kayashov.bar.model.entity.CocktailEntity;
|
||||
import ru.kayashov.bar.model.entity.GlassEntity;
|
||||
import ru.kayashov.bar.model.entity.SessionEntity;
|
||||
import ru.kayashov.bar.model.entity.UserRole;
|
||||
import ru.kayashov.bar.model.entity.Visitor;
|
||||
import ru.kayashov.bar.repository.BarEntityRepository;
|
||||
import ru.kayashov.bar.repository.CategoryRepository;
|
||||
import ru.kayashov.bar.repository.CocktailRepository;
|
||||
import ru.kayashov.bar.repository.GlassRepository;
|
||||
import ru.kayashov.bar.repository.RatingRepository;
|
||||
import ru.kayashov.bar.repository.SessionRepository;
|
||||
import ru.kayashov.bar.repository.VisitorsRepository;
|
||||
|
||||
import java.util.Comparator;
|
||||
import java.util.List;
|
||||
|
||||
@Slf4j
|
||||
@@ -32,39 +18,8 @@ import java.util.List;
|
||||
@RequiredArgsConstructor
|
||||
public class SessionService {
|
||||
|
||||
private final SessionRepository sessionRepository;
|
||||
private final VisitorsRepository visitorsRepository;
|
||||
private final GlassRepository glassRepository;
|
||||
private final CategoryRepository categoryRepository;
|
||||
private final CocktailRepository cocktailRepository;
|
||||
private final RatingRepository ratingRepository;
|
||||
private final VisitorService visitorService;
|
||||
private final BarEntityRepository barEntityRepository;
|
||||
private final BarResidentRepository barResidentRepository;
|
||||
|
||||
/**
|
||||
* Поиск матрицы коктейлей
|
||||
* Справочник состоит из количества недостающих компонентов и списка коктейлей
|
||||
*/
|
||||
// public Map<Long, List<CocktailEntity>> getCocktailMatrix() {
|
||||
// return cocktailRepository.findAll().stream()
|
||||
// .collect(Collectors.groupingBy(this::getRemainingIngredient));
|
||||
// }
|
||||
|
||||
/**
|
||||
* Поиск активной сессии
|
||||
*/
|
||||
public SessionEntity findActiveSession() {
|
||||
List<SessionEntity> sessions = sessionRepository.findAll()
|
||||
.stream()
|
||||
.sorted(Comparator.comparingLong(SessionEntity::getId).reversed())
|
||||
.limit(1)
|
||||
.toList();
|
||||
if (!sessions.isEmpty()) {
|
||||
return sessions.get(0);
|
||||
}
|
||||
return createEmptySession();
|
||||
}
|
||||
|
||||
public Visitor getVisitor() {
|
||||
Long id = ((Visitor) SecurityContextHolder.getContext()
|
||||
@@ -75,35 +30,6 @@ public class SessionService {
|
||||
.orElseThrow();
|
||||
}
|
||||
|
||||
public SessionEntity createEmptySession() {
|
||||
BarEntity bar = visitorService.getCurrentVisitor().getResidents()
|
||||
.stream()
|
||||
.filter(BarResident::getActive)
|
||||
.map(BarResident::getBar)
|
||||
.toList()
|
||||
.get(0);
|
||||
SessionEntity session = new SessionEntity();
|
||||
session.setBar(bar);
|
||||
session.setIsActive(true);
|
||||
return sessionRepository.save(session);
|
||||
}
|
||||
|
||||
// private long getRemainingIngredient(CocktailEntity cocktail) {
|
||||
// return cocktail.getReceipt()
|
||||
// .stream()
|
||||
// .map(ReceiptEntity::getIngredient)
|
||||
//// .map(IngredientEntity::getIsHave)
|
||||
// .filter(b -> !b).count();
|
||||
// }
|
||||
|
||||
public List<GlassEntity> getGlassList() {
|
||||
return glassRepository.findAll();
|
||||
}
|
||||
|
||||
public List<CategoryEntity> getCategoryList() {
|
||||
return categoryRepository.findAll();
|
||||
}
|
||||
|
||||
public List<ReceiptResponseDto> getReceiptList(Long cocktailId) {
|
||||
CocktailEntity cocktail = cocktailRepository.findById(cocktailId).orElseThrow();
|
||||
return cocktail.getReceipt().stream()
|
||||
@@ -114,77 +40,4 @@ public class SessionService {
|
||||
.build())
|
||||
.toList();
|
||||
}
|
||||
|
||||
public List<BarResponseDto> getBarList(Boolean my) {
|
||||
List<BarResident> residents = visitorService.getCurrentVisitor().getResidents();
|
||||
if (my) {
|
||||
return residents.stream()
|
||||
.map(r -> mapBarToDto(r.getBar(), r, true))
|
||||
.toList();
|
||||
}
|
||||
List<Long> myBar = residents.stream()
|
||||
.map(BarResident::getBar)
|
||||
.map(BarEntity::getId)
|
||||
.toList();
|
||||
return barEntityRepository.findAll().stream()
|
||||
.filter(b -> !myBar.contains(b.getId()))
|
||||
.map(b -> mapBarToDto(b, null, false))
|
||||
.toList();
|
||||
}
|
||||
|
||||
public void addToMyList(BarResponseDto dto) {
|
||||
Visitor visitor = visitorService.getCurrentVisitor();
|
||||
BarEntity bar = barEntityRepository.findById(dto.getId()).orElseThrow();
|
||||
|
||||
boolean noEnter = visitor.getResidents().stream().filter(BarResident::getActive).toList().isEmpty();
|
||||
|
||||
BarResident resident = new BarResident();
|
||||
resident.setBar(bar);
|
||||
resident.setInvited(true);
|
||||
resident.setRole(UserRole.USER);
|
||||
resident.setVisitor(visitor);
|
||||
resident.setActive(noEnter);
|
||||
barResidentRepository.save(resident);
|
||||
}
|
||||
|
||||
|
||||
public void enterChange(Long id, Boolean value) {
|
||||
Visitor visitor = visitorService.getCurrentVisitor();
|
||||
List<BarResident> residents = visitor.getResidents()
|
||||
.stream()
|
||||
.filter(r -> r.getBar().getId().equals(id))
|
||||
.toList();
|
||||
if(residents.isEmpty()) {
|
||||
throw new RuntimeException("Бар с id " + id + " отсутствует в списке баров пользователя с id " + visitor.getId());
|
||||
}
|
||||
residents.stream()
|
||||
.peek(r -> r.setActive(value))
|
||||
.forEach(barResidentRepository::save);
|
||||
|
||||
log.info("Пользователь {}-{} {} {} c id {}",
|
||||
visitor.getId(),
|
||||
visitor.getName().strip(),
|
||||
visitor.getLastName().strip(),
|
||||
value ? "вошел в бар" : "вышел из бара",
|
||||
id);
|
||||
}
|
||||
|
||||
private BarResponseDto mapBarToDto(BarEntity bar, BarResident resident, Boolean my) {
|
||||
BarResponseDto dto = new BarResponseDto();
|
||||
dto.setId(bar.getId());
|
||||
dto.setName(bar.getName());
|
||||
boolean barOpen = !bar.getSessions().stream().filter(SessionEntity::getIsActive).toList().isEmpty();
|
||||
if (my) {
|
||||
boolean invited = resident.getInvited();
|
||||
dto.setOpen(barOpen && invited);
|
||||
dto.setEnter(resident.getActive());
|
||||
dto.setMyRole(resident.getRole().toString());
|
||||
} else {
|
||||
dto.setOpen(barOpen);
|
||||
dto.setEnter(false);
|
||||
dto.setMyRole(UserRole.USER.toString());
|
||||
}
|
||||
|
||||
return dto;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,24 +3,17 @@ package ru.kayashov.bar.service;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.springframework.security.core.context.SecurityContextHolder;
|
||||
import org.springframework.stereotype.Service;
|
||||
import ru.kayashov.bar.controller.dto.VisitorResponseDto;
|
||||
import ru.kayashov.bar.model.entity.BarEntity;
|
||||
import ru.kayashov.bar.model.entity.BarIngredientStorage;
|
||||
import ru.kayashov.bar.model.entity.BarResident;
|
||||
import ru.kayashov.bar.repository.BarResidentRepository;
|
||||
import ru.kayashov.bar.model.entity.IngredientEntity;
|
||||
import ru.kayashov.bar.model.entity.Visitor;
|
||||
import ru.kayashov.bar.repository.VisitorsRepository;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
|
||||
@Service
|
||||
@RequiredArgsConstructor
|
||||
public class VisitorService {
|
||||
|
||||
private final VisitorsRepository visitorsRepository;
|
||||
private final BarResidentRepository barResidentRepository;
|
||||
|
||||
public Visitor getCurrentVisitor() {
|
||||
Long id = ((Visitor) SecurityContextHolder.getContext().getAuthentication().getPrincipal()).getId();
|
||||
@@ -32,47 +25,15 @@ public class VisitorService {
|
||||
}
|
||||
|
||||
public List<Long> getAllowedIngredients() {
|
||||
return getCurrentVisitor().getResidents()
|
||||
.stream()
|
||||
.filter(BarResident::getActive)
|
||||
.map(BarResident::getBar)
|
||||
.map(BarEntity::getIngredients)
|
||||
.flatMap(List::stream)
|
||||
.map(BarIngredientStorage::getIngredient)
|
||||
.map(IngredientEntity::getId)
|
||||
.toList();
|
||||
}
|
||||
|
||||
public List<VisitorResponseDto> findAll() {
|
||||
return getCurrentVisitor().getResidents().stream()
|
||||
.filter(BarResident::getActive)
|
||||
.map(BarResident::getBar)
|
||||
.map(BarEntity::getVisitors)
|
||||
.flatMap(List::stream)
|
||||
.map(this::mapBarResidentToDto)
|
||||
.toList();
|
||||
}
|
||||
|
||||
private VisitorResponseDto mapBarResidentToDto(BarResident resident) {
|
||||
VisitorResponseDto dto = new VisitorResponseDto();
|
||||
dto.setId(resident.getVisitor().getId());
|
||||
dto.setName(resident.getVisitor().getName());
|
||||
dto.setLastName(resident.getVisitor().getLastName());
|
||||
dto.setInvited(resident.getInvited());
|
||||
dto.setRole(resident.getRole().toString());
|
||||
dto.setIsActive(resident.getActive());
|
||||
return dto;
|
||||
}
|
||||
|
||||
public void invited(Boolean value, Long id) {
|
||||
getCurrentVisitor().getResidents()
|
||||
.stream()
|
||||
.filter(BarResident::getActive)
|
||||
.map(BarResident::getBar)
|
||||
.map(BarEntity::getVisitors)
|
||||
.flatMap(List::stream)
|
||||
.filter(v -> Objects.equals(v.getVisitor().getId(), id))
|
||||
.peek(v -> v.setInvited(value))
|
||||
.forEach(barResidentRepository::save);
|
||||
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();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,47 +0,0 @@
|
||||
package ru.kayashov.bar.service.integration.cocktail;
|
||||
|
||||
import org.springframework.http.HttpEntity;
|
||||
import org.springframework.http.RequestEntity;
|
||||
import org.springframework.stereotype.Service;
|
||||
import ru.kayashov.bar.model.Cocktail;
|
||||
import ru.kayashov.bar.model.Ingredient;
|
||||
import ru.kayashov.bar.model.api.cocktail.Cocktails;
|
||||
import ru.kayashov.bar.model.api.cocktail.Ingredients;
|
||||
import ru.kayashov.bar.service.RestUtil;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
|
||||
@Service
|
||||
public class CocktailApiService {
|
||||
|
||||
public Ingredient getIngredientById(long id) {
|
||||
String url = "https://www.thecocktaildb.com/api/json/v1/1/lookup.php?iid=";
|
||||
return RestUtil.sendRequest(Ingredients.class,
|
||||
RequestEntity.get(url + id).build())
|
||||
.map(HttpEntity::getBody)
|
||||
.map(Ingredients::getIngredients)
|
||||
.map(i -> i.get(0))
|
||||
.orElse(null);
|
||||
}
|
||||
|
||||
public Cocktail getCocktailById(long id) {
|
||||
String url = "https://www.thecocktaildb.com/api/json/v1/1/lookup.php?i=";
|
||||
return RestUtil.sendRequest(Cocktails.class,
|
||||
RequestEntity.get(url + id).build())
|
||||
.map(HttpEntity::getBody)
|
||||
.map(Cocktails::getDrinks)
|
||||
.map(i -> i.get(0))
|
||||
.orElse(null);
|
||||
}
|
||||
|
||||
public List<Cocktail> getCocktailByLiteral(String literal) {
|
||||
String url = "https://www.thecocktaildb.com/api/json/v1/1/search.php?f=";
|
||||
return RestUtil.sendRequest(Cocktails.class,
|
||||
RequestEntity.get(url + literal).build())
|
||||
.map(HttpEntity::getBody)
|
||||
.map(Cocktails::getDrinks)
|
||||
.orElse(Collections.emptyList());
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user