добавлена страница калькулятора

This commit is contained in:
Kayashov.SM
2025-08-21 02:49:24 +04:00
parent 34295bc045
commit 9da769beb5
22 changed files with 344 additions and 55 deletions

View File

@@ -15,6 +15,7 @@ import ru.kayashov.bar.model.entity.Visitor;
import ru.kayashov.bar.repository.VisitorsRepository;
import ru.kayashov.bar.security.JwtTokenProvider;
import javax.annotation.security.PermitAll;
import java.nio.charset.StandardCharsets;
import java.util.Base64;
import java.util.Optional;
@@ -29,6 +30,7 @@ public class AuthController {
private final VisitorsRepository visitorsRepository;
private final PasswordEncoder passwordEncoder;
@PermitAll
@PostMapping("/login")
public AuthResponseDto checkTelegramChat(@RequestBody AuthRequestDto dto) {
if (dto.getByLogin()) {
@@ -75,6 +77,7 @@ public class AuthController {
}
}
@PermitAll
@PostMapping("refresh")
public AuthResponseDto refreshToken(@RequestHeader("Authorization") String token) {
Claims claims = jwtTokenProvider.extractAllClaims(token);

View File

@@ -16,12 +16,12 @@ import ru.kayashov.bar.controller.dto.cocktail.ReceiptResponseDto;
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.UserRole;
import ru.kayashov.bar.model.entity.Visitor;
import ru.kayashov.bar.repository.UnitRepository;
import ru.kayashov.bar.service.SessionService;
import ru.kayashov.bar.service.VisitorService;
import javax.annotation.security.PermitAll;
import java.util.Arrays;
import java.util.List;
@@ -36,21 +36,25 @@ public class BarController {
private final VisitorService visitorService;
private final UnitRepository unitRepository;
@PermitAll
@GetMapping("/units")
public List<Unit> getUnitList() {
return unitRepository.findAll();
}
@PermitAll
@GetMapping("/glass")
public List<String> getGlass() {
return Arrays.stream(Glass.values()).map(Glass::getName).toList();
}
@PermitAll
@GetMapping("/category")
public List<String> getCategory() {
return Arrays.stream(Category.values()).map(Category::getName).toList();
}
@PermitAll
@GetMapping("/receipt")
public List<ReceiptResponseDto> getReceipt(@RequestParam("id") Long id) {
return sessionService.getReceiptList(id);

View File

@@ -3,7 +3,6 @@ package ru.kayashov.bar.controller;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.ResponseEntity;
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;
@@ -15,16 +14,16 @@ import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import ru.kayashov.bar.controller.dto.ErrorDto;
import ru.kayashov.bar.controller.dto.cocktail.CocktailFilterRequestDto;
import ru.kayashov.bar.controller.dto.cocktail.CocktailForIngredientModalDto;
import ru.kayashov.bar.controller.dto.cocktail.CocktailForListResponseDto;
import ru.kayashov.bar.controller.dto.ErrorDto;
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.model.entity.Visitor;
import ru.kayashov.bar.service.CocktailService;
import javax.annotation.security.PermitAll;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;
@@ -39,11 +38,17 @@ public class CocktailController {
private final CocktailService cocktailService;
//получить все
@PermitAll
@PostMapping("menu")
public List<CocktailForListResponseDto> menu(@RequestBody CocktailFilterRequestDto dto) {
return cocktailService.getMenu(dto);
}
@GetMapping("calc")
public List<CocktailForListResponseDto> calc() {
return cocktailService.calc();
}
@GetMapping("/instructions")
public String getInstructions(@RequestParam("id") Long id) {
return cocktailService.findInstructions(id);
@@ -56,7 +61,7 @@ public class CocktailController {
@PostMapping("/photo")
public String savePhoto(@RequestBody MultipartFile file) throws IOException {
if(file == null) {
if (file == null) {
return "";
}
return cocktailService.savePhoto(file);

View File

@@ -10,7 +10,7 @@ import java.util.List;
@Setter
@JsonIgnoreProperties(ignoreUnknown = true)
public class CocktailFilterRequestDto {
private String search;
private String search = "";
private Boolean onlyFavourite;
private List<String> glass;
private List<String> category;

View File

@@ -11,9 +11,12 @@ import ru.kayashov.bar.model.entity.IngredientEntity;
public class IngredientSimpleResponseDto {
private Long id;
private String name;
private String image;
private Boolean isHave;
public static IngredientSimpleResponseDto mapToDto(IngredientEntity ingredient) {
return new IngredientSimpleResponseDto(ingredient.getId(), ingredient.getName(), false/*ingredient.getIsHave()*/);
return new IngredientSimpleResponseDto(ingredient.getId(), ingredient.getName(),
"https://thecocktaildb.com/images/ingredients/" + ingredient.getEnName() + "-Medium.png",
ingredient.getIsHave());
}
}

View File

@@ -1,6 +1,8 @@
package ru.kayashov.bar.mapper;
import lombok.RequiredArgsConstructor;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContext;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
@@ -30,15 +32,12 @@ public class CocktailMapper {
private final BarEntityRepository barRepository;
@Transactional
public List<CocktailForListResponseDto> cocktailsToDtoList(List<CocktailEntity> cocktails, Boolean all) {
public List<CocktailForListResponseDto> cocktailsToDtoList(List<CocktailEntity> cocktails, Boolean all, boolean withReceipts) {
Visitor visitor = getCurrentVisitor();
// if(checkUserNotInBar(visitor)) {
// return new ArrayList<>();
// }
List<Long> barStopList = new ArrayList<>();
List<Long> allowedIngredients = getAllowedIngredients();
return cocktails.stream()
.map(c -> cocktailToDto(c, visitor, allowedIngredients, barStopList))
.map(c -> cocktailToDto(c, visitor, allowedIngredients, barStopList, withReceipts))
// .filter(c -> all || c.getIsAllowed())
// .filter(c -> all || c.getInMenu())
.toList();
@@ -59,7 +58,7 @@ public class CocktailMapper {
.build();
}
private CocktailForListResponseDto cocktailToDto(CocktailEntity e, Visitor visitor, List<Long> allowedIngredients, List<Long> barStopList) {
private CocktailForListResponseDto cocktailToDto(CocktailEntity e, Visitor visitor, List<Long> allowedIngredients, List<Long> barStopList, boolean withReceipts) {
boolean hasError = false;
int volume = 0;
Float abv = 0f;
@@ -106,8 +105,8 @@ public class CocktailMapper {
.rating(createRatingDto(e))
.isAllowed(calculateAllowed(e.getReceipt(), allowedIngredients))
.inMenu(!barStopList.contains(e.getId()))
.receipt(!withReceipts ? null : e.getReceipt().stream().map(ReceiptResponseDto::mapToDto).toList())
.build();
// d.setReceipt(e.getReceipt().stream().map(ReceiptResponseDto::mapToDto).toList());
}
private List<Long> getAllowedIngredients() {
@@ -148,7 +147,14 @@ public class CocktailMapper {
}
private Visitor getCurrentVisitor() {
Long visitorId = ((Visitor) SecurityContextHolder.getContext().getAuthentication().getPrincipal()).getId();
SecurityContext context = SecurityContextHolder.getContext();
Authentication authentication = context.getAuthentication();
Long visitorId;
if (authentication.getPrincipal() instanceof Visitor) {
visitorId = ((Visitor) authentication.getPrincipal()).getId();
} else {
visitorId = 1L;
}
return visitorsRepository.findById(visitorId).orElseThrow(RuntimeException::new);
}
@@ -181,7 +187,7 @@ public class CocktailMapper {
}
private IngredientSimpleResponseDto createIngredientResponseDto(IngredientEntity i, List<Long> allowedIngredients) {
return new IngredientSimpleResponseDto(i.getId(), i.getName(), allowedIngredients.contains(i.getId()));
return new IngredientSimpleResponseDto(i.getId(), i.getName(), "https://thecocktaildb.com/images/ingredients/" + i.getEnName() + "-Medium.png", allowedIngredients.contains(i.getId()));
}

View File

@@ -54,6 +54,8 @@ public class IngredientMapper {
}
private IngredientSimpleResponseDto mapIngredientToSimpleDto(IngredientEntity i, List<Long> allowedIngredients) {
return new IngredientSimpleResponseDto(i.getId(), i.getName(), allowedIngredients.contains(i.getId()));
return new IngredientSimpleResponseDto(i.getId(), i.getName(),
"https://thecocktaildb.com/images/ingredients/" + i.getEnName() + "-Medium.png",
allowedIngredients.contains(i.getId()));
}
}

View File

@@ -7,6 +7,7 @@ import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.authentication.AuthenticationProvider;
import org.springframework.security.authentication.dao.DaoAuthenticationProvider;
import org.springframework.security.config.annotation.authentication.configuration.AuthenticationConfiguration;
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
import org.springframework.security.config.annotation.method.configuration.EnableMethodSecurity;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
@@ -26,6 +27,7 @@ import static org.springframework.security.config.http.SessionCreationPolicy.STA
@Configuration
@EnableWebSecurity
@EnableMethodSecurity
@EnableGlobalMethodSecurity(prePostEnabled = true)
@RequiredArgsConstructor
public class SecurityConfig {
@@ -51,8 +53,11 @@ public class SecurityConfig {
private void authorizeConfiguration(AuthorizeHttpRequestsConfigurer<HttpSecurity>.AuthorizationManagerRequestMatcherRegistry request) {
request
// Можно указать конкретный путь, * - 1 уровень вложенности, ** - любое количество уровней вложенности
.antMatchers("/api/auth/**")
.permitAll()
.antMatchers("/api/auth/**").permitAll()
.antMatchers("/api/cocktail/menu").permitAll()
.antMatchers("/api/bar/category").permitAll()
.antMatchers("/api/bar/glass").permitAll()
.antMatchers("/api/ingredient/simple").permitAll()
.anyRequest()
.authenticated();
}

View File

@@ -14,6 +14,7 @@ import ru.kayashov.bar.controller.dto.cocktail.CocktailForListResponseDto;
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.controller.dto.cocktail.SortingEnum;
import ru.kayashov.bar.mapper.CocktailMapper;
import ru.kayashov.bar.model.entity.Alcoholic;
import ru.kayashov.bar.model.entity.Category;
@@ -67,7 +68,19 @@ public class CocktailService {
*/
@Transactional
public List<CocktailForListResponseDto> getMenu(CocktailFilterRequestDto dto) {
return mapper.cocktailsToDtoList(criteria(dto), dto.getAll());
return mapper.cocktailsToDtoList(criteria(dto), dto.getAll(), false);
}
@Transactional
public List<CocktailForListResponseDto> calc() {
CocktailFilterRequestDto dto = new CocktailFilterRequestDto();
dto.setPage(0);
dto.setSize(1000);
dto.setAll(false);
dto.setOnlyFavourite(false);
dto.setSort(SortingEnum.NAME_ASC);
return mapper.cocktailsToDtoList(criteria(dto),false, true);
}
private List<CocktailEntity> criteria(CocktailFilterRequestDto dto) {