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; import java.util.ArrayList; @RestController @RequestMapping("cupboard") public class CupboardController { private static final Logger LOG = Logger.getLogger(CupboardController.class.getName()); private ArrayList needs; private Cupboard cupboard; @PostMapping("") public void createNeed(@RequestBody Need need) { cupboard.createNeed(need); } @GetMapping("") public Need[] getNeeds() { return cupboard.getNeeds(); } @GetMapping("/") public Need searchNeeds(@RequestParam String name) { return cupboard.findNeeds(name); } /** * Responds to the GET request for a {@linkplain Need need} for the given id * * @param id The id used to locate the {@link Need need} * * @return ResponseEntity with {@link Need need} object and HTTP status of OK if found
* ResponseEntity with HTTP status of NOT_FOUND if not found
* ResponseEntity with HTTP status of INTERNAL_SERVER_ERROR otherwise */ @GetMapping("/{id}") public ResponseEntity getNeed(@PathVariable int id) { LOG.log(Level.INFO, "GET /need/{0}", id); try { Need need = cupboard.getNeed(id); if (need != null) { return new ResponseEntity<>(need, HttpStatus.OK); } else { return new ResponseEntity<>(HttpStatus.NOT_FOUND); } } catch (IOException e) { LOG.log(Level.SEVERE, e.getLocalizedMessage()); return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR); } } @PutMapping("") public void updateNeed(@RequestBody Need need) { cupboard.updateNeed(need); } @DeleteMapping("/{id}") public void deleteNeed(@PathVariable int id) { cupboard.removeNeed(id); } }