SymptomController.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.gringlobal.api.v2.impl;

import java.io.IOException;

import javax.validation.Valid;

import org.genesys.filerepository.InvalidRepositoryFileDataException;
import org.genesys.filerepository.InvalidRepositoryPathException;
import org.gringlobal.api.model.InventorySymptomMapAttachDTO;
import org.gringlobal.api.model.InventorySymptomMapDTO;
import org.gringlobal.api.model.SymptomAttachDTO;
import org.gringlobal.api.model.SymptomDTO;
import org.gringlobal.api.ApiBaseController;
import org.gringlobal.api.v2.CRUDController;
import org.gringlobal.api.v2.FilteredCRUDController;
import org.gringlobal.api.v2.facade.InventorySymptomMapApiService;
import org.gringlobal.api.v2.facade.InventorySymptomMapAttachApiService;
import org.gringlobal.api.v2.facade.InventorySymptomMapAttachApiService.InventorySymptomMapAttachRequestDTO;
import org.gringlobal.api.v2.facade.SymptomApiService;
import org.gringlobal.api.v2.facade.SymptomAttachApiService;
import org.gringlobal.model.community.InventorySymptomMap;
import org.gringlobal.model.community.InventorySymptomMapAttach;
import org.gringlobal.model.community.Symptom;
import org.gringlobal.model.community.SymptomAttach;
import org.gringlobal.service.SymptomAttachmentService;
import org.gringlobal.service.filter.SymptomFilter;
import org.springframework.beans.factory.annotation.Autowired;
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.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.RequestPart;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;

@RestController("symptomApi2")
@RequestMapping(SymptomController.API_URL)
@PreAuthorize("isAuthenticated()")
@Tag(name = "PlantHealth")
public class SymptomController extends FilteredCRUDController<SymptomDTO, Symptom, SymptomApiService, SymptomFilter> {

	/** The Constant API_URL. */
	public static final String API_URL = ApiBaseController.APIv2_BASE + "/symptom";

	@PostMapping(value = "/attach/{symptomId}", produces = { MediaType.APPLICATION_JSON_VALUE })
	@Operation(operationId = "uploadFile", description = "Attach symptom file", summary = "Attach file")
	public SymptomAttachDTO uploadFile(@PathVariable(name = "symptomId") final Long symptomId, @RequestPart(name = "file") final MultipartFile file,
		@RequestPart(name = "metadata") final SymptomAttachmentService.SymptomAttachmentRequest metadata) throws InvalidRepositoryPathException, InvalidRepositoryFileDataException, IOException {

		return serviceFacade.uploadFile(symptomId, file, metadata);
	}

	@DeleteMapping(value = "/attach/{symptomId}/{attachmentId}", produces = { MediaType.APPLICATION_JSON_VALUE })
	@Operation(operationId = "removeFile", description = "Remove attached file", summary = "Remove file")
	public SymptomAttachDTO removeFile(@PathVariable(name = "symptomId") final Long symptomId, @PathVariable(name = "attachmentId") final Long attachmentId) {
		return serviceFacade.removeFile(symptomId, attachmentId);
	}

	@RestController("symptomAttachApi2")
	@RequestMapping(SymptomController.SymptomAttachController.API_URL)
	@PreAuthorize("isAuthenticated()")
	@Tag(name = "PlantHealth")
	public static class SymptomAttachController extends CRUDController<SymptomAttachDTO, SymptomAttach, SymptomAttachApiService> {
		/** The Constant API_URL. */
		public static final String API_URL = SymptomController.API_URL + "/attach/meta";

		@Override
		@Operation(operationId = "createSymptomAttach", description = "Create SymptomAttach", summary = "Create")
		public SymptomAttachDTO create(@RequestBody SymptomAttachDTO entity) {
			// Throws UnsupportedOperationException
			return super.create(entity);
		}

		@Override
		@Operation(operationId = "updateSymptomAttach", description = "Update an existing record", summary = "Update")
		public SymptomAttachDTO update(@RequestBody SymptomAttachDTO entity) {
			return super.update(entity);
		}

