CRUDController.java
/*
* Copyright 2021 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;
import javax.validation.Valid;
import javax.validation.constraints.NotNull;
import org.genesys.blocks.model.EmptyModel;
import org.genesys.server.service.CRUDService;
import org.genesys.server.service.worker.ShortFilterProcessor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.validation.annotation.Validated;
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.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import io.swagger.v3.oas.annotations.Operation;
/**
* A basic API controller for CRUD operations
*
* @param <T> the entity type
* @param <S> the CRUD service
*/
@Validated
public abstract class CRUDController<T extends EmptyModel, S extends CRUDService<T>> extends ApiBaseController {
public static final String ENDPOINT_ID = "/{id:\\d+}";
@Autowired
protected ShortFilterProcessor shortFilterProcessor;
@Autowired
protected S crudService;
public CRUDController() {
super();
}
/**
* Get entity by id
*
* @param id the id
* @return the loaded record
*/
@GetMapping(value = ENDPOINT_ID, produces = { MediaType.APPLICATION_JSON_VALUE })
@Operation(operationId = "get", description = "Get record by ID", summary = "Get")
public T get(@PathVariable("id") final long id) {
return crudService.load(id);
}
/**
* Remove the entity.
*
* @param entity the entity for deleting
* @return the removed record
*/
@DeleteMapping(produces = { MediaType.APPLICATION_JSON_VALUE })
@Operation(operationId = "remove", description = "Delete given existing record", summary = "Delete")
public T remove(@RequestBody @Valid @NotNull final T entity) {
return crudService.remove(crudService.reload(entity));
}
/**
* Register a new entity.
*
* @param entity the site
* @return the recorded record
*/
@PutMapping(value = "", produces = { MediaType.APPLICATION_JSON_VALUE })
@Operation(operationId = "create", description = "Create a record", summary = "Add")
public T create(@RequestBody @Valid @NotNull final T entity) {
return crudService.load(crudService.create(entity).getId());
}
/**
* Update the entity.
*
* @param entity entity with updates
* @return the updated record
*/
@PostMapping(value = "", produces = { MediaType.APPLICATION_JSON_VALUE })
@Operation(operationId = "update", description = "Update an existing record", summary = "Update")
public T update(@RequestBody @Valid @NotNull final T entity) {
return crudService.update(entity, crudService.reload(entity));
}
}