| 34950 |
vikas |
1 |
package com.spice.profitmandi.web.controller;
|
|
|
2 |
|
|
|
3 |
import com.fasterxml.jackson.core.type.TypeReference;
|
|
|
4 |
import com.fasterxml.jackson.databind.ObjectMapper;
|
|
|
5 |
import com.spice.profitmandi.dao.entity.Page;
|
|
|
6 |
import com.spice.profitmandi.dao.entity.dtr.WebListing;
|
|
|
7 |
import com.spice.profitmandi.dao.repository.dtr.WebListingRepository;
|
|
|
8 |
import com.spice.profitmandi.service.page.PageRepository;
|
|
|
9 |
import org.apache.logging.log4j.LogManager;
|
|
|
10 |
import org.apache.logging.log4j.Logger;
|
|
|
11 |
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
12 |
import org.springframework.http.ResponseEntity;
|
|
|
13 |
import org.springframework.stereotype.Controller;
|
|
|
14 |
import org.springframework.transaction.annotation.Transactional;
|
|
|
15 |
import org.springframework.web.bind.annotation.GetMapping;
|
|
|
16 |
import org.springframework.web.bind.annotation.PathVariable;
|
|
|
17 |
import org.springframework.web.bind.annotation.RequestMapping;
|
|
|
18 |
|
|
|
19 |
import javax.servlet.http.HttpServletRequest;
|
|
|
20 |
import java.util.*;
|
|
|
21 |
import java.util.stream.Collectors;
|
|
|
22 |
|
|
|
23 |
@Controller
|
|
|
24 |
@RequestMapping("/pages")
|
|
|
25 |
@Transactional(rollbackFor = Throwable.class)
|
|
|
26 |
public class PageController {
|
|
|
27 |
private static final Logger logger = LogManager.getLogger(PageController.class);
|
|
|
28 |
|
|
|
29 |
@Autowired
|
|
|
30 |
private PageRepository pageRepository;
|
|
|
31 |
|
|
|
32 |
@Autowired
|
|
|
33 |
private WebListingRepository webListingRepository;
|
|
|
34 |
|
|
|
35 |
private final ObjectMapper objectMapper = new ObjectMapper();
|
|
|
36 |
|
|
|
37 |
@GetMapping
|
|
|
38 |
public ResponseEntity<?> index(HttpServletRequest request) {
|
|
|
39 |
List<Page> pages = pageRepository.selectAll();
|
|
|
40 |
List<Map<String, Object>> parsedPages = pages.stream().map(p -> {
|
|
|
41 |
try {
|
|
|
42 |
Map<String, Object> data = objectMapper.readValue(
|
|
|
43 |
p.getSectionData(),
|
|
|
44 |
new TypeReference<Map<String, Object>>() {}
|
|
|
45 |
);
|
|
|
46 |
|
|
|
47 |
data.put("tabTitle", p.getTabTitle());
|
|
|
48 |
|
|
|
49 |
Map<String, Object> pageMap = new HashMap<>();
|
|
|
50 |
pageMap.put("id", p.getId());
|
|
|
51 |
pageMap.put("title", p.getTitle());
|
|
|
52 |
pageMap.put("slug", p.getSlug());
|
|
|
53 |
pageMap.put("visibleOn", p.getVisibleOn());
|
|
|
54 |
pageMap.put("sectionGroup", p.getSectionGroup());
|
|
|
55 |
pageMap.put("sectionBelow", p.getSectionBelow());
|
|
|
56 |
pageMap.put("status", p.isStatus());
|
|
|
57 |
pageMap.put("sectionOrder", p.getSectionOrder());
|
|
|
58 |
pageMap.put("sectionData", data);
|
|
|
59 |
return pageMap;
|
|
|
60 |
} catch (Exception e) {
|
|
|
61 |
throw new RuntimeException("Failed to parse sectionData", e);
|
|
|
62 |
}
|
|
|
63 |
}).collect(Collectors.toList());
|
|
|
64 |
|
|
|
65 |
List<Map<String, Object>> merged = parsedPages.stream()
|
|
|
66 |
.filter(p -> "Parent".equals(p.get("sectionGroup"))) // only parents
|
|
|
67 |
.map(parent -> {
|
|
|
68 |
|
|
|
69 |
List<Map<String, Object>> children = parsedPages.stream()
|
|
|
70 |
.filter(c -> String.valueOf(parent.get("id")).equals(c.get("sectionGroup")))
|
|
|
71 |
.collect(Collectors.toList());
|
|
|
72 |
|
|
|
73 |
List<Object> sectionDataList = new ArrayList<>();
|
|
|
74 |
sectionDataList.add(parent.get("sectionData"));
|
|
|
75 |
children.forEach(c -> sectionDataList.add(c.get("sectionData")));
|
|
|
76 |
|
| 35079 |
vikas |
77 |
if (parent.get("sectionBelow") == null && !children.isEmpty()) {
|
|
|
78 |
Object firstChildBelow = children.get(0).get("sectionBelow");
|
|
|
79 |
if (firstChildBelow != null) {
|
|
|
80 |
parent.put("sectionBelow", firstChildBelow);
|
|
|
81 |
}
|
|
|
82 |
}
|
|
|
83 |
|
| 34950 |
vikas |
84 |
try {
|
|
|
85 |
parent.put("sectionData", objectMapper.writeValueAsString(sectionDataList));
|
|
|
86 |
} catch (Exception e) {
|
|
|
87 |
throw new RuntimeException("Failed to serialize sectionData list", e);
|
|
|
88 |
}
|
|
|
89 |
|
|
|
90 |
return parent;
|
|
|
91 |
})
|
|
|
92 |
.sorted(Comparator.comparingInt(p -> (int) p.get("sectionOrder")))
|
|
|
93 |
.collect(Collectors.toList());
|
|
|
94 |
|
|
|
95 |
return ResponseEntity.ok(merged);
|
|
|
96 |
}
|
|
|
97 |
|
|
|
98 |
@GetMapping("/{page}")
|
|
|
99 |
public ResponseEntity<?> getPage(HttpServletRequest request, @PathVariable String page) {
|
|
|
100 |
Page pageDetail = pageRepository.selectPageBySlug(page);
|
|
|
101 |
return ResponseEntity.ok(pageDetail);
|
|
|
102 |
}
|
|
|
103 |
|
|
|
104 |
@GetMapping("/web-listing/{id}")
|
|
|
105 |
public ResponseEntity<?> webListing(HttpServletRequest request, @PathVariable int id) {
|
|
|
106 |
WebListing web = webListingRepository.selectById(id);
|
|
|
107 |
return ResponseEntity.ok(web);
|
|
|
108 |
}
|
|
|
109 |
}
|