Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ bin/
out/
!**/src/main/**/out/
!**/src/test/**/out/
**/logs/**

### NetBeans ###
/nbproject/private/
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,11 @@
package com.example.spring.controller;

import com.example.spring.dto.CompanyDetails;
import com.example.spring.dto.CompanyDtoWithStatusDTO;
import com.example.spring.dto.CompanyFilterRequest;
import com.example.spring.dto.company.CompanyDTO;
import com.example.spring.model.Company;
import com.example.spring.model.UserCompanyStatus;
import com.example.spring.service.CompanyService;
import com.example.spring.service.UserCompanyStatusService;
import com.example.spring.utils.CompanyUtil;
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;
Expand Down Expand Up @@ -38,7 +35,7 @@ public class CompanyController {
public CompanyDtoWithStatusDTO getCompanyById(@PathVariable("id") Integer id) {
String userId = parseUserIdFromHeader();
CompanyDTO companyDto = companyService.getCompanyById(id).toCompanyDTO();
UserCompanyStatus userCompanyStatus = userCompanyStatusService
UserCompanyStatusModel userCompanyStatus = userCompanyStatusService
.getOneUserCompanyStatusByUserIdAndCompanyId(userId, id);

return CompanyUtil.fillCompanyDtoWithStatusDto(companyDto, userCompanyStatus);
Expand All @@ -50,12 +47,12 @@ public Page<CompanyDtoWithStatusDTO> getCompaniesSeenByUser(@RequestParam(defaul
@RequestParam(defaultValue = "10") int size) {
Pageable pageable = PageRequest.of(page, size);
String userId = parseUserIdFromHeader();
Page<Company> companies = companyService.getCompaniesSeenByUser(userId, pageable);
Page<CompanyModel> companies = companyService.getCompaniesSeenByUser(userId, pageable);

List<UserCompanyStatus> userCompanyStatuses = userCompanyStatusService
List<UserCompanyStatusModel> userCompanyStatuses = userCompanyStatusService
.getMultipleUserCompanyStatusByUserIdAndCompanyIds(userId, companies.getContent()
.stream()
.map(Company::getId)
.map(CompanyModel::getId)
.toList());

return CompanyUtil.fillPaginationCompanyDtoWithStatusDto(companies, userCompanyStatuses);
Expand All @@ -77,7 +74,7 @@ public Page<CompanyDtoWithStatusDTO> getCompaniesByFilters(
String userId = parseUserIdFromHeader();
Pageable pageable = PageRequest.of(filterRequest.getPage(), filterRequest.getSize());

Page<Company> companies = companyService.findCompaniesByFilters(
Page<CompanyModel> companies = companyService.findCompaniesByFilters(
filterRequest.getRegionNames(),
filterRequest.getCityNames(),
filterRequest.getIndustrySectorNames(),
Expand All @@ -90,10 +87,10 @@ public Page<CompanyDtoWithStatusDTO> getCompaniesByFilters(
pageable
);

List<UserCompanyStatus> userCompanyStatuses = userCompanyStatusService
List<UserCompanyStatusModel> userCompanyStatuses = userCompanyStatusService
.getMultipleUserCompanyStatusByUserIdAndCompanyIds(userId, companies.getContent()
.stream()
.map(Company::getId)
.map(CompanyModel::getId)
.toList());

return CompanyUtil.fillPaginationCompanyDtoWithStatusDto(companies, userCompanyStatuses);
Expand All @@ -105,11 +102,11 @@ public Page<CompanyDtoWithStatusDTO> getRandomUnseenCompanies(@RequestParam(defa
@RequestParam(defaultValue = "10") int size) {
Pageable pageable = PageRequest.of(page, size);
String userId = parseUserIdFromHeader();
Page<Company> companies = companyService.findRandomUnseenCompanies(userId, pageable);
List<UserCompanyStatus> userCompanyStatuses = userCompanyStatusService
Page<CompanyModel> companies = companyService.findRandomUnseenCompanies(userId, pageable);
List<UserCompanyStatusModel> userCompanyStatuses = userCompanyStatusService
.getMultipleUserCompanyStatusByUserIdAndCompanyIds(userId, companies.getContent()
.stream()
.map(Company::getId)
.map(CompanyModel::getId)
.toList());

return CompanyUtil.fillPaginationCompanyDtoWithStatusDto(companies, userCompanyStatuses);
Expand All @@ -119,7 +116,7 @@ public Page<CompanyDtoWithStatusDTO> getRandomUnseenCompanies(@RequestParam(defa
// Example: http://localhost:8080/api/v1/company/scrap?companyId=1
@GetMapping("/scrap")
public CompanyDTO scrapCompany(@RequestParam Integer companyId) {
Company company = companyService.getCompanyById(companyId);
CompanyModel company = companyService.getCompanyById(companyId);
if (company == null) {
throw new ResponseStatusException(HttpStatus.NOT_FOUND, "Company not found");
}
Expand All @@ -131,7 +128,7 @@ public CompanyDTO scrapCompany(@RequestParam Integer companyId) {
throw new ResponseStatusException(HttpStatus.TOO_EARLY, "The company was scrapped less than 1 day ago");
}

Company companyScraped = companyService.scrapCompany(company);
CompanyModel companyScraped = companyService.scrapCompany(company);
company.updateFrom(companyScraped);
companyService.saveCompany(company);

Expand All @@ -153,7 +150,7 @@ public Page<CompanyDTO> getCompaniesOnLandingByFilters(
Pageable pageable = PageRequest.of(0, 10);

// First try to get companies that the user has not seen yet
Page<Company> companiesPage = companyService.findCompaniesByFilters(null, cityNames, industrySectorNames, null,
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
Expand All @@ -164,8 +161,8 @@ public Page<CompanyDTO> getCompaniesOnLandingByFilters(

return new PageImpl<>(
companiesPage.getContent().stream()
.peek(Company::obstructCompany)
.map(Company::toCompanyDTO)
.peek(CompanyModel::obstructCompany)
.map(CompanyModel::toCompanyDTO)
.collect(Collectors.toList()),
companiesPage.getPageable(),
companiesPage.getTotalElements()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package com.example.spring.model;
package com.example.spring.app.company;

import com.example.spring.dto.company.CompanyDTO;
import com.example.spring.app.company.dto.CompanyDTO;
import com.example.spring.app.company.objects.ContactDTO;
import com.example.spring.app.company.objects.SocialMediaDTO;
import io.hypersistence.utils.hibernate.type.json.JsonBinaryType;
import jakarta.persistence.*;
import lombok.AllArgsConstructor;
Expand All @@ -13,16 +15,16 @@
import java.time.LocalDate;
import java.util.Map;

import static com.example.spring.mapper.FinancialPeriodMapper.toFinancialPeriodDTOList;
import static com.example.spring.utils.CompanyUtil.maskData;
import static com.example.spring.app.company.financial.FinancialPeriodMapper.toFinancialPeriodDTOList;
import static com.example.spring.app.company.CompanyUtil.maskData;

@Builder
@NoArgsConstructor
@AllArgsConstructor
@Data
@Entity
@Table(name = "companies")
public class Company {
public class CompanyModel {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer id;
Expand Down Expand Up @@ -161,7 +163,7 @@ public class Company {
private Integer numberOfEmployee;
private String companyCategory;

public void updateFrom(Company other) {
public void updateFrom(CompanyModel other) {
this.phoneNumber = other.getPhoneNumber();
this.website = other.getWebsite();
this.instagram = other.getInstagram();
Expand Down Expand Up @@ -207,7 +209,7 @@ public CompanyDTO toCompanyDTO() {
.reviews(this.getReviews())
.schedule(this.getSchedule())
.socialMedia(
com.example.spring.dto.company.SocialMedia.builder()
SocialMediaDTO.builder()
.instagram(this.getInstagram())
.facebook(this.getFacebook())
.twitter(this.getTwitter())
Expand All @@ -216,7 +218,7 @@ public CompanyDTO toCompanyDTO() {
.build()
)
.contact(
com.example.spring.dto.company.Contact.builder()
ContactDTO.builder()
.email(this.getEmail())
.phoneNumber(this.getPhoneNumber())
.website(this.getWebsite())
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package com.example.spring.repository;
package com.example.spring.app.company;

import com.example.spring.dto.CompanyDetails;
import com.example.spring.model.Company;
import com.example.spring.app.company.dto.CompanyDetails;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
Expand All @@ -10,27 +9,27 @@
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;

public interface CompanyRepository extends JpaRepository<Company, Integer>, JpaSpecificationExecutor<Company> {
public interface CompanyRepository extends JpaRepository<CompanyModel, Integer>, JpaSpecificationExecutor<CompanyModel> {

Company findCompanyById(Integer id);
CompanyModel findCompanyById(Integer id);

@Query("SELECT new com.example.spring.dto.CompanyDetails(c.id, c.companyName, c.industrySector, c.city, c.region) " +
"FROM Company c WHERE LOWER(c.companyName) LIKE LOWER(CONCAT(:companyName, '%'))")
@Query("SELECT new com.example.spring.app.company.dto.CompanyDetails(c.id, c.companyName, c.industrySector, c.city, c.region) " +
"FROM CompanyModel c WHERE LOWER(c.companyName) LIKE LOWER(CONCAT(:companyName, '%'))")
Page<CompanyDetails> findCompanyDetailsByCompanyName(@Param("companyName") String companyName, Pageable pageable);


// Specification
Page<Company> findAll(Specification<Company> specification, Pageable pageable);
Page<CompanyModel> findAll(Specification<CompanyModel> specification, Pageable pageable);

@Query(value = "SELECT c.* FROM companies c " +
"WHERE c.phone_number IS NOT NULL AND NOT " +
"EXISTS (SELECT 1 FROM user_company_status ucs " +
"WHERE ucs.company_id = c.id AND ucs.user_id = :userId)",
nativeQuery = true)
Page<Company> findRandomUnseenCompanies(@Param("userId") String userId, Pageable pageable);
Page<CompanyModel> findRandomUnseenCompanies(@Param("userId") String userId, Pageable pageable);

@Query(value = "SELECT c.* FROM companies c " +
"INNER JOIN user_company_status ucs ON c.id = ucs.company_id " +
"WHERE ucs.user_id = :userId", nativeQuery = true)
Page<Company> findCompaniesSeenByUser(@Param("userId") String userId, Pageable pageable);
Page<CompanyModel> findCompaniesSeenByUser(@Param("userId") String userId, Pageable pageable);
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
package com.example.spring.service;

import com.example.spring.dto.CompanyDetails;
import com.example.spring.dto.NumberOfEmployeeFilter;
import com.example.spring.dto.company.Contact;
import com.example.spring.dto.company.SocialMedia;
import com.example.spring.model.Company;
import com.example.spring.repository.CompanyRepository;
import com.example.spring.specification.CompanySpecification;
package com.example.spring.app.company;

import com.example.spring.app.company.dto.CompanyDetails;
import com.example.spring.app.company.dto.NumberOfEmployeeFilterDTO;
import com.example.spring.app.company.objects.ContactDTO;
import com.example.spring.app.company.objects.SocialMediaDTO;
import com.example.spring.utils.LogUtil;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.JsonNode;
Expand All @@ -30,19 +27,19 @@
import java.util.List;
import java.util.Map;

import static com.example.spring.utils.CompanyUtil.setIfNotEmpty;
import static com.example.spring.app.company.CompanyUtil.setIfNotEmpty;

@Service
public class CompanyService {

@Autowired
private CompanyRepository companyRepository;

public Company getCompanyById(Integer id) {
public CompanyModel getCompanyById(Integer id) {
return companyRepository.findCompanyById(id);
}

public Page<Company> getCompaniesSeenByUser(String userId, Pageable pageable) {
public Page<CompanyModel> getCompaniesSeenByUser(String userId, Pageable pageable) {
return companyRepository.findCompaniesSeenByUser(userId, pageable);
}

Expand All @@ -51,11 +48,11 @@ public Page<CompanyDetails> searchCompanies(String companyName, Pageable pageabl
return companyRepository.findCompanyDetailsByCompanyName(companyName, pageable);
}

public Page<Company> findCompaniesByFilters(List<String> regionNames, List<String> cityNames, List<String> industrySectorNames, List<String> legalFormNames,
NumberOfEmployeeFilter numberOfEmployeeFilter, SocialMedia socials, Contact contacts, Boolean isCompanySeen,
String userId, Pageable pageable) {
public Page<CompanyModel> findCompaniesByFilters(List<String> regionNames, List<String> cityNames, List<String> industrySectorNames, List<String> legalFormNames,
NumberOfEmployeeFilterDTO numberOfEmployeeFilter, SocialMediaDTO socials, ContactDTO contacts, Boolean isCompanySeen,
String userId, Pageable pageable) {

Specification<Company> specification = Specification.where(CompanySpecification.regionsIn(regionNames))
Specification<CompanyModel> specification = Specification.where(CompanySpecification.regionsIn(regionNames))
.and(CompanySpecification.citiesIn(cityNames))
.and(CompanySpecification.industrySectorsIn(industrySectorNames))
.and(CompanySpecification.legalFormsIn(legalFormNames))
Expand All @@ -69,9 +66,9 @@ public Page<Company> findCompaniesByFilters(List<String> regionNames, List<Strin

@Cacheable(value = "companyCounts", key = "#root.methodName + #regionNames + #cityNames + #industrySectorNames + #legalFormNames + #numberOfEmployeeFilter + #socials + #contacts")
public long countCompaniesByFilters(List<String> regionNames, List<String> cityNames, List<String> industrySectorNames, List<String> legalFormNames,
NumberOfEmployeeFilter numberOfEmployeeFilter, SocialMedia socials, Contact contacts) {
NumberOfEmployeeFilterDTO numberOfEmployeeFilter, SocialMediaDTO socials, ContactDTO contacts) {

Specification<Company> specification = Specification.where(CompanySpecification.regionsIn(regionNames))
Specification<CompanyModel> specification = Specification.where(CompanySpecification.regionsIn(regionNames))
.and(CompanySpecification.citiesIn(cityNames))
.and(CompanySpecification.industrySectorsIn(industrySectorNames))
.and(CompanySpecification.legalFormsIn(legalFormNames))
Expand All @@ -83,12 +80,12 @@ public long countCompaniesByFilters(List<String> regionNames, List<String> cityN
return companyRepository.count(specification);
}

public Page<Company> findRandomUnseenCompanies(String userId, Pageable pageable) {
public Page<CompanyModel> findRandomUnseenCompanies(String userId, Pageable pageable) {
return companyRepository.findRandomUnseenCompanies(userId, pageable);
}

@RateLimiter(name = "scrapService")
public Company scrapCompany(Company company) {
public CompanyModel scrapCompany(CompanyModel company) {
try {
HttpResponse<String> response;
try (HttpClient client = HttpClient.newHttpClient()) {
Expand All @@ -110,7 +107,7 @@ public Company scrapCompany(Company company) {
ObjectMapper objectMapper = new ObjectMapper();
JsonNode jsonNode = objectMapper.readTree(response.body());

Company companyScrapped = new Company();
CompanyModel companyScrapped = new CompanyModel();

// TODO: Verify what's ifNotEmpty means. Currently it still updates the values even if they are empty in the response
setIfNotEmpty(jsonNode, "companyName", companyScrapped::setCompanyName);
Expand All @@ -135,7 +132,7 @@ public Company scrapCompany(Company company) {
}

@CacheEvict(value = "companyCounts", allEntries = true)
public void saveCompany(Company company) {
public void saveCompany(CompanyModel company) {
companyRepository.save(company);
}

Expand Down
Loading