Commit f486f1ea authored by Ondřej Pavlica's avatar Ondřej Pavlica
Browse files

Cart fixes, grape delete cascade fixes

parent 42ed3f88
Pipeline #140860 passed with stages
in 2 minutes and 58 seconds
...@@ -76,4 +76,10 @@ public class WineBottle extends EntityBase{ ...@@ -76,4 +76,10 @@ public class WineBottle extends EntityBase{
*/ */
@OneToMany(mappedBy = "wineBottle", fetch = FetchType.EAGER, cascade = CascadeType.REMOVE) @OneToMany(mappedBy = "wineBottle", fetch = FetchType.EAGER, cascade = CascadeType.REMOVE)
private Set<ProductReview> reviews = new HashSet<>(); private Set<ProductReview> reviews = new HashSet<>();
/**
* The order items containing this wine bottle. Placed here just for cascading purposes.
*/
@OneToMany(mappedBy = "bottle", fetch = FetchType.EAGER, cascade = CascadeType.REMOVE)
private Set<OrderItem> items = new HashSet<>();
} }
...@@ -17,14 +17,16 @@ import java.util.stream.Collectors; ...@@ -17,14 +17,16 @@ import java.util.stream.Collectors;
public class CartServiceImpl implements CartService { public class CartServiceImpl implements CartService {
private final CookieService cookieService; private final CookieService cookieService;
private final LoginContextService loginContextService;
public CartServiceImpl(CookieService cookieService) { public CartServiceImpl(CookieService cookieService, LoginContextService loginContextService) {
this.cookieService = cookieService; this.cookieService = cookieService;
this.loginContextService = loginContextService;
} }
@Override @Override
public CartDto loadFromCookie() { public CartDto loadFromCookie() {
var cookieValue = cookieService.getCookie("wineryCart"); var cookieValue = cookieService.getCookie(getCartCookieName());
if (cookieValue == null) { if (cookieValue == null) {
return new CartDto(); return new CartDto();
} }
...@@ -48,7 +50,7 @@ public class CartServiceImpl implements CartService { ...@@ -48,7 +50,7 @@ public class CartServiceImpl implements CartService {
catch (Exception exception) { catch (Exception exception) {
json = null; json = null;
} }
cookieService.setCookie("wineryCart", json); cookieService.setCookie(getCartCookieName(), json);
} }
@Override @Override
...@@ -66,4 +68,12 @@ public class CartServiceImpl implements CartService { ...@@ -66,4 +68,12 @@ public class CartServiceImpl implements CartService {
order.setItems(items); order.setItems(items);
return order; return order;
} }
private String getCartCookieName() {
var user = loginContextService.getCurrentUser();
if (user == null) {
return "wineryCart";
}
return "wineryCart_" + user.getId();
}
} }
package cz.muni.fi.pa165.winery.services;
import cz.muni.fi.pa165.winery.dto.user.UserDto;
public interface LoginContextService {
public UserDto getCurrentUser();
}
package cz.muni.fi.pa165.winery.webapp.controllers; package cz.muni.fi.pa165.winery.webapp.controllers;
import cz.muni.fi.pa165.winery.dto.user.UserDto; import cz.muni.fi.pa165.winery.dto.user.UserDto;
import cz.muni.fi.pa165.winery.services.LoginContextService;
import cz.muni.fi.pa165.winery.services.user.UserService; import cz.muni.fi.pa165.winery.services.user.UserService;
import cz.muni.fi.pa165.winery.webapp.models.ViewModelBase; import cz.muni.fi.pa165.winery.webapp.models.ViewModelBase;
import cz.muni.fi.pa165.winery.services.CacheService; import cz.muni.fi.pa165.winery.services.CacheService;
...@@ -18,10 +19,7 @@ public class ControllerBase { ...@@ -18,10 +19,7 @@ public class ControllerBase {
private static final Pattern CONTROLLER_NAME_REGEX = Pattern.compile("^(?<name>\\w+?)(Controller)?$", Pattern.CASE_INSENSITIVE); private static final Pattern CONTROLLER_NAME_REGEX = Pattern.compile("^(?<name>\\w+?)(Controller)?$", Pattern.CASE_INSENSITIVE);
@Autowired @Autowired
private CacheService cacheService; private LoginContextService loginContextService;
@Autowired
private UserService userService;
protected ModelAndView view() { protected ModelAndView view() {
var callingContext = getControllerAndAction(); var callingContext = getControllerAndAction();
...@@ -100,11 +98,7 @@ public class ControllerBase { ...@@ -100,11 +98,7 @@ public class ControllerBase {
} }
protected UserDto getCurrentUser() { protected UserDto getCurrentUser() {
var authentication = SecurityContextHolder.getContext().getAuthentication(); return loginContextService.getCurrentUser();
var dto = cacheService.cache(
"_currentUser:" + authentication.getName(),
() -> userService.getByEmail(authentication.getName()));
return dto;
} }
private void initializeBindingMap(Map<String, Object> map) { private void initializeBindingMap(Map<String, Object> map) {
......
package cz.muni.fi.pa165.winery.webapp.services;
import cz.muni.fi.pa165.winery.dto.user.UserDto;
import cz.muni.fi.pa165.winery.services.CacheService;
import cz.muni.fi.pa165.winery.services.LoginContextService;
import cz.muni.fi.pa165.winery.services.user.UserService;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;
@Service
public class LoginContextServiceImpl implements LoginContextService {
private final CacheService cacheService;
private final UserService userService;
public LoginContextServiceImpl(CacheService cacheService, UserService userService) {
this.cacheService = cacheService;
this.userService = userService;
}
@Override
public UserDto getCurrentUser() {
var authentication = SecurityContextHolder.getContext().getAuthentication();
var dto = cacheService.cache(
"_currentUser:" + authentication.getName(),
() -> userService.getByEmail(authentication.getName()));
return dto;
}
}
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment