From d6f563c6288052e719f48939f484148b3068fead Mon Sep 17 00:00:00 2001 From: sowgro Date: Sun, 16 Feb 2025 15:05:39 -0500 Subject: Refactor: Remove Cupboard.java --- .../ufundapi/controller/CupboardController.java | 30 ++++++++----- .../com/ufund/api/ufundapi/model/Cupboard.java | 51 ---------------------- 2 files changed, 19 insertions(+), 62 deletions(-) delete mode 100644 ufund-api/src/main/java/com/ufund/api/ufundapi/model/Cupboard.java (limited to 'ufund-api/src/main/java') diff --git a/ufund-api/src/main/java/com/ufund/api/ufundapi/controller/CupboardController.java b/ufund-api/src/main/java/com/ufund/api/ufundapi/controller/CupboardController.java index 3018ac1..e30708b 100644 --- a/ufund-api/src/main/java/com/ufund/api/ufundapi/controller/CupboardController.java +++ b/ufund-api/src/main/java/com/ufund/api/ufundapi/controller/CupboardController.java @@ -3,9 +3,9 @@ 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 com.ufund.api.ufundapi.persistence.CupboardDAO; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.*; @@ -16,7 +16,16 @@ import java.io.IOException; @RequestMapping("cupboard") public class CupboardController { private static final Logger LOG = Logger.getLogger(CupboardController.class.getName()); - private Cupboard cupboard; + private final CupboardDAO cupboardDAO; + + /** + * Create a cupboard controller to recive REST signals + * + * @param cupboardDAO The Data Access Object + */ + public CupboardController(CupboardDAO cupboardDAO) { + this.cupboardDAO = cupboardDAO; + } /** * Creates a Need with the provided object @@ -27,7 +36,7 @@ public class CupboardController { @PostMapping("") public ResponseEntity createNeed(@RequestBody Need need) { try { - cupboard.createNeed(need); + cupboardDAO.createNeed(need); return new ResponseEntity<>(need, HttpStatus.OK); } catch (IOException ex) { return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR); @@ -47,7 +56,7 @@ public class CupboardController { LOG.info("GET /needs"); try { - Need[] needs = cupboard.getNeeds(); + Need[] needs = cupboardDAO.getNeeds(); return new ResponseEntity<>(needs, HttpStatus.OK); } catch (IOException e) { LOG.log(Level.SEVERE, e.getLocalizedMessage()); @@ -71,8 +80,8 @@ public class CupboardController { LOG.info("GET /need/?name="+name); try { - Need[] needArray = cupboard.findNeeds(name); - return new ResponseEntity(needArray,HttpStatus.OK); + Need[] needArray = cupboardDAO.findNeeds(name); + return new ResponseEntity<>(needArray, HttpStatus.OK); } catch (IOException e) { LOG.log(Level.SEVERE,e.getLocalizedMessage()); return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR); @@ -94,7 +103,7 @@ public class CupboardController { LOG.log(Level.INFO, "GET /need/{0}", id); try { - Need need = cupboard.getNeed(id); + Need need = cupboardDAO.getNeed(id); if (need != null) { return new ResponseEntity<>(need, HttpStatus.OK); } else { @@ -118,7 +127,7 @@ public class CupboardController { @PutMapping("") public ResponseEntity updateNeed(@RequestBody Need need) { try { - need = cupboard.updateNeed(need); + need = cupboardDAO.updateNeed(need); return new ResponseEntity<>(need, HttpStatus.OK); } catch (IOException e) { return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR); @@ -130,13 +139,12 @@ public class CupboardController { * * @param id The need's ID * @return OK if the need was deleted, NOT_FOUND if the need was not found, or INTERNAL_SERVER_ERROR if an error occurred - */ @DeleteMapping("/{id}") public ResponseEntity deleteNeed(@PathVariable int id) { try { - if (cupboard.getNeed(id) != null) { - cupboard.removeNeed(id); + if (cupboardDAO.getNeed(id) != null) { + cupboardDAO.deleteNeed(id); return new ResponseEntity<>(HttpStatus.OK); } else { return new ResponseEntity<>(HttpStatus.NOT_FOUND); diff --git a/ufund-api/src/main/java/com/ufund/api/ufundapi/model/Cupboard.java b/ufund-api/src/main/java/com/ufund/api/ufundapi/model/Cupboard.java deleted file mode 100644 index 0ce015c..0000000 --- a/ufund-api/src/main/java/com/ufund/api/ufundapi/model/Cupboard.java +++ /dev/null @@ -1,51 +0,0 @@ -package com.ufund.api.ufundapi.model; - -import com.ufund.api.ufundapi.persistence.CupboardDAO; -import com.ufund.api.ufundapi.persistence.CupboardFileDao; - -import java.io.IOException; - -public class Cupboard { - CupboardDAO dao = new CupboardFileDao(); - - public void createNeed(Need need) throws IOException { - int id = dao.getNeeds().length; - dao.createNeed(need); - } - -// public void updateID(int id) throws IOException { -// for (int i = 0; i < getNeeds().length; i++) { -// needs.get(i).setID(i); -// } -// } - - public Need[] findNeeds(String name) throws IOException { - return dao.findNeeds(name); - } - - public Need updateNeed(Need need) throws IOException { - return dao.updateNeed(need); - } - - public void removeNeed(int id) throws IOException { - dao.deleteNeed(id); - } - - public void removeNeed(String name) throws IOException { - for (Need need : getNeeds()) { - if (need.getName().equals(name)) { - dao.deleteNeed(need.getId()); - return; - } - } - } - - public Need[] getNeeds() throws IOException { - return dao.getNeeds(); - } - - public Need getNeed(int id) throws IOException { - return dao.getNeed(id); - } - -} -- cgit v1.2.3