package io.github.jhipster.sample.web.rest;
import com.codahale.metrics.annotation.Timed;
import io.github.jhipster.sample.domain.Operation;
import io.github.jhipster.sample.repository.OperationRepository;
import io.github.jhipster.sample.repository.search.OperationSearchRepository;
import io.github.jhipster.sample.web.rest.util.HeaderUtil;
import io.github.jhipster.sample.web.rest.util.PaginationUtil;
import io.swagger.annotations.ApiParam;
import io.github.jhipster.web.util.ResponseUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import javax.validation.Valid;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;
import java.util.stream.StreamSupport;
import static org.elasticsearch.index.query.QueryBuilders.*;
/**
* REST controller for managing Operation.
*/
@RestController
@RequestMapping("/api")
public class OperationResource {
private final Logger log = LoggerFactory.getLogger(OperationResource.class);
private static final String ENTITY_NAME = "operation";
private final OperationRepository operationRepository;
private final OperationSearchRepository operationSearchRepository;
public OperationResource(OperationRepository operationRepository, OperationSearchRepository operationSearchRepository) {
this.operationRepository = operationRepository;
this.operationSearchRepository = operationSearchRepository;
}
/**
* POST /operations : Create a new operation.
*
* @param operation the operation to create
* @return the ResponseEntity with status 201 (Created) and with body the new operation, or with status 400 (Bad Request) if the operation has already an ID
* @throws URISyntaxException if the Location URI syntax is incorrect
*/
@PostMapping("/operations")
@Timed
public ResponseEntity<Operation> createOperation(@Valid @RequestBody Operation operation) throws URISyntaxException {
log.debug("REST request to save Operation : {}", operation);
if (operation.getId() != null) {
return ResponseEntity.badRequest().headers(HeaderUtil.createFailureAlert(ENTITY_NAME, "idexists", "A new operation cannot already have an ID")).body(null);
}
Operation result = operationRepository.save(operation);
operationSearchRepository.save(result);
return ResponseEntity.created(new URI("/api/operations/" + result.getId()))
.headers(HeaderUtil.createEntityCreationAlert(ENTITY_NAME, result.getId().toString()))
.body(result);
}
/**
* PUT /operations : Updates an existing operation.
*
* @param operation the operation to update
* @return the ResponseEntity with status 200 (OK) and with body the updated operation,
* or with status 400 (Bad Request) if the operation is not valid,
* or with status 500 (Internal Server Error) if the operation couldnt be updated
* @throws URISyntaxException if the Location URI syntax is incorrect
*/
@PutMapping("/operations")
@Timed
public ResponseEntity<Operation> updateOperation(@Valid @RequestBody Operation operation) throws URISyntaxException {
log.debug("REST request to update Operation : {}", operation);
if (operation.getId() == null) {
return createOperation(operation);
}
Operation result = operationRepository.save(operation);
operationSearchRepository.save(result);
return ResponseEntity.ok()
.headers(HeaderUtil.createEntityUpdateAlert(ENTITY_NAME, operation.getId().toString()))
.body(result);
}
/**
* GET /operations : get all the operations.
*
* @param pageable the pagination information
* @return the ResponseEntity with status 200 (OK) and the list of operations in body
*/
@GetMapping("/operations")
@Timed
public ResponseEntity<List<Operation>> getAllOperations(@ApiParam Pageable pageable) {
log.debug("REST request to get a page of Operations");
Page<Operation> page = operationRepository.findAll(pageable);
HttpHeaders headers = PaginationUtil.generatePaginationHttpHeaders(page, "/api/operations");
return new ResponseEntity<>(page.getContent(), headers, HttpStatus.OK);
}
/**
* GET /operations/:id : get the "id" operation.
*
* @param id the id of the operation to retrieve
* @return the ResponseEntity with status 200 (OK) and with body the operation, or with status 404 (Not Found)
*/
@GetMapping("/operations/{id}")
@Timed
public ResponseEntity<Operation> getOperation(@PathVariable Long id) {
log.debug("REST request to get Operation : {}", id);
Operation operation = operationRepository.findOneWithEagerRelationships(id);
return ResponseUtil.wrapOrNotFound(Optional.ofNullable(operation));
}
/**
* DELETE /operations/:id : delete the "id" operation.
*
* @param id the id of the operation to delete
* @return the ResponseEntity with status 200 (OK)
*/
@DeleteMapping("/operations/{id}")
@Timed
public ResponseEntity<Void> deleteOperation(@PathVariable Long id) {
log.debug("REST request to delete Operation : {}", id);
operationRepository.delete(id);
operationSearchRepository.delete(id);
return ResponseEntity.ok().headers(HeaderUtil.createEntityDeletionAlert(ENTITY_NAME, id.toString())).build();
}
/**
* SEARCH /_search/operations?query=:query : search for the operation corresponding
* to the query.
*
* @param query the query of the operation search
* @param pageable the pagination information
* @return the result of the search
*/
@GetMapping("/_search/operations")
@Timed
public ResponseEntity<List<Operation>> searchOperations(@RequestParam String query, @ApiParam Pageable pageable) {
log.debug("REST request to search for a page of Operations for query {}", query);
Page<Operation> page = operationSearchRepository.search(queryStringQuery(query), pageable);
HttpHeaders headers = PaginationUtil.generateSearchPaginationHttpHeaders(query, page, "/api/_search/operations");
return new ResponseEntity<>(page.getContent(), headers, HttpStatus.OK);
}
}