forked from Hexlet/hexlet-correction
-
Notifications
You must be signed in to change notification settings - Fork 0
/
WorkspaceApi.java
45 lines (40 loc) · 1.9 KB
/
WorkspaceApi.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
package io.hexlet.typoreporter.web;
import io.hexlet.typoreporter.service.TypoService;
import io.hexlet.typoreporter.service.dto.typo.ReportedTypo;
import io.hexlet.typoreporter.service.dto.typo.TypoReport;
import io.hexlet.typoreporter.web.exception.WorkspaceNotFoundException;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.security.core.Authentication;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.util.UriComponentsBuilder;
import org.springframework.web.bind.annotation.CrossOrigin;
import static org.springframework.http.ResponseEntity.created;
@RestController
@RequestMapping("/api/workspaces")
@RequiredArgsConstructor
public class WorkspaceApi {
private final TypoService service;
@PostMapping("/typos")
// TODO: allow sending a request only from the pages specified in the Workspace settings
@CrossOrigin(
originPatterns = {"*"},
allowCredentials = "true"
)
public ResponseEntity<ReportedTypo> addTypoReport(Authentication authentication,
@Valid @RequestBody TypoReport typoReport,
UriComponentsBuilder builder) {
final var wksIdStr = authentication.getName();
try {
final var id = Long.parseLong(wksIdStr);
final var uri = builder.path("/workspace").pathSegment(wksIdStr).path("/typos").build().toUri();
return created(uri).body(service.addTypoReport(typoReport, id));
} catch (NumberFormatException e) {
throw new WorkspaceNotFoundException(wksIdStr, e);
}
}
}