|
1 | 1 | package eu.nampi.backend.controller; |
2 | 2 |
|
3 | | -import java.util.List; |
4 | 3 | import java.util.Optional; |
5 | 4 | import java.util.UUID; |
| 5 | +import javax.validation.Valid; |
6 | 6 | import org.apache.jena.rdf.model.Literal; |
7 | 7 | import org.apache.jena.rdf.model.Property; |
8 | 8 | import org.apache.jena.rdf.model.Resource; |
|
16 | 16 | import org.springframework.web.bind.annotation.PathVariable; |
17 | 17 | import org.springframework.web.bind.annotation.PostMapping; |
18 | 18 | import org.springframework.web.bind.annotation.PutMapping; |
| 19 | +import org.springframework.web.bind.annotation.RequestBody; |
19 | 20 | import org.springframework.web.bind.annotation.RequestHeader; |
20 | 21 | import org.springframework.web.bind.annotation.RequestParam; |
21 | 22 | import org.springframework.web.bind.annotation.RestController; |
22 | 23 | import eu.nampi.backend.exception.ForbiddenException; |
23 | | -import eu.nampi.backend.model.DateRange; |
| 24 | +import eu.nampi.backend.model.EventMutationPayload; |
24 | 25 | import eu.nampi.backend.model.InsertResult; |
25 | 26 | import eu.nampi.backend.model.OrderByClauses; |
26 | 27 | import eu.nampi.backend.model.QueryParameters; |
27 | | -import eu.nampi.backend.model.ResourceCouple; |
28 | 28 | import eu.nampi.backend.repository.EventRepository; |
29 | 29 | import eu.nampi.backend.repository.UserRepository; |
30 | 30 |
|
@@ -73,57 +73,35 @@ public ResponseEntity<String> getEvent(@RequestHeader("accept") Lang lang, |
73 | 73 | } |
74 | 74 |
|
75 | 75 | @PostMapping(value = "/events", produces = {"application/ld+json", "text/turtle", |
76 | | - "application/rdf+xml", "application/n-triples"}) |
| 76 | + "application/rdf+xml", "application/n-triples"}, consumes = {"application/json"}) |
77 | 77 | public ResponseEntity<String> postEvent( |
78 | 78 | @RequestHeader("accept") Lang lang, |
79 | | - @RequestParam("types[]") List<Resource> types, |
80 | | - @RequestParam("labels[]") List<Literal> labels, |
81 | | - @RequestParam(value = "comments[]", required = false) List<Literal> comments, |
82 | | - @RequestParam(value = "texts[]", required = false) List<Literal> texts, |
83 | | - @RequestParam("authors[]") List<Resource> authors, |
84 | | - @RequestParam("source") Resource source, |
85 | | - @RequestParam("sourceLocation") Literal sourceLocation, |
86 | | - @RequestParam("mainParticipant") ResourceCouple mainParticipant, |
87 | | - @RequestParam(value = "otherParticipants[]", |
88 | | - required = false) List<ResourceCouple> otherParticipants, |
89 | | - @RequestParam(value = "aspects[]", required = false) List<ResourceCouple> aspects, |
90 | | - @RequestParam("place") Optional<Resource> place, |
91 | | - @RequestParam("date") Optional<DateRange> date) { |
92 | | - InsertResult result = |
93 | | - eventRepository.insert(lang, types, labels, asList(comments), asList(texts), |
94 | | - authors, source, sourceLocation, mainParticipant, asList(otherParticipants), |
95 | | - asList(aspects), place, date); |
| 79 | + @Valid @RequestBody EventMutationPayload payload) { |
| 80 | + InsertResult result = eventRepository.insert(lang, payload.getTypes(), payload.getLabels(), |
| 81 | + asList(payload.getComments()), asList(payload.getTexts()), payload.getAuthors(), |
| 82 | + payload.getSource(), payload.getSourceLocation(), payload.getMainParticipant(), |
| 83 | + asList(payload.getOtherParticipants()), asList(payload.getAspects()), |
| 84 | + Optional.ofNullable(payload.getPlace()), Optional.ofNullable(payload.getDate())); |
96 | 85 | HttpHeaders headers = new HttpHeaders(); |
97 | 86 | headers.add("Location", result.getEntity().getURI()); |
98 | 87 | return new ResponseEntity<String>(result.getResponseBody(), headers, HttpStatus.CREATED); |
99 | 88 | } |
100 | 89 |
|
101 | 90 | @PutMapping(value = "/events/{id}", produces = {"application/ld+json", "text/turtle", |
102 | | - "application/rdf+xml", "application/n-triples"}) |
| 91 | + "application/rdf+xml", "application/n-triples"}, consumes = {"application/json"}) |
103 | 92 | public ResponseEntity<String> putEvent( |
104 | 93 | @PathVariable("id") UUID id, |
105 | 94 | @RequestHeader("accept") Lang lang, |
106 | | - @RequestParam("types[]") List<Resource> types, |
107 | | - @RequestParam("labels[]") List<Literal> labels, |
108 | | - @RequestParam(value = "comments[]", required = false) List<Literal> comments, |
109 | | - @RequestParam(value = "texts[]", required = false) List<Literal> texts, |
110 | | - @RequestParam("authors[]") List<Resource> authors, |
111 | | - @RequestParam("source") Resource source, |
112 | | - @RequestParam("sourceLocation") Literal sourceLocation, |
113 | | - @RequestParam("mainParticipant") ResourceCouple mainParticipant, |
114 | | - @RequestParam(value = "otherParticipants[]", |
115 | | - required = false) List<ResourceCouple> otherParticipants, |
116 | | - @RequestParam(value = "aspects[]", required = false) List<ResourceCouple> aspects, |
117 | | - @RequestParam("place") Optional<Resource> place, |
118 | | - @RequestParam("date") Optional<DateRange> date) { |
| 95 | + @Valid @RequestBody EventMutationPayload payload) { |
119 | 96 | UUID userId = userRepository.getCurrentUser().map(user -> user.getRdfId()).orElseThrow(); |
120 | 97 | if (!eventRepository.isAuthor(userId, id)) { |
121 | 98 | throw new ForbiddenException(); |
122 | 99 | } |
123 | | - String newEvent = |
124 | | - eventRepository.update(lang, id, types, labels, asList(comments), asList(texts), authors, |
125 | | - source, sourceLocation, mainParticipant, asList(otherParticipants), asList(aspects), |
126 | | - place, date); |
| 100 | + String newEvent = eventRepository.update(lang, id, payload.getTypes(), payload.getLabels(), |
| 101 | + asList(payload.getComments()), asList(payload.getTexts()), payload.getAuthors(), |
| 102 | + payload.getSource(), payload.getSourceLocation(), payload.getMainParticipant(), |
| 103 | + asList(payload.getOtherParticipants()), asList(payload.getAspects()), |
| 104 | + Optional.ofNullable(payload.getPlace()), Optional.ofNullable(payload.getDate())); |
127 | 105 | return new ResponseEntity<String>(newEvent, HttpStatus.CREATED); |
128 | 106 | } |
129 | 107 |
|
|
0 commit comments