NetworkController.java

/*
 * Copyright 2025 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.genesys.server.api.v2.impl;

import io.swagger.annotations.Api;
import org.genesys.server.api.ApiBaseController;
import org.genesys.server.api.Pagination;
import org.genesys.server.api.v2.facade.PGRFANetworkApiService;
import org.genesys.server.api.v2.model.impl.FaoInstituteDTO;
import org.genesys.server.api.v2.model.impl.PGRFANetworkDTO;
import org.genesys.server.api.v2.model.impl.TranslatedArticleDTO;
import org.genesys.server.exception.NotFoundElement;
import org.genesys.server.exception.SearchException;
import org.springdoc.api.annotations.ParameterObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
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.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.List;
import java.util.Set;

@RestController("networkApi2")
@PreAuthorize("isAuthenticated()")
@RequestMapping(value = { NetworkController.CONTROLLER_URL, "/json/v2/network" })
@Api(tags = { "network" })
public class NetworkController extends ApiBaseController {

	// Rest controller base URL
	public static final String CONTROLLER_URL = ApiBaseController.APIv2_BASE + "/network";

	@Autowired
	private PGRFANetworkApiService networkService;

	@GetMapping(value = "", produces = { MediaType.APPLICATION_JSON_VALUE })
	public List<PGRFANetworkDTO> listNetworks(@RequestParam(value = "page", required = false, defaultValue = "1") int page) {
		return networkService.list(PageRequest.of(page - 1, 50)).getContent();
	}

	@PostMapping(value = "", produces = { MediaType.APPLICATION_JSON_VALUE })
	public PGRFANetworkDTO updateNetwork(@RequestBody @Valid PGRFANetworkDTO organizationJson) {
		return networkService.updateNetwork(organizationJson);
	}

	@GetMapping(value = "/{shortName}", produces = { MediaType.APPLICATION_JSON_VALUE })
	public PGRFANetworkDTO getNetwork(@PathVariable("shortName") String shortName) {
		PGRFANetworkDTO network = networkService.getNetwork(shortName);
		if (network == null)
			throw new NotFoundElement("Network not found");
		return network;
	}

	@GetMapping(value = "/{shortName}/details", produces = { MediaType.APPLICATION_JSON_VALUE })
	public PGRFANetworkApiService.NetworkDetailsDTO getNetworkDetails(@PathVariable("shortName") String shortName, @RequestParam(value = "language", defaultValue = "en") String language) throws SearchException {
		return networkService.getNetworkDetails(shortName, language);
	}

	@GetMapping(value = "/{shortName}/blurb/{language}", produces = { MediaType.APPLICATION_JSON_VALUE })
	public TranslatedArticleDTO getBlurb(@PathVariable("shortName") String shortName, @PathVariable("language") String language) {
		return networkService.getBlurb(shortName, language);
	}

	@DeleteMapping(value = "/{shortName}", produces = { MediaType.APPLICATION_JSON_VALUE })
	public PGRFANetworkDTO deleteNetwork(@PathVariable("shortName") String shortName) {
		return networkService.deleteNetwork(shortName);
	}

	@GetMapping(value = "/{shortName}/institutes/codes", produces = { MediaType.APPLICATION_JSON_VALUE })
	public Set<String> getNetworkInstitutesCodes(@PathVariable("shortName") String shortName) {
		return networkService.getNetworkInstitutesCodes(shortName);
	}

	@GetMapping(value = "/{shortName}/institutes", produces = { MediaType.APPLICATION_JSON_VALUE })
	public Page<FaoInstituteDTO> getNetworkInstitutes(@PathVariable("shortName") String shortName, @ParameterObject final Pagination page) {
		return networkService.getNetworkInstitutes(shortName, page.toPageRequest(MAX_PAGE_SIZE, DEFAULT_PAGE_SIZE, Sort.Direction.DESC, "accessionCount"));
	}

	@RequestMapping(value = "/{slug}/add-institutes", method = { RequestMethod.POST, RequestMethod.PUT }, produces = { MediaType.APPLICATION_JSON_VALUE })
	public PGRFANetworkDTO addNetworkInstitutes(@PathVariable(value = "slug") String slug, @RequestBody List<String> instituteList) {
		return networkService.addNetworkInstitutes(slug, instituteList);
	}

	@RequestMapping(value = "/{slug}/set-institutes", method = { RequestMethod.POST, RequestMethod.PUT }, produces = { MediaType.APPLICATION_JSON_VALUE })
	public PGRFANetworkDTO setNetworkInstitutes(@PathVariable(value = "slug") String slug, @RequestBody List<String> instituteList) {
		return networkService.setNetworkInstitutes(slug, instituteList);
	}

}