Commit 0cc877fd authored by oleg.pahhomov's avatar oleg.pahhomov

SA0140-537 | create new my dashboard when asking for it

parent 8fc1cdc8
......@@ -7,6 +7,7 @@ import ee.stat.dashboard.controller.user.UserSessionHolder;
import ee.stat.dashboard.model.widget.back.enums.Language;
import ee.stat.dashboard.service.dashboard.DashboardSaveService;
import ee.stat.dashboard.service.dashboard.DashboardService;
import ee.stat.dashboard.service.dashboard.MyDashboardService;
import ee.stat.dashboard.service.dashboard.dto.DashboardResponse;
import ee.stat.dashboard.service.dashboard.dto.UserDashboardDto;
import ee.stat.dashboard.service.dashboard.dto.UserWidgetsDto;
......@@ -30,6 +31,7 @@ import static ee.stat.dashboard.controller.Response.of;
public class DashboardController {
private DashboardService dashboardService;
private MyDashboardService myDashboardService;
private DashboardSaveService dashboardSaveService;
@GetMapping
......@@ -49,7 +51,7 @@ public class DashboardController {
@Secured(RoleString.USER)
public Response<DashboardResponse> myDashboard(@RequestParam(defaultValue = "et") String language) {
StatUser user = UserSessionHolder.getLoggedInUserOrThrow("user");
return of(dashboardService.findMyDashboard(Language.getValue(language), user));
return of(myDashboardService.findMyDashboard(Language.getValue(language), user));
}
@PatchMapping(path = "/{dashboardId}/region")
......
......@@ -112,6 +112,14 @@ public class DashboardSaveService {
}
}
public Dashboard findExistingOrCreateNew(StatUser user) {
List<Dashboard> myDashboards = dashboardRepository.findByAppUser(user.getId());
if (isNotEmpty(myDashboards)) {
return first(myDashboards);
}
return saveNewUserDashboard(user.getId());
}
private List<Long> getWidgetIds(List<WidgetDomain> widgetDomains) {
return widgetDomains.stream().map(WidgetDomain::getWidget).collect(Collectors.toList());
}
......
......@@ -31,17 +31,12 @@ public class DashboardService {
return dashboardConverter.mapSimple(dashboardServiceCache.findAllByLevel(), lang, user);
}
public DashboardResponse findMyDashboard(Language lang, StatUser user) {
Dashboard dashboard = dashboardFinder.findValidMyDashboard(user);
return map(dashboard, lang, user);
}
public DashboardResponse findById(Long id, Language lang, StatUser user) {
Dashboard dashboard = dashboardFinder.findValidDashboard(id, user);
return map(dashboard, lang, user);
}
private DashboardResponse map(Dashboard dashboard, Language lang, StatUser user) {
public DashboardResponse map(Dashboard dashboard, Language lang, StatUser user) {
DashboardResponse dto = dashboardConverter.mapSimple(dashboard, lang, user);
if (dashboard.getUserType().isUser()) {
dto.setWidgets(dashboardWidgetService.getWidgets(dashboard, lang));
......
package ee.stat.dashboard.service.dashboard;
import ee.stat.dashboard.config.security.StatUser;
import ee.stat.dashboard.model.dashboard.Dashboard;
import ee.stat.dashboard.model.widget.back.enums.Language;
import ee.stat.dashboard.service.dashboard.dto.DashboardResponse;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
@Slf4j
@Service
@Transactional
@AllArgsConstructor
public class MyDashboardService {
private DashboardService dashboardService;
private DashboardSaveService dashboardSaveService;
public DashboardResponse findMyDashboard(Language lang, StatUser user) {
Dashboard dashboard = dashboardSaveService.findExistingOrCreateNew(user);
return dashboardService.map(dashboard, lang, user);
}
}
......@@ -34,7 +34,6 @@ import static org.apache.commons.collections4.CollectionUtils.isNotEmpty;
public class WidgetSaveService {
private UserWidgetRepository userWidgetRepository;
private DashboardRepository dashboardRepository;
private DashboardWidgetRepository dashboardWidgetRepository;
private DashboardSaveService dashboardSaveService;
private UserWidgetDeleteService userWidgetDeleteService;
......@@ -63,7 +62,7 @@ public class WidgetSaveService {
}
private void unpinFlow(StatUser user, Widget widget) {
Dashboard myDashboard = findExistingOrCreateNew(user);
Dashboard myDashboard = dashboardSaveService.findExistingOrCreateNew(user);
if (!dashboardWidgetRepository.existsByDashboardAndWidget(myDashboard.getId(), widget.getId())) {
return;
}
......@@ -78,7 +77,7 @@ public class WidgetSaveService {
}
private void pinFlow(StatUser user, Dashboard dashboard, Widget widget) {
Dashboard myDashboard = findExistingOrCreateNew(user);
Dashboard myDashboard = dashboardSaveService.findExistingOrCreateNew(user);
if (dashboardWidgetRepository.existsByDashboardAndWidget(myDashboard.getId(), widget.getId())) {
return;
}
......@@ -97,12 +96,4 @@ public class WidgetSaveService {
dashboardWidgetRepository.save(dashboardWidget);
return dashboardWidget;
}
private Dashboard findExistingOrCreateNew(StatUser user) {
List<Dashboard> myDashboards = dashboardRepository.findByAppUser(user.getId());
if (isNotEmpty(myDashboards)) {
return first(myDashboards);
}
return dashboardSaveService.saveNewUserDashboard(user.getId());
}
}
Markdown is supported
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