|
| 1 | +package backend.fullstack.document.api; |
| 2 | + |
| 3 | +import java.util.List; |
| 4 | + |
| 5 | +import org.springframework.http.ContentDisposition; |
| 6 | +import org.springframework.http.HttpHeaders; |
| 7 | +import org.springframework.http.HttpStatus; |
| 8 | +import org.springframework.http.MediaType; |
| 9 | +import org.springframework.http.ResponseEntity; |
| 10 | +import org.springframework.security.access.prepost.PreAuthorize; |
| 11 | +import org.springframework.security.core.annotation.AuthenticationPrincipal; |
| 12 | +import org.springframework.web.bind.annotation.DeleteMapping; |
| 13 | +import org.springframework.web.bind.annotation.GetMapping; |
| 14 | +import org.springframework.web.bind.annotation.PathVariable; |
| 15 | +import org.springframework.web.bind.annotation.PostMapping; |
| 16 | +import org.springframework.web.bind.annotation.RequestMapping; |
| 17 | +import org.springframework.web.bind.annotation.RequestParam; |
| 18 | +import org.springframework.web.bind.annotation.RestController; |
| 19 | +import org.springframework.web.multipart.MultipartFile; |
| 20 | + |
| 21 | +import backend.fullstack.config.ApiResponse; |
| 22 | +import backend.fullstack.config.JwtPrincipal; |
| 23 | +import backend.fullstack.document.api.dto.DocumentResponse; |
| 24 | +import backend.fullstack.document.application.DocumentService; |
| 25 | +import backend.fullstack.document.domain.Document; |
| 26 | +import backend.fullstack.document.domain.DocumentCategory; |
| 27 | +import io.swagger.v3.oas.annotations.Operation; |
| 28 | +import io.swagger.v3.oas.annotations.Parameter; |
| 29 | +import io.swagger.v3.oas.annotations.responses.ApiResponses; |
| 30 | +import io.swagger.v3.oas.annotations.security.SecurityRequirement; |
| 31 | +import io.swagger.v3.oas.annotations.tags.Tag; |
| 32 | + |
| 33 | +/** |
| 34 | + * REST controller for document storage management. |
| 35 | + * Provides upload, download, list, and delete operations for organizational documents |
| 36 | + * such as policies, training materials, and certifications. |
| 37 | + */ |
| 38 | +@RestController |
| 39 | +@RequestMapping("/api/documents") |
| 40 | +@Tag(name = "Documents", description = "Document storage for policies, training materials, and certifications") |
| 41 | +@SecurityRequirement(name = "Bearer Auth") |
| 42 | +public class DocumentController { |
| 43 | + |
| 44 | + private final DocumentService documentService; |
| 45 | + |
| 46 | + public DocumentController(DocumentService documentService) { |
| 47 | + this.documentService = documentService; |
| 48 | + } |
| 49 | + |
| 50 | + /** |
| 51 | + * Lists all documents for the authenticated user's organization. |
| 52 | + * |
| 53 | + * @param principal the authenticated user |
| 54 | + * @param category optional category filter |
| 55 | + * @return list of document metadata |
| 56 | + */ |
| 57 | + @GetMapping |
| 58 | + @PreAuthorize("hasAnyRole('ADMIN','MANAGER','STAFF','SUPERVISOR')") |
| 59 | + @Operation( |
| 60 | + summary = "List documents", |
| 61 | + description = "Returns all documents for the organization, optionally filtered by category" |
| 62 | + ) |
| 63 | + @ApiResponses(value = { |
| 64 | + @io.swagger.v3.oas.annotations.responses.ApiResponse(responseCode = "200", description = "Documents retrieved"), |
| 65 | + @io.swagger.v3.oas.annotations.responses.ApiResponse(responseCode = "401", description = "Unauthorized") |
| 66 | + }) |
| 67 | + public ResponseEntity<ApiResponse<List<DocumentResponse>>> listDocuments( |
| 68 | + @AuthenticationPrincipal JwtPrincipal principal, |
| 69 | + @RequestParam(required = false) @Parameter(description = "Filter by document category") DocumentCategory category |
| 70 | + ) { |
| 71 | + List<DocumentResponse> documents = documentService |
| 72 | + .listDocuments(principal.organizationId(), category) |
| 73 | + .stream() |
| 74 | + .map(DocumentResponse::from) |
| 75 | + .toList(); |
| 76 | + return ResponseEntity.ok(ApiResponse.success("Documents retrieved", documents)); |
| 77 | + } |
| 78 | + |
| 79 | + /** |
| 80 | + * Downloads a document by its ID. |
| 81 | + * |
| 82 | + * @param principal the authenticated user |
| 83 | + * @param id the document ID |
| 84 | + * @return the file content as a byte stream |
| 85 | + */ |
| 86 | + @GetMapping("/{id}/download") |
| 87 | + @PreAuthorize("hasAnyRole('ADMIN','MANAGER','STAFF','SUPERVISOR')") |
| 88 | + @Operation( |
| 89 | + summary = "Download a document", |
| 90 | + description = "Downloads the file content of a specific document" |
| 91 | + ) |
| 92 | + @ApiResponses(value = { |
| 93 | + @io.swagger.v3.oas.annotations.responses.ApiResponse(responseCode = "200", description = "File downloaded"), |
| 94 | + @io.swagger.v3.oas.annotations.responses.ApiResponse(responseCode = "404", description = "Document not found"), |
| 95 | + @io.swagger.v3.oas.annotations.responses.ApiResponse(responseCode = "401", description = "Unauthorized") |
| 96 | + }) |
| 97 | + public ResponseEntity<byte[]> downloadDocument( |
| 98 | + @AuthenticationPrincipal JwtPrincipal principal, |
| 99 | + @PathVariable Long id |
| 100 | + ) { |
| 101 | + Document doc = documentService.getDocument(id, principal.organizationId()); |
| 102 | + |
| 103 | + HttpHeaders headers = new HttpHeaders(); |
| 104 | + headers.setContentType(MediaType.parseMediaType(doc.getContentType())); |
| 105 | + headers.setContentDisposition( |
| 106 | + ContentDisposition.attachment().filename(doc.getFileName()).build() |
| 107 | + ); |
| 108 | + headers.setContentLength(doc.getFileSize()); |
| 109 | + |
| 110 | + return ResponseEntity.ok().headers(headers).body(doc.getFileData()); |
| 111 | + } |
| 112 | + |
| 113 | + /** |
| 114 | + * Gets document metadata by its ID (without file content). |
| 115 | + * |
| 116 | + * @param principal the authenticated user |
| 117 | + * @param id the document ID |
| 118 | + * @return the document metadata |
| 119 | + */ |
| 120 | + @GetMapping("/{id}") |
| 121 | + @PreAuthorize("hasAnyRole('ADMIN','MANAGER','STAFF','SUPERVISOR')") |
| 122 | + @Operation( |
| 123 | + summary = "Get document metadata", |
| 124 | + description = "Returns metadata for a specific document without the file content" |
| 125 | + ) |
| 126 | + @ApiResponses(value = { |
| 127 | + @io.swagger.v3.oas.annotations.responses.ApiResponse(responseCode = "200", description = "Document metadata retrieved"), |
| 128 | + @io.swagger.v3.oas.annotations.responses.ApiResponse(responseCode = "404", description = "Document not found"), |
| 129 | + @io.swagger.v3.oas.annotations.responses.ApiResponse(responseCode = "401", description = "Unauthorized") |
| 130 | + }) |
| 131 | + public ResponseEntity<ApiResponse<DocumentResponse>> getDocument( |
| 132 | + @AuthenticationPrincipal JwtPrincipal principal, |
| 133 | + @PathVariable Long id |
| 134 | + ) { |
| 135 | + Document doc = documentService.getDocument(id, principal.organizationId()); |
| 136 | + return ResponseEntity.ok(ApiResponse.success("Document retrieved", DocumentResponse.from(doc))); |
| 137 | + } |
| 138 | + |
| 139 | + /** |
| 140 | + * Uploads a new document. |
| 141 | + * |
| 142 | + * @param principal the authenticated user |
| 143 | + * @param file the file to upload (max 10 MB) |
| 144 | + * @param title document title |
| 145 | + * @param description optional description |
| 146 | + * @param category document category |
| 147 | + * @return the created document metadata |
| 148 | + */ |
| 149 | + @PostMapping(consumes = MediaType.MULTIPART_FORM_DATA_VALUE) |
| 150 | + @PreAuthorize("hasAnyRole('ADMIN','MANAGER','SUPERVISOR')") |
| 151 | + @Operation( |
| 152 | + summary = "Upload a document", |
| 153 | + description = "Uploads a new document (max 10 MB). Accepts policies, training materials, " |
| 154 | + + "certifications, inspection reports, and other documents." |
| 155 | + ) |
| 156 | + @ApiResponses(value = { |
| 157 | + @io.swagger.v3.oas.annotations.responses.ApiResponse(responseCode = "201", description = "Document uploaded"), |
| 158 | + @io.swagger.v3.oas.annotations.responses.ApiResponse(responseCode = "400", description = "Invalid file or parameters"), |
| 159 | + @io.swagger.v3.oas.annotations.responses.ApiResponse(responseCode = "401", description = "Unauthorized"), |
| 160 | + @io.swagger.v3.oas.annotations.responses.ApiResponse(responseCode = "403", description = "Forbidden — STAFF cannot upload") |
| 161 | + }) |
| 162 | + public ResponseEntity<ApiResponse<DocumentResponse>> uploadDocument( |
| 163 | + @AuthenticationPrincipal JwtPrincipal principal, |
| 164 | + @RequestParam("file") MultipartFile file, |
| 165 | + @RequestParam("title") String title, |
| 166 | + @RequestParam(value = "description", required = false) String description, |
| 167 | + @RequestParam("category") DocumentCategory category |
| 168 | + ) { |
| 169 | + Document doc = documentService.uploadDocument( |
| 170 | + principal.organizationId(), |
| 171 | + principal.userId(), |
| 172 | + title, |
| 173 | + description, |
| 174 | + category, |
| 175 | + file |
| 176 | + ); |
| 177 | + return ResponseEntity.status(HttpStatus.CREATED) |
| 178 | + .body(ApiResponse.success("Document uploaded", DocumentResponse.from(doc))); |
| 179 | + } |
| 180 | + |
| 181 | + /** |
| 182 | + * Deletes a document by its ID. |
| 183 | + * |
| 184 | + * @param principal the authenticated user |
| 185 | + * @param id the document ID |
| 186 | + * @return success message |
| 187 | + */ |
| 188 | + @DeleteMapping("/{id}") |
| 189 | + @PreAuthorize("hasAnyRole('ADMIN','SUPERVISOR')") |
| 190 | + @Operation( |
| 191 | + summary = "Delete a document", |
| 192 | + description = "Permanently deletes a document. Only ADMIN and SUPERVISOR can delete." |
| 193 | + ) |
| 194 | + @ApiResponses(value = { |
| 195 | + @io.swagger.v3.oas.annotations.responses.ApiResponse(responseCode = "200", description = "Document deleted"), |
| 196 | + @io.swagger.v3.oas.annotations.responses.ApiResponse(responseCode = "404", description = "Document not found"), |
| 197 | + @io.swagger.v3.oas.annotations.responses.ApiResponse(responseCode = "401", description = "Unauthorized"), |
| 198 | + @io.swagger.v3.oas.annotations.responses.ApiResponse(responseCode = "403", description = "Forbidden") |
| 199 | + }) |
| 200 | + public ResponseEntity<ApiResponse<Void>> deleteDocument( |
| 201 | + @AuthenticationPrincipal JwtPrincipal principal, |
| 202 | + @PathVariable Long id |
| 203 | + ) { |
| 204 | + documentService.deleteDocument(id, principal.organizationId()); |
| 205 | + return ResponseEntity.ok(ApiResponse.success("Document deleted", null)); |
| 206 | + } |
| 207 | +} |
0 commit comments