|
1 | 1 | package dev.coms4156.project.metadetect.controller; |
2 | 2 |
|
3 | 3 | import dev.coms4156.project.metadetect.dto.Dtos; |
| 4 | +import dev.coms4156.project.metadetect.model.Image; |
4 | 5 | import dev.coms4156.project.metadetect.service.ImageService; |
| 6 | +import dev.coms4156.project.metadetect.service.UserService; |
| 7 | +import dev.coms4156.project.metadetect.service.errors.ForbiddenException; |
| 8 | +import dev.coms4156.project.metadetect.service.errors.NotFoundException; |
5 | 9 | import java.util.List; |
| 10 | +import java.util.UUID; |
| 11 | +import java.util.stream.Collectors; |
| 12 | +import org.springframework.http.HttpStatus; |
6 | 13 | import org.springframework.http.ResponseEntity; |
7 | 14 | import org.springframework.web.bind.annotation.DeleteMapping; |
| 15 | +import org.springframework.web.bind.annotation.ExceptionHandler; |
8 | 16 | import org.springframework.web.bind.annotation.GetMapping; |
9 | 17 | import org.springframework.web.bind.annotation.PathVariable; |
10 | 18 | import org.springframework.web.bind.annotation.PutMapping; |
|
14 | 22 | import org.springframework.web.bind.annotation.RestController; |
15 | 23 |
|
16 | 24 | /** |
17 | | - * CRUD around user images (records, not binary). |
| 25 | + * CRUD for image metadata (not binary files). Enforces ownership via ImageService. |
18 | 26 | */ |
19 | 27 | @RestController |
20 | 28 | @RequestMapping("/api/images") |
21 | 29 | public class ImageController { |
22 | 30 |
|
23 | 31 | private final ImageService imageService; |
| 32 | + private final UserService userService; |
24 | 33 |
|
25 | | - public ImageController(ImageService imageService) { |
| 34 | + public ImageController(ImageService imageService, UserService userService) { |
26 | 35 | this.imageService = imageService; |
| 36 | + this.userService = userService; |
27 | 37 | } |
28 | 38 |
|
| 39 | + /** GET /api/images?page=0&size=20 — list current user's images (simple paging). */ |
29 | 40 | @GetMapping |
30 | | - public ResponseEntity<List<Dtos.ImageDto>> list(@RequestParam(defaultValue = "0") int page, |
31 | | - @RequestParam(defaultValue = "20") int size) { |
32 | | - // TODO: paginate by owner user |
33 | | - return ResponseEntity.ok(List.of()); |
| 41 | + public ResponseEntity<List<Dtos.ImageDto>> list( |
| 42 | + @RequestParam(defaultValue = "0") int page, |
| 43 | + @RequestParam(defaultValue = "20") int size) { |
| 44 | + |
| 45 | + if (page < 0 || size <= 0) { |
| 46 | + return ResponseEntity.badRequest().build(); |
| 47 | + } |
| 48 | + |
| 49 | + UUID userId = userService.getCurrentUserIdOrThrow(); |
| 50 | + List<Image> all = imageService.listByOwner(userId); |
| 51 | + |
| 52 | + int from = Math.min(page * size, all.size()); |
| 53 | + int to = Math.min(from + size, all.size()); |
| 54 | + |
| 55 | + List<Dtos.ImageDto> items = all.subList(from, to) |
| 56 | + .stream() |
| 57 | + .map(this::toDto) |
| 58 | + .collect(Collectors.toList()); |
| 59 | + |
| 60 | + return ResponseEntity.ok(items); |
34 | 61 | } |
35 | 62 |
|
| 63 | + /** GET /api/images/{id} — fetch a single image (ownership enforced in service). */ |
36 | 64 | @GetMapping("/{id}") |
37 | 65 | public ResponseEntity<Dtos.ImageDto> get(@PathVariable String id) { |
38 | | - // TODO: fetch by id + ownership |
39 | | - return ResponseEntity.ok(new Dtos.ImageDto(id, "original.jpg", "owner-uid", null)); |
| 66 | + UUID userId = userService.getCurrentUserIdOrThrow(); |
| 67 | + UUID imageId = UUID.fromString(id); |
| 68 | + |
| 69 | + Image img = imageService.getById(userId, imageId); |
| 70 | + return ResponseEntity.ok(toDto(img)); |
40 | 71 | } |
41 | 72 |
|
| 73 | + /** PUT /api/images/{id} — update mutable fields. */ |
42 | 74 | @PutMapping("/{id}") |
43 | | - public ResponseEntity<Dtos.ImageDto> update(@PathVariable String id, |
44 | | - @RequestBody Dtos.UpdateImageRequest req) { |
45 | | - // TODO: update mutable fields (labels, note) |
46 | | - return ResponseEntity.ok(new Dtos.ImageDto(id, "original.jpg", "owner-uid", null)); |
| 75 | + public ResponseEntity<Dtos.ImageDto> update( |
| 76 | + @PathVariable String id, |
| 77 | + @RequestBody Dtos.UpdateImageRequest req) { |
| 78 | + |
| 79 | + UUID userId = userService.getCurrentUserIdOrThrow(); |
| 80 | + UUID imageId = UUID.fromString(id); |
| 81 | + |
| 82 | + // Convert List<String> -> String[] for the service layer |
| 83 | + String[] labels = (req.labels() == null) ? null : req.labels().toArray(new String[0]); |
| 84 | + |
| 85 | + Image updated = imageService.update( |
| 86 | + userId, |
| 87 | + imageId, |
| 88 | + null, // filename (not changed via this endpoint) |
| 89 | + null, // storagePath (not changed via this endpoint) |
| 90 | + labels, // labels |
| 91 | + req.note() // note |
| 92 | + ); |
| 93 | + |
| 94 | + return ResponseEntity.ok(toDto(updated)); |
47 | 95 | } |
48 | 96 |
|
| 97 | + |
| 98 | + /** DELETE /api/images/{id} — hard delete metadata record. */ |
49 | 99 | @DeleteMapping("/{id}") |
50 | 100 | public ResponseEntity<Void> delete(@PathVariable String id) { |
51 | | - // TODO: soft/hard delete image + reports |
| 101 | + UUID userId = userService.getCurrentUserIdOrThrow(); |
| 102 | + UUID imageId = UUID.fromString(id); |
| 103 | + |
| 104 | + imageService.delete(userId, imageId); |
52 | 105 | return ResponseEntity.noContent().build(); |
53 | 106 | } |
| 107 | + |
| 108 | + // ---- Exception → HTTP mapping (controller-scoped) ---- |
| 109 | + |
| 110 | + @ExceptionHandler(NotFoundException.class) |
| 111 | + public ResponseEntity<String> handleNotFound(NotFoundException ex) { |
| 112 | + return ResponseEntity.status(HttpStatus.NOT_FOUND).body(ex.getMessage()); |
| 113 | + } |
| 114 | + |
| 115 | + @ExceptionHandler(ForbiddenException.class) |
| 116 | + public ResponseEntity<String> handleForbidden(ForbiddenException ex) { |
| 117 | + return ResponseEntity.status(HttpStatus.FORBIDDEN).body(ex.getMessage()); |
| 118 | + } |
| 119 | + |
| 120 | + // ---- Mapping helper ---- |
| 121 | + // Matches Dtos.ImageDto(id, filename, ownerId, uploadedAt) |
| 122 | + private Dtos.ImageDto toDto(Image img) { |
| 123 | + return new Dtos.ImageDto( |
| 124 | + img.getId().toString(), |
| 125 | + img.getFilename(), |
| 126 | + img.getUserId().toString(), |
| 127 | + img.getUploadedAt(), |
| 128 | + (img.getLabels() == null ? List.of() : List.of(img.getLabels())), |
| 129 | + img.getNote() |
| 130 | + ); |
| 131 | + } |
| 132 | + |
54 | 133 | } |
0 commit comments