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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
|
package com.ufund.api.ufundapi.controller;
import java.io.IOException;
import java.util.Map;
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.DuplicateKeyException;
import com.ufund.api.ufundapi.model.Need;
import com.ufund.api.ufundapi.model.Need.GoalType;
import com.ufund.api.ufundapi.service.CupboardService;
@RestController
@RequestMapping("cupboard")
public class CupboardController {
private static final Logger LOG = Logger.getLogger(CupboardController.class.getName());
private final CupboardService cupboardService;
/**
* Create a cupboard controller to receive REST signals
*
* @param cupboardService The Data Access Object
*/
public CupboardController(CupboardService cupboardService) {
this.cupboardService = cupboardService;
}
/**
* Creates a Need with the provided object
*
* @param params The need to create
* @return OK response and the need if it was successful,
* CONFLICT if another need with the same name exists
* UNPROCESSABLE_ENTITY if the need contains bad data
* INTERNAL_SERVER_ERROR otherwise
*/
@PostMapping("")
public ResponseEntity<Need> createNeed(@RequestBody Map<String, Object> params) {
System.out.println(params);
String name = (String) params.get("name");
String location = (String) params.get("location");
double maxGoal = ((Number) params.get("maxGoal")).doubleValue();
boolean urgent = (Boolean) params.get("urgent");
Need.GoalType goalType = GoalType.valueOf((String) params.get("type"));
try {
Need need = cupboardService.createNeed(name, location, maxGoal, goalType, urgent);
return new ResponseEntity<>(need, HttpStatus.OK);
} catch (DuplicateKeyException ex) {
return new ResponseEntity<>(HttpStatus.CONFLICT);
} catch (IllegalArgumentException ex) {
return new ResponseEntity<>(HttpStatus.BAD_REQUEST);
} 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 = cupboardService.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[] 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);
}
}
/**
* 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>
*/
@GetMapping("/{id}")
public ResponseEntity<Need> getNeed(@PathVariable int id) {
LOG.log(Level.INFO, "GET /need/{0}", id);
try {
Need need = cupboardService.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("/{id}")
public ResponseEntity<Need> updateNeed(@RequestBody Need need, @PathVariable int id) {
LOG.log(Level.INFO, "Updating need: " + need);
try {
Need updatedNeed = cupboardService.updateNeed(need, id);
if (updatedNeed != null) {
return new ResponseEntity<>(need, HttpStatus.OK);
} else {
return new ResponseEntity<>(HttpStatus.NOT_FOUND);
}
} catch (IllegalArgumentException ex) {
ex.printStackTrace();
return new ResponseEntity<>(HttpStatus.BAD_REQUEST);
} catch (IOException ex) {
ex.printStackTrace();
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 {
Need need = cupboardService.getNeed(id);
if (cupboardService.deleteNeed(id)) {
return new ResponseEntity<>(need, HttpStatus.OK);
} else {
return new ResponseEntity<>(HttpStatus.NOT_FOUND);
}
} catch (IOException e) {
return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR);
}
}
}
|