Skip to content

Commit 2a9190f

Browse files
committed
feat: implement POST /items endpoint for creating media items
- Add validation with @Valid for automatic error handling - Convert MediaItemRequest to MediaItem using existing helper - Add items to library via librarian authorization - Return created item in response with proper status codes
1 parent fb4d4cf commit 2a9190f

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed

lesson_23/api/java/api_app/src/main/java/com/codedifferently/lesson23/web/MediaItemsController.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,13 @@
88
import java.util.List;
99
import java.util.Set;
1010
import java.util.UUID;
11+
import jakarta.validation.Valid;
1112
import org.springframework.http.ResponseEntity;
1213
import org.springframework.web.bind.annotation.CrossOrigin;
1314
import org.springframework.web.bind.annotation.GetMapping;
1415
import org.springframework.web.bind.annotation.PathVariable;
16+
import org.springframework.web.bind.annotation.PostMapping;
17+
import org.springframework.web.bind.annotation.RequestBody;
1518
import org.springframework.web.bind.annotation.RestController;
1619

1720
@RestController
@@ -51,4 +54,21 @@ public ResponseEntity<MediaItemResponse> getItem(@PathVariable String id) {
5154
return ResponseEntity.badRequest().build();
5255
}
5356
}
57+
58+
@PostMapping("/items")
59+
public ResponseEntity<CreateMediaItemResponse> createItem(@Valid @RequestBody CreateMediaItemRequest request) {
60+
try {
61+
MediaItem newItem = MediaItemRequest.asMediaItem(request.getItem());
62+
library.addMediaItem(newItem, librarian);
63+
64+
MediaItemResponse itemResponse = MediaItemResponse.from(newItem);
65+
CreateMediaItemResponse response = CreateMediaItemResponse.builder()
66+
.item(itemResponse)
67+
.build();
68+
69+
return ResponseEntity.ok(response);
70+
} catch (Exception e) {
71+
return ResponseEntity.badRequest().build();
72+
}
73+
}
5474
}

0 commit comments

Comments
 (0)