-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathContactsController.java
122 lines (98 loc) · 3.53 KB
/
ContactsController.java
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
package com.kubra.rest;
import com.kubra.rest.models.Contact;
import java.net.URI;
import java.util.Arrays;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
import java.util.UUID;
import org.springframework.context.MessageSource;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
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.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping(
path = "/contacts",
produces = MediaType.APPLICATION_JSON_VALUE,
consumes = MediaType.APPLICATION_JSON_VALUE
)
public class ContactsController {
private MessageSource messageSource;
public ContactsController(MessageSource messageSource) {
this.messageSource = messageSource;
}
public static final Map<UUID, Contact> contacts = new HashMap<>();
@PostMapping
public ResponseEntity<Contact> create(@RequestBody Contact contact) {
UUID id = UUID.randomUUID();
contact.setId(id);
contacts.put(id, contact);
URI uri = URI.create("contacts/" + id);
return ResponseEntity.ok().location(uri).build();
}
@GetMapping
public ResponseEntity<Collection<Contact>> list() {
Collection<Contact> contactList = contacts.values();
return ResponseEntity.ok(contactList);
}
@GetMapping(path = "{id}")
public ResponseEntity<Contact> get(@PathVariable UUID id) {
Contact contact = contacts.get(id);
if (Objects.isNull(contact)) {
return ResponseEntity.notFound().build();
} else {
return ResponseEntity.ok(contact);
}
}
@DeleteMapping(path = "{id}")
public ResponseEntity delete(@PathVariable UUID id) {
if (!contacts.containsKey(id)) {
return ResponseEntity.notFound().build();
} else {
contacts.remove(id);
return ResponseEntity.noContent().build();
}
}
@PutMapping(path = "{id}")
public ResponseEntity<Contact> update(@PathVariable UUID id, @RequestBody Contact contact) {
if (Objects.isNull(contacts.get(id))) {
return ResponseEntity.notFound().build();
} else {
contact.setId(id);
contacts.put(id, contact);
return ResponseEntity.ok(contact);
}
}
@GetMapping("secret")
public ResponseEntity<Contact> secretContact(
@RequestHeader(required = false, name = "X-Secret-Code") String xSecretCode,
@RequestParam(value = "superSecret", required = false) String superSecret) {
if (!"42".equals(xSecretCode)) {
return ResponseEntity.notFound().build();
}
Contact secretContact = new Contact();
String name = "James Bond";
if (Objects.nonNull(superSecret)) {
name += " (triple agent)";
}
secretContact.setName(name);
HttpHeaders headers = new HttpHeaders();
headers.put(HttpHeaders.ETAG, Arrays.asList("EEEEEETag"));
return ResponseEntity.ok().headers(headers).body(secretContact);
}
@GetMapping("illegal")
public ResponseEntity illegal() {
throw new IllegalArgumentException("Endpoint is bad");
}
}