diff options
| -rw-r--r-- | ufund-api/src/main/java/com/ufund/api/ufundapi/controller/CupboardController.java | 44 | ||||
| -rw-r--r-- | ufund-api/src/main/java/com/ufund/api/ufundapi/model/Cupboard.java | 61 | 
2 files changed, 48 insertions, 57 deletions
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 dba88db..c99a95b 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 @@ -2,46 +2,44 @@ package com.ufund.api.ufundapi.controller;  import com.ufund.api.ufundapi.model.Cupboard;  import com.ufund.api.ufundapi.model.Need; -import com.ufund.api.ufundapi.persistence.CupboardDAO; +import org.springframework.web.bind.annotation.*;  import java.util.ArrayList; +@RestController +@RequestMapping("cupboard")  public class CupboardController {      private ArrayList<Need> needs;      private Cupboard cupboard; -    private CupboardDAO dao; -    public CupboardController(CupboardDAO dao) { -        this.dao = dao; -    } - -    public void createNeed(String name, double max, Need.GoalType type) {; -        cupboard.createNeed(name, max, type); -        //dao.createNeed(need); -        //implement in dao +    @PostMapping("") +    public void createNeed(@RequestBody Need need) {; +        cupboard.createNeed(need);      } +    @GetMapping("")      public Need[] getNeeds() {          return cupboard.getNeeds(); -        //return dao.getNeeds(); -        //implement in dao      } -    public Need getNeed(int index) { -        return cupboard.getNeed(index); -        //return dao.getNeed(); -        //implement in dao +    @GetMapping("/") +    public Need searchNeeds(@RequestParam String name) { +        return cupboard.findNeeds(name);      } -    public void updateNeed(Need need) { + +    @GetMapping("/{id}") +    public Need getNeed(@PathVariable int id) { +        return cupboard.getNeed(id); +    } + +    @PutMapping("") +    public void updateNeed(@RequestBody Need need) {          cupboard.updateNeed(need); -        //dao.updateNeed(need); -        //implement in dao      } -    public void deleteNeed(String name) { -        cupboard.removeNeed(name); -        //dao.removeNeed(name); -        //implement in dao +    @DeleteMapping("/{id}") +    public void deleteNeed(@PathVariable int id) { +        cupboard.removeNeed(id);      }  } 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 index fedf04e..de1695a 100644 --- 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 @@ -1,58 +1,51 @@  package com.ufund.api.ufundapi.model; -import java.util.ArrayList; +import com.ufund.api.ufundapi.persistence.CupboardDAO; +import com.ufund.api.ufundapi.persistence.CupboardFileDao; -public class Cupboard { -    ArrayList<Need> needs; +import java.io.IOException; -    public Cupboard() { -        needs = new ArrayList<>(); -    } +public class Cupboard { +    CupboardDAO dao = new CupboardFileDao(); -    public void addNeed(Need need) { -        needs.add(need); +    public void createNeed(Need need) throws IOException { +        int id = dao.getNeeds().length; +        dao.createNeed(need);      } -    public void createNeed(String name, double max, Need.GoalType type) { -        int id = needs.size(); -        Need need = new Need(name, id, max, type); -        addNeed(need); -    } +//    public void updateID(int id) throws IOException { +//        for (int i = 0; i < getNeeds().length; i++) { +//            needs.get(i).setID(i); +//        } +//    } -    public void updateID(int id) { -        for (int i = 0; i < needs.size(); i++) { -            needs.get(i).setID(i); -        } +    public Need[] findNeeds(String name) throws IOException { +        return dao.findNeeds(name);      } -    public void removeNeed(Need need) { -        needs.remove(need); +    public void updateNeed(Need need) throws IOException { +        dao.updateNeed(need);      } -    public void updateNeed(Need need) { -        for (int i = 0; i < needs.size(); i++) { -            if (needs.get(i).getID() == need.getID()) { -                needs.set(i, need); -                return; -            } -        } +    public void removeNeed(int id) throws IOException { +        dao.deleteNeed(id);      } -    public void removeNeed(String name) { -        for (int i = 0; i < needs.size(); i++) { -            if (needs.get(i).getName().equals(name)) { -                needs.remove(i); +    public void removeNeed(String name) throws IOException { +        for (Need need : getNeeds()) { +            if (need.getName().equals(name)) { +                dao.deleteNeed(need.getID());                  return;              }          }      } -    public Need[] getNeeds() { -        return needs.toArray(new Need[0]); +    public Need[] getNeeds() throws IOException { +        return dao.getNeeds();      } -    public Need getNeed(int index) { -        return needs.get(index); +    public Need getNeed(int id) throws IOException { +        return dao.getNeed(id);      }  }  | 
