diff options
5 files changed, 133 insertions, 56 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 4b2a04d..6b0bb71 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 @@ -1,6 +1,7 @@  package com.ufund.api.ufundapi.controller;  import java.io.IOException; +import java.util.Map;  import java.util.logging.Level;  import java.util.logging.Logger; @@ -17,21 +18,23 @@ import org.springframework.web.bind.annotation.RequestParam;  import org.springframework.web.bind.annotation.RestController;  import com.ufund.api.ufundapi.model.Need; -import com.ufund.api.ufundapi.persistence.CupboardDAO; +import com.ufund.api.ufundapi.model.Need.GoalType; +import com.ufund.api.ufundapi.service.CupboardService; +import com.ufund.api.ufundapi.service.CupboardService.DuplicateKeyException;  @RestController  @RequestMapping("cupboard")  public class CupboardController {      private static final Logger LOG = Logger.getLogger(CupboardController.class.getName()); -    private final CupboardDAO cupboardDAO; +    private final CupboardService cupboardService;      /**       * Create a cupboard controller to receive REST signals       * -     * @param cupboardDAO The Data Access Object +     * @param cupboardService The Data Access Object       */ -    public CupboardController(CupboardDAO cupboardDAO) { -        this.cupboardDAO = cupboardDAO; +    public CupboardController(CupboardService cupboardService) { +        this.cupboardService = cupboardService;      }      /** @@ -41,16 +44,20 @@ public class CupboardController {       * @return OK response and the need if it was successful, INTERNAL_SERVER_ERROR otherwise       */      @PostMapping("") -    public ResponseEntity<Need> createNeed(@RequestBody Need need) { +    public ResponseEntity<Need> createNeed(@RequestBody Map<String, String> params) { +        String name = params.get("name"); +        int maxGoal = Integer.parseInt(params.get("maxGoal")); +        Need.GoalType goalType = GoalType.valueOf(params.get("maxGoal")); +          try { -            if (need.getMaxGoal() <= 0) { -                return new ResponseEntity<>(HttpStatus.BAD_REQUEST); -            } -            if (need.getMaxGoal() < need.getCurrent()) { -                return new ResponseEntity<>(HttpStatus.BAD_REQUEST); -            } -            cupboardDAO.createNeed(need); +             +            Need need = cupboardService.createNeed(name, maxGoal, goalType);              return new ResponseEntity<>(need, HttpStatus.OK); + +        } catch (DuplicateKeyException ex) { +            return new ResponseEntity<>(HttpStatus.CONFLICT); +        } catch (IllegalArgumentException ex) { +            return new ResponseEntity<>(HttpStatus.UNPROCESSABLE_ENTITY);          } catch (IOException ex) {              return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR);          } @@ -69,7 +76,7 @@ public class CupboardController {          LOG.info("GET /needs");          try { -            Need[] needs = cupboardDAO.getNeeds(); +            Need[] needs = cupboardService.getNeeds();              return new ResponseEntity<>(needs, HttpStatus.OK);          } catch (IOException e) {              LOG.log(Level.SEVERE, e.getLocalizedMessage()); @@ -93,8 +100,8 @@ public class CupboardController {          LOG.info("GET /need/?name="+name);          try { -            Need[] needArray = cupboardDAO.findNeeds(name); -            return new ResponseEntity<>(needArray, HttpStatus.OK); +            Need[] needs = cupboardService.searchNeeds(name); +            return new ResponseEntity<>(needs, HttpStatus.OK);          } catch (IOException e) {              LOG.log(Level.SEVERE,e.getLocalizedMessage());              return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR); @@ -116,7 +123,7 @@ public class CupboardController {          LOG.log(Level.INFO, "GET /need/{0}", id);          try { -            Need need = cupboardDAO.getNeed(id); +            Need need = cupboardService.getNeed(id);              if (need != null) {                  return new ResponseEntity<>(need, HttpStatus.OK);              } else { @@ -140,8 +147,12 @@ public class CupboardController {      @PutMapping("")      public ResponseEntity<Need> updateNeed(@RequestBody Need need) {          try { -            need = cupboardDAO.updateNeed(need); -            return new ResponseEntity<>(need, HttpStatus.OK); +            Need updatedNeed = cupboardService.updateNeed(need); +            if (updatedNeed != null) { +                return new ResponseEntity<>(need, HttpStatus.OK); +            } else { +                return new ResponseEntity<>(HttpStatus.NOT_FOUND); +            }          } catch (IOException e) {              return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR);          } @@ -156,9 +167,9 @@ public class CupboardController {      @DeleteMapping("/{id}")      public ResponseEntity<Need> deleteNeed(@PathVariable int id) {          try { -            if (cupboardDAO.getNeed(id) != null) { -                cupboardDAO.deleteNeed(id); -                return new ResponseEntity<>(HttpStatus.OK); +            Need need = cupboardService.getNeed(id); +            if (cupboardService.deleteNeed(id)) { +                return new ResponseEntity<>(need, HttpStatus.OK);              } else {                  return new ResponseEntity<>(HttpStatus.NOT_FOUND);              }  diff --git a/ufund-api/src/main/java/com/ufund/api/ufundapi/model/Need.java b/ufund-api/src/main/java/com/ufund/api/ufundapi/model/Need.java index 2611357..9ca097a 100644 --- a/ufund-api/src/main/java/com/ufund/api/ufundapi/model/Need.java +++ b/ufund-api/src/main/java/com/ufund/api/ufundapi/model/Need.java @@ -32,6 +32,19 @@ public class Need {      }      /** +     * Create a new need +     * +     * @param name    The name of the need +     * @param maxGoal The maximum goal for this need +     * @param type    The type of need (monetary, physical) +     */ +    public Need(String name, GoalType type, double maxGoal) { +        this.name = name; +        this.type = type; +        this.maxGoal = maxGoal; +    } + +    /**       * Create a deep copy of another need       *       * @param other The need to copy from diff --git a/ufund-api/src/main/java/com/ufund/api/ufundapi/persistence/CupboardDAO.java b/ufund-api/src/main/java/com/ufund/api/ufundapi/persistence/CupboardDAO.java index 1435410..6baf3e4 100644 --- a/ufund-api/src/main/java/com/ufund/api/ufundapi/persistence/CupboardDAO.java +++ b/ufund-api/src/main/java/com/ufund/api/ufundapi/persistence/CupboardDAO.java @@ -1,9 +1,9 @@  package com.ufund.api.ufundapi.persistence; -import com.ufund.api.ufundapi.model.Need; -  import java.io.IOException; +import com.ufund.api.ufundapi.model.Need; +  /**   * Defines the interface for Need object persistence   *  @@ -20,17 +20,6 @@ public interface CupboardDAO {      Need[] getNeeds() throws IOException;      /** -     * Finds all {@linkplain Need needs} whose name contains the given text -     *  -     * @param targetName The text to match against -     *  -     * @return An array of {@link Need needs} whose names contains the given text, may be empty -     *  -     * @throws IOException if an issue with underlying storage -     */ -    Need[] findNeeds(String targetName) throws IOException; - -    /**       * Retrieves a {@linkplain Need need} with the given name       *        * @param id The ID of the {@link Need need} to get @@ -54,7 +43,7 @@ public interface CupboardDAO {       *        * @throws IOException if an issue with underlying storage       */ -    Need createNeed(Need need) throws IOException; +    Need addNeed(Need need) throws IOException;      /**       * Updates and saves a {@linkplain Need need} diff --git a/ufund-api/src/main/java/com/ufund/api/ufundapi/persistence/CupboardFileDao.java b/ufund-api/src/main/java/com/ufund/api/ufundapi/persistence/CupboardFileDao.java index 81ee7c0..84ea693 100644 --- a/ufund-api/src/main/java/com/ufund/api/ufundapi/persistence/CupboardFileDao.java +++ b/ufund-api/src/main/java/com/ufund/api/ufundapi/persistence/CupboardFileDao.java @@ -61,18 +61,6 @@ public class CupboardFileDao implements CupboardDAO {      }      /** -     * Returns an array of needs filtered by a search -     * -     * @param search The search substring -     * @return The requested array -     */ -    private Need[] getNeedsArray(String search) { -        return needs.values().stream() -                .filter(i -> i.getName().toLowerCase().contains(search.toLowerCase())) -                .toArray(Need[]::new); -    } - -    /**       * Saves the needs to json       *       * @return True if the save was successful, false otherwise @@ -93,13 +81,6 @@ public class CupboardFileDao implements CupboardDAO {      }      @Override -    public Need[] findNeeds(String targetName) { -        synchronized (needs) { -            return getNeedsArray(targetName); -        } -    } - -    @Override      public Need getNeed(int id) {          synchronized (needs) {              return needs.getOrDefault(id, null); @@ -107,7 +88,7 @@ public class CupboardFileDao implements CupboardDAO {      }      @Override -    public Need createNeed(Need need) throws IOException { +    public Need addNeed(Need need) throws IOException {          synchronized (needs) {              Need newNeed = new Need(need);              newNeed.setID(nextId()); diff --git a/ufund-api/src/main/java/com/ufund/api/ufundapi/service/CupboardService.java b/ufund-api/src/main/java/com/ufund/api/ufundapi/service/CupboardService.java new file mode 100644 index 0000000..860a2a8 --- /dev/null +++ b/ufund-api/src/main/java/com/ufund/api/ufundapi/service/CupboardService.java @@ -0,0 +1,83 @@ +package com.ufund.api.ufundapi.service; + +import java.io.IOException; +import java.util.Arrays; + +import com.ufund.api.ufundapi.model.Need; +import com.ufund.api.ufundapi.persistence.CupboardDAO; + +public class CupboardService { + +    private final CupboardDAO cupboardDAO; + +    public class DuplicateKeyException extends Exception { + +        public DuplicateKeyException(String message) { +            super(message); +        } + +    } + +    public CupboardService(CupboardDAO cupboardDAO) { +        this.cupboardDAO = cupboardDAO; +    } + +    public Need createNeed(String name, int maxGoal, Need.GoalType goalType) throws IOException, DuplicateKeyException { +         +        Need need = new Need(name, goalType, maxGoal); + +        if (need.getMaxGoal() <= 0) { +            throw new IllegalArgumentException("Max Goal must be greater than zero"); +        } else { +            for (Need searchNeed : cupboardDAO.getNeeds()) { +                if (need.getName().equalsIgnoreCase(searchNeed.getName())) { +                    throw new DuplicateKeyException("Duplicate names are not allowed"); +                } +            } +            return cupboardDAO.addNeed(need); +        } +         +    } + +    public Need[] getNeeds() throws IOException { +        return cupboardDAO.getNeeds(); +    } + +    /** +     * Returns an array of needs filtered by a search +     * +     * @param search The search substring +     * @return The requested array +     * @throws IOException  +     */ +    public Need[] searchNeeds(String search) throws IOException { +        return Arrays.stream(cupboardDAO.getNeeds()) +                .filter(i -> i.getName().toLowerCase().contains(search.toLowerCase())) +                .toArray(Need[]::new); +    } + +    /** +     * @param id +     * @return +     * @throws IOException +     */ +    public Need getNeed(int id) throws IOException { +        return cupboardDAO.getNeed(id); +    } + +    /** +     *  +     * @param need +     * @return +     * @throws IOException +     */ +    public Need updateNeed(Need need) throws IOException { +        return cupboardDAO.updateNeed(need); +    } + +    public boolean deleteNeed(int id) throws IOException { +        return cupboardDAO.deleteNeed(id); +    } + +     +}  | 
