1 | package edu.gjoko.schedlr.controllers.rest;
|
---|
2 |
|
---|
3 | import edu.gjoko.schedlr.dto.ServiceDto;
|
---|
4 | import edu.gjoko.schedlr.entity.Service;
|
---|
5 | import edu.gjoko.schedlr.mappers.ServiceDtoMapper;
|
---|
6 | import edu.gjoko.schedlr.services.ServicesService;
|
---|
7 | import lombok.AllArgsConstructor;
|
---|
8 | import org.springframework.util.CollectionUtils;
|
---|
9 | import org.springframework.web.bind.annotation.*;
|
---|
10 |
|
---|
11 | import javax.servlet.http.HttpServletRequest;
|
---|
12 | import java.util.List;
|
---|
13 | import java.util.Objects;
|
---|
14 |
|
---|
15 | @RestController
|
---|
16 | @RequestMapping("api/service")
|
---|
17 | @AllArgsConstructor
|
---|
18 | public class ServiceApi {
|
---|
19 |
|
---|
20 |
|
---|
21 | private ServicesService service;
|
---|
22 | private ServiceDtoMapper serviceDtoMapper;
|
---|
23 | @PostMapping(path="/delete")
|
---|
24 | public void saveBusiness(@RequestBody List<Service> serviceList) {
|
---|
25 | if(!CollectionUtils.isEmpty(serviceList)) {
|
---|
26 | List<Long> serviceIds = serviceList.stream()
|
---|
27 | .map(Service::getId)
|
---|
28 | .filter(Objects::nonNull)
|
---|
29 | .toList();
|
---|
30 | service.deleteByIds(serviceIds);
|
---|
31 | }
|
---|
32 | }
|
---|
33 |
|
---|
34 | @PatchMapping
|
---|
35 | public void updateServices (@RequestBody List<Service> serviceList, HttpServletRequest request) {
|
---|
36 | Long ownerId = (long) request.getSession(true).getAttribute("stakeholderId");
|
---|
37 | service.saveOrUpdateServices(serviceList, ownerId);
|
---|
38 | }
|
---|
39 |
|
---|
40 | @GetMapping
|
---|
41 | public List<ServiceDto> searchServicesByKeyWord(@RequestParam(value = "searchKeyword", required = false) String searchKeyWord) {
|
---|
42 | return service.getServicesByKeyword(searchKeyWord)
|
---|
43 | .stream()
|
---|
44 | .map(serviceDtoMapper::serviceToServiceDto)
|
---|
45 | .toList();
|
---|
46 | }
|
---|
47 | }
|
---|