CodeValuesController.java

/*
 * Copyright 2020 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.v1.impl;

import javax.validation.Valid;
import javax.validation.constraints.NotNull;

import org.gringlobal.api.v1.ApiBaseController;
import org.gringlobal.api.v1.TranslatedCRUDController;
import org.gringlobal.model.CodeValue;
import org.gringlobal.model.CodeValueLang;
import org.gringlobal.service.CodeValueService;
import org.gringlobal.service.CodeValueTranslationService.TranslatedCodeValue;
import org.gringlobal.service.filter.CodeValueFilter;
import org.gringlobal.service.impl.CodeValueServiceImpl;
import org.springframework.http.MediaType;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.DeleteMapping;
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.RestController;

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

import java.util.List;

@RestController("codeValuesApi1")
@RequestMapping(CodeValuesController.API_URL)
@PreAuthorize("isAuthenticated()")
@Tag(name = "CodeValues")
public class CodeValuesController extends TranslatedCRUDController<CodeValue, TranslatedCodeValue, CodeValueLang, CodeValueService, CodeValueFilter> {

	/** The Constant API_URL. */
	public static final String API_URL = ApiBaseController.APIv1_BASE + "/codes";

	/**
	 * Replace an existing value with another value
	 */
	@PostMapping(value = "/replace", produces = { MediaType.APPLICATION_JSON_VALUE })
	@Operation(operationId = "replace", description = "Replace and remove a code value", summary = "Replace")
	public CodeValue replaceAndRemove(@RequestBody @Valid @NotNull final ReplaceAndRemove replaceArgs) {
		return translatedCrudService.replaceAndRemove(replaceArgs.current, replaceArgs.replacement);
	}

	/**
	 * Delete given CodeValue
	 */
	@DeleteMapping(value = "", produces = { MediaType.APPLICATION_JSON_VALUE })
	@Operation(operationId = "deleteGiven", description = "Delete given CodeValue", summary = "Delete given")
	public CodeValue remove(@RequestBody @Valid @NotNull final CodeValue target) {
		return translatedCrudService.remove(target);
	}

	/**
	 * Get CodeValue statistics list
	 */
	@GetMapping(value = "/{groupName}/stats", produces = { MediaType.APPLICATION_JSON_VALUE })
	@Operation(operationId = "stats", description = "Get current usage numbers for this code value", summary = "Statistics")
	public List<CodeValueServiceImpl.CodeValueStats> getStatistics(@PathVariable String groupName) {
		return translatedCrudService.getStatistics(groupName);
	}

	public static class ReplaceAndRemove {
		@NotNull
		public CodeValue current;
		@NotNull
		public CodeValue replacement;
	}
}