aboutsummaryrefslogtreecommitdiff
path: root/ufund-api/src/main/java/com/ufund/api/ufundapi/controller/CupboardController.java
blob: c7fc9acbc16942714f532d13d3c0c287ee948f29 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
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 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<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 = 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);
    }

}