package com.ufund.api.ufundapi.controller; import java.util.logging.Level; import java.util.logging.Logger; import com.ufund.api.ufundapi.model.Cupboard; import com.ufund.api.ufundapi.model.Need; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.*; import java.io.IOException; @RestController @RequestMapping("cupboard") public class CupboardController { private static final Logger LOG = Logger.getLogger(CupboardController.class.getName()); private Cupboard cupboard; /** * Creates a Need with the provided object * * @param need The need to create * @return OK response and the need if it was successful, INTERNAL_SERVER_ERROR otherwise */ @PostMapping("") public ResponseEntity createNeed(@RequestBody Need need) { try { cupboard.createNeed(need); return new ResponseEntity<>(need, HttpStatus.OK); } catch (IOException ex) { return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR); } } /** * Responds to the GET request for all {@linkplain Need needs} * * @return ResponseEntity with array of {@link Need needs} objects (may be empty) * and * HTTP status of OK
* ResponseEntity with HTTP status of INTERNAL_SERVER_ERROR otherwise */ @GetMapping("") public ResponseEntity getNeeds() { LOG.info("GET /needs"); try { Need[] needs = cupboard.getNeeds(); return new ResponseEntity<>(needs, HttpStatus.OK); } catch (IOException e) { LOG.log(Level.SEVERE, e.getLocalizedMessage()); return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR); } } /** * Responds to the GET request for all {@linkplain Need need} whose name contains * the text in name * * @param name The name parameter which contains the text used to find the {@link Need need} * * @return ResponseEntity with array of {@link Need need} objects (may be empty) and * HTTP status of OK
* ResponseEntity with HTTP status of INTERNAL_SERVER_ERROR otherwise *

*/ @GetMapping("/") public ResponseEntity searchNeeds(@RequestParam String name) { LOG.info("GET /need/?name="+name); try { Need[] needArray = cupboard.findNeeds(name); return new ResponseEntity(needArray,HttpStatus.OK); } catch (IOException e) { LOG.log(Level.SEVERE,e.getLocalizedMessage()); return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR); } } @GetMapping("/{id}") public Need getNeed(@PathVariable int id) { return cupboard.getNeed(id); } /** * Updates a Need with the provided one * * @param need The need to update * @return OK response and the need if it was successful, or INTERNAL_SERVER_ERROR if there was an issue */ @PutMapping("") public ResponseEntity updateNeed(@RequestBody Need need) { try { need = cupboard.updateNeed(need); return new ResponseEntity<>(need, HttpStatus.OK); } catch (IOException e) { return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR); } } @DeleteMapping("/{id}") public ResponseEntity deleteNeed(@PathVariable int id) { try { if (cupboard.getNeed(id) != null) { cupboard.removeNeed(id); return new ResponseEntity<>(HttpStatus.OK); } else { return new ResponseEntity<>(HttpStatus.NOT_FOUND); } } catch (IOException e) { return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR); } } }