Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Applying "filter by" on latest and search results page #25

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@

public interface HomepageService {

public List<Blog> getTrending(Integer pageNo);
public List<Blog> getTrending(Integer pageNo, String type);

public List<Blog> getLatest(Integer pageNo);
public List<Blog> getLatest(Integer pageNo, String type);

public List<Blog> getTopDiscussed(Integer pageNo);
public List<Blog> getTopDiscussed(Integer pageNo, String type);

public List<Blog> getTopLiked(Integer pageNo);
public List<Blog> getTopLiked(Integer pageNo, String type);

List<CompanyCount> getBlogsCountByCompany();

Expand Down
20 changes: 10 additions & 10 deletions src/main/java/TechVault/services/homepage/HomepageServiceImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,27 +23,27 @@ public class HomepageServiceImpl implements HomepageService {
private HomepageDao homepageDao;

@Override
public List<Blog> getTrending(Integer pageNo) {
return getBlogs(pageNo, VIEWED);
public List<Blog> getTrending(Integer pageNo, String type) {
return getBlogs(pageNo, type, VIEWED);
}

@Override
public List<Blog> getLatest(Integer pageNo) {
return getBlogs(pageNo, DATE);
public List<Blog> getLatest(Integer pageNo, String type) {
return getBlogs(pageNo, type, DATE);
}

@Override
public List<Blog> getTopDiscussed(Integer pageNo) {
return getBlogs(pageNo, DISCUSSED);
public List<Blog> getTopDiscussed(Integer pageNo, String type) {
return getBlogs(pageNo, type, DISCUSSED);
}

@Override
public List<Blog> getTopLiked(Integer pageNo) {
return getBlogs(pageNo, LIKED);
public List<Blog> getTopLiked(Integer pageNo, String type) {
return getBlogs(pageNo, type, LIKED);
}

private List<Blog> getBlogs(Integer pageNo, String sortBy) {
return homepageDao.getBlog(pageNo, PAGESIZE, sortBy);
private List<Blog> getBlogs(Integer pageNo, String type, String sortBy) {
return homepageDao.getBlog(pageNo, type, PAGESIZE, sortBy);
}

@Override
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/TechVault/services/homepage/SearchService.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import java.util.List;

public interface SearchService {
public List<Blog> searchByCompanyNames(List<String> companyNames, Integer pageNo);
public List<Blog> searchByCompanyNames(List<String> companyNames, String type, Integer pageNo);

public List<Blog> searchByKeywords(List<String> keywords, Integer pageNo);
public List<Blog> searchByKeywords(List<String> keywords, String type, Integer pageNo);
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,12 @@ public class SearchServiceImpl implements SearchService {
private HomepageDao homepageDao;

@Override
public List<Blog> searchByCompanyNames(List<String> companyNames, Integer pageNo) {
return homepageDao.getBlogsByCompanies(companyNames, pageNo, PAGE_SIZE, DATE);
public List<Blog> searchByCompanyNames(List<String> companyNames, String type, Integer pageNo) {
return homepageDao.getBlogsByCompanies(companyNames, type, pageNo, PAGE_SIZE, DATE);
}

@Override
public List<Blog> searchByKeywords(List<String> keywords, Integer pageNo) {
return homepageDao.getContentsByKeywords(keywords, pageNo, PAGE_SIZE, DATE);
public List<Blog> searchByKeywords(List<String> keywords, String type, Integer pageNo) {
return homepageDao.getContentsByKeywords(keywords, type, pageNo, PAGE_SIZE, DATE);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,11 @@ public class HomepageController {
*/
@CrossOrigin
@RequestMapping(method = RequestMethod.GET, value = "/liked")
public ResponseEntity<?> getTopLiked(@RequestParam(defaultValue = "0") Integer pageNo) {
public ResponseEntity<?> getTopLiked(@RequestParam(defaultValue = "0") Integer pageNo,
@RequestParam(defaultValue = "all") String type) {
List<Blog> blogs = null;
try {
blogs = service.getTopLiked(pageNo);
blogs = service.getTopLiked(pageNo, type);
} catch (Exception e) {
LOGGER.error("Unable to get blogs ", e);
return new ResponseEntity<>("ERROR", HttpStatus.INTERNAL_SERVER_ERROR);
Expand All @@ -46,10 +47,11 @@ public ResponseEntity<?> getTopLiked(@RequestParam(defaultValue = "0") Integer p
*/
@CrossOrigin
@RequestMapping(method = RequestMethod.GET, value = "/discussed")
public ResponseEntity<?> getTopDiscussed(@RequestParam(defaultValue = "0") Integer pageNo) {
public ResponseEntity<?> getTopDiscussed(@RequestParam(defaultValue = "0") Integer pageNo,
@RequestParam(defaultValue = "all") String type) {
List<Blog> blogs = null;
try {
blogs = service.getTopDiscussed(pageNo);
blogs = service.getTopDiscussed(pageNo, type);
} catch (Exception e) {
LOGGER.error("Unable to get blogs ", e);
return new ResponseEntity<>("ERROR", HttpStatus.INTERNAL_SERVER_ERROR);
Expand All @@ -64,10 +66,11 @@ public ResponseEntity<?> getTopDiscussed(@RequestParam(defaultValue = "0") Integ
*/
@CrossOrigin
@RequestMapping(method = RequestMethod.GET, value = "/latest")
public ResponseEntity<?> getLatest(@RequestParam(defaultValue = "0") Integer pageNo) {
public ResponseEntity<?> getLatest(@RequestParam(defaultValue = "0") Integer pageNo,
@RequestParam(defaultValue = "all") String type) {
List<Blog> blogs = null;
try {
blogs = service.getLatest(pageNo);
blogs = service.getLatest(pageNo, type);
} catch (Exception e) {
LOGGER.error("Unable to get blogs ", e);
return new ResponseEntity<>("ERROR", HttpStatus.INTERNAL_SERVER_ERROR);
Expand All @@ -81,10 +84,11 @@ public ResponseEntity<?> getLatest(@RequestParam(defaultValue = "0") Integer pag
*/
@CrossOrigin
@RequestMapping(method = RequestMethod.GET, value = "")
public ResponseEntity<?> getTrending(@RequestParam(defaultValue = "0") Integer pageNo) {
public ResponseEntity<?> getTrending(@RequestParam(defaultValue = "0") Integer pageNo,
@RequestParam(defaultValue = "all") String type) {
List<Blog> blogs = null;
try {
blogs = service.getTrending(pageNo);
blogs = service.getTrending(pageNo, type);
} catch (Exception e) {
LOGGER.error("Unable to get blogs ", e);
return new ResponseEntity<>("ERROR", HttpStatus.INTERNAL_SERVER_ERROR);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,11 @@ public class SearchController {
@CrossOrigin
@RequestMapping(method = RequestMethod.POST, value = "/company")
public ResponseEntity<?> searchByCompanyName(@RequestBody @Valid SearchByCompanyOrKeywordRequest searchByCompanyOrKeywordRequest,
@RequestParam(defaultValue = "0") Integer pageNo) {
@RequestParam(defaultValue = "0") Integer pageNo,
@RequestParam(defaultValue = "all") String type) {
List<Blog> blogs = null;
try {
blogs = service.searchByCompanyNames(searchByCompanyOrKeywordRequest.getSearchWords(), pageNo);
blogs = service.searchByCompanyNames(searchByCompanyOrKeywordRequest.getSearchWords(), type, pageNo);
} catch (Exception e) {
return new ResponseEntity<>("ERROR", HttpStatus.INTERNAL_SERVER_ERROR);
}
Expand All @@ -45,10 +46,11 @@ public ResponseEntity<?> searchByCompanyName(@RequestBody @Valid SearchByCompany
@CrossOrigin
@RequestMapping(method = RequestMethod.POST, value = "/keyword")
public ResponseEntity<?> searchByKeyword(@RequestBody @Valid SearchByCompanyOrKeywordRequest searchByCompanyOrKeywordRequest,
@RequestParam(defaultValue = "0") Integer pageNo) {
@RequestParam(defaultValue = "0") Integer pageNo,
@RequestParam(defaultValue = "all") String type) {
List<Blog> blogs = null;
try {
blogs = service.searchByKeywords(searchByCompanyOrKeywordRequest.getSearchWords(), pageNo);
blogs = service.searchByKeywords(searchByCompanyOrKeywordRequest.getSearchWords(), type, pageNo);
} catch (Exception e) {
return new ResponseEntity<>("ERROR", HttpStatus.INTERNAL_SERVER_ERROR);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,13 @@

public interface HomepageDao {

List<Blog> getBlog(Integer pageNo, Integer pageSize, String sortBy);
List<Blog> getBlog(Integer pageNo, String type, Integer pageSize, String sortBy);

List<CompanyCount> getBlogsCountByCompany();

List<KeywordCount> getBlogsCountByKeywords();

List<Blog> getBlogsByCompanies(List<String> companyName, Integer pageNo, Integer pageSize, String sortBy);
List<Blog> getBlogsByCompanies(List<String> companyName, String type, Integer pageNo, Integer pageSize, String sortBy);

List<Blog> getContentsByKeywords(List<String> keyword, Integer pageNo, Integer pageSize, String sortBy);
List<Blog> getContentsByKeywords(List<String> keyword, String type, Integer pageNo, Integer pageSize, String sortBy);
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,24 +22,38 @@ public class HomepageDaoImpl implements HomepageDao {
private HomepageRepository repo;

@Override
public List<Blog> getBlog(Integer pageNo, Integer pageSize, String sortBy) {
Pageable pageable = PageRequest.of(pageNo, pageSize, Sort.by(sortBy).descending());
Page<Blog> page = repo.findAll(pageable);

public List<Blog> getBlog(Integer pageNo, String type, Integer pageSize, String sortBy) {
Pageable pageable = pageable = PageRequest.of(pageNo, pageSize, Sort.by(sortBy).descending());
Page<Blog> page;
if (type.equals("all")) {
page = repo.findAll(pageable);
} else {
page = repo.findByType(type, pageable);
}
return page.getContent();
}

@Override
public List<Blog> getBlogsByCompanies(List<String> companyNames, Integer pageNo, Integer pageSize, String sortBy) {
public List<Blog> getBlogsByCompanies(List<String> companyNames, String type, Integer pageNo, Integer pageSize, String sortBy) {
Pageable pageable = PageRequest.of(pageNo, pageSize, Sort.by(sortBy).descending());
Page<Blog> page = repo.findByCompanyIn(companyNames, pageable);
Page<Blog> page = null;
if (type.equals("all")) {
page = repo.findByCompanyIn(companyNames, pageable);
} else {
page = repo.findByTypeAndCompanyIn(type, companyNames, pageable);
}
return page.getContent();
}

@Override
public List<Blog> getContentsByKeywords(List<String> keyword, Integer pageNo, Integer pageSize, String sortBy) {
public List<Blog> getContentsByKeywords(List<String> keyword, String type, Integer pageNo, Integer pageSize, String sortBy) {
Pageable pageable = PageRequest.of(pageNo, pageSize, Sort.by(sortBy).descending());
Page<Blog> page = repo.findAnyOfTheseValues(keyword, pageable);
Page<Blog> page = null;
if (type.equals("all")) {
page = repo.findAnyOfTheseValues(keyword, pageable);
} else {
page = repo.findByTypeAndAnyOfTheseValues(keyword, type, pageable);
}
return page.getContent();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,13 @@ public interface HomepageRepository extends PagingAndSortingRepository<Blog, Int

Page<Blog> findByCompanyIn(List<String> companyName, Pageable pageable);

Page<Blog> findByTypeAndCompanyIn(String type, List<String> companyName, Pageable pageable);

Page<Blog> findByType(String type, Pageable pageable);

@Query(value = "{ 'keywords' : {$all : [?0] }}")
Page<Blog> findAnyOfTheseValues(List<String> arrayValues, Pageable pageable);

@Query(value = "{$and: [{ 'keywords' : {$all : [?0] }}, { 'type' : ?1}]}")
Page<Blog> findByTypeAndAnyOfTheseValues(List<String> arrayValues, String type, Pageable pageable);
}