		@Override
		@Operation(operationId = "getSymptomAttach", description = "Get record by ID", summary = "Get")
		public SymptomAttachDTO get(@PathVariable long id) {
			return super.get(id);
		}

		@Override
		@Operation(operationId = "deleteSymptomAttach", description = "Delete existing record by ID", summary = "Delete")
		public SymptomAttachDTO remove(@PathVariable long id) {
			return super.remove(id);
		}
	}

	@RestController("inventorySymptomMapApi2")
	@RequestMapping(SymptomController.InventorySymptomMapController.API_URL)
	@PreAuthorize("isAuthenticated()")
	@Tag(name = "PlantHealth")
	public static class InventorySymptomMapController extends CRUDController<InventorySymptomMapDTO, InventorySymptomMap, InventorySymptomMapApiService> {
		/** The Constant API_URL. */
		public static final String API_URL = SymptomController.API_URL + "/inventory";

		@Autowired
		private InventorySymptomMapAttachApiService inventorySymptomMapAttachApiService;

		@PostMapping(value = "/attach/{inventorySymptomId}", produces = { MediaType.APPLICATION_JSON_VALUE })
		@Operation(operationId = "uploadFile", description = "Attach inventorySymptom file", summary = "Attach file")
		public InventorySymptomMapAttachDTO uploadFile(@PathVariable(name = "inventorySymptomId") final Long inventorySymptomId, @RequestPart(name = "file") final MultipartFile file,
			@RequestPart(name = "metadata") final @Valid InventorySymptomMapAttachRequestDTO metadata) throws InvalidRepositoryPathException, InvalidRepositoryFileDataException, IOException {

			return inventorySymptomMapAttachApiService.uploadFile(inventorySymptomId, file, metadata);
		}

		@DeleteMapping(value = "/attach/{inventorySymptomId}/{attachmentId}", produces = { MediaType.APPLICATION_JSON_VALUE })
		@Operation(operationId = "removeFile", description = "Remove attached file", summary = "Remove file")
		public InventorySymptomMapAttachDTO removeFile(@PathVariable(name = "inventorySymptomId") final Long inventorySymptomId, @PathVariable(name = "attachmentId") final Long attachmentId) {
			return inventorySymptomMapAttachApiService.removeFile(inventorySymptomId, attachmentId);
		}

		@RestController("inventorySymptomMapAttachApi2")
		@RequestMapping(InventorySymptomMapController.InventorySymptomMapAttachController.API_URL)
		@PreAuthorize("isAuthenticated()")
		@Tag(name = "PlantHealth")
		public static class InventorySymptomMapAttachController extends CRUDController<InventorySymptomMapAttachDTO, InventorySymptomMapAttach, InventorySymptomMapAttachApiService> {
			/** The Constant API_URL. */
			public static final String API_URL = InventorySymptomMapController.API_URL + "/attach/meta";

			@Override
			@Operation(operationId = "createInventorySymptomMapAttach", description = "Create InventorySymptomMapAttach", summary = "Create")
			public InventorySymptomMapAttachDTO create(@RequestBody InventorySymptomMapAttachDTO entity) {
				// Throws UnsupportedOperationException
				return super.create(entity);
			}

			@Override
			@Operation(operationId = "updateInventorySymptomMapAttach", description = "Update an existing record", summary = "Update")
			public InventorySymptomMapAttachDTO update(@RequestBody InventorySymptomMapAttachDTO entity) {
				return super.update(entity);
			}

			@Override
			@Operation(operationId = "getInventorySymptomMapAttach", description = "Get record by ID", summary = "Get")
			public InventorySymptomMapAttachDTO get(@PathVariable long id) {
				return super.get(id);
			}

			@Override
			@Operation(operationId = "deleteInventorySymptomMapAttach", description = "Delete existing record by ID", summary = "Delete")
			public InventorySymptomMapAttachDTO remove(@PathVariable long id) {
				return super.remove(id);
			}
		}
	}
}