diff options
| author | Gunther6070 <haydenhartman10@yahoo.com> | 2025-02-26 13:22:33 -0500 | 
|---|---|---|
| committer | Gunther6070 <haydenhartman10@yahoo.com> | 2025-02-26 13:22:33 -0500 | 
| commit | 380b0ca3f3ebd1564d2e961a67e0fc8a8dac80f2 (patch) | |
| tree | 7e6d0680d21f4cc9d59a6691bcad0bd015d51696 /ufund-api/src/main/java/com/ufund/api | |
| parent | cbcd49f2b96dffe359b99b8791ccfb62cbd4717f (diff) | |
| download | JellySolutions-380b0ca3f3ebd1564d2e961a67e0fc8a8dac80f2.tar.gz JellySolutions-380b0ca3f3ebd1564d2e961a67e0fc8a8dac80f2.tar.bz2 JellySolutions-380b0ca3f3ebd1564d2e961a67e0fc8a8dac80f2.zip  | |
Created UserController class to handle http requests
Diffstat (limited to 'ufund-api/src/main/java/com/ufund/api')
| -rw-r--r-- | ufund-api/src/main/java/com/ufund/api/ufundapi/controller/UserController.java | 170 | 
1 files changed, 170 insertions, 0 deletions
diff --git a/ufund-api/src/main/java/com/ufund/api/ufundapi/controller/UserController.java b/ufund-api/src/main/java/com/ufund/api/ufundapi/controller/UserController.java new file mode 100644 index 0000000..223963d --- /dev/null +++ b/ufund-api/src/main/java/com/ufund/api/ufundapi/controller/UserController.java @@ -0,0 +1,170 @@ +package com.ufund.api.ufundapi.controller; + +import java.io.IOException; +import java.util.logging.Level; +import java.util.logging.Logger; + +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.DeleteMapping; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.PathVariable; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.PutMapping; +import org.springframework.web.bind.annotation.RequestBody; +import org.springframework.web.bind.annotation.RequestMapping; +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.UserDAO; + +@RestController +@RequestMapping("cupboard") +public class UserController { +    private static final Logger LOG = Logger.getLogger(CupboardController.class.getName()); +    private final UserDAO UserDAO; + +    /** +     * Create a cupboard controller to receive REST signals +     * +     * @param UserDAO The Data Access Object +     */ +    public UserController(UserDAO userDAO) { +        this.UserDAO = userDAO; +    } + +    /** +     * 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<User> createUser(@RequestBody Need need) { +        try { +            if (need.getMaxGoal() <= 0) { +                return new ResponseEntity<>(HttpStatus.BAD_REQUEST); +            } +            if (need.getMaxGoal() < need.getCurrent()) { +                return new ResponseEntity<>(HttpStatus.BAD_REQUEST); +            } +            UserDAO.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<br> +     *         ResponseEntity with HTTP status of INTERNAL_SERVER_ERROR otherwise +     */ +    @GetMapping("") +    public ResponseEntity<Need[]> getNeeds() { +        LOG.info("GET /needs"); + +        try { +            Need[] needs = UserDAO.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<br> +     * ResponseEntity with HTTP status of INTERNAL_SERVER_ERROR otherwise +     * <p> +     */ +    @GetMapping("/") +    public ResponseEntity<Need[]> searchNeeds(@RequestParam String name) { +        LOG.info("GET /need/?name="+name); + +        try { +            Need[] needArray = UserDAO.findNeeds(name); +            return new ResponseEntity<>(needArray, HttpStatus.OK); +        } catch (IOException e) { +            LOG.log(Level.SEVERE,e.getLocalizedMessage()); +            return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR); +        } +    } + +    /** +     * 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<br> +     *         ResponseEntity with HTTP status of NOT_FOUND if not found<br> +     *         ResponseEntity with HTTP status of INTERNAL_SERVER_ERROR otherwise +     */ +    @GetMapping("/{id}") +    public ResponseEntity<Need> getNeed(@PathVariable int id) { +        LOG.log(Level.INFO, "GET /need/{0}", id); + +        try { +            Need need = UserDAO.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); +        } + +    } + +    /** +     * 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<Need> updateNeed(@RequestBody Need need) { +        try { +            need = UserDAO.updateNeed(need); +            return new ResponseEntity<>(need, HttpStatus.OK); +        } catch (IOException e) { +            return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR); +        } +    } + +    /** +     * Deletes a single need from the cupboard using the Need's id +     *  +     * @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<Need> deleteNeed(@PathVariable int id) { +        try { +            if (UserDAO.getNeed(id) != null) { +                UserDAO.deleteNeed(id); +                return new ResponseEntity<>(HttpStatus.OK); +            } else { +                return new ResponseEntity<>(HttpStatus.NOT_FOUND); +            }  +        } catch (IOException e) { +            return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR); +        } +    } + +}  | 
