-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathBatchController.java
More file actions
73 lines (57 loc) · 2.71 KB
/
BatchController.java
File metadata and controls
73 lines (57 loc) · 2.71 KB
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
package com.revature.controllers;
import com.revature.domain.Batch;
import com.revature.services.BatchLogic;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
@CrossOrigin
@RestController
@RequestMapping(value = "/api/v1/")
@PreAuthorize("hasRole('ADMIN')")
public class BatchController {
@Autowired
private BatchLogic batchLogic;
@RequestMapping(method = RequestMethod.GET, value = "batches")
public ResponseEntity<Page<Batch>> getBatches(Pageable pageable) {
return ResponseEntity.ok(batchLogic.getAllBatches(pageable));
}
@RequestMapping(method = RequestMethod.GET, value = "/batches/{batchId}")
public ResponseEntity<Batch> getBatch(@PathVariable String batchId){
Batch batch;
try{
int value = Integer.parseInt(batchId);
batch = batchLogic.getBatchById(value);
} catch(NumberFormatException e){
batch = batchLogic.getBatchByName(batchId);
}
return ResponseEntity.ok(batch);
}
@RequestMapping(method = RequestMethod.DELETE, value = "/batches/{batchId}")
public ResponseEntity<String> deleteBatch(@PathVariable Integer batchId){
return ResponseEntity.ok(batchLogic.deleteBatch(batchId));
}
@RequestMapping(method = RequestMethod.PUT, value = "/batches/{batchId}")
public ResponseEntity<Batch> updateBatch(@PathVariable Integer batchId, @RequestBody Batch newBatch){
return ResponseEntity.ok(batchLogic.updateBatch(newBatch, batchId));
}
@RequestMapping(method = RequestMethod.POST, value = "/batches")
public ResponseEntity<Batch> createBatch(@RequestBody Batch newBatch){
return ResponseEntity.ok(batchLogic.createBatch(newBatch));
}
@RequestMapping(method = RequestMethod.POST, value = "/batches/{batchId}/members")
public ResponseEntity<Batch> addMembersToBatch(@PathVariable Integer batchId, @RequestBody Integer[] personIds){
return ResponseEntity.ok(batchLogic.addPersonsToBatch(batchId, personIds));
}
@RequestMapping(method = RequestMethod.DELETE, value = "/batches/{batchId}/members")
public ResponseEntity<Batch> removeMembersFromBatch(@PathVariable Integer batchId, @RequestBody Integer[] personIds){
return ResponseEntity.ok(batchLogic.removePersonsFromBatch(batchId, personIds));
}
}