All Downloads are FREE. Search and download functionalities are using the official Maven repository.

fi.evolver.basics.spring.common.ConfigurationController Maven / Gradle / Ivy

There is a newer version: 6.5.1
Show newest version
package fi.evolver.basics.spring.common;

import org.springdoc.core.converters.models.PageableAsQueryParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.data.web.PageableDefault;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;

import fi.evolver.basics.spring.common.entity.Configuration;
import fi.evolver.basics.spring.http.HttpInterceptor;
import fi.evolver.basics.spring.http.MessageType;
import fi.evolver.basics.spring.http.exception.HttpNotFoundException;
import fi.evolver.basics.spring.job.ResultState;
import fi.evolver.basics.spring.log.LogPolicy;
import fi.evolver.basics.spring.log.LogPolicy.Policy;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.media.Content;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.responses.ApiResponses;
import jakarta.validation.Valid;


@RestController
@RequestMapping("/configuration")
public class ConfigurationController {

	private final ConfigurationRepository repository;

	@Autowired
	public ConfigurationController(ConfigurationRepository repository) {
		this.repository = repository;
	}


	@LogPolicy(Policy.NONE)
	@Operation(summary = "Fetch a page of entities")
	@PageableAsQueryParam
	@ApiResponses(value = {
			@ApiResponse(responseCode = "200", description = "A page of entities"),
			@ApiResponse(responseCode = "500", description = "Internal server error", content = @Content)
	})
	@MessageType("Configuration/list")
	@GetMapping
	public Page list(@PageableDefault(size = 100, direction = Direction.DESC, sort = "id") Pageable page) {
		return repository.findAllActive(page);
	}


	@LogPolicy(Policy.NONE)
	@Operation(summary = "Fetch a single entity by id")
	@ApiResponses(value = {
			@ApiResponse(responseCode = "200", description = "The requested entity"),
			@ApiResponse(responseCode = "404", description = "The requested entity was not found", content = @Content),
			@ApiResponse(responseCode = "500", description = "Internal server error", content = @Content)
	})
	@GetMapping("/{id}")
	@MessageType("Configuration/show")
	public Configuration show(@PathVariable long id) {
		return repository.findById(id).orElseThrow(HttpNotFoundException::new);
	}


	@Operation(summary = "Create a new entity or update an existing entity")
	@ApiResponses(value = {
			@ApiResponse(responseCode = "204", description = "The entity was created or updated successfully"),
			@ApiResponse(responseCode = "500", description = "Internal server error")
	})
	@PostMapping
	@MessageType("Configuration/save")
	@ResponseStatus(value = HttpStatus.NO_CONTENT)
	public void save(@RequestBody @Valid Configuration entity) {
		entity.setId(0);
		repository.saveAndFlush(entity);
		HttpInterceptor.setResultState(ResultState.ok("Configuration persisted"));
	}


	@Operation(summary = "Delete an entity by id")
	@ApiResponses(value = {
			@ApiResponse(responseCode = "204", description = "The entity was deleted successfully"),
			@ApiResponse(responseCode = "404", description = "The requested entity was not found"),
			@ApiResponse(responseCode = "500", description = "Internal server error")
	})
	@PostMapping("/{id}/delete")
	@MessageType("Configuration/delete")
	@ResponseStatus(value = HttpStatus.NO_CONTENT)
	public void delete(@PathVariable long id) {
		Configuration configuration = repository.findById(id).orElseThrow(HttpNotFoundException::new).makeInactive();
		repository.saveAndFlush(configuration);
		HttpInterceptor.setResultState(ResultState.ok("Configuration %s marked inactive", configuration.getKey()));
	}

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy