GenotypeController.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 io.swagger.v3.oas.annotations.Operation;
import org.genesys.server.api.ApiBaseController;
import org.genesys.server.api.v2.FilteredCRUDController;
import org.genesys.server.api.v2.MultiOp;
import org.genesys.server.api.v2.facade.GenotypeApiService;
import org.genesys.server.api.v2.model.impl.GenotypeDTO;
import org.genesys.server.model.impl.Genotype;
import org.genesys.server.service.filter.GenotypeFilter;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RequestPart;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.validation.Valid;
import javax.validation.constraints.NotNull;
import java.util.List;

/**
 * Genotype API v2
 */
@RestController("genotypeApi2")
@RequestMapping(GenotypeController.CONTROLLER_URL)
@Api(tags = { "accession" })
public class GenotypeController extends FilteredCRUDController<GenotypeDTO, Genotype, GenotypeApiService, GenotypeFilter> {

	public static final String CONTROLLER_URL = ApiBaseController.APIv2_BASE + "/genotype-ids";

	@PostMapping(value = "", produces = { MediaType.APPLICATION_JSON_VALUE })
	@Operation(description = "Create a record", summary = "Add")
	@Override
	public GenotypeDTO create(@RequestBody @Valid @NotNull final GenotypeDTO entity) {
		throw new UnsupportedOperationException("Individual records management is not supported");
	}

	@PutMapping(value = "", produces = { MediaType.APPLICATION_JSON_VALUE })
	@Operation(description = "Update an existing record", summary = "Update")
	@Override
	public GenotypeDTO update(@RequestBody final GenotypeDTO entity) {
		throw new UnsupportedOperationException("Individual records management is not supported");
	}

	@PutMapping(value = "/many", produces = { MediaType.APPLICATION_JSON_VALUE })
	@Operation(description = "Update an existing record", summary = "Update")
	public MultiOp<GenotypeDTO> updateMany(@RequestBody @Valid @NotNull final List<GenotypeDTO> updates) {
		throw new UnsupportedOperationException("Record updates are not supported.");
	}

	@DeleteMapping(value = ENDPOINT_ID, produces = { MediaType.APPLICATION_JSON_VALUE })
	@Operation(description = "Delete existing record by ID", summary = "Delete")
	@Override
	public GenotypeDTO remove(@PathVariable("id") final long id) {
		throw new UnsupportedOperationException("Individual records management is not supported");
	}

	@PostMapping(value = "/delete", produces = { MediaType.APPLICATION_JSON_VALUE })
	@Operation(description = "Delete records by filter", summary = "DeleteByFilter")
	public MultiOp<GenotypeDTO> remove(@RequestBody final GenotypeFilter filter) {
		return serviceFacade.remove(filter);
	}

	/**
	 * Upload genotypes from a CSV file 
	 *
	 * @param file the csv file with Genotypes data to upload
	 * @param quotechar the csv file quotechar
	 * @param separator the csv file separator
	 */
	@PostMapping(value = "/upload")
	public void uploadGenotypeIDs(@RequestPart(name = "file") final MultipartFile file,
		@RequestParam(required = false, defaultValue = "\t") char separator, 
		@RequestParam(required = false, defaultValue = "\"") char quotechar) 
		throws Exception {

		serviceFacade.uploadGenotypeIDs(file.getInputStream(), separator, quotechar);
	}
}