Commit 3c4b6b99 authored by Matija Obreza's avatar Matija Obreza

Merge branch 'ui-16-institutes-public-pages'

-  Api for institute pages
parents 154da81e 1fc0f171
/*
* Copyright 2018 Global Crop Diversity Trust
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.genesys2.server.api.v1;
import com.fasterxml.jackson.annotation.JsonView;
import io.swagger.annotations.Api;
import org.genesys.blocks.model.JsonViews;
import org.genesys.catalog.api.FilteredPage;
import org.genesys.catalog.service.ShortFilterService;
import org.genesys2.server.api.ApiBaseController;
import org.genesys2.server.model.impl.FaoInstitute;
import org.genesys2.server.service.InstituteService;
import org.genesys2.server.service.filter.InstituteFilter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.http.MediaType;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import java.io.IOException;
/**
* Institute API v1
*/
@RestController("instituteApi1")
@PreAuthorize("isAuthenticated()")
@RequestMapping(InstituteController.API_BASE)
@Api(tags = { "institute" })
public class InstituteController {
/** The Constant API_BASE. */
public static final String API_BASE = ApiBaseController.APIv1_BASE + "/wiews";
@Autowired
private InstituteService instituteService;
/** The short filter service. */
@Autowired
protected ShortFilterService shortFilterService;
/**
* Gets the institute
*
* @param code the code
* @return the institute
*/
@GetMapping(value = "/{code}", produces = { MediaType.APPLICATION_JSON_VALUE })
@JsonView({ JsonViews.Protected.class })
public FaoInstitute getByCode(@PathVariable("code") final String code) {
return instituteService.getInstitute(code);
}
/**
* List institutes by filterCode or filter
*
* @param page the page
* @param pageSize the page size
* @param direction the direction
* @param sort the sort
* @param filterCode short filter code
* @param filter the filter
* @return the page
* @throws IOException
*/
@PostMapping(value = "/list", produces = { MediaType.APPLICATION_JSON_VALUE })
@JsonView({ JsonViews.Public.class })
public FilteredPage<FaoInstitute> list(@RequestParam(name = "p", required = false, defaultValue = "0") final int page,
@RequestParam(name = "l", required = false, defaultValue = "50") final int pageSize,
@RequestParam(name = "d", required = false, defaultValue = "ASC") final Sort.Direction direction,
@RequestParam(name = "s", required = false, defaultValue = "id") final String[] sort,
@RequestParam(name = "f", required = false) String filterCode,
@RequestBody(required = false) InstituteFilter filter) throws IOException {
if (filterCode != null) {
filter = shortFilterService.filterByCode(filterCode, InstituteFilter.class);
} else {
filterCode = shortFilterService.getCode(filter);
}
return new FilteredPage<>(filterCode, filter, instituteService.list(filter, new PageRequest(page, Integer.min(pageSize, 100), direction, sort)));
}
}
......@@ -27,9 +27,10 @@ import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.querydsl.QueryDslPredicateExecutor;
import org.springframework.data.repository.query.Param;
public interface FaoInstituteRepository extends JpaRepository<FaoInstitute, Long> {
public interface FaoInstituteRepository extends JpaRepository<FaoInstitute, Long>, QueryDslPredicateExecutor<FaoInstitute> {
FaoInstitute findByCode(String code);
......
......@@ -21,8 +21,10 @@ import java.util.List;
import java.util.Locale;
import java.util.Map;
import org.genesys2.server.model.elastic.Institute;
import org.genesys2.server.model.impl.Country;
import org.genesys2.server.model.impl.FaoInstitute;
import org.genesys2.server.service.filter.InstituteFilter;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
......@@ -33,6 +35,7 @@ import org.springframework.data.domain.Sort;
public interface InstituteService {
Page<FaoInstitute> list(Pageable p);
Page<FaoInstitute> list(InstituteFilter filter, Pageable page);
FaoInstitute getInstitute(String wiewsCode);
FaoInstitute findInstitute(String wiewsCode);
......
......@@ -36,6 +36,7 @@ import org.genesys2.server.persistence.GenesysLowlevelRepository;
import org.genesys2.server.service.CRMException;
import org.genesys2.server.service.ContentService;
import org.genesys2.server.service.InstituteService;
import org.genesys2.server.service.filter.InstituteFilter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
......@@ -82,6 +83,11 @@ public class InstituteServiceImpl implements InstituteService {
return instituteRepository.listInstitutes(pageable);
}
@Override
public Page<FaoInstitute> list(InstituteFilter filter, Pageable page) {
return instituteRepository.findAll(filter.buildQuery(), page);
}
@Override
public Page<FaoInstitute> listActive(Pageable pageable) {
return instituteRepository.listAllActive(pageable);
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment