Created
June 23, 2018 05:19
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.javabycode.controller; | |
import com.javabycode.model.Fruit; | |
import com.javabycode.service.FruitService; | |
import org.springframework.beans.factory.annotation.Autowired; | |
import org.springframework.http.HttpHeaders; | |
import org.springframework.http.HttpStatus; | |
import org.springframework.http.ResponseEntity; | |
import org.springframework.web.bind.annotation.*; | |
import org.springframework.web.util.UriComponentsBuilder; | |
import java.util.List; | |
@RestController | |
@RequestMapping("/fruits") | |
public class FruitController { | |
@Autowired | |
private FruitService fruitService; | |
@RequestMapping(method = RequestMethod.GET) | |
public ResponseEntity getAll(@RequestParam(value = "offset", defaultValue = "0") int index, | |
@RequestParam(value = "numberOfRecord", defaultValue = "10") int numberOfRecord) { | |
System.out.println("We are in getAll()"); | |
return new ResponseEntity("Test", HttpStatus.OK); | |
} | |
@RequestMapping(value = "{id}", method = RequestMethod.GET) | |
public ResponseEntity<Fruit> get(@PathVariable("id") int id) { | |
//LOG.info("Getting fruit with id: {}", id); | |
Fruit fruit = fruitService.findById(id); | |
if (fruit == null) { | |
return new ResponseEntity<Fruit>(HttpStatus.NOT_FOUND); | |
} | |
return new ResponseEntity<Fruit>(fruit, HttpStatus.OK); | |
} | |
@RequestMapping(method = RequestMethod.POST) | |
public ResponseEntity<Void> create(@RequestBody Fruit fruit, UriComponentsBuilder ucBuilder) { | |
//LOG.info("Creating fruit: {}", fruit); | |
if (fruitService.exists(fruit)) { | |
return new ResponseEntity<Void>(HttpStatus.CONFLICT); | |
} | |
fruitService.create(fruit); | |
HttpHeaders headers = new HttpHeaders(); | |
headers.setLocation(ucBuilder.path("/fruit/{id}").buildAndExpand(fruit.getId()).toUri()); | |
return new ResponseEntity<Void>(headers, HttpStatus.CREATED); | |
} | |
@RequestMapping(value = "{id}", method = RequestMethod.PUT) | |
public ResponseEntity<Fruit> update(@PathVariable int id, @RequestBody Fruit fruit) { | |
//LOG.info("Updating fruit: {}", fruit); | |
Fruit currentFruit = fruitService.findById(id); | |
if (currentFruit == null) { | |
return new ResponseEntity<Fruit>(HttpStatus.NOT_FOUND); | |
} | |
currentFruit.setId(fruit.getId()); | |
currentFruit.setName(fruit.getName()); | |
fruitService.update(fruit); | |
return new ResponseEntity<Fruit>(currentFruit, HttpStatus.OK); | |
} | |
@RequestMapping(value = "{id}", method = RequestMethod.DELETE) | |
public ResponseEntity<Void> delete(@PathVariable("id") int id) { | |
//LOG.info("Deleting fruit with id: {}", id); | |
Fruit fruit = fruitService.findById(id); | |
if (fruit == null) { | |
return new ResponseEntity<Void>(HttpStatus.NOT_FOUND); | |
} | |
fruitService.delete(id); | |
return new ResponseEntity<Void>(HttpStatus.OK); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment