package com.mycompany.myapp.web.rest;
import com.codahale.metrics.annotation.Timed;
import com.mycompany.myapp.domain.Operation;
import com.mycompany.myapp.repository.OperationRepository;
import com.mycompany.myapp.web.rest.util.HeaderUtil;
import com.mycompany.myapp.web.rest.util.PaginationUtil;
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.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import javax.inject.Inject;
import javax.validation.Valid;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.List;
import java.util.Optional;
/**
* REST controller for managing Operation.
*/
@RestController
@RequestMapping("/api")
public class OperationResource {
private final Logger log = LoggerFactory.getLogger(OperationResource.class);
@Inject
private OperationRepository operationRepository;
/**
* POST /operations -> Create a new operation.
*/
@RequestMapping(value = "/operations",
method = RequestMethod.POST,
produces = MediaType.APPLICATION_JSON_VALUE)
@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().header("Failure", "A new operation cannot already have an ID").body(null);
}
Operation result = operationRepository.save(operation);
return ResponseEntity.created(new URI("/api/operations/" + result.getId()))
.headers(HeaderUtil.createEntityCreationAlert("operation", result.getId().toString()))
.body(result);
}
/**
* PUT /operations -> Updates an existing operation.
*/
@RequestMapping(value = "/operations",
method = RequestMethod.PUT,
produces = MediaType.APPLICATION_JSON_VALUE)
@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);
return ResponseEntity.ok()
.headers(HeaderUtil.createEntityUpdateAlert("operation", operation.getId().toString()))
.body(result);
}
/**
* GET /operations -> get all the operations.
*/
@RequestMapping(value = "/operations",
method = RequestMethod.GET,
produces = MediaType.APPLICATION_JSON_VALUE)
@Timed
public ResponseEntity<List<Operation>> getAllOperations(Pageable pageable)
throws URISyntaxException {
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.
*/
@RequestMapping(value = "/operations/{id}",
method = RequestMethod.GET,
produces = MediaType.APPLICATION_JSON_VALUE)
@Timed
public ResponseEntity<Operation> getOperation(@PathVariable Long id) {
log.debug("REST request to get Operation : {}", id);
return Optional.ofNullable(operationRepository.findOneWithEagerRelationships(id))
.map(operation -> new ResponseEntity<>(
operation,
HttpStatus.OK))
.orElse(new ResponseEntity<>(HttpStatus.NOT_FOUND));
}
/**
* DELETE /operations/:id -> delete the "id" operation.
*/
@RequestMapping(value = "/operations/{id}",
method = RequestMethod.DELETE,
produces = MediaType.APPLICATION_JSON_VALUE)
@Timed
public ResponseEntity<Void> deleteOperation(@PathVariable Long id) {
log.debug("REST request to delete Operation : {}", id);
operationRepository.delete(id);
return ResponseEntity.ok().headers(HeaderUtil.createEntityDeletionAlert("operation", id.toString())).build();
}
}