-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCompanyController.java
More file actions
171 lines (141 loc) · 8.03 KB
/
CompanyController.java
File metadata and controls
171 lines (141 loc) · 8.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
package com.example.spring.app.company;
import com.example.spring.app.company.dto.CompanyDTO;
import com.example.spring.app.company.dto.CompanyDetails;
import com.example.spring.app.company.dto.CompanyDtoWithStatusDTO;
import com.example.spring.app.company.dto.CompanyFilterRequest;
import com.example.spring.app.userCompanyStatus.UserCompanyStatusModel;
import com.example.spring.app.userCompanyStatus.UserCompanyStatusService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.*;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.server.ResponseStatusException;
import java.time.LocalDate;
import java.util.List;
import java.util.stream.Collectors;
import static com.example.spring.utils.HeadersUtil.parseUserIdFromHeader;
@CrossOrigin
@RestController
@RequestMapping("/v1/company")
public class CompanyController {
@Autowired
private CompanyService companyService;
@Autowired
private UserCompanyStatusService userCompanyStatusService;
// Example: http://localhost:8080/api/v1/company/get-by-id/123
@GetMapping("/get-by-id/{id}")
public CompanyDtoWithStatusDTO getCompanyById(@PathVariable("id") Integer id) {
String userId = parseUserIdFromHeader();
CompanyDTO companyDto = companyService.getCompanyById(id).toCompanyDTO();
UserCompanyStatusModel userCompanyStatus = userCompanyStatusService
.getOneUserCompanyStatusByUserIdAndCompanyId(userId, id);
return CompanyUtil.fillCompanyDtoWithStatusDto(companyDto, userCompanyStatus);
}
// Example: http://localhost:8080/api/v1/company/get-seen-by-user?page=0
@GetMapping("/get-seen-by-user")
public Page<CompanyDtoWithStatusDTO> getCompaniesSeenByUser(@RequestParam(defaultValue = "0") int page,
@RequestParam(defaultValue = "10") int size) {
Pageable pageable = PageRequest.of(page, size);
String userId = parseUserIdFromHeader();
Page<CompanyModel> companies = companyService.getCompaniesSeenByUser(userId, pageable);
List<UserCompanyStatusModel> userCompanyStatuses = userCompanyStatusService
.getMultipleUserCompanyStatusByUserIdAndCompanyIds(userId, companies.getContent()
.stream()
.map(CompanyModel::getId)
.toList());
return CompanyUtil.fillPaginationCompanyDtoWithStatusDto(companies, userCompanyStatuses);
}
// Example: http://localhost:8080/api/v1/company/search-by-name?companyName=ExampleCompany&page=0
@GetMapping("/search-by-name")
public Page<CompanyDetails> searchCompaniesByName(@RequestParam("companyName") String companyName,
@RequestParam(defaultValue = "0") int page,
@RequestParam(defaultValue = "10") int size) {
Pageable pageable = PageRequest.of(page, size, Sort.by("id").ascending());
return companyService.searchCompanies(companyName, pageable);
}
// Example: http://localhost:8080/api/v1/company/filter-by-parameters?regions=region1,region2&cities=city1,city2&industrySectors=sector1,sector2&legalForms=form1,form2&page=0
@PostMapping("/filter-by-parameters")
public Page<CompanyDtoWithStatusDTO> getCompaniesByFilters(
@RequestBody(required = false) CompanyFilterRequest filterRequest) {
String userId = parseUserIdFromHeader();
Pageable pageable = PageRequest.of(filterRequest.getPage(), filterRequest.getSize());
Page<CompanyModel> companies = companyService.findCompaniesByFilters(
filterRequest.getRegionNames(),
filterRequest.getCityNames(),
filterRequest.getIndustrySectorNames(),
filterRequest.getLegalFormNames(),
filterRequest.getNumberOfEmployeeFilter(),
filterRequest.getSocials(),
filterRequest.getContacts(),
filterRequest.getIsCompanySeen(),
userId,
pageable
);
List<UserCompanyStatusModel> userCompanyStatuses = userCompanyStatusService
.getMultipleUserCompanyStatusByUserIdAndCompanyIds(userId, companies.getContent()
.stream()
.map(CompanyModel::getId)
.toList());
return CompanyUtil.fillPaginationCompanyDtoWithStatusDto(companies, userCompanyStatuses);
}
// Example: http://localhost:8080/api/v1/company/random-unseen?page=0
@GetMapping("/random-unseen")
public Page<CompanyDtoWithStatusDTO> getRandomUnseenCompanies(@RequestParam(defaultValue = "0") int page,
@RequestParam(defaultValue = "10") int size) {
Pageable pageable = PageRequest.of(page, size);
String userId = parseUserIdFromHeader();
Page<CompanyModel> companies = companyService.findRandomUnseenCompanies(userId, pageable);
List<UserCompanyStatusModel> userCompanyStatuses = userCompanyStatusService
.getMultipleUserCompanyStatusByUserIdAndCompanyIds(userId, companies.getContent()
.stream()
.map(CompanyModel::getId)
.toList());
return CompanyUtil.fillPaginationCompanyDtoWithStatusDto(companies, userCompanyStatuses);
}
// Make a request to the scrap API
// Example: http://localhost:8080/api/v1/company/scrap?companyId=1
@GetMapping("/scrap")
public CompanyDTO scrapCompany(@RequestParam Integer companyId) {
CompanyModel company = companyService.getCompanyById(companyId);
if (company == null) {
throw new ResponseStatusException(HttpStatus.NOT_FOUND, "Company not found");
}
boolean needsScraping = company.getScrapingDate() == null ||
company.getScrapingDate().isBefore(LocalDate.now().minusDays(1));
if (!needsScraping) {
throw new ResponseStatusException(HttpStatus.TOO_EARLY, "The company was scrapped less than 1 day ago");
}
CompanyModel companyScraped = companyService.scrapCompany(company);
company.updateFrom(companyScraped);
companyService.saveCompany(company);
return company.toCompanyDTO();
}
// Example: http://localhost:8080/api/v1/company/filter-by-parameters?regions=region1,region2&cities=city1,city2&industrySectors=sector1,sector2&legalForms=form1,form2&page=0
@GetMapping("/landing-filter")
public Page<CompanyDTO> getCompaniesOnLandingByFilters(
//@RequestParam(required = false) List<String> regions,
@RequestParam(required = false) List<String> cityNames,
@RequestParam(required = false) List<String> industrySectorNames
//@RequestParam(required = false) List<String> legalForms,
//@RequestParam(required = false) String comparator,
//@RequestParam(required = false) Integer numberOfEmployee
) {
Pageable pageable = PageRequest.of(0, 10);
// First try to get companies that the user has not seen yet
Page<CompanyModel> companiesPage = companyService.findCompaniesByFilters(null, cityNames, industrySectorNames, null,
null, null, null, false, null, pageable);
// If there are not enough unseen companies, get seen companies to fill the page
if (companiesPage.getTotalElements() < 7) {
companiesPage = companyService.findCompaniesByFilters(null, cityNames, industrySectorNames, null,
null, null, null, true, null, pageable);
}
return new PageImpl<>(
companiesPage.getContent().stream()
.peek(CompanyModel::obstructCompany)
.map(CompanyModel::toCompanyDTO)
.collect(Collectors.toList()),
companiesPage.getPageable(),
companiesPage.getTotalElements()
);
}
}