-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathAttachmentController.java
More file actions
82 lines (61 loc) · 2.9 KB
/
AttachmentController.java
File metadata and controls
82 lines (61 loc) · 2.9 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
74
75
76
77
78
79
80
81
82
package org.example.vet1177.controller;
import jakarta.validation.ConstraintViolation;
import jakarta.validation.ConstraintViolationException;
import jakarta.validation.Validator;
import org.example.vet1177.dto.request.attachment.AttachmentRequest;
import org.example.vet1177.dto.response.attachment.AttachmentResponse;
import org.example.vet1177.entities.User;
import org.example.vet1177.services.AttachmentService;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import java.util.List;
import java.util.UUID;
@RestController
@RequestMapping("/api/attachments")
public class AttachmentController {
private final AttachmentService attachmentService;
private final Validator validator;
public AttachmentController(AttachmentService attachmentService, Validator validator) {
this.attachmentService = attachmentService;
this.validator = validator;
}
@PostMapping(value = "/record/{recordId}", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
public ResponseEntity<AttachmentResponse> uploadAttachment(
@AuthenticationPrincipal User currentUser,
@PathVariable UUID recordId,
@RequestPart("file") MultipartFile file,
@RequestPart(value = "description", required = false) String description) {
AttachmentRequest request = new AttachmentRequest(recordId, description);
var violations = validator.validate(request);
if (!violations.isEmpty()) {
throw new ConstraintViolationException(violations);
}
AttachmentResponse response = attachmentService.uploadAttachment(currentUser, file, request);
return new ResponseEntity<>(response, HttpStatus.CREATED);
}
@GetMapping("/record/{recordId}")
public ResponseEntity<List<AttachmentResponse>> listAttachments(
@AuthenticationPrincipal User currentUser,
@PathVariable UUID recordId) {
List<AttachmentResponse> responses = attachmentService.getAttachmentsByRecord(currentUser, recordId);
return ResponseEntity.ok(responses);
}
@GetMapping("/{id}/download")
public ResponseEntity<AttachmentResponse> downloadAttachment(
@AuthenticationPrincipal User currentUser,
@PathVariable UUID id) {
AttachmentResponse response = attachmentService.getAttachment(currentUser, id);
return ResponseEntity.ok(response);
}
@DeleteMapping("/{id}")
public ResponseEntity<Void> deleteAttachment(
@AuthenticationPrincipal User currentUser,
@PathVariable UUID id) {
attachmentService.deleteAttachment(currentUser, id);
return ResponseEntity.noContent().build();
}
}